如何添加在OSX上按预期工作的菜单项分隔符?

Whi*_*ler 8 delphi menuitem separator delphi-xe2 firemonkey

Windows平台上,使用VCL,当我们要在菜单中添加分隔符时,我们添加一个TMenuItem带有Caption := '-';

使用FireMonkey,我们添加了TMenuItem一个Text := '-';

它在Windows平台上按预期工作,带有Text =' - '的项目显示为分隔符.

但是,当我在OSX上运行相同的应用程序时,我看到减号...

我没有找到任何属性TMenuItem来指定它是一个分隔符 ...

我尝试了a TMainMenu和a TMenuBar(UseOSMenu := True|False;),我仍然有这个问题.

有没有想过创建一个真正的分隔符?(否则,我会检查操作系统并在OSX中删除它...)

小智 5

这是 FireMonkey 中的一个错误。我相信他们会解决的。但同时你可以使用下面的代码。在主窗体的 OnActivate 事件中调用过程 FixSeparatorItemsForMac。

不要忘记使用列表中的 mac 特定文件。

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}
Run Code Online (Sandbox Code Playgroud)