3

I'm working on a Cocoa app that has a main window and a preview window. I'd like the preview window to automatically position itself to minimize overlap with other windows — it definitely shouldn't overlap my app's main window, and it should try not to overlap other applications' windows.

How should I do this?

smokris
  • 11,740
  • 2
  • 39
  • 59
  • 1
    Your question is a bit vague. Have you tried anything so far? Which part of this are you most unsure about? Getting other windows' positions? An algorithm for determining a rectangle with the least-overlap of other rectangles? Writing Cocoa code to actually position an NSWindow? – Andrew Madsen Mar 04 '13 at 18:35
  • First, I'd like to know whether there's some simple built-in API for doing this. Failing that, an outline for an algorithm. – smokris Mar 04 '13 at 19:45

1 Answers1

1

Apple has a sample application called Son of Grab that shows you how to consume all current windows.

CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID); will give you the current windows. You can interrogate the kCGWindowBounds value to get the bounds of the returned windows.

Fruity Geek
  • 7,351
  • 1
  • 32
  • 41