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

Commit

Permalink
Add SetFullScreen function to Window (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie authored May 11, 2022
1 parent eace8e5 commit 3aea3f6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion astilectron.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Versions
const (
DefaultAcceptTCPTimeout = 30 * time.Second
DefaultVersionAstilectron = "0.54.0"
DefaultVersionAstilectron = "0.55.0"
DefaultVersionElectron = "11.4.3"
)

Expand Down
44 changes: 44 additions & 0 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ const (
EventNameWindowCmdWebContentsOpenDevTools = "window.cmd.web.contents.open.dev.tools"
EventNameWindowCmdWebContentsExecuteJavaScript = "window.cmd.web.contents.execute.javascript"
EventNameWindowCmdSetAlwaysOnTop = "window.cmd.set.always.on.top"
EventNameWindowCmdSetFullScreen = "window.cmd.set.full.screen"
EventNameWindowEventBlur = "window.event.blur"
EventNameWindowEventClosed = "window.event.closed"
EventNameWindowEventContentProtectionSet = "window.event.content.protection.set"
EventNameWindowEventDidFinishLoad = "window.event.did.finish.load"
EventNameWindowEventEnterFullScreen = "window.event.enter.full.screen"
EventNameWindowEventFocus = "window.event.focus"
EventNameWindowEventHide = "window.event.hide"
EventNameWindowEventLeaveFullScreen = "window.event.leave.full.screen"
EventNameWindowEventMaximize = "window.event.maximize"
eventNameWindowEventMessage = "window.event.message"
eventNameWindowEventMessageCallback = "window.event.message.callback"
Expand Down Expand Up @@ -234,6 +237,20 @@ func newWindow(ctx context.Context, l astikit.SeverityLogger, o Options, p Paths
return true
})

// Fullscreen state
w.On(EventNameWindowEventEnterFullScreen, func(e Event) (deleteListener bool) {
w.m.Lock()
defer w.m.Unlock()
w.o.Fullscreen = astikit.BoolPtr(true)
return
})
w.On(EventNameWindowEventLeaveFullScreen, func(e Event) (deleteListener bool) {
w.m.Lock()
defer w.m.Unlock()
w.o.Fullscreen = astikit.BoolPtr(false)
return
})

// Show
w.On(EventNameWindowEventHide, func(e Event) (deleteListener bool) {
w.m.Lock()
Expand Down Expand Up @@ -400,6 +417,16 @@ func (w *Window) Hide() (err error) {
return
}

// IsFullScreen returns whether the window is in full screen mode
func (w *Window) IsFullScreen() bool {
if w.ctx.Err() != nil {
return false
}
w.m.Lock()
defer w.m.Unlock()
return w.o.Fullscreen != nil && *w.o.Fullscreen
}

// IsShown returns whether the window is shown
func (w *Window) IsShown() bool {
if w.ctx.Err() != nil {
Expand Down Expand Up @@ -552,6 +579,23 @@ func (w *Window) SetContentProtection(enable bool) (err error) {
return
}

// SetFullScreen sets the fullscreen flag of the window
func (w *Window) SetFullScreen(enable bool) (err error) {
if err = w.ctx.Err(); err != nil {
return
}

var eventNameDone string
if enable {
eventNameDone = EventNameWindowEventEnterFullScreen
} else {
eventNameDone = EventNameWindowEventLeaveFullScreen
}

_, err = synchronousEvent(w.ctx, w, w.w, Event{Name: EventNameWindowCmdSetFullScreen, TargetID: w.id, Enable: astikit.BoolPtr(enable)}, eventNameDone)
return
}

// Restore restores the window
func (w *Window) Restore() (err error) {
if err = w.ctx.Err(); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func TestWindow_Actions(t *testing.T) {
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\":{\"alwaysOnTop\":true,\"height\":2,\"show\":true,\"width\":1,\"x\":4,\"y\":6,\"custom\":{\"hideOnClose\":true}}}\n", EventNameWindowEventUpdatedCustomOptions, nil)
testObjectAction(t, func() error { return w.Unmaximize() }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdUnmaximize+"\",\"targetID\":\""+w.id+"\"}\n", EventNameWindowEventUnmaximize, nil)
testObjectAction(t, func() error { return w.SetFullScreen(true) }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdSetFullScreen+"\",\"targetID\":\""+w.id+"\",\"enable\":true}\n", EventNameWindowEventEnterFullScreen, &Event{WindowOptions: &WindowOptions{Fullscreen: astikit.BoolPtr(true)}})
assert.Equal(t, true, w.IsFullScreen())
testObjectAction(t, func() error { return w.SetFullScreen(false) }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdSetFullScreen+"\",\"targetID\":\""+w.id+"\",\"enable\":false}\n", EventNameWindowEventLeaveFullScreen, &Event{WindowOptions: &WindowOptions{Fullscreen: astikit.BoolPtr(false)}})
assert.Equal(t, false, w.IsFullScreen())
testObjectAction(t, func() error { return w.ExecuteJavaScript("console.log('test');") }, w.object, wrt, "{\"name\":\""+EventNameWindowCmdWebContentsExecuteJavaScript+"\",\"targetID\":\""+w.id+"\",\"code\":\"console.log('test');\"}\n", EventNameWindowEventWebContentsExecutedJavaScript, nil)
}

Expand Down

0 comments on commit 3aea3f6

Please sign in to comment.