Cocoa/Config: integrate various Menu config(TMacOS_AppMenuIntf/TMacOS_DockMenuIntf) into CocoaConfig.TCocoaConfigMenu

This commit is contained in:
rich2014 2024-08-22 21:04:53 +08:00
parent b67c2a2386
commit ab440b6eb1
5 changed files with 70 additions and 53 deletions

View File

@ -5,6 +5,32 @@
we can modify the default config in the APP.
}
var
CocoaConfigMenu: TCocoaConfigMenu = (
menuItem: (
defaultCheckImageName: nil; // set in cocoaconfig.pas
defaultRadioImageName: nil // set in cocoaconfig.pas
);
// Application interface provided to facilitate APP to operate App Menu.
// it's easy to set About, Preferences, and customized menus,
// only the LCL TMenuItem is needed to pass in.
// and we can control whether Cocoa is needed to automatically add
// Hide, Hide Others, Show All, and Quit menu items.
appMenu: (
aboutItem: nil;
preferencesItem: nil;
customMenus: nil;
dontAutoCreateItems: False;
);
// Application interface provided to facilitate APP to operate Dock Menu.
// only the LCL TMenuItem is needed to pass in.
dockMenu: (
customMenus: nil;
);
);
var
CocoaConfigGlobal: TCocoaConfigGlobal = (
// for compatiblity with LCL 1.8 release. The macOS base is 72ppi

View File

@ -7,8 +7,39 @@ unit CocoaConfig;
interface
uses
Menus,
CocoaAll, Cocoa_Extra, CocoaConst;
type
TCocoaConfigMenuItem = record
defaultCheckImageName: NSString;
defaultRadioImageName: NSString;
end;
// Application interface provided to facilitate APP to operate App Menu.
// it's easy to set About, Preferences, and customized menus,
// only the LCL TMenuItem is needed to pass in.
// and we can control whether Cocoa is needed to automatically add
// Hide, Hide Others, Show All, and Quit menu items.
TCocoaConfigAppMenu = record
aboutItem: TMenuItem;
preferencesItem: TMenuItem;
customMenus: TMenuItem;
dontAutoCreateItems: Boolean;
end;
// Application interface provided to facilitate APP to operate Dock Menu.
// only the LCL TMenuItem is needed to pass in.
TCocoaConfigDockMenu = record
customMenus: TMenuItem;
end;
TCocoaConfigMenu = record
menuItem: TCocoaConfigMenuItem;
appMenu: TCocoaConfigAppMenu;
dockMenu: TCocoaConfigDockMenu;
end;
type
TCocoaConfigGlobal = record
basePPI: Integer;
@ -151,12 +182,6 @@ type
buttonType: NSButtonType;
end;
type
TCocoaConfigMenu = record
defaultCheckImageName: NSString;
defaultRadioImageName: NSString;
end;
type
TCocoaConfigNotification = record
alwaysPresent: Boolean;
@ -200,9 +225,6 @@ type
{$include cocoaconfig.inc}
var
CocoaConfigMenu: TCocoaConfigMenu = (
{%H-});
CocoaConfigFocusRing: TCocoaConfigFocusRing;
implementation
@ -246,8 +268,9 @@ begin
end;
initialization
CocoaConfigMenu.defaultCheckImageName:= NSSTR('NSMenuCheckmark');
CocoaConfigMenu.defaultRadioImageName:= NSSTR('NSDatePickerCalendarHome');
CocoaConfigMenu.menuItem.defaultCheckImageName:= NSSTR('NSMenuCheckmark');
CocoaConfigMenu.menuItem.defaultRadioImageName:= NSSTR('NSDatePickerCalendarHome');
CocoaConfigFocusRing:= TCocoaConfigFocusRing.Create;
end.

View File

@ -12,7 +12,7 @@ uses
// LCL
Forms, Menus, LCLType, Classes, LCLStrConsts,
// LCL Cocoa
CocoaAll, CocoaPrivate, CocoaCallback, CocoaUtils, CocoaConst;
CocoaAll, CocoaPrivate, CocoaCallback, CocoaUtils, CocoaConfig, CocoaConst;
type
IMenuItemCallback = interface(ICommonCallBack)
@ -63,38 +63,12 @@ type
function worksWhenModal: LCLObjCBoolean; message 'worksWhenModal';
end;
{ TMacOS_AppMenuIntf }
// Application interface provided to facilitate APP to operate App Menu.
// it's easy to set About, Preferences, and customized menus,
// only the LCL TMenuItem is needed to pass in.
// and we can control whether Cocoa is needed to automatically add
// Hide, Hide Others, Show All, and Quit menu items.
TMacOS_AppMenuIntf = class
public
aboutItem: TMenuItem;
preferencesItem: TMenuItem;
customMenus: TMenuItem;
dontAutoCreateAppMenuItems: Boolean;
end;
{ TMacOS_DockMenuIntf }
// Application interface provided to facilitate APP to operate Dock Menu.
// only the LCL TMenuItem is needed to pass in.
TMacOS_DockMenuIntf = class
public
customMenus: TMenuItem;
end;
TMenuItemHandleCreateFunc = function(const AMenuItem: TMenuItem): NSMenuItem;
const
isMenuEnabled : Boolean = true;
var
macOS_AppMenuIntf: TMacOS_AppMenuIntf;
macOS_DockMenuIntf: TMacOS_DockMenuIntf;
menuItemHandleCreateFunc: TMenuItemHandleCreateFunc;
procedure MenuTrackStarted(mn: NSMenu);
@ -533,24 +507,24 @@ begin
attachedAppleMenuItems := True;
if NOT Assigned(self.FMenuItemTarget) then
self.FMenuItemTarget:= macOS_AppMenuIntf.customMenus;
self.FMenuItemTarget:= CocoaConfigMenu.appMenu.customMenus;
// APP Custom
NSMenuAddItemsFromLCLMenu(self.submenu, macOS_AppMenuIntf.customMenus);
NSMenuAddItemsFromLCLMenu(self.submenu, CocoaConfigMenu.appMenu.customMenus);
// About
attachSpecialMenuItem(
macOS_AppMenuIntf.aboutItem,
CocoaConfigMenu.appMenu.aboutItem,
Format(rsMacOSMenuAbout, [Application.Title]) );
// Preferences
attachSpecialMenuItem(
macOS_AppMenuIntf.preferencesItem,
CocoaConfigMenu.appMenu.preferencesItem,
rsMacOSMenuPreferences,
VK_OEM_COMMA, [ssMeta]);
// Auto Create App Menu below?
if macOS_AppMenuIntf.dontAutoCreateAppMenuItems then
if CocoaConfigMenu.appMenu.dontAutoCreateItems then
Exit;
// Separator
@ -723,15 +697,9 @@ begin
menuTrack.removeAllObjects;
end;
initialization
macOS_AppMenuIntf:= TMacOS_AppMenuIntf.Create;
macOS_DockMenuIntf:= TMacOS_DockMenuIntf.Create;
finalization
MenuTrackCancelAll;
if menuTrack <> nil then menuTrack.release;
FreeAndNil(macOS_AppMenuIntf);
FreeAndNil(macOS_DockMenuIntf);
end.

View File

@ -724,8 +724,8 @@ end;
function TAppDelegate.applicationDockMenu(sender: NSApplication): NSMenu;
begin
Result:= NSMenu.alloc.init;
if Assigned(macOS_DockMenuIntf.customMenus) then
NSMenuAddItemsFromLCLMenu(Result, macOS_DockMenuIntf.customMenus);
if Assigned(CocoaConfigMenu.dockMenu.customMenus) then
NSMenuAddItemsFromLCLMenu(Result, CocoaConfigMenu.dockMenu.customMenus);
end;
procedure TAppDelegate.handleQuitAppEvent_withReplyEvent(event: NSAppleEventDescriptor; replyEvent: NSAppleEventDescriptor);

View File

@ -161,12 +161,12 @@ end;
// used from the MenuMadness example
class function TCocoaWSMenuItem.NSMenuCheckmark: NSImage;
begin
Result:=NSImage.imageNamed(CocoaConfigMenu.defaultCheckImageName);
Result:=NSImage.imageNamed(CocoaConfigMenu.menuItem.defaultCheckImageName);
end;
class function TCocoaWSMenuItem.NSMenuRadio: NSImage;
begin
Result:=NSImage.imageNamed(CocoaConfigMenu.defaultRadioImageName);
Result:=NSImage.imageNamed(CocoaConfigMenu.menuItem.defaultRadioImageName);
end;
class function TCocoaWSMenuItem.isSeparator(const ACaption: AnsiString): Boolean;