From fe48b9d03dd1a64dcc02c0428b8c99779674885e Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Thu, 21 Sep 2023 19:14:44 +1000 Subject: [PATCH] [windows] Add `systray.OpenMenu` --- v3/examples/systray/main.go | 9 ++++++++- v3/pkg/application/popupmenu_windows.go | 15 ++++++++++----- v3/pkg/application/systemtray.go | 12 +++++++++++- v3/pkg/application/systemtray_windows.go | 15 +++++++++++++++ v3/pkg/application/webview_window_windows.go | 4 ++++ 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/v3/examples/systray/main.go b/v3/examples/systray/main.go index 657cbdf96..1c77a14d9 100644 --- a/v3/examples/systray/main.go +++ b/v3/examples/systray/main.go @@ -18,12 +18,15 @@ func main() { }, }) + systemTray := app.NewSystemTray() + window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{ Width: 500, Height: 800, Frameless: true, AlwaysOnTop: true, Hidden: true, + HTML: "

This window should be centered to the icon!

F12 should open the menu

", ShouldClose: func(window *application.WebviewWindow) bool { window.Hide() return false @@ -31,9 +34,13 @@ func main() { Windows: application.WindowsWindow{ HiddenOnTaskbar: true, }, + KeyBindings: map[string]func(window *application.WebviewWindow){ + "F12": func(window *application.WebviewWindow) { + systemTray.OpenMenu() + }, + }, }) - systemTray := app.NewSystemTray() if runtime.GOOS == "darwin" { systemTray.SetTemplateIcon(icons.SystrayMacTemplate) systemTray.SetLabel("\u001B[1;31mW\u001B[1;32ma\u001B[1;33mi\u001B[1;34ml\u001B[1;35ms\u001B[0m") diff --git a/v3/pkg/application/popupmenu_windows.go b/v3/pkg/application/popupmenu_windows.go index 3ff6a073a..c386f8c85 100644 --- a/v3/pkg/application/popupmenu_windows.go +++ b/v3/pkg/application/popupmenu_windows.go @@ -151,11 +151,7 @@ func NewApplicationMenu(parent w32.HWND, inputMenu *Menu) *Win32Menu { return result } -func (p *Win32Menu) ShowAtCursor() { - x, y, ok := w32.GetCursorPos() - if ok == false { - w32.Fatal("GetCursorPos failed") - } +func (p *Win32Menu) ShowAt(x int, y int) { w32.SetForegroundWindow(p.parent) @@ -177,6 +173,15 @@ func (p *Win32Menu) ShowAtCursor() { } +func (p *Win32Menu) ShowAtCursor() { + x, y, ok := w32.GetCursorPos() + if ok == false { + w32.Fatal("GetCursorPos failed") + } + + p.ShowAt(x, y) +} + func (p *Win32Menu) ProcessCommand(cmdMsgID int) bool { item := p.menuMapping[cmdMsgID] if item == nil { diff --git a/v3/pkg/application/systemtray.go b/v3/pkg/application/systemtray.go index bf805762d..01cfd8442 100644 --- a/v3/pkg/application/systemtray.go +++ b/v3/pkg/application/systemtray.go @@ -35,6 +35,7 @@ type systemTrayImpl interface { bounds() (*Rect, error) getScreen() (*Screen, error) positionWindow(window *WebviewWindow, offset int) error + openMenu() } type PositionOptions struct { @@ -150,7 +151,6 @@ func (s *SystemTray) SetDarkModeIcon(icon []byte) *SystemTray { } func (s *SystemTray) SetMenu(menu *Menu) *SystemTray { - fmt.Println("SystemTray.SetMenu", menu, s.impl) if s.impl == nil { s.menu = menu } else { @@ -286,3 +286,13 @@ func (s *SystemTray) defaultClickHandler() { s.attachedWindow.Window.Show().Focus() } } + +func (s *SystemTray) OpenMenu() { + if s.menu == nil { + return + } + if s.impl == nil { + return + } + invokeSync(s.impl.openMenu) +} diff --git a/v3/pkg/application/systemtray_windows.go b/v3/pkg/application/systemtray_windows.go index d40739b1a..a7a40b355 100644 --- a/v3/pkg/application/systemtray_windows.go +++ b/v3/pkg/application/systemtray_windows.go @@ -30,6 +30,21 @@ type windowsSystemTray struct { currentIcon w32.HICON } +func (s *windowsSystemTray) openMenu() { + if s.menu == nil { + return + } + // Get the system tray bounds + trayBounds, err := s.bounds() + if err != nil { + return + } + + // Show the menu at the tray bounds + s.menu.ShowAt(trayBounds.X, trayBounds.Y) + +} + func (s *windowsSystemTray) positionWindow(window *WebviewWindow, offset int) error { // Get the trayBounds of this system tray diff --git a/v3/pkg/application/webview_window_windows.go b/v3/pkg/application/webview_window_windows.go index 3026045cb..a24eab698 100644 --- a/v3/pkg/application/webview_window_windows.go +++ b/v3/pkg/application/webview_window_windows.go @@ -64,6 +64,10 @@ type windowsWebviewWindow struct { focusingChromium bool } +func (w *windowsWebviewWindow) handleKeyEvent(_ string) { + // Unused on windows +} + func (w *windowsWebviewWindow) setAbsolutePosition(x int, y int) { // Set the window's absolute position w32.SetWindowPos(w.hwnd, 0, x, y, 0, 0, w32.SWP_NOSIZE|w32.SWP_NOZORDER)