go-astilectron icon indicating copy to clipboard operation
go-astilectron copied to clipboard

Manipulate WindowOptions

Open Happy-Ferret opened this issue 2 years ago • 5 comments

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.

Happy-Ferret avatar Apr 19 '22 00:04 Happy-Ferret

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

asticode avatar Apr 19 '22 07:04 asticode

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 avatar Apr 19 '22 13:04 Happy-Ferret

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)
    });

Happy-Ferret avatar Apr 19 '22 15:04 Happy-Ferret

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

asticode avatar Apr 19 '22 16:04 asticode

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.

Happy-Ferret avatar Apr 19 '22 17:04 Happy-Ferret