From 48e2c737305970686f7367effdf9d5e5a31999e3 Mon Sep 17 00:00:00 2001 From: Xackery Date: Mon, 9 Oct 2023 11:04:04 -0700 Subject: [PATCH] Add clean close example --- .gitignore | 1 + examples/build.bat | 17 ++++++++ examples/close/close.exe.manifest | 15 +++++++ examples/close/close.go | 67 ++++++++++++++++++++++++++++++ examples/close/rsrc.syso | Bin 0 -> 1008 bytes 5 files changed, 100 insertions(+) create mode 100644 .gitignore create mode 100644 examples/build.bat create mode 100644 examples/close/close.exe.manifest create mode 100644 examples/close/close.go create mode 100644 examples/close/rsrc.syso diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..adb36c821 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.exe \ No newline at end of file diff --git a/examples/build.bat b/examples/build.bat new file mode 100644 index 000000000..8d7721716 --- /dev/null +++ b/examples/build.bat @@ -0,0 +1,17 @@ +@echo off +:: cd every subdirectory and run command go build +for /d %%i in (*) do ( + :: skip img dir + if "%%i"=="img" goto :skip + set lastdir=%%i + cd %%i || goto error + echo Building %%i + go build || goto error + cd .. +) + +exit /b 0 + +:error +echo Error building %lastdir%: %errorlevel% +exit /b %errorlevel% \ No newline at end of file diff --git a/examples/close/close.exe.manifest b/examples/close/close.exe.manifest new file mode 100644 index 000000000..86aa0d39e --- /dev/null +++ b/examples/close/close.exe.manifest @@ -0,0 +1,15 @@ + + + + + + + + + + + PerMonitorV2, PerMonitor + True + + + diff --git a/examples/close/close.go b/examples/close/close.go new file mode 100644 index 000000000..c4ed9996a --- /dev/null +++ b/examples/close/close.go @@ -0,0 +1,67 @@ +// Copyright 2013 The Walk Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +import ( + "context" + "fmt" + "os" + "time" + + "github.com/tailscale/walk" + + . "github.com/tailscale/walk/declarative" +) + +func main() { + err := run() + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + fmt.Println("Exited cleanly") + os.Exit(0) +} + +func run() error { + var mw *walk.MainWindow + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + if err := (MainWindow{ + AssignTo: &mw, + Title: "Window Closing Test", + Layout: VBox{Spacing: 2}, + Size: Size{800, 600}, + }.Create()); err != nil { + walk.MsgBox(nil, "Error", fmt.Sprintf("%v", err), walk.MsgBoxIconError) + return fmt.Errorf("creating main window: %w", err) + } + + mw.Closing().Attach(func(canceled *bool, reason walk.CloseReason) { + //walk.MsgBox(nil, "Info", fmt.Sprintf("Closing now (reason %d)", reason), walk.MsgBoxIconInformation) + //check if context is done + if ctx.Err() != nil { + return + } + *canceled = true + fmt.Println("Got close message") + mw.SetTitle("Closing...") + cancel() + }) + + go func() { + <-ctx.Done() + fmt.Println("Doing clean up process...") + time.Sleep(1 * time.Second) + mw.Close() + walk.App().Exit(0) + }() + code := mw.Run() + if code != 0 { + return fmt.Errorf("main window closed with %d", code) + } + return nil +} diff --git a/examples/close/rsrc.syso b/examples/close/rsrc.syso new file mode 100644 index 0000000000000000000000000000000000000000..61c3644f87da2160056f3b7c533e7677c8357ce0 GIT binary patch literal 1008 zcmb7D-EPw`6i&5Cs7Ty%8Tk^?r0v=Ys&u28G;M++q?W<0>zuaMVmq>(b-CjqcpEPE z3cKbdcmY`|UvV$Oqd|X}hUJ>6|Dr1@`2{V#|A!n!0o<`Id5bGrO5kQ(5$+VmrwgW*;PjIG{Q5)N1 zN=&H{!R>(ff=a#tW3St62-SVWx{5(sZu4I*6!hJtQmq&mqx4*}9k;-2rtvy2h%c$Y z#Zn2l$mC7_9?eV8Kzy60xUuK*WJtn-Cx#RxaRS_;~jlm}_ zW3@NIxe`Lj-no)itHh8-#l&g&yNWbhC!D>7e5GyxGcg&z9>}s)EUtUOyA3!gZ