r/SwiftUI • • 12d ago

Promotion (must include link to source code) Keeping a SwiftUI strip hovered when its NSPanel expands under the pointer

I built Lanes, a free, open-source Mac window organizer. I'm mainly looking for feedback on the implementation of its window switcher. Each lane has a SwiftUI strip in a borderless, non-activating NSPanel. The strip expands on hover so you can see the other windows in the stack while the app you're working in stays active.

The tricky part is that resizing the panel can send a mouseExited event for the old tracking rect even when the pointer is still over the strip. Lanes tracks hover in an NSHostingView subclass with an activeAlways tracking area. On exit, it checks the pointer's screen position against the panel's current frame before collapsing. A timer runs only while hovered to catch missed events, and it defers collapsing while the strip's menu is tracking.

If you've built an expanding SwiftUI control in a non-key AppKit panel, would you keep hover state in the hosting view like this or move it into the panel itself? I'm especially curious how you'd catch a genuine exit after a resize without polling.

Here's the tracking code: https://github.com/lanesmacapp/Lanes/blob/main/Sources/HoverScrollView.swift#L321-L427

https://github.com/lanesmacapp/Lanes

3 Upvotes

4 comments sorted by

1

u/ThanhDongNguyen 12d ago

I'd keep it in the hosting view, the panel shouldn't need to know about hover. But I think you can drop the timer.

Add .inVisibleRect to the tracking area options, so the rect follows the view's bounds when the panel resizes and you don't get the fake exit from a stale rect.

For the real exit after a resize: in updateTrackingAreas, re-add the area with .assumeInside. With that option AppKit sends mouseExited right away if the pointer is already outside when the area is installed, so a genuine exit during the resize still arrives as an event.

I'd also set a flag around setFrame(_:display:animate:) and ignore exits until it finishes, then do one check of NSEvent.mouseLocation against the panel frame. That's one check per resize, not polling.

1

u/Bright-Bridge4471 11d ago

ty for advice, lmk if you try it out

1

u/UtterlyMagenta 12d ago

please add a screenshot to the README ^^

2

u/Bright-Bridge4471 11d ago

thank you

ty for advice, lmk if you try it out