« See you at Claris Eng… | Home | How long do you wait … »

More Menubar options in macOS Sonoma

Your desktop app made in Xojo can take advantage of new menu features in macOS Sonoma. You can use section headers as well as color palettes. And you may use our SystemInformationMBS.isSonoma function a lot to check if your application runs on macOS Sonoma. Then you can dynamically use the new features, if you have macOS 14.0 or higher.

Let's start with the Opening event of the application or a window, where we edit the menu bar. We Query menu item (NSMenuItemMBS) representing the EditMenu and start by adding a separator item there. Then we create a section header. To avoid the objects being destroyed too early, we store the references in properties.

For the color palette, we first collect a few NSColorMBS objects in an array as well as the matching titles. Then we create a palette menu for them. To later catch the event for a selection, we connect the PaletteSelectionChange event and add the new menu as a submenu to a new menu item.

Here is the example code:

EventHandler Sub Opening() dim m as NSMenuItemMBS = NSMenuItemMBS.MenuItem(EditMenu) m.submenu.addItem NSMenuItemMBS.separatorItem // add the header dim h as NSMenuItemMBS = NSMenuItemMBS.sectionHeaderWithTitle("Section Header") h.Enabled = true m.submenu.addItem h self.SectionHeader = h // and the new entry with our MyNSMenuItemMBS class to handle validate and action. dim n as new MyNSMenuItemMBS("Hello") n.Enabled = true m.submenu.addItem n self.HelloMenu = n // keep references // define the colors and titles and create palette menu dim colors() as NSColorMBS colors.Append NSColorMBS.whiteColor colors.Append NSColorMBS.redColor colors.Append NSColorMBS.blueColor dim titles() as string = array("White", "Red", "Blue") paletteMenu = NSMenuMBS.paletteMenuWithColors(Colors, titles) AddHandler paletteMenu.PaletteSelectionChange, WeakAddressOf PaletteSelectionChange // the palette is hosted by an unnamed menu item dim p as new MyNSMenuItemMBS("") p.Enabled = true p.submenu = paletteMenu m.submenu.addItem p self.paletteMenuItem = p End EventHandler

In the PaletteSelectionChange method, we show which items got selected. Use selectedItems method to query selected items:

Sub PaletteSelectionChange(m as NSMenuMBS) // we got a color selected dim selectedItems() as NSMenuItemMBS = m.selectedItems dim Titles() as string for each item as NSMenuItemMBS in selectedItems Titles.append item.Title next MessageBox Join(titles, " and ")+" selected" End Sub

We need some properties to store the object references. But technically you only need an array (of variant) to keep them all together. The properties are more convenient to pick later a specific one:

Property HelloMenu As NSMenuItemMBS
Property PaletteMenu As NSMenuMBS
Property SectionHeader As NSMenuItemMBS
Property paletteMenuItem As NSMenuItemMBS

In order to handle events for normal NSMenuItemMBS objects, we make our own subclass MyNSMenuItemMBS and add event code there to handle clicks and to validate the menu items:

Class MyNSMenuItemMBS Inherits NSMenuItemMBS
EventHandler Sub Action() // a menu item was selected MessageBox me.Title+" clicked" End EventHandler
EventHandler Function validateMenuItem(menuItem as NSMenuItemMBS) As boolean // return true to enable menu item Return true End EventHandler
End Class

Please try the new enhancements with MBS Xojo Plugins 23.5 and let us know if you have questions.

07 10 23 - 13:13