Skip to content

Commit b2c43c0

Browse files
committed
[windows]Better popup menu positioning
1 parent 1dff481 commit b2c43c0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

docs/src/content/docs/changelog.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
110110
- `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)
111111
- `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)
112112
- 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)
113+
- Improved systray menu positioning on Windows by [@leaanthony](https://github.com/leaanthony)
113114

114115
## v3.0.0-alpha.9 - 2025-01-13
115116

v3/pkg/application/popupmenu_windows.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package application
22

33
import (
44
"github.com/wailsapp/wails/v3/pkg/w32"
5+
"unsafe"
56
)
67

78
const (
@@ -191,7 +192,26 @@ func (p *Win32Menu) ShowAt(x int, y int) {
191192
p.onMenuOpen()
192193
}
193194

194-
if !w32.TrackPopupMenuEx(p.menu, w32.TPM_LEFTALIGN, int32(x), int32(y-5), p.parent, nil) {
195+
// Get screen dimensions to determine menu positioning
196+
monitor := w32.MonitorFromWindow(p.parent, w32.MONITOR_DEFAULTTONEAREST)
197+
var monitorInfo w32.MONITORINFO
198+
monitorInfo.CbSize = uint32(unsafe.Sizeof(monitorInfo))
199+
if !w32.GetMonitorInfo(monitor, &monitorInfo) {
200+
globalApplication.fatal("GetMonitorInfo failed")
201+
}
202+
203+
// Set flags to always position the menu above the cursor
204+
menuFlags := uint32(w32.TPM_LEFTALIGN | w32.TPM_BOTTOMALIGN)
205+
206+
// Check if we're close to the right edge of the screen
207+
// If so, right-align the menu with some padding
208+
if x > int(monitorInfo.RcWork.Right)-200 { // Assuming 200px as a reasonable menu width
209+
menuFlags = uint32(w32.TPM_RIGHTALIGN | w32.TPM_BOTTOMALIGN)
210+
// Add a small padding (10px) from the right edge
211+
x = int(monitorInfo.RcWork.Right) - 10
212+
}
213+
214+
if !w32.TrackPopupMenuEx(p.menu, menuFlags, int32(x), int32(y), p.parent, nil) {
195215
globalApplication.fatal("TrackPopupMenu failed")
196216
}
197217

0 commit comments

Comments
 (0)