Skip to content

Commit

Permalink
internal/uidriver/glfw: Bug fix: Crash on some operations on native f…
Browse files Browse the repository at this point in the history
…ullscreen mode (macOS)

This change forbids some operations when the wiindow is natively
fullscreened on macOS in order to avoid crashes.

Closes hajimehoshi#1578
  • Loading branch information
hajimehoshi committed Apr 18, 2021
1 parent b3afcae commit b8e8485
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/uidriver/glfw/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,18 @@ func (u *UserInterface) SetFullscreen(fullscreen bool) {
}

var update bool
var nativeFullscreen bool
_ = u.t.Call(func() error {
update = u.isFullscreen() != fullscreen
nativeFullscreen = u.isNativeFullscreen()
return nil
})
if !update {
return
}
if nativeFullscreen {
return
}

var w, h int
_ = u.t.Call(func() error {
Expand Down
9 changes: 9 additions & 0 deletions internal/uidriver/glfw/ui_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ package glfw
// *x = bounds.origin.x;
// *y = bounds.origin.y;
// }
//
// static bool isNativeFullscreen() {
// return [[NSApplication sharedApplication] currentSystemPresentationOptions] &
// NSApplicationPresentationFullScreen;
// }
import "C"

import (
Expand Down Expand Up @@ -83,3 +88,7 @@ func currentMonitorByOS(w *glfw.Window) *glfw.Monitor {
func (u *UserInterface) nativeWindow() uintptr {
return u.window.GetCocoaWindow()
}

func (u *UserInterface) isNativeFullscreen() bool {
return bool(C.isNativeFullscreen())
}
4 changes: 4 additions & 0 deletions internal/uidriver/glfw/ui_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ func (u *UserInterface) nativeWindow() uintptr {
// TODO: Implement this.
return 0
}

func (u *UserInterface) isNativeFullscreen() bool {
return false
}
4 changes: 4 additions & 0 deletions internal/uidriver/glfw/ui_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,7 @@ func currentMonitorByOS(_ *glfw.Window) *glfw.Monitor {
func (u *UserInterface) nativeWindow() uintptr {
return u.window.GetWin32Window()
}

func (u *UserInterface) isNativeFullscreen() bool {
return false
}
12 changes: 12 additions & 0 deletions internal/uidriver/glfw/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ func (w *window) SetDecorated(decorated bool) {
}

_ = w.ui.t.Call(func() error {
if w.ui.isNativeFullscreen() {
return nil
}

v := glfw.False
if decorated {
v = glfw.True
Expand Down Expand Up @@ -81,6 +85,10 @@ func (w *window) SetResizable(resizable bool) {
return
}
_ = w.ui.t.Call(func() error {
if w.ui.isNativeFullscreen() {
return nil
}

v := glfw.False
if resizable {
v = glfw.True
Expand Down Expand Up @@ -108,6 +116,10 @@ func (w *window) SetFloating(floating bool) {
return
}
_ = w.ui.t.Call(func() error {
if w.ui.isNativeFullscreen() {
return nil
}

v := glfw.False
if floating {
v = glfw.True
Expand Down
3 changes: 3 additions & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ func IsFullscreen() bool {
//
// SetFullscreen does nothing on mobiles.
//
// SetFullscreen does nothing on macOS when the window is fullscreened natively by the macOS desktop
// instead of SetFullscreen(true).
//
// SetFullscreen is concurrent-safe.
func SetFullscreen(fullscreen bool) {
uiDriver().SetFullscreen(fullscreen)
Expand Down
9 changes: 9 additions & 0 deletions window.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func IsWindowDecorated() bool {
// SetWindowDecorated works only on desktops.
// SetWindowDecorated does nothing on other platforms.
//
// SetWindowDecorated does nothing on macOS when the window is fullscreened natively by the macOS desktop
// instead of SetFullscreen(true).
//
// SetWindowDecorated is concurrent-safe.
func SetWindowDecorated(decorated bool) {
if w := uiDriver().Window(); w != nil {
Expand All @@ -67,6 +70,9 @@ func IsWindowResizable() bool {
//
// If SetWindowResizable is called with true and Run is used, SetWindowResizable panics. Use RunGame instead.
//
// SetWindowResizable does nothing on macOS when the window is fullscreened natively by the macOS desktop
// instead of SetFullscreen(true).
//
// SetWindowResizable is concurrent-safe.
func SetWindowResizable(resizable bool) {
theUIContext.setWindowResizable(resizable)
Expand Down Expand Up @@ -253,6 +259,9 @@ func IsWindowFloating() bool {
//
// SetWindowFloating does nothing on browsers or mobiles.
//
// SetWindowFloating does nothing on macOS when the window is fullscreened natively by the macOS desktop
// instead of SetFullscreen(true).
//
// SetWindowFloating is concurrent-safe.
func SetWindowFloating(float bool) {
if w := uiDriver().Window(); w != nil {
Expand Down

0 comments on commit b8e8485

Please sign in to comment.