From a05d1b4d222592208d951f1345fdcceb538bfe00 Mon Sep 17 00:00:00 2001 From: Joesis Date: Fri, 8 May 2020 01:06:18 -0700 Subject: [PATCH] try coexist with walk --- example/example.manifest | 15 +++++++++++++++ example/main.go | 37 +++++++++++++++++++++++++++++++++---- example/rsrc.syso | Bin 0 -> 1045 bytes systray.go | 16 ++++++++++++++++ systray_windows.go | 18 ++++++++++++------ 5 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 example/example.manifest create mode 100644 example/rsrc.syso diff --git a/example/example.manifest b/example/example.manifest new file mode 100644 index 0000000..bf75b80 --- /dev/null +++ b/example/example.manifest @@ -0,0 +1,15 @@ + + + + + + + + + + + PerMonitorV2, PerMonitor + True + + + \ No newline at end of file diff --git a/example/main.go b/example/main.go index 71fbf3a..f426fca 100644 --- a/example/main.go +++ b/example/main.go @@ -1,24 +1,53 @@ package main import ( + "flag" "fmt" "io/ioutil" "time" + "github.com/lxn/walk" + "github.com/getlantern/systray" "github.com/getlantern/systray/example/icon" "github.com/skratchdot/open-golang/open" ) +var ( + webview = flag.Bool("webview", false, "show a webview window along with the systray") +) + func main() { + flag.Parse() onExit := func() { - fmt.Println("Starting onExit") now := time.Now() ioutil.WriteFile(fmt.Sprintf(`on_exit_%d.txt`, now.UnixNano()), []byte(now.String()), 0644) - fmt.Println("Finished onExit") } - // Should be called at the very beginning of main(). - systray.Run(onReady, onExit) + + if *webview { + systray.Register(onExit) + onReady() + mainWindow, err := walk.NewMainWindow() + if err != nil { + panic(fmt.Sprintf("Failed to create main window: %v\n", err)) + } + mainWindow.SetTitle("Webview") + mainWindow.SetWidth(800) + mainWindow.SetHeight(600) + layout := walk.NewVBoxLayout() + if err := mainWindow.SetLayout(layout); err != nil { + panic(fmt.Sprintf("Failed to set layout: %v\n", err)) + } + webView, err := walk.NewWebView(mainWindow) + if err != nil { + panic(fmt.Sprintf("Failed to create webview window: %v\n", err)) + } + webView.SetURL("https://www.getlantern.org") + mainWindow.SetVisible(true) + mainWindow.Run() + } else { + systray.Run(onReady, onExit) + } } func onReady() { diff --git a/example/rsrc.syso b/example/rsrc.syso new file mode 100644 index 0000000000000000000000000000000000000000..dbad19625abfeeea83ca5dc19af6828354f8e964 GIT binary patch literal 1045 zcmeZaWMlw=|I7>w5EcugUQuyTGDr}LcQP|D*f20Kd}W3RfYdoKFgQSXFp`l$f`I{q z8(<<3nh|WS0RsbuI427Og9ifxLj(f@Ln72{kRBWRirgH9vecsD%=|oCB||*}C56{=NoFR7CML0qBjeFt`|J`Ug5z$IXQw@EB0$jMAjEXmBz zBQo}kl@LK=Qx5THaB4|OW?p(RLI&)Pl!8ph^2DOlywu`inCTfMB?VUc`q1Q|2TvY) z$@#hZ!M>jQMh1pv`p^J`nX6)l~P)1{CK+fu*Y|YHT!oa`)0GzZXj{pDw literal 0 HcmV?d00001 diff --git a/systray.go b/systray.go index 0258ed3..2109657 100644 --- a/systray.go +++ b/systray.go @@ -103,6 +103,22 @@ func Run(onReady func(), onExit func()) { nativeLoop() } +func Register(onExit func()) { + runtime.LockOSThread() + atomic.StoreInt64(&hasStarted, 1) + // unlike onReady, onExit runs in the event loop to make sure it has time to + // finish before the process terminates + if onExit == nil { + onExit = func() {} + } + systrayExit = onExit + register() +} + +func Run2() { + run() +} + // Quit the systray func Quit() { if atomic.LoadInt64(&hasStarted) == 1 && atomic.CompareAndSwapInt64(&hasQuit, 0, 1) { diff --git a/systray_windows.go b/systray_windows.go index c235796..4736b02 100644 --- a/systray_windows.go +++ b/systray_windows.go @@ -238,6 +238,7 @@ func (t *winTray) wndProc(hWnd windows.Handle, message uint32, wParam, lParam ui const ( WM_COMMAND = 0x0111 WM_DESTROY = 0x0002 + WM_CLOSE = 0x0010 WM_ENDSESSION = 0x16 WM_RBUTTONUP = 0x0205 WM_LBUTTONUP = 0x0202 @@ -249,6 +250,9 @@ func (t *winTray) wndProc(hWnd windows.Handle, message uint32, wParam, lParam ui if menuItemId != 0 { systrayMenuItemSelected(menuItemId) } + case WM_CLOSE: + pDestroyWindow.Call(uintptr(t.window)) + t.wcex.unregister() case WM_DESTROY: // same as WM_ENDSESSION, but throws 0 exit code after all defer pPostQuitMessage.Call(uintptr(int32(0))) @@ -709,6 +713,12 @@ func (t *winTray) iconToBitmap(hIcon windows.Handle) (windows.Handle, error) { } func nativeLoop() { + register() + go systrayReady() + run() +} + +func register() { if err := wt.initInstance(); err != nil { log.Errorf("Unable to init instance: %v", err) return @@ -719,13 +729,9 @@ func nativeLoop() { return } - defer func() { - pDestroyWindow.Call(uintptr(wt.window)) - wt.wcex.unregister() - }() - - go systrayReady() +} +func run() { // Main message pump. m := &struct { WindowHandle windows.Handle