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

发布于 2024-12-05 13:20:51 字数 614 浏览 1 评论 0 原文

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

使用 FireMonkey,我们添加一个 TMenuItemText := '-';

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

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

我没有在 TMenuItem 上找到任何属性来指定它是 < strong>separator...

我尝试过使用 TMainMenuTMenuBar (UseOSMenu := True|False;) 和我仍然有这个问题。

有想法创建一个真正的分隔符吗?(否则,我会检查操作系统并删除它,如果 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...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2024-12-12 13:20:51

这是 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}

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}
两个我 2024-12-12 13:20:51

我从来没有为 Mac 编程过,而且我也没有 Mac,但出于好奇,我找到了一些关于它的 Apple 文档。

菜单分隔符项是一个禁用的空白菜单项,也许您可​​以用它来伪造:

分隔符项目

返回一个菜单项,用于分隔菜单的逻辑组
命令。
+ (NSMenuItem *)separatorItem 返回值

用于分隔菜单命令逻辑组的菜单项。

讨论

此菜单项已禁用。默认分隔符项是空格。

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

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

The Menu Separator item is a disabled blank menu item, maybe you can fake with that:

separatorItem

Returns a menu item that is used to separate logical groups of menu
commands.
+ (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 separator item is blank space.

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

铜锣湾横着走 2024-12-12 13:20:51

我没有测试这个的设施,但值得一试。

默认情况下,FireMonkey 创建自己的完全样式菜单,但将 TMenuBar.UseOSMenu 属性设置为 true 并使用操作系统调用来创建菜单。

然后,您可以将其与已经讨论过的创建 Cocoa 菜单的建议结合起来。

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

“将 TMenuBar.UseOSMenu 属性设置为 True 会导致 FireMonkey 通过操作系统调用创建菜单树,从而生成本机菜单。在 Windows 上,此菜单位于父窗体的顶部,并使用当前的外观主题显示。在 Mac OS X 上,只要应用程序获得焦点,菜单就会显示在主屏幕顶部的全局菜单栏中。”

I don't have the facilities to test this, but it's worth a try.

By default, FireMonkey creates it's own fully styled menus, but set the TMenuBar.UseOSMenu property to true and it uses OS calls to create the menus.

You can then combine this with the advice for creating Cocoa menus already discussed.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文