From b2c43c034fa55ae3254da30735f21cce0d53d016 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Thu, 27 Feb 2025 21:01:14 +1100 Subject: [PATCH] [windows]Better popup menu positioning --- docs/src/content/docs/changelog.mdx | 1 + v3/pkg/application/popupmenu_windows.go | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/src/content/docs/changelog.mdx b/docs/src/content/docs/changelog.mdx index 5047a6feb..c4d990635 100644 --- a/docs/src/content/docs/changelog.mdx +++ b/docs/src/content/docs/changelog.mdx @@ -110,6 +110,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `ServiceStartup` hooks are now invoked when `App.Run` is called, not in `application.New` by [@fbbdev](https://github.com/fbbdev) in [#4066](https://github.com/wailsapp/wails/pull/4066) - `ServiceStartup` errors are now returned from `App.Run` instead of terminating the process by [@fbbdev](https://github.com/fbbdev) in [#4066](https://github.com/wailsapp/wails/pull/4066) - Binding and dialog calls from JS now reject with error objects instead of strings by [@fbbdev](https://github.com/fbbdev) in [#4066](https://github.com/wailsapp/wails/pull/4066) +- Improved systray menu positioning on Windows by [@leaanthony](https://github.com/leaanthony) ## v3.0.0-alpha.9 - 2025-01-13 diff --git a/v3/pkg/application/popupmenu_windows.go b/v3/pkg/application/popupmenu_windows.go index 34bee3995..fbfd5eef9 100644 --- a/v3/pkg/application/popupmenu_windows.go +++ b/v3/pkg/application/popupmenu_windows.go @@ -2,6 +2,7 @@ package application import ( "github.com/wailsapp/wails/v3/pkg/w32" + "unsafe" ) const ( @@ -191,7 +192,26 @@ func (p *Win32Menu) ShowAt(x int, y int) { p.onMenuOpen() } - if !w32.TrackPopupMenuEx(p.menu, w32.TPM_LEFTALIGN, int32(x), int32(y-5), p.parent, nil) { + // Get screen dimensions to determine menu positioning + monitor := w32.MonitorFromWindow(p.parent, w32.MONITOR_DEFAULTTONEAREST) + var monitorInfo w32.MONITORINFO + monitorInfo.CbSize = uint32(unsafe.Sizeof(monitorInfo)) + if !w32.GetMonitorInfo(monitor, &monitorInfo) { + globalApplication.fatal("GetMonitorInfo failed") + } + + // Set flags to always position the menu above the cursor + menuFlags := uint32(w32.TPM_LEFTALIGN | w32.TPM_BOTTOMALIGN) + + // Check if we're close to the right edge of the screen + // If so, right-align the menu with some padding + if x > int(monitorInfo.RcWork.Right)-200 { // Assuming 200px as a reasonable menu width + menuFlags = uint32(w32.TPM_RIGHTALIGN | w32.TPM_BOTTOMALIGN) + // Add a small padding (10px) from the right edge + x = int(monitorInfo.RcWork.Right) - 10 + } + + if !w32.TrackPopupMenuEx(p.menu, menuFlags, int32(x), int32(y), p.parent, nil) { globalApplication.fatal("TrackPopupMenu failed") }