try coexist with walk

This commit is contained in:
Joesis
2020-05-10 23:33:32 -07:00
parent f422d90725
commit a05d1b4d22
5 changed files with 76 additions and 10 deletions
+15
View File
@@ -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="Lantern" 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>
+33 -4
View File
@@ -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() {
BIN
View File
Binary file not shown.
+16
View File
@@ -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) {
+12 -6
View File
@@ -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