Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clean close example #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.exe
17 changes: 17 additions & 0 deletions examples/build.bat
Original file line number Diff line number Diff line change
@@ -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%
15 changes: 15 additions & 0 deletions examples/close/close.exe.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="SomeFunkyNameHere" type="win32"/>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
</dependentAssembly>
</dependency>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True</dpiAware>
</windowsSettings>
</application>
</assembly>
67 changes: 67 additions & 0 deletions examples/close/close.go
Original file line number Diff line number Diff line change
@@ -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
}
Binary file added examples/close/rsrc.syso
Binary file not shown.