Question

I am trying to create a Mac Application that has an NSStatusItem icon in the Status Bar. The status bar icon should support file drag and drop and must also show a menu when clicked.

The problem is that I have managed to achieve both functionalities separately and am having a hard time merging them together.

I was able to create a Status Bar Application using this link :

http://cocoatutorial.grapewave.com/2010/01/creating-a-status-bar-application/

And then I was able to achieve drag and drop functionality on the status bar icon using the following link

Drag and Drop with NSStatusItem

The problem that I am facing is as follows, in order to get drag and drop, I have to create another view and then assign that view to the NSStatusItem as shown below :

NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];

ViewWithDragFunctionality* viewWithDrag = [[ViewWithDragFunctionality alloc] initWithFrame:NSMakeRect(0, 0, 24, 24)];
[statusItem.view addSubview:viewWithDrag];

Since this is just a view, it does not, obviously, behave as the NSStatusItem's default view and does not support mouse interactions or anything else. I managed to find a way around that by adding the following function to ViewWithDragFunctionality.m

- (void)mouseDown:(NSEvent *)theEvent {
    NSLog(@"Status Bar Icon Clicked");
}

The function is called whenever the status bar icon is clicked and file drag and drop is also being detected.

But now I am stuck with figuring out how to get the menu to display when clicking the status bar icon.

Any help will be much appreciated. I am working on a solution for this and will post it here if I find something first :)

Regards

Shumais

Was it helpful?

Solution

After many days of hit and trial, looking for appropriate tutorials and banging my head against the wall to no avail, I finally stumbled across an imgur application code base generously hosted on github for the public.

The code was hosted at gihthub by a user called ZBUC.

The code that helped me out is at the following repository location on github : https://github.com/zbuc/imgurBar

This is exactly what was needed, after studying how he/they did things in there and combining what I learned/found with the links mentioned in the question, I was able to create a Custom status menu item for my application, was able to get a proper menu to drop down as is the case with a default status menu item and was also able to add drag and drop functionality to my applications status menu item.

So now I have a Custom Status Menu for my application which works like a normal status menu and also supports drag and drop functionality.

I hope that the links in the question, along with the repository link posted above, are of help to everyone who needs what I did.

Thank You

Shumais

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top