Skip to content

Popup anchor system refinement - #4646

Merged
ogoffart merged 46 commits into
rust-windowing:masterfrom
Murmele:popup-anchor
Sep 2, 2026
Merged

ogoffart merged 46 commits into
rust-windowing:masterfrom
Murmele:popup-anchor

Conversation

@Murmele

@Murmele Murmele commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Implement possibility to change the anchor properties of a popup on wayland and imitate the behaviour for other platforms which do not support such anchoring system

  • Added an entry to the changelog module if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality

Tested on:

  • Windows
  • Wayland
  • MacOs
  • X11 Does not support popups

In the example for wayland when clicking outside of the window all popups get closed

  • This is a compositor request because with_active (request keyboard grab) is true. Setting in the example active to false the popup will not be dismissed by the compositor. It is just because it looses the grab and then it gets dismissed

@Murmele Murmele mentioned this pull request Jul 27, 2026
2 of 8 tasks
@Murmele
Murmele marked this pull request as ready for review July 28, 2026 05:51
Comment thread winit-appkit/src/window_delegate.rs Resolved
Comment thread winit-appkit/src/window_delegate.rs Resolved
Comment thread winit-appkit/src/window_delegate.rs Resolved
Comment thread winit-core/src/window/positioner.rs Resolved
Comment thread winit-core/src/window.rs Resolved
Previously every close request left the event_loop which is not correct when only a popup closes
Reason: otherwise the popup might disappear behind the task bar
@Murmele
Murmele requested a review from ogoffart August 12, 2026 14:47
@Murmele

Murmele commented Aug 12, 2026

Copy link
Copy Markdown
Contributor Author

But we can't re-use it for ChildWindow in Slint because slint-core don't depend on winit on purpose. in fact, the implementation of that function might be better placed in winit-common

winit-common depends on winit-core so we cannot use this as well. It has to be public otherwise the winit-win32 crate and the macos crate are not able to access it

Comment thread winit-core/src/window.rs Resolved
Comment thread winit-core/src/window.rs Resolved
Comment thread winit-wayland/src/popup.rs Resolved
Comment thread winit-win32/src/window.rs Resolved
Reason: otherwise the popup will be placed wrongly
@Murmele
Murmele requested a review from ogoffart August 20, 2026 07:02
#Conflicts:
#	winit-core/src/window.rs
@ogoffart
ogoffart merged commit 4ddca89 into rust-windowing:master Sep 2, 2026
50 checks passed
kchibisov pushed a commit that referenced this pull request Sep 4, 2026
Implement possibility to change the anchor properties of a popup on wayland and imitate the behaviour for other platforms which do not support such anchoring system
ogoffart pushed a commit that referenced this pull request Sep 19, 2026
)

This fixes a crash introduced by #4646. It has already shipped in 0.31.0-beta.3, so hopefully this fix can be merged before the next stable release. We hit it by dragging the window in our project, [Graphite](https://github.com/GraphiteEditor/Graphite), which freezes the app on the first move and followed shortly by a crash to desktop.

`reposition_owned_windows` runs on every `WM_WINDOWPOSCHANGED` that moves a window. It enumerates all windows on the thread that are owned by the moved window, reads each one's `GWL_USERDATA`, and dereferences the value as `*mut WindowData`:

```rs
let userdata_ptr = unsafe { util::get_window_long(hwnd, GWL_USERDATA) } as *mut WindowData;
if !userdata_ptr.is_null() {
    let userdata = unsafe { &*userdata_ptr };
```

Nothing checks that the enumerated window was created by winit. `GWL_USERDATA` is free for any window's creator to use, so any owned window that stores something non-zero there is dereferenced as a winit struct. That covers windows the application creates itself through `raw-window-handle`, and potentially dialogs or controls created on the same thread by other libraries.

In Graphite, we are drawing a custom frame and implementing native resize borders with an invisible helper window owned by the winit window. The helper stores the main window's `HWND` in `GWLP_USERDATA`. Moving the winit window therefore crashes on the first position change, every time:

- `0xC0000005` access violation in `winit_win32::event_loop::reposition_owned_windows::enum_proc`
- followed by `0xC000041D` (`STATUS_FATAL_USER_CALLBACK_EXCEPTION`), since the fault happens inside the window procedure while Windows is dispatching the message

This PR fixes it by comparing the window's `GWL_WNDPROC` against `public_window_callback` before touching `GWL_USERDATA` and skipping the window when they differ. Winit's window procedure is what defines the meaning of that slot, so this is the narrowest test that the pointer really is a `WindowData`. Anchored popups still follow their owner with this change.

To reproduce outside of Graphite, on winit's `master`, in any of its examples: create a top-level Win32 window (e.g. `WS_POPUP`) with `CreateWindowExW`, passing the winit window's `HWND` as the owner, call `SetWindowLongPtrW(helper, GWLP_USERDATA, 1)`, then move the winit window by dragging it or calling `set_outer_position`. It dies on the first move. A debug build panics with "misaligned pointer dereference" at the `&*userdata_ptr` line, and a release build exits with `0xC000041D`. With this PR it does not.

Tested on Windows 11 25H2 (build 26200.9445).
wilsonglasser pushed a commit to wilsonglasser/winit that referenced this pull request Sep 27, 2026
…st-windowing#4700)

This fixes a crash introduced by rust-windowing#4646. It has already shipped in 0.31.0-beta.3, so hopefully this fix can be merged before the next stable release. We hit it by dragging the window in our project, [Graphite](https://github.com/GraphiteEditor/Graphite), which freezes the app on the first move and followed shortly by a crash to desktop.

`reposition_owned_windows` runs on every `WM_WINDOWPOSCHANGED` that moves a window. It enumerates all windows on the thread that are owned by the moved window, reads each one's `GWL_USERDATA`, and dereferences the value as `*mut WindowData`:

```rs
let userdata_ptr = unsafe { util::get_window_long(hwnd, GWL_USERDATA) } as *mut WindowData;
if !userdata_ptr.is_null() {
    let userdata = unsafe { &*userdata_ptr };
```

Nothing checks that the enumerated window was created by winit. `GWL_USERDATA` is free for any window's creator to use, so any owned window that stores something non-zero there is dereferenced as a winit struct. That covers windows the application creates itself through `raw-window-handle`, and potentially dialogs or controls created on the same thread by other libraries.

In Graphite, we are drawing a custom frame and implementing native resize borders with an invisible helper window owned by the winit window. The helper stores the main window's `HWND` in `GWLP_USERDATA`. Moving the winit window therefore crashes on the first position change, every time:

- `0xC0000005` access violation in `winit_win32::event_loop::reposition_owned_windows::enum_proc`
- followed by `0xC000041D` (`STATUS_FATAL_USER_CALLBACK_EXCEPTION`), since the fault happens inside the window procedure while Windows is dispatching the message

This PR fixes it by comparing the window's `GWL_WNDPROC` against `public_window_callback` before touching `GWL_USERDATA` and skipping the window when they differ. Winit's window procedure is what defines the meaning of that slot, so this is the narrowest test that the pointer really is a `WindowData`. Anchored popups still follow their owner with this change.

To reproduce outside of Graphite, on winit's `master`, in any of its examples: create a top-level Win32 window (e.g. `WS_POPUP`) with `CreateWindowExW`, passing the winit window's `HWND` as the owner, call `SetWindowLongPtrW(helper, GWLP_USERDATA, 1)`, then move the winit window by dragging it or calling `set_outer_position`. It dies on the first move. A debug build panics with "misaligned pointer dereference" at the `&*userdata_ptr` line, and a release build exits with `0xC000041D`. With this PR it does not.

Tested on Windows 11 25H2 (build 26200.9445).
kchibisov pushed a commit that referenced this pull request Oct 24, 2026
)

This fixes a crash introduced by #4646. It has already shipped in 0.31.0-beta.3, so hopefully this fix can be merged before the next stable release. We hit it by dragging the window in our project, [Graphite](https://github.com/GraphiteEditor/Graphite), which freezes the app on the first move and followed shortly by a crash to desktop.

`reposition_owned_windows` runs on every `WM_WINDOWPOSCHANGED` that moves a window. It enumerates all windows on the thread that are owned by the moved window, reads each one's `GWL_USERDATA`, and dereferences the value as `*mut WindowData`:

```rs
let userdata_ptr = unsafe { util::get_window_long(hwnd, GWL_USERDATA) } as *mut WindowData;
if !userdata_ptr.is_null() {
    let userdata = unsafe { &*userdata_ptr };
```

Nothing checks that the enumerated window was created by winit. `GWL_USERDATA` is free for any window's creator to use, so any owned window that stores something non-zero there is dereferenced as a winit struct. That covers windows the application creates itself through `raw-window-handle`, and potentially dialogs or controls created on the same thread by other libraries.

In Graphite, we are drawing a custom frame and implementing native resize borders with an invisible helper window owned by the winit window. The helper stores the main window's `HWND` in `GWLP_USERDATA`. Moving the winit window therefore crashes on the first position change, every time:

- `0xC0000005` access violation in `winit_win32::event_loop::reposition_owned_windows::enum_proc`
- followed by `0xC000041D` (`STATUS_FATAL_USER_CALLBACK_EXCEPTION`), since the fault happens inside the window procedure while Windows is dispatching the message

This PR fixes it by comparing the window's `GWL_WNDPROC` against `public_window_callback` before touching `GWL_USERDATA` and skipping the window when they differ. Winit's window procedure is what defines the meaning of that slot, so this is the narrowest test that the pointer really is a `WindowData`. Anchored popups still follow their owner with this change.

To reproduce outside of Graphite, on winit's `master`, in any of its examples: create a top-level Win32 window (e.g. `WS_POPUP`) with `CreateWindowExW`, passing the winit window's `HWND` as the owner, call `SetWindowLongPtrW(helper, GWLP_USERDATA, 1)`, then move the winit window by dragging it or calling `set_outer_position`. It dies on the first move. A debug build panics with "misaligned pointer dereference" at the `&*userdata_ptr` line, and a release build exits with `0xC000041D`. With this PR it does not.

Tested on Windows 11 25H2 (build 26200.9445).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants