How to add menu item separators that work as expected on OSX?

On a Windows platform with VCL , when we want to add a separator to the menu, add TMenuItem with a Caption := '-' ;

With FireMonkey, add TMenuItem with Text := '-' ;

It works as expected on a Windows platform, an element with text = '-' is displayed as a separator.

But when I run the same application on OSX , I have a minus sign visible ...

I did not find any property in TMenuItem to indicate that it is a delimiter ...

I tried with TMainMenu and TMenuBar ( UseOSMenu := True|False; ) and I still have this problem.

Any idea to create a real delimiter? (otherwise I will check the OS and remove it if OSX ...)

+8
delphi separator delphi-xe2 firemonkey menuitem
source share
3 answers

This is a bug in FireMonkey. I am sure they will resolve this. But for now, you can use the code below. Call the FixSeparatorItemsForMac procedure in the OnActivate event of your main form.

Be sure to list specific Mac files in your use list.

 uses ... {$IFDEF MACOS} ,Macapi.ObjectiveC,MacApi.AppKit,MacApi.Foundation,FMX.Platform.Mac {$ENDIF} {$IFDEF MACOS} Procedure FixSeparatorItemsForMenuItem(MenuItem:NSMenuItem); var i:Integer; subItem:NSMenuItem; begin if (MenuItem.hasSubmenu = false) then exit; for i := 0 to MenuItem.submenu.itemArray.count -1 do begin subItem := MenuItem.submenu.itemAtIndex(i); if (subItem.title.isEqualToString(NSSTR('-'))= true) then begin MenuItem.submenu.removeItemAtIndex(i); MenuItem.submenu.insertItem(TNSMenuItem.Wrap(TNSMenuItem.OCClass.separatorItem),i); end else begin FixSeparatorItemsForMenuItem(subItem); end; end; end; Procedure FixSeparatorItemsForMac; var NSApp:NSApplication; MainMenu:NSMenu; AppItem: NSMenuItem; i: Integer; begin NSApp := TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication); MainMenu := NSApp.mainMenu; if (MainMenu <> nil) then begin for i := 0 to MainMenu.itemArray.count -1 do begin AppItem := mainMenu.itemAtIndex(i); FixSeparatorItemsForMenuItem(AppItem); end; end; end; {$ENDIF} 
+4
source share

I have never programmed for Mac, and I don’t have eveb, I have Mac, but out of curiosity I found Apple Documentation about it.

The Separator menu item is a locked empty menu item, maybe you can fake this:

separatorItem

Returns the menu item that is used to separate the logical groups of the command menu. + (NSMenuItem *) separatorItem Return Value

A menu item that is used to separate logical groups of menu commands.

Discussion

This menu item is disabled. The default delimiter element is empty.

(From: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSMenuItem_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSMenuItem )

0
source share

I have no way to verify this, but worth a try.

By default, FireMonkey creates its own fully styled menus, but sets the TMenuBar.UseOSMenu property to true and uses the OS calls to create the menu.

You can then combine this with the tip to create the Cocoa menu, which has already been discussed.

From http://docwiki.embarcadero.com/RADStudio/en/FireMonkey_Application_Design#Menus :

"Setting the TMenuBar.UseOSMenu property to True causes FireMonkey to create a menu tree with OS calls, which results in a menu of its own. On Windows, this menu is located at the top of the parent form and is displayed using the current Appearance theme on Mac OS The X menu appears in the global menu bar on top of the main screen whenever the application has focus. "

-one
source share

All Articles