UPD: refactoring uDarwinApplication from uMyDarwin

This commit is contained in:
rich2014 2025-12-08 22:11:26 +08:00
commit 2f2c660fb5
4 changed files with 150 additions and 130 deletions

View file

@ -332,7 +332,7 @@ end;"/>
<PackageName Value="Image32"/>
</Item13>
</RequiredPackages>
<Units Count="291">
<Units Count="292">
<Unit0>
<Filename Value="doublecmd.lpr"/>
<IsPartOfProject Value="True"/>
@ -2119,6 +2119,11 @@ end;"/>
<ResourceBaseClass Value="Form"/>
<UnitName Value="fMasterKey"/>
</Unit290>
<Unit291>
<Filename Value="platform\unix\darwin\udarwinapplication.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="uDarwinApplication"/>
</Unit291>
</Units>
</ProjectOptions>
<CompilerOptions>

View file

@ -58,6 +58,7 @@ uses
{$ELSEIF DEFINED(DARWIN)}
, CocoaConfig
, uMyDarwin
, uDarwinApplication
{$ENDIF}
, Types, LMessages;

View file

@ -0,0 +1,141 @@
unit uDarwinApplication;
{$mode delphi}
{$modeswitch objectivec2}
interface
uses
Classes, SysUtils,
CocoaAll, CocoaInt, Cocoa_Extra, CocoaUtils,
uDarwinUtil;
// MacOS Service Integration
type
TNSServiceProviderCallBack = Procedure( filenames:TStringList ) of object;
TNSServiceMenuIsReady = Function(): Boolean of object;
TNSServiceMenuGetFilenames = Function(): TStringArray of object;
TNSThemeChangedHandler = Procedure() of object;
TNSServiceProvider = objcclass(NSObject)
public
onOpenWithNewTab: TNSServiceProviderCallBack;
public
procedure openWithNewTab( pboard:NSPasteboard; userData:NSString; error:NSStringPtr ); message 'openWithNewTab:userData:error:';
end;
TDCCocoaApplication = objcclass(TCocoaApplication)
function validRequestorForSendType_returnType (sendType: NSString; returnType: NSString): id; override;
function writeSelectionToPasteboard_types (pboard: NSPasteboard; types: NSArray): ObjCBOOL; message 'writeSelectionToPasteboard:types:';
procedure observeValueForKeyPath_ofObject_change_context(keyPath: NSString;
object_: id; change: NSDictionary; context: pointer); override;
public
serviceMenuIsReady: TNSServiceMenuIsReady;
serviceMenuGetFilenames: TNSServiceMenuGetFilenames;
end;
procedure InitNSServiceProvider(
serveCallback: TNSServiceProviderCallBack;
isReadyFunc: TNSServiceMenuIsReady;
getFilenamesFunc: TNSServiceMenuGetFilenames );
procedure InitNSThemeChangedObserver( handler: TNSThemeChangedHandler );
var
NSServiceProvider: TNSServiceProvider;
NSThemeChangedHandler: TNSThemeChangedHandler;
implementation
function TDCCocoaApplication.validRequestorForSendType_returnType (sendType: NSString; returnType: NSString): id;
begin
Result:= nil;
if not NSFilenamesPboardType.isEqualToString(sendType) then exit;
if returnType<>nil then exit;
if self.serviceMenuIsReady() then Result:=self;
end;
function TDCCocoaApplication.writeSelectionToPasteboard_types( pboard: NSPasteboard; types: NSArray): ObjCBOOL;
var
lclArray: TStringArray;
cocoaArray: NSArray;
begin
Result:= false;
lclArray:= self.serviceMenuGetFilenames();
if lclArray=nil then exit;
cocoaArray:= StringArrayFromLCLToNS( lclArray );
pboard.declareTypes_owner( NSArray.arrayWithObject(NSFileNamesPboardType), nil );
pboard.setPropertyList_forType( cocoaArray, NSFileNamesPboardType );
Result:= true;
end;
procedure TDCCocoaApplication.observeValueForKeyPath_ofObject_change_context(
keyPath: NSString; object_: id; change: NSDictionary; context: pointer);
begin
Inherited observeValueForKeyPath_ofObject_change_context( keyPath, object_, change, context );
if keyPath.isEqualToString(NSSTR('effectiveAppearance')) then
begin
NSAppearance.setCurrentAppearance( self.appearance );
if Assigned(NSThemeChangedHandler) then NSThemeChangedHandler;
end;
end;
procedure TNSServiceProvider.openWithNewTab( pboard:NSPasteboard; userData:NSString; error:NSStringPtr );
var
filenameArray: NSArray;
filenameList: TStringList;
begin
filenameArray := pboard.propertyListForType(NSFilenamesPboardType);
if filenameArray <> nil then
begin
if Assigned(onOpenWithNewTab) then
begin
filenameList:= NSArrayToList( filenameArray );
onOpenWithNewTab( filenameList );
FreeAndNil( filenameList );
end;
end;
end;
procedure InitNSServiceProvider(
serveCallback: TNSServiceProviderCallBack;
isReadyFunc: TNSServiceMenuIsReady;
getFilenamesFunc: TNSServiceMenuGetFilenames );
var
DCApp: TDCCocoaApplication;
sendTypes: NSArray;
returnTypes: NSArray;
begin
DCApp:= TDCCocoaApplication( NSApp );
// MacOS Service menu incoming setup
if not Assigned(NSServiceProvider) then
begin
NSServiceProvider:= TNSServiceProvider.alloc.init;
DCApp.setServicesProvider( NSServiceProvider );
NSUpdateDynamicServices;
end;
NSServiceProvider.onOpenWithNewTab:= serveCallback;
// MacOS Service menu outgoing setup
sendTypes:= NSArray.arrayWithObject(NSFilenamesPboardType);
returnTypes:= nil;
DCApp.serviceMenuIsReady:= isReadyFunc;
DCApp.serviceMenuGetFilenames:= getFilenamesFunc;
DCApp.registerServicesMenuSendTypes_returnTypes( sendTypes, returnTypes );
end;
procedure InitNSThemeChangedObserver( handler: TNSThemeChangedHandler );
begin
if Assigned(NSThemeChangedHandler) then exit;
NSApp.addObserver_forKeyPath_options_context(
NSApp, NSSTR('effectiveAppearance'), 0, nil );
NSThemeChangedHandler:= handler;
end;
end.

View file

@ -40,7 +40,7 @@ uses
uLng, uLog, uDebug,
Cocoa_Extra, MacOSAll, CocoaAll, QuickLookUI,
CocoaUtils, CocoaInt, CocoaPrivate, CocoaConst, CocoaMenus,
uDarwinFSWatch, uDarwinFinder, uDarwinFinderModel, uDarwinUtil;
uDarwinApplication, uDarwinFSWatch, uDarwinFinder, uDarwinFinderModel, uDarwinUtil;
const
FINDER_FAVORITE_TAGS_MENU_ITEM_CAPTION = #$EF#$BF#$BC'FinderFavoriteTags';
@ -127,28 +127,7 @@ public
property monitor: TDarwinFSWatcher read _monitor;
end;
// MacOS Service Integration
type TNSServiceProviderCallBack = Procedure( filenames:TStringList ) of object;
type TNSServiceMenuIsReady = Function(): Boolean of object;
type TNSServiceMenuGetFilenames = Function(): TStringArray of object;
type TDCCocoaApplication = objcclass(TCocoaApplication)
function validRequestorForSendType_returnType (sendType: NSString; returnType: NSString): id; override;
function writeSelectionToPasteboard_types (pboard: NSPasteboard; types: NSArray): ObjCBOOL; message 'writeSelectionToPasteboard:types:';
procedure observeValueForKeyPath_ofObject_change_context( keyPath: NSString; object_: id; change: NSDictionary; context: pointer); override;
public
serviceMenuIsReady: TNSServiceMenuIsReady;
serviceMenuGetFilenames: TNSServiceMenuGetFilenames;
end;
type TNSServiceProvider = objcclass(NSObject)
private
onOpenWithNewTab: TNSServiceProviderCallBack;
public
procedure openWithNewTab( pboard:NSPasteboard; userData:NSString; error:NSStringPtr ); message 'openWithNewTab:userData:error:';
end;
type
type
{ TMacosServiceMenuHelper }
@ -165,11 +144,6 @@ type
procedure PopUp( const menu: TPopupMenu; const caption: String; const paths: TStringArray );
end;
procedure InitNSServiceProvider(
serveCallback: TNSServiceProviderCallBack;
isReadyFunc: TNSServiceMenuIsReady;
getFilenamesFunc: TNSServiceMenuGetFilenames );
procedure performMacOSService( serviceName: String );
procedure showQuickLookPanel;
@ -180,16 +154,9 @@ function getMacOSSpecificFileProperty( const path: String ): TFileMacOSSpecificP
procedure showMacOSSharingServiceMenu;
procedure showMacOSAirDropDialog;
// MacOS Theme
type TNSThemeChangedHandler = Procedure() of object;
procedure InitNSThemeChangedObserver( handler: TNSThemeChangedHandler );
var
HasMountURL: Boolean = False;
NSServiceProvider: TNSServiceProvider;
MacosServiceMenuHelper: TMacosServiceMenuHelper;
NSThemeChangedHandler: TNSThemeChangedHandler;
type
@ -406,100 +373,6 @@ begin
end;
end;
procedure InitNSServiceProvider(
serveCallback: TNSServiceProviderCallBack;
isReadyFunc: TNSServiceMenuIsReady;
getFilenamesFunc: TNSServiceMenuGetFilenames );
var
DCApp: TDCCocoaApplication;
sendTypes: NSArray;
returnTypes: NSArray;
begin
DCApp:= TDCCocoaApplication( NSApp );
// MacOS Service menu incoming setup
if not Assigned(NSServiceProvider) then
begin
NSServiceProvider:= TNSServiceProvider.alloc.init;
DCApp.setServicesProvider( NSServiceProvider );
NSUpdateDynamicServices;
end;
NSServiceProvider.onOpenWithNewTab:= serveCallback;
// MacOS Service menu outgoing setup
sendTypes:= NSArray.arrayWithObject(NSFilenamesPboardType);
returnTypes:= nil;
DCApp.serviceMenuIsReady:= isReadyFunc;
DCApp.serviceMenuGetFilenames:= getFilenamesFunc;
DCApp.registerServicesMenuSendTypes_returnTypes( sendTypes, returnTypes );
end;
procedure TNSServiceProvider.openWithNewTab( pboard:NSPasteboard; userData:NSString; error:NSStringPtr );
var
filenameArray{, lClasses}: NSArray;
filenameList: TStringList;
begin
filenameArray := pboard.propertyListForType(NSFilenamesPboardType);
if filenameArray <> nil then
begin
if Assigned(onOpenWithNewTab) then
begin
filenameList:= NSArrayToList( filenameArray );
onOpenWithNewTab( filenameList );
FreeAndNil( filenameList );
end;
end;
end;
function TDCCocoaApplication.validRequestorForSendType_returnType (sendType: NSString; returnType: NSString): id;
var
isSendTypeMatch: ObjcBool;
isReturnTypeMatch: ObjcBool;
begin
Result:= nil;
if not NSFilenamesPboardType.isEqualToString(sendType) then exit;
if returnType<>nil then exit;
if self.serviceMenuIsReady() then Result:=self;
end;
function TDCCocoaApplication.writeSelectionToPasteboard_types( pboard: NSPasteboard; types: NSArray): ObjCBOOL;
var
lclArray: TStringArray;
cocoaArray: NSArray;
begin
Result:= false;
lclArray:= self.serviceMenuGetFilenames();
if lclArray=nil then exit;
cocoaArray:= StringArrayFromLCLToNS( lclArray );
pboard.declareTypes_owner( NSArray.arrayWithObject(NSFileNamesPboardType), nil );
pboard.setPropertyList_forType( cocoaArray, NSFileNamesPboardType );
Result:= true;
end;
procedure TDCCocoaApplication.observeValueForKeyPath_ofObject_change_context(
keyPath: NSString; object_: id; change: NSDictionary; context: pointer);
begin
Inherited observeValueForKeyPath_ofObject_change_context( keyPath, object_, change, context );
if keyPath.isEqualToString(NSSTR('effectiveAppearance')) then
begin
NSAppearance.setCurrentAppearance( self.appearance );
if Assigned(NSThemeChangedHandler) then NSThemeChangedHandler;
end;
end;
procedure InitNSThemeChangedObserver( handler: TNSThemeChangedHandler );
begin
if Assigned(NSThemeChangedHandler) then exit;
NSApp.addObserver_forKeyPath_options_context(
NSApp, NSSTR('effectiveAppearance'), 0, nil );
NSThemeChangedHandler:= handler;
end;
procedure showMacOSSharingServiceMenu;
var
picker: NSSharingServicePicker;