5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 08:31:47 +08:00

Removed OnShouldClose: Create single way of handling conditional and unconditional close.

This commit is contained in:
Lea Anthony 2024-12-23 08:23:07 +11:00
parent 8e98d6dd19
commit 65f95b0380
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
16 changed files with 105 additions and 89 deletions

View File

@ -2,12 +2,11 @@ package main
import ( import (
_ "embed" _ "embed"
"log"
"runtime"
"github.com/wailsapp/wails/v3/pkg/application" "github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events" "github.com/wailsapp/wails/v3/pkg/events"
"github.com/wailsapp/wails/v3/pkg/icons" "github.com/wailsapp/wails/v3/pkg/icons"
"log"
"runtime"
) )
func main() { func main() {
@ -29,16 +28,16 @@ func main() {
AlwaysOnTop: false, AlwaysOnTop: false,
Hidden: false, Hidden: false,
DisableResize: false, DisableResize: false,
ShouldClose: func(window *application.WebviewWindow) bool {
println("close")
window.Hide()
return false
},
Windows: application.WindowsWindow{ Windows: application.WindowsWindow{
HiddenOnTaskbar: true, HiddenOnTaskbar: true,
}, },
}) })
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
window.Hide()
e.Cancel()
})
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
systemTray.SetTemplateIcon(icons.SystrayMacTemplate) systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
_ "embed" _ "embed"
"github.com/wailsapp/wails/v3/pkg/events"
"log" "log"
"runtime" "runtime"
@ -29,10 +30,6 @@ func main() {
AlwaysOnTop: true, AlwaysOnTop: true,
Hidden: true, Hidden: true,
DisableResize: true, DisableResize: true,
ShouldClose: func(window *application.WebviewWindow) bool {
window.Hide()
return false
},
Windows: application.WindowsWindow{ Windows: application.WindowsWindow{
HiddenOnTaskbar: true, HiddenOnTaskbar: true,
}, },
@ -43,6 +40,11 @@ func main() {
}, },
}) })
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
window.Hide()
e.Cancel()
})
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
systemTray.SetTemplateIcon(icons.SystrayMacTemplate) systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
_ "embed" _ "embed"
"github.com/wailsapp/wails/v3/pkg/events"
"log" "log"
"runtime" "runtime"
@ -29,15 +30,16 @@ func main() {
AlwaysOnTop: true, AlwaysOnTop: true,
Hidden: true, Hidden: true,
DisableResize: true, DisableResize: true,
ShouldClose: func(window *application.WebviewWindow) bool {
window.Hide()
return false
},
Windows: application.WindowsWindow{ Windows: application.WindowsWindow{
HiddenOnTaskbar: true, HiddenOnTaskbar: true,
}, },
}) })
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
window.Hide()
e.Cancel()
})
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
systemTray.SetTemplateIcon(icons.SystrayMacTemplate) systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
} }

View File

@ -2,6 +2,7 @@ package main
import ( import (
_ "embed" _ "embed"
"github.com/wailsapp/wails/v3/pkg/events"
"log" "log"
"runtime" "runtime"
@ -29,10 +30,6 @@ func main() {
AlwaysOnTop: true, AlwaysOnTop: true,
Hidden: true, Hidden: true,
DisableResize: true, DisableResize: true,
ShouldClose: func(window *application.WebviewWindow) bool {
window.Hide()
return false
},
Windows: application.WindowsWindow{ Windows: application.WindowsWindow{
HiddenOnTaskbar: true, HiddenOnTaskbar: true,
}, },
@ -43,6 +40,11 @@ func main() {
}, },
}) })
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
window.Hide()
e.Cancel()
})
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
systemTray.SetTemplateIcon(icons.SystrayMacTemplate) systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
} }

View File

@ -283,31 +283,29 @@ func main() {
myMenu.Add("New WebviewWindow (Hides on Close one time)"). myMenu.Add("New WebviewWindow (Hides on Close one time)").
SetAccelerator("CmdOrCtrl+H"). SetAccelerator("CmdOrCtrl+H").
OnClick(func(ctx *application.Context) { OnClick(func(ctx *application.Context) {
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ w := app.NewWebviewWindow()
// This will be called when the user clicks the close button
// on the window. It will hide the window for 5 seconds. w.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
// If the user clicks the close button again, the window will if !lo.Contains(hiddenWindows, w) {
// close. hiddenWindows = append(hiddenWindows, w)
ShouldClose: func(window *application.WebviewWindow) bool { go func() {
if !lo.Contains(hiddenWindows, window) { time.Sleep(5 * time.Second)
hiddenWindows = append(hiddenWindows, window) w.Show()
go func() { }()
time.Sleep(5 * time.Second) w.Hide()
window.Show() e.Cancel()
}() }
window.Hide() // Remove the window from the hiddenWindows list
return false hiddenWindows = lo.Without(hiddenWindows, w)
} })
// Remove the window from the hiddenWindows list
hiddenWindows = lo.Without(hiddenWindows, window) w.SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)).
return true
},
}).
SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)).
SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)).
SetURL("https://wails.io"). SetURL("https://wails.io").
Show() Show()
windowCounter++ windowCounter++
}) })
myMenu.Add("New WebviewWindow (Frameless)"). myMenu.Add("New WebviewWindow (Frameless)").
SetAccelerator("CmdOrCtrl+F"). SetAccelerator("CmdOrCtrl+F").

View File

@ -60,27 +60,21 @@ func main() {
myMenu.Add("New WebviewWindow (Hides on Close one time)"). myMenu.Add("New WebviewWindow (Hides on Close one time)").
SetAccelerator("CmdOrCtrl+H"). SetAccelerator("CmdOrCtrl+H").
OnClick(func(ctx *application.Context) { OnClick(func(ctx *application.Context) {
app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ w := app.NewWebviewWindow()
// This will be called when the user clicks the close button w.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
// on the window. It will hide the window for 5 seconds. if !lo.Contains(hiddenWindows, w) {
// If the user clicks the close button again, the window will hiddenWindows = append(hiddenWindows, w)
// close. go func() {
ShouldClose: func(window *application.WebviewWindow) bool { time.Sleep(5 * time.Second)
if !lo.Contains(hiddenWindows, window) { w.Show()
hiddenWindows = append(hiddenWindows, window) }()
go func() { w.Hide()
time.Sleep(5 * time.Second) e.Cancel()
window.Show() }
}() // Remove the window from the hiddenWindows list
window.Hide() hiddenWindows = lo.Without(hiddenWindows, w)
return false })
} w.SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)).
// Remove the window from the hiddenWindows list
hiddenWindows = lo.Without(hiddenWindows, window)
return true
},
}).
SetTitle("WebviewWindow "+strconv.Itoa(windowCounter)).
SetRelativePosition(rand.Intn(1000), rand.Intn(800)). SetRelativePosition(rand.Intn(1000), rand.Intn(800)).
SetURL("https://wails.io"). SetURL("https://wails.io").
Show() Show()

View File

@ -1,7 +1,7 @@
{ {
"name": "@wailsio/runtime", "name": "@wailsio/runtime",
"type": "module", "type": "module",
"version": "3.0.0-alpha.34", "version": "3.0.0-alpha.35",
"description": "Wails Runtime", "description": "Wails Runtime",
"types": "types/index.d.ts", "types": "types/index.d.ts",
"exports": { "exports": {

View File

@ -19,7 +19,7 @@ export const EventTypes = {
WindowRestore: "windows:WindowRestore", WindowRestore: "windows:WindowRestore",
WindowMinimise: "windows:WindowMinimise", WindowMinimise: "windows:WindowMinimise",
WindowUnMinimise: "windows:WindowUnMinimise", WindowUnMinimise: "windows:WindowUnMinimise",
WindowClose: "windows:WindowClose", WindowClosing: "windows:WindowClosing",
WindowSetFocus: "windows:WindowSetFocus", WindowSetFocus: "windows:WindowSetFocus",
WindowKillFocus: "windows:WindowKillFocus", WindowKillFocus: "windows:WindowKillFocus",
WindowDragDrop: "windows:WindowDragDrop", WindowDragDrop: "windows:WindowDragDrop",

View File

@ -19,7 +19,7 @@ export declare const EventTypes: {
WindowRestore: string, WindowRestore: string,
WindowMinimise: string, WindowMinimise: string,
WindowUnMinimise: string, WindowUnMinimise: string,
WindowClose: string, WindowClosing: string,
WindowSetFocus: string, WindowSetFocus: string,
WindowKillFocus: string, WindowKillFocus: string,
WindowDragDrop: string, WindowDragDrop: string,

View File

@ -162,6 +162,9 @@ type WebviewWindow struct {
runtimeLoaded bool runtimeLoaded bool
// pendingJS holds JS that was sent to the window before the runtime was loaded // pendingJS holds JS that was sent to the window before the runtime was loaded
pendingJS []string pendingJS []string
// unconditionallyClose marks the window to be unconditionally closed
unconditionallyClose bool
} }
// EmitEvent emits an event from the window // EmitEvent emits an event from the window
@ -250,15 +253,10 @@ func NewWindow(options WebviewWindowOptions) *WebviewWindow {
// Listen for window closing events and de // Listen for window closing events and de
result.OnWindowEvent(events.Common.WindowClosing, func(event *WindowEvent) { result.OnWindowEvent(events.Common.WindowClosing, func(event *WindowEvent) {
shouldClose := true result.unconditionallyClose = true
if result.options.ShouldClose != nil { result.markAsDestroyed()
shouldClose = result.options.ShouldClose(result) InvokeSync(result.impl.close)
} globalApplication.deleteWindowByID(result.id)
if shouldClose {
result.markAsDestroyed()
InvokeSync(result.impl.close)
globalApplication.deleteWindowByID(result.id)
}
}) })
// Process keybindings // Process keybindings
@ -395,6 +393,7 @@ func (w *WebviewWindow) Run() {
return return
} }
w.impl = newWindowImpl(w) w.impl = newWindowImpl(w)
InvokeSync(w.impl.run) InvokeSync(w.impl.run)
} }
@ -991,7 +990,12 @@ func (w *WebviewWindow) ZoomOut() {
// Close closes the window // Close closes the window
func (w *WebviewWindow) Close() { func (w *WebviewWindow) Close() {
// NOOP? if w.impl == nil && !w.isDestroyed() {
return
}
InvokeAsync(func() {
w.emit(events.Common.WindowClosing)
})
} }
func (w *WebviewWindow) Zoom() { func (w *WebviewWindow) Zoom() {
@ -1337,3 +1341,9 @@ func (w *WebviewWindow) delete() {
w.impl.delete() w.impl.delete()
} }
} }
func (w *WebviewWindow) redo() {
if w.impl == nil && !w.isDestroyed() {
w.impl.redo()
}
}

View File

@ -956,6 +956,7 @@ func (w *macosWebviewWindow) windowZoom() {
func (w *macosWebviewWindow) close() { func (w *macosWebviewWindow) close() {
C.windowClose(w.nsWindow) C.windowClose(w.nsWindow)
// TODO: Check if we need to unregister the window here or not
} }
func (w *macosWebviewWindow) zoomIn() { func (w *macosWebviewWindow) zoomIn() {

View File

@ -128,10 +128,6 @@ type WebviewWindowOptions struct {
MaximiseButtonState ButtonState MaximiseButtonState ButtonState
CloseButtonState ButtonState CloseButtonState ButtonState
// ShouldClose is called when the window is about to close.
// Return true to allow the window to close, or false to prevent it from closing.
ShouldClose func(window *WebviewWindow) bool
// If true, the window's devtools will be available (default true in builds without the `production` build tag) // If true, the window's devtools will be available (default true in builds without the `production` build tag)
DevToolsEnabled bool DevToolsEnabled bool

View File

@ -577,9 +577,7 @@ func (w *windowsWebviewWindow) setZoom(zoom float64) {
} }
func (w *windowsWebviewWindow) close() { func (w *windowsWebviewWindow) close() {
// Unregister the window with the application // Send WM_CLOSE message to trigger the same flow as clicking the X button
windowsApp := globalApplication.impl.(*windowsApp)
windowsApp.unregisterWindow(w)
w32.SendMessage(w.hwnd, w32.WM_CLOSE, 0, 0) w32.SendMessage(w.hwnd, w32.WM_CLOSE, 0, 0)
} }
@ -1033,8 +1031,22 @@ func (w *windowsWebviewWindow) WndProc(msg uint32, wparam, lparam uintptr) uintp
} }
} }
case w32.WM_CLOSE: case w32.WM_CLOSE:
w.parent.emit(events.Windows.WindowClose)
return 0 if w.parent.unconditionallyClose == false {
// We were called by `Close()` or pressing the close button on the window
w.parent.emit(events.Windows.WindowClosing)
return 0
}
defer func() {
windowsApp := globalApplication.impl.(*windowsApp)
windowsApp.unregisterWindow(w)
}()
// Now do the actual close
return w32.DefWindowProc(w.hwnd, w32.WM_CLOSE, 0, 0)
case w32.WM_KILLFOCUS: case w32.WM_KILLFOCUS:
if w.focusingChromium { if w.focusingChromium {
return 0 return 0

View File

@ -4,7 +4,7 @@ import "runtime"
var defaultWindowEventMapping = map[string]map[WindowEventType]WindowEventType{ var defaultWindowEventMapping = map[string]map[WindowEventType]WindowEventType{
"windows": { "windows": {
Windows.WindowClose: Common.WindowClosing, Windows.WindowClosing: Common.WindowClosing,
Windows.WindowInactive: Common.WindowLostFocus, Windows.WindowInactive: Common.WindowLostFocus,
Windows.WindowClickActive: Common.WindowFocus, Windows.WindowClickActive: Common.WindowFocus,
Windows.WindowActive: Common.WindowFocus, Windows.WindowActive: Common.WindowFocus,

View File

@ -374,7 +374,7 @@ type windowsEvents struct {
WindowRestore WindowEventType WindowRestore WindowEventType
WindowMinimise WindowEventType WindowMinimise WindowEventType
WindowUnMinimise WindowEventType WindowUnMinimise WindowEventType
WindowClose WindowEventType WindowClosing WindowEventType
WindowSetFocus WindowEventType WindowSetFocus WindowEventType
WindowKillFocus WindowEventType WindowKillFocus WindowEventType
WindowDragDrop WindowEventType WindowDragDrop WindowEventType
@ -421,7 +421,7 @@ func newWindowsEvents() windowsEvents {
WindowRestore: 1175, WindowRestore: 1175,
WindowMinimise: 1176, WindowMinimise: 1176,
WindowUnMinimise: 1177, WindowUnMinimise: 1177,
WindowClose: 1178, WindowClosing: 1178,
WindowSetFocus: 1179, WindowSetFocus: 1179,
WindowKillFocus: 1180, WindowKillFocus: 1180,
WindowDragDrop: 1181, WindowDragDrop: 1181,
@ -608,7 +608,7 @@ var eventToJS = map[uint]string{
1175: "windows:WindowRestore", 1175: "windows:WindowRestore",
1176: "windows:WindowMinimise", 1176: "windows:WindowMinimise",
1177: "windows:WindowUnMinimise", 1177: "windows:WindowUnMinimise",
1178: "windows:WindowClose", 1178: "windows:WindowClosing",
1179: "windows:WindowSetFocus", 1179: "windows:WindowSetFocus",
1180: "windows:WindowKillFocus", 1180: "windows:WindowKillFocus",
1181: "windows:WindowDragDrop", 1181: "windows:WindowDragDrop",

View File

@ -152,7 +152,7 @@ windows:WindowUnFullscreen
windows:WindowRestore windows:WindowRestore
windows:WindowMinimise windows:WindowMinimise
windows:WindowUnMinimise windows:WindowUnMinimise
windows:WindowClose windows:WindowClosing
windows:WindowSetFocus windows:WindowSetFocus
windows:WindowKillFocus windows:WindowKillFocus
windows:WindowDragDrop windows:WindowDragDrop