Matt Legend Gemmell Modesty is Lying

Mac OS X Cocoa and iPhone Development Services available at Instinctive Code.
Mac OS X Cocoa and iPhone Developer for hire

Other Pages

Categories

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.


7 Comments

MAAttachedWindow
3 October 2007 @ 2pm

[...] 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. [...]


Pieter Omvlee
3 October 2007 @ 3pm

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


Brandon Walkin
3 October 2007 @ 3pm

This looks great for inline help. Thanks!


Shamyl Zakariya
3 October 2007 @ 5pm

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


Jenix
4 October 2007 @ 2am

Cool!!! :)


Petey Dennings
4 October 2007 @ 6am

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?


Matt Legend Gemmell
4 October 2007 @ 8am

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!


Leave a Comment

MATrackingArea for NSView Setting the text color of an NSButton