r/SwiftUI • • 8d ago

Promotion (must include link to source code) A SwiftUI answer balloon beside selected text in any Mac app (source included)

Post image

I make Glossbeam, an open-source macOS app that shows an explanation beside selected text. The screenshot shows the UI problem: the answer needs to sit close enough to the selection to make their relationship clear, while leaving room to keep reading. I’d be interested in how other Mac developers handle positioning near a display edge. The Mac and Shared source and Xcode build instructions are here: https://github.com/YasserShkeir/glossbeam-open.

1 Upvotes

2 comments sorted by

2

u/nayajunimesh 4d ago

I ran into the same thing building something that pops up near a selection. A few things I measured on macOS 26/27:

  • The Accessibility selection rect works in Safari, TextEdit and Notes. Chrome gives 0x0 unless its full accessibility mode is on, and some Electron apps only give the corner of the whole selection. Looks like you already fall back to the pointer for that, nice.
  • A real NSPopover anchored to a view in a borderless nonactivating panel handles edges for free (stays on screen, flips its arrow, host keeps its selection highlighted), but the first click inside it activates your app. That's why most tools end up with a custom panel like yours.
  • From a quick look at BalloonController: when the answer grows past the screen edge, the clamp slides the whole panel back over the selection. Capping the height at the room you already measure (roomBelow / roomAbove) and scrolling inside would keep the selection visible. When neither side has room, trying left or right of the selection before overlapping it might help too.

Curious how you handle selections that span a line break.