Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add menu items separators which work as expected on OSX?

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

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

It works as expected on Windows platform, the item with the Text='-' is displayed as a separator.

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

I haven't found any property on the TMenuItem to specify it is a separator...

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

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

like image 707
Whiler Avatar asked Sep 21 '11 19:09

Whiler


1 Answers

This is a bug in FireMonkey. I am sure they will solve it. But meanwhile you can use the below code. Call the procedure FixSeparatorItemsForMac in the OnActivate event of your main form.

Dont forget mac specific files in the uses 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}
like image 121
mehmed.ali Avatar answered Nov 11 '22 11:11

mehmed.ali