From d5e21d50036c44e4a61ff71e6be99d8ce40acfa4 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Fri, 9 Dec 2022 08:22:17 +1100 Subject: [PATCH] Fix race condition in closing window during navigating Add sane defaults --- exp/go.mod | 5 +- exp/go.sum | 2 - exp/pkg/application/application.go | 8 ++ exp/pkg/application/window_darwin.go | 6 +- exp/pkg/options/application.go | 136 --------------------------- exp/pkg/options/mac.go | 105 +++++++++++++++++++++ exp/pkg/options/window.go | 30 ++++++ 7 files changed, 147 insertions(+), 145 deletions(-) create mode 100644 exp/pkg/options/mac.go create mode 100644 exp/pkg/options/window.go diff --git a/exp/go.mod b/exp/go.mod index 6b99bc397..1969eb348 100644 --- a/exp/go.mod +++ b/exp/go.mod @@ -2,7 +2,4 @@ module github.com/wailsapp/wails/exp go 1.19 -require ( - github.com/gofrs/uuid v4.3.1+incompatible - github.com/leaanthony/clir v1.3.0 -) +require github.com/leaanthony/clir v1.3.0 diff --git a/exp/go.sum b/exp/go.sum index 119fea365..ec90627a4 100644 --- a/exp/go.sum +++ b/exp/go.sum @@ -1,4 +1,2 @@ -github.com/gofrs/uuid v4.3.1+incompatible h1:0/KbAdpx3UXAx1kEOWHJeOkpbgRFGHVgv+CFIY7dBJI= -github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/leaanthony/clir v1.3.0 h1:L9nPDWrmc/qU9UWZZvRaFajWYuO0np9V5p+5gxyYno0= github.com/leaanthony/clir v1.3.0/go.mod h1:k/RBkdkFl18xkkACMCLt09bhiZnrGORoxmomeMvDpE0= diff --git a/exp/pkg/application/application.go b/exp/pkg/application/application.go index 9dce249ee..8f01314d5 100644 --- a/exp/pkg/application/application.go +++ b/exp/pkg/application/application.go @@ -33,6 +33,14 @@ func (a *App) On(s string, callback func()) { } func (a *App) NewWindow(options *options.Window) *Window { + // Ensure we have sane defaults + if options.Width == 0 { + options.Width = 1024 + } + if options.Height == 0 { + options.Height = 768 + } + newWindow := NewWindow(options) id := newWindow.id if a.windows == nil { diff --git a/exp/pkg/application/window_darwin.go b/exp/pkg/application/window_darwin.go index 06d28010a..d06cb4667 100644 --- a/exp/pkg/application/window_darwin.go +++ b/exp/pkg/application/window_darwin.go @@ -497,9 +497,6 @@ func (w *macosWindow) run() { w.setResizable(!w.options.DisableResize) w.setMinSize(w.options.MinWidth, w.options.MinHeight) w.setMaxSize(w.options.MaxWidth, w.options.MaxHeight) - if w.options.URL != "" { - w.navigateToURL(w.options.URL) - } if w.options.EnableDevTools { w.enableDevTools() } @@ -540,6 +537,9 @@ func (w *macosWindow) run() { } } + if w.options.URL != "" { + w.navigateToURL(w.options.URL) + } C.windowShow(w.nsWindow) }) } diff --git a/exp/pkg/options/application.go b/exp/pkg/options/application.go index a8b17c21f..afc004245 100644 --- a/exp/pkg/options/application.go +++ b/exp/pkg/options/application.go @@ -3,139 +3,3 @@ package options type Application struct { Mac *Mac } - -// Mac contains macOS specific options - -type ActivationPolicy int - -const ( - ActivationPolicyRegular ActivationPolicy = iota - ActivationPolicyAccessory - ActivationPolicyProhibited -) - -type Mac struct { - // ActivationPolicy is the activation policy for the application. Defaults to - // applicationActivationPolicyRegular. - ActivationPolicy ActivationPolicy -} - -type Window struct { - Title string - Width, Height int - AlwaysOnTop bool - URL string - DisableResize bool - Resizable bool - MinWidth int - MinHeight int - MaxWidth int - MaxHeight int - EnableDevTools bool - StartState WindowState - Mac *MacWindow - BackgroundColour *RGBA -} - -type RGBA struct { - Red, Green, Blue, Alpha uint8 -} - -type MacBackdrop int - -const ( - MacBackdropNormal MacBackdrop = iota - MacBackdropTransparent - MacBackdropTranslucent -) - -type WindowState int - -const ( - WindowStateNormal WindowState = iota - WindowStateMinimised - WindowStateMaximised - WindowStateFullscreen -) - -// MacWindow contains macOS specific options -type MacWindow struct { - Backdrop MacBackdrop - TitleBar *TitleBar - Appearance MacAppearanceType -} - -// TitleBar contains options for the Mac titlebar -type TitleBar struct { - AppearsTransparent bool - Hide bool - HideTitle bool - FullSizeContent bool - UseToolbar bool - HideToolbarSeparator bool -} - -// TitleBarDefault results in the default Mac Titlebar -func TitleBarDefault() *TitleBar { - return &TitleBar{ - AppearsTransparent: false, - Hide: false, - HideTitle: false, - FullSizeContent: false, - UseToolbar: false, - HideToolbarSeparator: false, - } -} - -// Credit: Comments from Electron site - -// TitleBarHidden results in a hidden title bar and a full size content window, -// yet the title bar still has the standard window controls (“traffic lights”) -// in the top left. -func TitleBarHidden() *TitleBar { - return &TitleBar{ - AppearsTransparent: true, - Hide: false, - HideTitle: true, - FullSizeContent: true, - UseToolbar: false, - HideToolbarSeparator: false, - } -} - -// TitleBarHiddenInset results in a hidden title bar with an alternative look where -// the traffic light buttons are slightly more inset from the window edge. -func TitleBarHiddenInset() *TitleBar { - - return &TitleBar{ - AppearsTransparent: true, - Hide: false, - HideTitle: true, - FullSizeContent: true, - UseToolbar: true, - HideToolbarSeparator: true, - } - -} - -// MacAppearanceType is a type of Appearance for Cocoa windows -type MacAppearanceType string - -const ( - // DefaultAppearance uses the default system value - DefaultAppearance MacAppearanceType = "" - // NSAppearanceNameAqua - The standard light system appearance. - NSAppearanceNameAqua MacAppearanceType = "NSAppearanceNameAqua" - // NSAppearanceNameDarkAqua - The standard dark system appearance. - NSAppearanceNameDarkAqua MacAppearanceType = "NSAppearanceNameDarkAqua" - // NSAppearanceNameVibrantLight - The light vibrant appearance - NSAppearanceNameVibrantLight MacAppearanceType = "NSAppearanceNameVibrantLight" - // NSAppearanceNameAccessibilityHighContrastAqua - A high-contrast version of the standard light system appearance. - NSAppearanceNameAccessibilityHighContrastAqua MacAppearanceType = "NSAppearanceNameAccessibilityHighContrastAqua" - // NSAppearanceNameAccessibilityHighContrastDarkAqua - A high-contrast version of the standard dark system appearance. - NSAppearanceNameAccessibilityHighContrastDarkAqua MacAppearanceType = "NSAppearanceNameAccessibilityHighContrastDarkAqua" - // NSAppearanceNameAccessibilityHighContrastVibrantLight - A high-contrast version of the light vibrant appearance. - NSAppearanceNameAccessibilityHighContrastVibrantLight MacAppearanceType = "NSAppearanceNameAccessibilityHighContrastVibrantLight" - // NSAppearanceNameAccessibilityHighContrastVibrantDark - A high-contrast version of the dark vibrant appearance. - NSAppearanceNameAccessibilityHighContrastVibrantDark MacAppearanceType = "NSAppearanceNameAccessibilityHighContrastVibrantDark" -) diff --git a/exp/pkg/options/mac.go b/exp/pkg/options/mac.go new file mode 100644 index 000000000..40730bc7e --- /dev/null +++ b/exp/pkg/options/mac.go @@ -0,0 +1,105 @@ +package options + +type ActivationPolicy int + +const ( + ActivationPolicyRegular ActivationPolicy = iota + ActivationPolicyAccessory + ActivationPolicyProhibited +) + +type Mac struct { + // ActivationPolicy is the activation policy for the application. Defaults to + // applicationActivationPolicyRegular. + ActivationPolicy ActivationPolicy +} + +type MacBackdrop int + +const ( + MacBackdropNormal MacBackdrop = iota + MacBackdropTransparent + MacBackdropTranslucent +) + +// MacWindow contains macOS specific options +type MacWindow struct { + Backdrop MacBackdrop + TitleBar *TitleBar + Appearance MacAppearanceType +} + +// TitleBar contains options for the Mac titlebar +type TitleBar struct { + AppearsTransparent bool + Hide bool + HideTitle bool + FullSizeContent bool + UseToolbar bool + HideToolbarSeparator bool +} + +// TitleBarDefault results in the default Mac Titlebar +func TitleBarDefault() *TitleBar { + return &TitleBar{ + AppearsTransparent: false, + Hide: false, + HideTitle: false, + FullSizeContent: false, + UseToolbar: false, + HideToolbarSeparator: false, + } +} + +// Credit: Comments from Electron site + +// TitleBarHidden results in a hidden title bar and a full size content window, +// yet the title bar still has the standard window controls (“traffic lights”) +// in the top left. +func TitleBarHidden() *TitleBar { + return &TitleBar{ + AppearsTransparent: true, + Hide: false, + HideTitle: true, + FullSizeContent: true, + UseToolbar: false, + HideToolbarSeparator: false, + } +} + +// TitleBarHiddenInset results in a hidden title bar with an alternative look where +// the traffic light buttons are slightly more inset from the window edge. +func TitleBarHiddenInset() *TitleBar { + + return &TitleBar{ + AppearsTransparent: true, + Hide: false, + HideTitle: true, + FullSizeContent: true, + UseToolbar: true, + HideToolbarSeparator: true, + } + +} + +// MacAppearanceType is a type of Appearance for Cocoa windows +type MacAppearanceType string + +const ( + // DefaultAppearance uses the default system value + DefaultAppearance MacAppearanceType = "" + // NSAppearanceNameAqua - The standard light system appearance. + NSAppearanceNameAqua MacAppearanceType = "NSAppearanceNameAqua" + // NSAppearanceNameDarkAqua - The standard dark system appearance. + NSAppearanceNameDarkAqua MacAppearanceType = "NSAppearanceNameDarkAqua" + // NSAppearanceNameVibrantLight - The light vibrant appearance + NSAppearanceNameVibrantLight MacAppearanceType = "NSAppearanceNameVibrantLight" + // NSAppearanceNameAccessibilityHighContrastAqua - A high-contrast version of the standard light system appearance. + NSAppearanceNameAccessibilityHighContrastAqua MacAppearanceType = "NSAppearanceNameAccessibilityHighContrastAqua" + // NSAppearanceNameAccessibilityHighContrastDarkAqua - A high-contrast version of the standard dark system appearance. + NSAppearanceNameAccessibilityHighContrastDarkAqua MacAppearanceType = "NSAppearanceNameAccessibilityHighContrastDarkAqua" + // NSAppearanceNameAccessibilityHighContrastVibrantLight - A high-contrast version of the light vibrant appearance. + NSAppearanceNameAccessibilityHighContrastVibrantLight MacAppearanceType = "NSAppearanceNameAccessibilityHighContrastVibrantLight" + // NSAppearanceNameAccessibilityHighContrastVibrantDark - A high-contrast version of the dark vibrant appearance. + NSAppearanceNameAccessibilityHighContrastVibrantDark MacAppearanceType = "NSAppearanceNameAccessibilityHighContrastVibrantDark" +) diff --git a/exp/pkg/options/window.go b/exp/pkg/options/window.go new file mode 100644 index 000000000..8c94114c3 --- /dev/null +++ b/exp/pkg/options/window.go @@ -0,0 +1,30 @@ +package options + +type WindowState int + +const ( + WindowStateNormal WindowState = iota + WindowStateMinimised + WindowStateMaximised + WindowStateFullscreen +) + +type Window struct { + Title string + Width, Height int + AlwaysOnTop bool + URL string + DisableResize bool + MinWidth int + MinHeight int + MaxWidth int + MaxHeight int + EnableDevTools bool + StartState WindowState + Mac *MacWindow + BackgroundColour *RGBA +} + +type RGBA struct { + Red, Green, Blue, Alpha uint8 +}