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

Posted
4 March 2008 @ 2pm

Categories
Development, Source

Tags
, , , , , ,

Using MAAttachedWindow with an NSStatusItem

I get asked how to do this disproportionately often, so I put together a sample project showing how you can have an NSStatusItem in the menubar which spawns an MAAttachedWindow instead of a menu (really, it’s ridiculously simple - no tricks involved). It looks like this:

screenshot

Could be cool for status displays, alerts, or just having a pic of your significant other floating around on your screen, and it should work on Leopard, Tiger, and very likely Panther too. You can grab the source here.


8 Comments

Ben Perry
4 March 2008 @ 3pm

Might be a nice clean way to show Twitter updates.


Philip
4 March 2008 @ 7pm

The code is nice and clean but one thing I was wondering about is the ability to command click and move the status item along the menubar. To be able to insert it into a different location. Is this easily added?


Collin
5 March 2008 @ 7pm

that looks familiar

great post matt, im glad you helped me with this, I really appreciated it!


Jordy/Jediknil
18 March 2008 @ 1am

Hey, Matt…a question about this spread to the CocoaDev forums and I’d like to point out that you don’t have to put in a custom view. NSStatusItem supports target/action messages that fire when you click on the status item view. You would have to move the center-finding code around but it would eliminate the pretty-much-pointless custom view.

Of course, if the project already uses a custom view then this is no worse.


Ben Perry
25 March 2008 @ 7am

Jordy/Jediknil: I’ve been working on a project using this custom window of Matts and I don’t think you can ditch the custom view. I can’t see another way to find the position of the NSStatusItem to be able to place the MAAttachedWindow properly.

Am I missing something?


Ben Perry
25 March 2008 @ 7am

Sorry I should have mentioned that I’m trying to make the MAAttachedWindow appear periodically - not on a mouse event - kinda like growl i guess, but the arrow needs to be pointing at my NSStatusItem. Still possibly without the custom view??


Philip
25 March 2008 @ 12pm

I’ve been playing around with the NSStatusItem and developed a hack for accessing the StatusItem location.

@interface NSStatusItem (hack)
- (NSRect)hackFrame;
@end

@implementation NSStatusItem (hack)
- (NSRect)hackFrame
{
return [_fWindow frame];
}
@end

This simply sub classes the NSStatusItem and add a new method “hackFrame”. Then in your code you simple call:

NSRect itemRect = [statusItem hackFrame];

statusItem being your object class name. The position of your NSStatusItem is returned in the NSRect.

I’ve been using this to use the genie effect and make my views warp in and out of the status item.


Matthieu Cormier
27 March 2008 @ 2pm

Philip, you have not sub classed NSStatusItem, you’ve added an extension to the already defined class.

With some slight modifications to MAAttachedWindow, by making the following methods public.
- (void)_updateGeometry;
- (MAWindowPosition)_bestSideForAutomaticPosition;
and making the following variables protected
@protected
NSPoint _point;
MAWindowPosition _side;

I extended MAAttached window and am passing it a point when the user clicks on the menuItem.

@implementation PPAttachedWindow

-(void)resetClickPoint:(NSPoint)latestPoint {
_point = latestPoint;

//Get the screen with the menubar
NSScreen* menuBarScreen = [[NSScreen screens] objectAtIndex:0];
_point.y = [menuBarScreen frame].size.height + [menuBarScreen frame].origin.y - 20;

if (_side == MAPositionAutomatic) {
_side = [super _bestSideForAutomaticPosition];
}

[super _updateGeometry];
}

The source kode will be available when I release the next version of ogsPing.

M@


Leave a Comment

Computing Society Promo Video Icon Resource - Learn to create your own OS X icons