I’m surprised that Xojo still doesn’t have a “menulet” Desktop application demo (the upper right hand corner near the clock) for their TrayItem/Status for OS X. Both Windows’ system tray and Linux’s status icon is covered but no “menulet” (what Apple calls the area).
Does anyone know how to implement this in Xojo, perhaps through Cocoa declares? I noticed that MBS had a NSMenuItem component, but for doing things natively through Cocoa would be ideal. Any seasoned Mac devs know?
Searching NSStatusItem I found a hello world example here creatingitems.html which didn’t take long to translate…
[code]Window Window1
Constant Cocoa = “Cocoa.framework”
Property theItem As Ptr
//PushButton buttonAdd
Sub Action()
if theItem <> nil then return
soft declare function NSClassFromString lib Cocoa (aClassName as CFStringRef) as Ptr
soft declare function systemStatusBar lib Cocoa selector “systemStatusBar” (cls As Ptr) as Ptr
soft declare function statusItemWithLength lib Cocoa selector “statusItemWithLength:” (id As Ptr, length As Single) as Ptr
soft declare sub retain lib Cocoa selector “retain” (id as Ptr)
soft declare sub setTitle lib Cocoa selector “setTitle:” (id As Ptr, newTitle As CFStringRef)
soft declare sub setHighlightMode lib Cocoa selector “setHighlightMode:” (id As Ptr, flag As Boolean)
dim bar As Ptr = systemStatusBar(NSClassFromString("NSStatusBar"))
theItem = statusItemWithLength(bar, -1) //NSVariableStatusItemLength (-1)
retain(theItem)
setTitle(theItem, "xoji:)")
setHighlightMode(theItem, true)
'setMenu(theItem, ?)
End Sub
//Pushbutton buttonRemove
Sub Action()
if theItem = nil then return
soft declare sub release lib Cocoa selector “release” (instsRef as Ptr)
Please take a look at my fork of macoslib here .
It’s a little bit outdated since I no longer have time to maintain it, but here you will find a complete implementation of the NSStatusItem using Cocoa declares. I’m sure you can extract the NSStatusItem class and wire it to the official macoslib.
[quote=26826:@Massimo Valle]Please take a look at my fork of macoslib here .
It’s a little bit outdated since I no longer have time to maintain it, but here you will find a complete implementation of the NSStatusItem using Cocoa declares. I’m sure you can extract the NSStatusItem class and wire it to the official macoslib.[/quote]
Interestingly, while MacOS Lib already has the necessary classes it lacks an example.
Your example window, though, is a drop-in that works perfectly with the “stock” macoslib as of today.
Apologies for resurrecting this conversation, I have added MacOSLib and can create a NSStatusItem menu in my app. How do I get this to work without showing any application window? I want to just have the NSStatusItem menu showing and then show/hide modal dialog boxes and Windows in response to menu selections. If I set the App’s default Window to None the app runs and quits straight away. Is there a correct way to make this work?
I case it helps anyone else the mistake that I was making was to set self.AutoQuit = true :[ . Thanks to Gavin for the sample and therefore making it easy to spot my mistake.
Basically my goal would be to display a Xojo window in lieu of a pop-up menu. Though I can’t wrap my head on how to detect an initial click on the status bar icon itself. I’m thinking I might be able to detect the menu about to open, but still far from figuring this one out. Then the next question is positioning. All I have so far…
statusBar = NSStatusBar.SystemStatusBar()
statusItem = statusBar.CreateStatusItem(-1, AddressOf Hit)
statusItem.HighlightMode = True
Dim image As NSImage
statusItem.Image = myappicon
statusItem.ToolTip = “Click here to popup the control panel”
//PushButton buttonAdd
Sub Action()
if theItem <> nil then return
soft declare function NSClassFromString lib Cocoa (aClassName as CFStringRef) as Ptr
soft declare function systemStatusBar lib Cocoa selector “systemStatusBar” (cls As Ptr) as Ptr
soft declare function statusItemWithLength lib Cocoa selector “statusItemWithLength:” (id As Ptr, length As Single) as Ptr
soft declare sub retain lib Cocoa selector “retain” (id as Ptr)
soft declare sub setTitle lib Cocoa selector “setTitle:” (id As Ptr, newTitle As CFStringRef)
soft declare sub setHighlightMode lib Cocoa selector “setHighlightMode:” (id As Ptr, flag As Boolean)
dim bar As Ptr = systemStatusBar(NSClassFromString("NSStatusBar"))
theItem = statusItemWithLength(bar, -1) //NSVariableStatusItemLength (-1)
retain(theItem)
setTitle(theItem, "xoji:)")
setHighlightMode(theItem, true)
'setMenu(theItem, ?)
End Sub
//Pushbutton buttonRemove
Sub Action()
if theItem = nil then return
soft declare sub release lib Cocoa selector “release” (instsRef as Ptr)
release(theItem)
theItem = nil
End Sub
End Window [/quote]
Does anyone know how I can create MenuItems in this example?