From ffc0765a2f894bbbcc2b56857be250eb5a44d6c3 Mon Sep 17 00:00:00 2001 From: "Lea\\Anthony" Date: Sat, 4 Dec 2021 14:31:59 +1100 Subject: [PATCH] [website] Add OnBeforeClose docs --- website/docs/howdoesitwork.mdx | 2 ++ website/docs/reference/options.mdx | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/website/docs/howdoesitwork.mdx b/website/docs/howdoesitwork.mdx index 24c028c68..6b8b368a6 100644 --- a/website/docs/howdoesitwork.mdx +++ b/website/docs/howdoesitwork.mdx @@ -119,6 +119,8 @@ A standard Go context is passed to this method. This context is required when ca a reference to in this method. Just before the application shuts down, the [OnShutdown](/docs/reference/options#OnShutdown) callback is called in the same way, again with the context. There is also an [OnDomReady](/docs/reference/options#OnDomReady) callback for when the frontend has completed loading all assets in `index.html` and is equivalent of the [`body onload`](https://www.w3schools.com/jsref/event_onload.asp) event in Javascript. +It is also possible to hook into the window close (or application quit) event by setting the +option [OnBeforeClose](/docs/reference/options#OnBeforeClose). #### Method Binding diff --git a/website/docs/reference/options.mdx b/website/docs/reference/options.mdx index 9744afe6f..4af7a330e 100644 --- a/website/docs/reference/options.mdx +++ b/website/docs/reference/options.mdx @@ -260,6 +260,31 @@ Type: func(ctx context.Context) This callback is called after the frontend has been destroyed, just before the application terminates. It is given the application context. +### OnBeforeClose + +Name: OnBeforeClose + +Type: func(ctx context.Context) bool + +If this callback is set, it will be called when the application is about to quit, either by clicking the window close +button or calling `runtime.Quit`. Returning true will cause the application to continue, false will continue shutdown +as normal. This is good for confirming with the user that they wish to exit the program. + +Example: +```go title=windowsapp.go +func (b *App) beforeClose(ctx context.Context) (prevent bool) { + dialog, err := runtime.MessageDialog(ctx, runtime.MessageDialogOptions{ + Type: runtime.QuestionDialog, + Title: "Quit?", + Message: "Are you sure you want to quit?", + }) + + if err != nil { + return false + } + return dialog != "Yes" +``` + ### WindowStartState Name: WindowStartState