MAAttachedWindow NSWindow subclass

I once again come bearing gifts for you: this time it’s MAAttachedWindow, an NSWindow subclass that lets you visually attach borderless windows to other windows or just to specified points on the screen. It’s potentially useful for displaying contextual help, options related to a part of your UI, or just for drawing attention to things.

Take a look at the pic below to see what I mean (you can click it to see a larger version; you’re seeing the demo app included in the download).

I’ve made the class quite configurable, and hopefully you’ll find it useful. Drop me a line in the comments here or via email if you’re using it, or just to lavish some goddamn praise. It’s why I do this, after all (and you thought it was sheer altruism, hah).

There are a bunch of different constructors available, but in most cases you’ll specify a point (either in an existing window, or on screen) at which to attach the window, what side of the point the window should attach to (there are 12 options, plus an “automatic” setting which tries to position the attached window sensibly), and what view to show in the window. Take a look at the sample app to see more about how it works.

Download, as ever, from the Cocoa Source Code page. Please do read the documentation in the MAAttachedWindow.h file; it’ll help you get the most out of the class.

Update: The source has been updated today (5pm GMT on Fri 5th Oct 2007), to fix a possible recursion bug due to the attached window registering for frame-change notifications on itself. We now prevent a potential infinite loop. We also now accept key (but not main) status by default, and have an implementation of -validateMenuItem: which passes off to either the window we’re attached to, or to super.

13 comments

  1. [...] The ever prolific Matt Gemmell has released another great bit of Cocoa code, MAAttachedWindow. It lets you attach borderless windows to other windows or any part of the screen. [...]

  2. This looks very cool. Haven’t tried it yet, but I think you did an excellent job.
    Very impressed :-)

  3. This looks great for inline help. Thanks!

  4. Shamyl Zakariya

    Wow! What’s funny is I made almost exactly this a few months back — a message bubble to help a user input data by forwarding error messages from formatters…

    But given what else I’ve seen here, I pretty much assume yours is better :P

  5. As someone just learning Cocoa, this is very nice! Thanks!

    A question: I noticed that you are declaring several class variables as floats (e.g. hasArrow, drawsRoundCornerBesideArrow), yet they are being set to BOOL values (YES, NO).

    Is this some Cocoaism, or just a slip?

  6. Hi Peter, glad you asked about that. It’s actually a fairly advanced technique known as… copy-pasting a variable declaration several times then forgetting to change some of the types. ;) Fixed in the source now. Cheers for catching that one!

  7. Looks really nice!

    I tried it and while compiling your code works just fine, using it in my project somehow adds a ghost window around the attached window. As if the frame was being drawn too.

    Am I missing something in the project setup in IB?

    Well, I probably am ;-) but if you see what I’m talking about, let me know!

    Thanks a lot.
    Oli./.

  8. Sorry I can’t edit the previous post. I was able to remove this ghost frame by editing the MAAttachedWindow.m and setting the hasShadow to NO.

    Not exactly as nice ;-) but it works. I’ll try to see if I can find the root cause!

    In any case thanks a lot!

  9. [...] 2009-07-14: If I were doing this again I’d use the wonderful MAAttachedWindow for displaying the detail [...]

  10. Thank ye’ Matt.

    In response to Olivers comment, I encountered the same issue integrating it with my code, but only when I made the addChildWindow:ordered: call from within an awakeFromNib.

  11. [...] main interaction point is a MAAttachedWindow (hattip to Matt Gemmell) attached to a NSStatusItem that lives in the Menu Bar. The user can invoke [...]

Leave a comment