Я создал подразделение, чтобы попытаться управлять тем, что я хотел бы ...
С его помощью я могу использовать определенный TMenuItem ... и переместить его подэлементы в подменю приложения ... (Я до сих пор не знаю, как добавить его с нуля ...)
Я также использую ответ от Мехмеда Али для управления разделителями ...
unit uMenu;
interface
uses
FMX.Dialogs, System.SysUtils,
FMX.Menus
{$IFDEF MACOS}
,Macapi.ObjectiveC,MacApi.AppKit,MacApi.Foundation,FMX.Platform.Mac
{$ENDIF}
;
type
ManageMenu = class
private
{$IFDEF MACOS}
class procedure FixSeparatorItemsForMenuItem (MenuItem: NSMenuItem);
class procedure MoveItemsToMacApplicationMenu(source, target: NSMenuItem); overload;
class procedure MoveItemsToMacApplicationMenu(index: Integer); overload;
{$ENDIF}
public
class procedure FixSeparatorItemsForMac;
class procedure MoveItemsToMacApplicationMenu(index: Integer; menu: TMainMenu); overload;
class procedure MoveItemsToMacApplicationMenu(index: Integer; menu: TMenuBar); overload;
end;
implementation
{ ManageMenu }
{$IFDEF MACOS}
class procedure ManageMenu.FixSeparatorItemsForMenuItem(MenuItem:NSMenuItem);
var
i : Integer;
subItem: NSMenuItem;
begin
if (MenuItem.hasSubmenu = False) then exit;
for i := 0 to Pred(MenuItem.submenu.itemArray.count) 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;
{$ENDIF}
class procedure ManageMenu.FixSeparatorItemsForMac;
{$IFDEF MACOS}
var
NSApp : NSApplication;
MainMenu: NSMenu;
AppItem : NSMenuItem;
i : Integer;
{$ENDIF}
begin
{$IFDEF MACOS}
NSApp := TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication);
MainMenu := NSApp.mainMenu;
if (MainMenu <> nil) then
begin
for i := 0 to Pred(MainMenu.itemArray.Count) do
begin
AppItem := mainMenu.itemAtIndex(i);
FixSeparatorItemsForMenuItem(AppItem);
end;
end;
{$ENDIF}
end;
{$IFDEF MACOS}
class procedure ManageMenu.MoveItemsToMacApplicationMenu(source, target: NSMenuItem);
var
iLoop, iMax: Integer;
subItem : NSMenuItem;
begin
if (source.hasSubmenu = False) then exit;
iMax := Pred(source.submenu.itemArray.count);
for iLoop := iMax downto 0 do
begin
subItem := source.submenu.itemAtIndex(iLoop);
source.submenu.removeItemAtIndex(iLoop);
target.submenu.insertItem(subItem, 0);
end;
// Hide the parent
source.setHidden(True);
end;
{$ENDIF}
{$IFDEF MACOS}
class procedure ManageMenu.MoveItemsToMacApplicationMenu(index: Integer);
var
NSApp : NSApplication;
MainMenu: NSMenu;
source : NSMenuItem;
target : NSMenuItem;
begin
NSApp := TNSApplication.Wrap(TNSApplication.OCClass.sharedApplication);
MainMenu := NSApp.mainMenu;
if (MainMenu <> nil) then
begin
begin
if (MainMenu.itemArray.count > 1) then
begin
source := mainMenu.itemAtIndex(Succ(index));
target := mainMenu.itemAtIndex(0);
MoveItemsToMacApplicationMenu(source, target);
end;
end;
end;
end;
{$ENDIF}
class procedure ManageMenu.MoveItemsToMacApplicationMenu(index: Integer; menu: TMainMenu);
begin
{$IFDEF MACOS}
MoveItemsToMacApplicationMenu(index);
{$ELSE}
// (menu.Children[Succ(index)] as TMenuItem).Visible := False;
// menu.RemoveObject(...);
// ... I don't knwo how to remove items on Windows ;o(((
{$ENDIF}
end;
class procedure ManageMenu.MoveItemsToMacApplicationMenu(index: Integer; menu: TMenuBar);
begin
{$IFDEF MACOS}
MoveItemsToMacApplicationMenu(index);
{$ELSE}
if (menu.ChildrenCount > Succ(index)) and (menu.Children[Succ(index)] is TMenuItem) then
begin
// (menu.Children[Succ(index)] as TMenuItem).Visible := False;
// menu.RemoveObject(...);
// ... I don't knwo how to remove items on Windows ;o(((
// menu.BeginUpdate;
// menu.RemoveObject((menu.Children[Succ(index)] as TMenuItem));
// menu.RemoveFreeNotify((menu.Children[Succ(index)] as TMenuItem));
// menu.DeleteChildren;
// (menu.Children[Succ(index)] as TMenuItem).View.Visible := False;
// .Free;
// (menu.Children[Succ(index)] as TMenuItem).Destroy;
// menu.EndUpdate;
end;
{$ENDIF}
end;
end.
Он работает как положено, и это то, что я хочу на OSX ...
procedure TfrmMain.FormActivate(Sender: TObject);
begin
if not bAlreadyActivated then
begin
bAlreadyActivated := True;
ManageMenu.FixSeparatorItemsForMac;
ManageMenu.MoveItemsToMacApplicationMenu(0, MainMenu1);
end;
end;
но теперь у меня проблема с Windows, потому что, что бы я ни пытался, у меня все еще есть меню, добавленное для Mac, отображаемое под Windows ...; o (