5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 18:13:20 +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 (
_ "embed"
"log"
"runtime"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
"github.com/wailsapp/wails/v3/pkg/icons"
"log"
"runtime"
)
func main() {
@ -29,16 +28,16 @@ func main() {
AlwaysOnTop: false,
Hidden: false,
DisableResize: false,
ShouldClose: func(window *application.WebviewWindow) bool {
println("close")
window.Hide()
return false
},
Windows: application.WindowsWindow{
HiddenOnTaskbar: true,
},
})
window.RegisterHook(events.Common.WindowClosing, func(e *application.WindowEvent) {
window.Hide()
e.Cancel()
})
if runtime.GOOS == "darwin" {
systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
}

View File

@ -2,6 +2,7 @@ package main
import (
_ "embed"
"github.com/wailsapp/wails/v3/pkg/events"
"log"
"runtime"
@ -29,10 +30,6 @@ func main() {
AlwaysOnTop: true,
Hidden: true,
DisableResize: true,
ShouldClose: func(window *application.WebviewWindow) bool {
window.Hide()
return false
},
Windows: application.WindowsWindow{
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" {
systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
}

View File

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

View File

@ -2,6 +2,7 @@ package main
import (
_ "embed"
"github.com/wailsapp/wails/v3/pkg/events"
"log"
"runtime"
@ -29,10 +30,6 @@ func main() {
AlwaysOnTop: true,
Hidden: true,
DisableResize: true,
ShouldClose: func(window *application.WebviewWindow) bool {
window.Hide()
return false
},
Windows: application.WindowsWindow{
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" {
systemTray.SetTemplateIcon(icons.SystrayMacTemplate)
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -128,10 +128,6 @@ type WebviewWindowOptions struct {
MaximiseButtonState 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)
DevToolsEnabled bool

View File

@ -577,9 +577,7 @@ func (w *windowsWebviewWindow) setZoom(zoom float64) {
}
func (w *windowsWebviewWindow) close() {
// Unregister the window with the application
windowsApp := globalApplication.impl.(*windowsApp)
windowsApp.unregisterWindow(w)
// Send WM_CLOSE message to trigger the same flow as clicking the X button
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:
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:
if w.focusingChromium {
return 0

View File

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

View File

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

View File

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