Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
Add setAlwaysOnTop support (#361)
Browse files Browse the repository at this point in the history
* Add setAlwaysOnTop support

* Add missing tests
  • Loading branch information
Happy-Ferret committed Apr 22, 2022
1 parent 1a9ca6a commit a95bcff
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion astilectron.go
Expand Up @@ -13,7 +13,7 @@ import (
// Versions
const (
DefaultAcceptTCPTimeout = 30 * time.Second
DefaultVersionAstilectron = "0.51.0"
DefaultVersionAstilectron = "0.52.0"
DefaultVersionElectron = "11.4.3"
)

Expand Down
14 changes: 14 additions & 0 deletions window.go
Expand Up @@ -38,6 +38,7 @@ const (
EventNameWindowCmdWebContentsCloseDevTools = "window.cmd.web.contents.close.dev.tools"
EventNameWindowCmdWebContentsOpenDevTools = "window.cmd.web.contents.open.dev.tools"
EventNameWindowCmdWebContentsExecuteJavaScript = "window.cmd.web.contents.execute.javascript"
EventNameWindowCmdSetAlwaysOnTop = "window.cmd.set.always.on.top"
EventNameWindowEventBlur = "window.event.blur"
EventNameWindowEventClosed = "window.event.closed"
EventNameWindowEventContentProtectionSet = "window.event.content.protection.set"
Expand All @@ -60,6 +61,7 @@ const (
EventNameWindowEventWebContentsExecutedJavaScript = "window.event.web.contents.executed.javascript"
EventNameWindowEventWillNavigate = "window.event.will.navigate"
EventNameWindowEventUpdatedCustomOptions = "window.event.updated.custom.options"
EventNameWindowEventAlwaysOnTopChanged = "window.event.always.on.top.changed"
)

// Title bar styles
Expand Down Expand Up @@ -459,6 +461,18 @@ func (w *Window) OpenDevTools() (err error) {
return w.w.write(Event{Name: EventNameWindowCmdWebContentsOpenDevTools, TargetID: w.id})
}

// SetAlwaysOnTop sets whether the window should show always on top of other windows.
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, Enable: astikit.BoolPtr(flag)}, EventNameWindowEventAlwaysOnTopChanged)
return
}

// Resize resizes the window
func (w *Window) Resize(width, height int) (err error) {
if err = w.ctx.Err(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion window_test.go
Expand Up @@ -67,9 +67,10 @@ func TestWindow_Actions(t *testing.T) {
testObjectAction(t, func() error { return w.Resize(1, 2) }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdResize+"\",\"targetID\":\""+w.id+"\",\"windowOptions\":{\"height\":2,\"width\":1}}\n", EventNameWindowEventResize)
testObjectAction(t, func() error { return w.ResizeContent(1, 2) }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdResizeContent+"\",\"targetID\":\""+w.id+"\",\"windowOptions\":{\"height\":2,\"width\":1}}\n", EventNameWindowEventResizeContent)
testObjectAction(t, func() error { return w.Restore() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdRestore+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventRestore)
testObjectAction(t, func() error { return w.SetAlwaysOnTop(true) }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdSetAlwaysOnTop+"\",\"targetID\":\""+w.id+"\",\"enable\":true}\n", EventNameWindowEventAlwaysOnTopChanged)
testObjectAction(t, func() error { return w.Show() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdShow+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventShow)
assert.Equal(t, true, w.IsShown())
testObjectAction(t, func() error { return w.UpdateCustomOptions(WindowCustomOptions{HideOnClose: astikit.BoolPtr(true)}) }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdUpdateCustomOptions+"\",\"targetID\":\""+w.id+"\",\"windowOptions\":{\"height\":2,\"show\":true,\"width\":1,\"x\":4,\"y\":6,\"custom\":{\"hideOnClose\":true}}}\n", EventNameWindowEventUpdatedCustomOptions)
testObjectAction(t, func() error { return w.UpdateCustomOptions(WindowCustomOptions{HideOnClose: astikit.BoolPtr(true)}) }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdUpdateCustomOptions+"\",\"targetID\":\""+w.id+"\",\"windowOptions\":{\"alwaysOnTop\":true,\"height\":2,\"show\":true,\"width\":1,\"x\":4,\"y\":6,\"custom\":{\"hideOnClose\":true}}}\n", EventNameWindowEventUpdatedCustomOptions)
testObjectAction(t, func() error { return w.Unmaximize() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdUnmaximize+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventUnmaximize)
testObjectAction(t, func() error { return w.ExecuteJavaScript("console.log('test');") }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdWebContentsExecuteJavaScript+"\",\"targetID\":\""+w.id+"\",\"code\":\"console.log('test');\"}\n", EventNameWindowEventWebContentsExecutedJavaScript)
}
Expand Down

0 comments on commit a95bcff

Please sign in to comment.