5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-06 12:41:55 +08:00

[windows] Add systray.OpenMenu

This commit is contained in:
Lea Anthony 2023-09-21 19:14:44 +10:00
parent 013ec1d726
commit fe48b9d03d
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
5 changed files with 48 additions and 7 deletions

View File

@ -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: "<html><body><h1>This window should be centered to the icon!</h1><p>F12 should open the menu</p></body></html>",
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")

View File

@ -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 {

View File

@ -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)
}

View File

@ -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

View File

@ -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)