asticode / go-astilectron

Build cross platform GUI apps with GO and HTML/JS/CSS (powered by Electron)
MIT License
4.9k stars 344 forks source link

Manipulate WindowOptions #360

Open Happy-Ferret opened 2 years ago

Happy-Ferret commented 2 years ago

I'd like to dynamically set AlwaysOnTop based on a setting inside my app.

Is there a way to access the WindowOptions outside the bootstrapping procedure (I've been unable to find it, or rather direct access is private) or will I have to make-do with require("electron").remote.BrowserWindow.getFocusedWindow().setAlwaysOnTop(bool_value) from inside JS? I'd rather like to avoid doing it like this.

asticode commented 2 years ago

Right now this is not possible in GO but adding it is not that complex. I just won't have time to work on this anytime time soon though. If you feel like it, I can describe what has to be done so that you can make a PR

Happy-Ferret commented 2 years ago

Right now this is not possible in GO but adding it is not that complex. I just won't have time to work on this anytime time soon though. If you feel like it, I can describe what has to be done so that you can make a PR

I think I know what needs to be done. Something similar to w.OpenDevTools(), I assume?

w.Sticky(astikit.BoolPtr), perhaps?

Happy-Ferret commented 2 years ago

Weird. I tried adding the changes that, to the best of my knowledge, are necessary but end up with a no-op.

Any idea what I'm doing wrong?

window.go:

...
// Window event names
const (
...
    EventNameWindowCmdSetAlwaysOnTop              = "window.cmd.set.always.on.top"
    EventNameWindowEventAlwaysOnTopChanged            = "window.event.always.on.top.changed"
...

func (w *Window) SetAlwaysOnTop(flag bool) (err error) {
    if err = w.ctx.Err(); err != nil {
        return
    }
    w.m.Lock()
    w.o.AlwaysOnTop = astikit.BoolPtr(flag)
    w.m.Unlock()
    _, err = synchronousEvent(w.ctx, w, w.w, Event{Name: EventNameWindowCmdSetAlwaysOnTop, TargetID: w.id, WindowOptions: &WindowOptions{AlwaysOnTop: astikit.BoolPtr(flag)}}, EventNameWindowEventAlwaysOnTopChanged)
    return
}

App

message.go

package desktop

import (
    "github.com/asticode/go-astilectron"
    "github.com/asticode/go-astilectron-bootstrap"
)

func HandleMessages(w *astilectron.Window, m bootstrap.MessageIn) (payload interface{}, err error) {
    switch m.Name {
    case "always_on_top":
        w.SetAlwaysOnTop(true)
        payload = "AlwaysOnTop set"
        return
    }
    return
}

app.js

astilectron.sendMessage(
        { name: "always_on_top", payload: "" },
        message => {
        console.log("received " + message)
    });
asticode commented 2 years ago

You need to update astilectron (the JS part) as well so that it parses the new window.cmd.set.always.on.top key 👍

Happy-Ferret commented 2 years ago

You need to update astilectron (the JS part) as well so that it parses the new window.cmd.set.always.on.top key 👍

How do I ensure the correct astilectron js is being used? I edited the files inside %APPDATA%//vendor/astilectron but it doesn't seem to use those.

EDIT: NVM. It is being used.