UPD: refactoring Panel from uMyDarwin to uDarwinPanel

(cherry picked from commit b75ac1e82f)
This commit is contained in:
rich2014 2025-12-10 22:28:06 +08:00 committed by Alexander Koblov
commit 2eac8cbd4e
5 changed files with 232 additions and 203 deletions

View file

@ -332,7 +332,7 @@ end;"/>
<PackageName Value="Image32"/>
</Item13>
</RequiredPackages>
<Units Count="295">
<Units Count="296">
<Unit0>
<Filename Value="doublecmd.lpr"/>
<IsPartOfProject Value="True"/>
@ -2139,6 +2139,11 @@ end;"/>
<IsPartOfProject Value="True"/>
<UnitName Value="uDarwinSimpleFSWatch"/>
</Unit294>
<Unit295>
<Filename Value="platform\unix\darwin\udarwinpanel.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="uDarwinPanel"/>
</Unit295>
</Units>
</ProjectOptions>
<CompilerOptions>

View file

@ -11,7 +11,7 @@ uses
fMain, uDCUtils, uLng,
uFileView, uBriefFileView, uColumnsFileView, uThumbFileView,
uMyDarwin, uDarwinUtil, uDarwinApplication, uDarwinFileUtil,
uDarwinFinder, uDarwinFinderModel, uDarwinFileView,
uDarwinFinder, uDarwinFinderModel, uDarwinFileView, uDarwinPanel,
CocoaAll, CocoaConfig, CocoaToolBar, Cocoa_Extra;
procedure initCocoaModernFormConfig;
@ -69,17 +69,17 @@ end;
procedure airdropAction( const Sender: id );
begin
showMacOSAirDropDialog;
TDarwinPanelUtil.showAirDrop;
end;
procedure quickLookAction( const Sender: id );
begin
showQuickLookPanel;
TDarwinPanelUtil.showQuickLook;
end;
procedure editTagAction( const Sender: id );
begin
showEditFinderTagsPanel( Sender, frmMain );
TDarwinPanelUtil.showEditFinderTags( Sender, frmMain );
end;
procedure finderRevealAction( const Sender: id );

View file

@ -0,0 +1,219 @@
unit uDarwinPanel;
{$mode delphi}
{$modeswitch objectivec2}
interface
uses
Classes, SysUtils,
Controls, Forms,
CocoaAll, QuickLookUI, CocoaUtils,
uDarwinApplication, uDarwinFinderModel, uDarwinFinder;
type
{ TDarwinPanelUtil }
TDarwinPanelUtil = class
public
class procedure showQuickLook;
class procedure showEditFinderTags( const Sender: id; const control: TWinControl );
class procedure showSharingService;
class procedure showAirDrop;
end;
implementation
type
{ TQLPreviewPanelMate }
TQLPreviewPanelMate = objcclass( NSObject, QLPreviewPanelDataSourceProtocol )
private
_urlArray: NSArray;
public
function numberOfPreviewItemsInPreviewPanel (panel: QLPreviewPanel): NSInteger;
function previewPanel_previewItemAtIndex (panel: QLPreviewPanel; index: NSInteger): QLPreviewItemProtocol;
public
function initWithItems( urlArray: NSArray ): id; message 'setUrlArray:';
procedure dealloc; override;
end;
function TQLPreviewPanelMate.numberOfPreviewItemsInPreviewPanel(
panel: QLPreviewPanel): NSInteger;
begin
Result:= _urlArray.count;
end;
function TQLPreviewPanelMate.previewPanel_previewItemAtIndex(panel: QLPreviewPanel;
index: NSInteger): QLPreviewItemProtocol;
begin
Result:= QLPreviewItemProtocol( _urlArray.objectAtIndex(index) );
end;
function TQLPreviewPanelMate.initWithItems(urlArray: NSArray): id;
begin
Result:= Inherited init;
_urlArray:= urlArray;
_urlArray.retain;
end;
procedure TQLPreviewPanelMate.dealloc;
begin
_urlArray.release;
Inherited;
end;
class procedure TDarwinPanelUtil.showQuickLook;
var
lclArray: TStringArray;
mate: TQLPreviewPanelMate;
panel: QLPreviewPanel;
begin
lclArray:= TDCCocoaApplication(NSApp).serviceMenuGetFilenames;
if lclArray = nil then
Exit;
mate:= TQLPreviewPanelMate.alloc.initWithItems( UrlArrayFromLCLToNS(lclArray) );
panel:= QLPreviewPanel.sharedPreviewPanel;
panel.setDataSource( mate );
panel.makeKeyAndOrderFront( nil );
mate.release;
end;
type
{ TFinderTagsEditorPanelHandler }
TFinderTagsEditorPanelHandler = class
private
_urls: NSArray;
_oldTagNames: NSArray;
public
constructor Create( const paths: TStringArray );
destructor Destroy; override;
procedure onClose( const cancel: Boolean; const newTagNames: NSArray );
end;
constructor TFinderTagsEditorPanelHandler.Create( const paths: TStringArray );
begin
_urls:= UrlArrayFromLCLToNS( paths );
_urls.retain;
_oldTagNames:= uDarwinFinderModelUtil.getTagNamesOfFiles( _urls );
_oldTagNames.retain;
end;
destructor TFinderTagsEditorPanelHandler.Destroy;
begin
_oldTagNames.release;
_urls.release;
end;
procedure TFinderTagsEditorPanelHandler.onClose( const cancel: Boolean; const newTagNames: NSArray );
procedure processRemovedTags;
var
removedTagNames: NSMutableSet;
tagName: NSString;
begin
removedTagNames:= NSMutableSet.setWithArray( _oldTagNames );
removedTagNames.minusSet( NSSet.setWithArray(newTagNames) );
for tagName in removedTagNames do begin
uDarwinFinderModelUtil.removeTagForFiles( _urls, tagName );
end;
end;
procedure processAddedTags;
var
addedTagNames: NSMutableSet;
tagName: NSString;
begin
addedTagNames:= NSMutableSet.setWithArray( newTagNames );
addedTagNames.minusSet( NSSet.setWithArray(_oldTagNames) );
for tagName in addedTagNames do begin
uDarwinFinderModelUtil.addTagForFiles( _urls, tagName );
end;
end;
begin
if cancel then
Exit;
if _urls.count = 1 then begin
uDarwinFinderModelUtil.setTagNamesOfFile( NSURL(_urls.objectAtIndex(0)), newTagNames );
end else begin
processRemovedTags;
processAddedTags;
end;
end;
class procedure TDarwinPanelUtil.showEditFinderTags(const Sender: id;
const control: TWinControl);
var
tagItem: NSToolBarItem absolute Sender;
filenames: TStringArray;
view: NSView;
handler: TFinderTagsEditorPanelHandler;
begin
filenames:= TDCCocoaApplication(NSApp).serviceMenuGetFilenames;
if length(filenames) = 0 then
Exit;
view:= nil;
if Assigned(tagItem) then
view:= tagItem.valueForKey( NSSTR('_itemViewer') );
if (view=nil) or (view.window=nil) then
view:= NSView( control.Handle );
handler:= TFinderTagsEditorPanelHandler.Create( filenames );
uDarwinFinderUtil.popoverFileTagsEditor( filenames, handler.onClose, view , NSMaxYEdge );
end;
// Sharing Service
class procedure TDarwinPanelUtil.showSharingService;
var
picker: NSSharingServicePicker;
cocoaArray: NSArray;
lclArray: TStringArray;
point: TPoint;
popupNSRect: NSRect;
control: TWinControl;
begin
if not TDCCocoaApplication(NSApp).serviceMenuIsReady then
exit;
lclArray:= TDCCocoaApplication(NSApp).serviceMenuGetFilenames;
if lclArray = nil then
Exit;
cocoaArray:= UrlArrayFromLCLToNS( lclArray );
control:= Screen.ActiveControl;
point:= control.ScreenToClient( Mouse.CursorPos );
popupNSRect.origin.x:= point.X;
popupNSRect.origin.y:= point.Y;
popupNSRect.size:= NSMakeSize( 1, 1 );
picker:= NSSharingServicePicker.alloc.initWithItems( cocoaArray );
picker.showRelativeToRect_ofView_preferredEdge( popupNSRect, NSView(control.handle) , NSMaxXEdge );
picker.release;
end;
class procedure TDarwinPanelUtil.showAirDrop;
var
service: NSSharingService;
lclArray: TStringArray;
cocoaArray: NSArray;
begin
lclArray:= TDCCocoaApplication(NSApp).serviceMenuGetFilenames;
if lclArray = nil then
Exit;
cocoaArray:= UrlArrayFromLCLToNS( lclArray );
service:= NSSharingService.sharingServiceNamed( NSSharingServiceNameSendViaAirDrop );
service.performWithItems( cocoaArray );
end;
end.

View file

@ -63,13 +63,6 @@ type
procedure PopUp( const menu: TPopupMenu; const caption: String; const paths: TStringArray );
end;
procedure showQuickLookPanel;
procedure showEditFinderTagsPanel( const Sender: id; const control: TWinControl );
// MacOS Sharing
procedure showMacOSSharingServiceMenu;
procedure showMacOSAirDropDialog;
var
MacosServiceMenuHelper: TMacosServiceMenuHelper;
@ -219,50 +212,6 @@ begin
end;
end;
procedure showMacOSSharingServiceMenu;
var
picker: NSSharingServicePicker;
cocoaArray: NSArray;
lclArray: TStringArray;
point: TPoint;
popupNSRect: NSRect;
control: TWinControl;
begin
if not TDCCocoaApplication(NSApp).serviceMenuIsReady then
exit;
lclArray:= TDCCocoaApplication(NSApp).serviceMenuGetFilenames;
if lclArray = nil then
Exit;
cocoaArray:= UrlArrayFromLCLToNS( lclArray );
control:= Screen.ActiveControl;
point:= control.ScreenToClient( Mouse.CursorPos );
popupNSRect.origin.x:= point.X;
popupNSRect.origin.y:= point.Y;
popupNSRect.size:= NSMakeSize( 1, 1 );
picker:= NSSharingServicePicker.alloc.initWithItems( cocoaArray );
picker.showRelativeToRect_ofView_preferredEdge( popupNSRect, NSView(control.handle) , NSMaxXEdge );
picker.release;
end;
procedure showMacOSAirDropDialog;
var
service: NSSharingService;
lclArray: TStringArray;
cocoaArray: NSArray;
begin
lclArray:= TDCCocoaApplication(NSApp).serviceMenuGetFilenames;
if lclArray = nil then
Exit;
cocoaArray:= UrlArrayFromLCLToNS( lclArray );
service:= NSSharingService.sharingServiceNamed( NSSharingServiceNameSendViaAirDrop );
service.performWithItems( cocoaArray );
end;
procedure Initialize;
begin
MacosServiceMenuHelper:= TMacosServiceMenuHelper.Create;
@ -274,150 +223,6 @@ begin
FreeAndNil( MacosServiceMenuHelper );
end;
type
{ TDCQLPreviewPanelMate }
TDCQLPreviewPanelMate = objcclass( NSObject, QLPreviewPanelDataSourceProtocol )
private
_urlArray: NSArray;
public
function numberOfPreviewItemsInPreviewPanel (panel: QLPreviewPanel): NSInteger;
function previewPanel_previewItemAtIndex (panel: QLPreviewPanel; index: NSInteger): QLPreviewItemProtocol;
public
function initWithItems( urlArray: NSArray ): id; message 'setUrlArray:';
procedure dealloc; override;
end;
function TDCQLPreviewPanelMate.numberOfPreviewItemsInPreviewPanel(
panel: QLPreviewPanel): NSInteger;
begin
Result:= _urlArray.count;
end;
function TDCQLPreviewPanelMate.previewPanel_previewItemAtIndex(panel: QLPreviewPanel;
index: NSInteger): QLPreviewItemProtocol;
begin
Result:= QLPreviewItemProtocol( _urlArray.objectAtIndex(index) );
end;
function TDCQLPreviewPanelMate.initWithItems(urlArray: NSArray): id;
begin
Result:= Inherited init;
_urlArray:= urlArray;
_urlArray.retain;
end;
procedure TDCQLPreviewPanelMate.dealloc;
begin
_urlArray.release;
Inherited;
end;
procedure showQuickLookPanel;
var
lclArray: TStringArray;
mate: TDCQLPreviewPanelMate;
panel: QLPreviewPanel;
begin
lclArray:= TDCCocoaApplication(NSApp).serviceMenuGetFilenames;
if lclArray = nil then
Exit;
mate:= TDCQLPreviewPanelMate.alloc.initWithItems( UrlArrayFromLCLToNS(lclArray) );
panel:= QLPreviewPanel.sharedPreviewPanel;
panel.setDataSource( mate );
panel.makeKeyAndOrderFront( nil );
mate.release;
end;
type
{ TFinderTagsEditorPanelHandler }
TFinderTagsEditorPanelHandler = class
private
_urls: NSArray;
_oldTagNames: NSArray;
public
constructor Create( const paths: TStringArray );
destructor Destroy; override;
procedure onClose( const cancel: Boolean; const newTagNames: NSArray );
end;
constructor TFinderTagsEditorPanelHandler.Create( const paths: TStringArray );
begin
_urls:= UrlArrayFromLCLToNS( paths );
_urls.retain;
_oldTagNames:= uDarwinFinderModelUtil.getTagNamesOfFiles( _urls );
_oldTagNames.retain;
end;
destructor TFinderTagsEditorPanelHandler.Destroy;
begin
_oldTagNames.release;
_urls.release;
end;
procedure TFinderTagsEditorPanelHandler.onClose( const cancel: Boolean; const newTagNames: NSArray );
procedure processRemovedTags;
var
removedTagNames: NSMutableSet;
tagName: NSString;
begin
removedTagNames:= NSMutableSet.setWithArray( _oldTagNames );
removedTagNames.minusSet( NSSet.setWithArray(newTagNames) );
for tagName in removedTagNames do begin
uDarwinFinderModelUtil.removeTagForFiles( _urls, tagName );
end;
end;
procedure processAddedTags;
var
addedTagNames: NSMutableSet;
tagName: NSString;
begin
addedTagNames:= NSMutableSet.setWithArray( newTagNames );
addedTagNames.minusSet( NSSet.setWithArray(_oldTagNames) );
for tagName in addedTagNames do begin
uDarwinFinderModelUtil.addTagForFiles( _urls, tagName );
end;
end;
begin
if cancel then
Exit;
if _urls.count = 1 then begin
uDarwinFinderModelUtil.setTagNamesOfFile( NSURL(_urls.objectAtIndex(0)), newTagNames );
end else begin
processRemovedTags;
processAddedTags;
end;
end;
procedure showEditFinderTagsPanel( const Sender: id; const control: TWinControl );
var
tagItem: NSToolBarItem absolute Sender;
filenames: TStringArray;
view: NSView;
handler: TFinderTagsEditorPanelHandler;
begin
filenames:= TDCCocoaApplication(NSApp).serviceMenuGetFilenames;
if length(filenames) = 0 then
Exit;
view:= nil;
if Assigned(tagItem) then
view:= tagItem.valueForKey( NSSTR('_itemViewer') );
if (view=nil) or (view.window=nil) then
view:= NSView( control.Handle );
handler:= TFinderTagsEditorPanelHandler.Create( filenames );
uDarwinFinderUtil.popoverFileTagsEditor( filenames, handler.onClose, view , NSMaxYEdge );
end;
initialization
Initialize;

View file

@ -78,7 +78,7 @@ uses
uOSUtils, uFileProcs, uShellExecute, uLng, uPixMapManager, uMyUnix, uOSForms,
fMain, fFileProperties, DCOSUtils, DCStrUtils, uExts, uArchiveFileSourceUtil, uSysFolders
{$IF DEFINED(DARWIN)}
, LCLStrConsts, MacOSAll, CocoaAll, uMyDarwin, uDarwinUtil
, LCLStrConsts, MacOSAll, CocoaAll, uMyDarwin, uDarwinPanel, uDarwinUtil
{$ELSEIF NOT DEFINED(HAIKU)}
, uKeyFile, uMimeActions
{$IF DEFINED(LINUX)}
@ -657,12 +657,12 @@ end;
procedure TShellContextMenu.SharingMenuItemAction(Sender: TObject);
begin
showMacOSSharingServiceMenu;
TDarwinPanelUtil.showSharingService;
end;
procedure TShellContextMenu.EditFinderTagsAction(Sender: TObject);
begin
showEditFinderTagsPanel( nil, frmMain );
TDarwinPanelUtil.showEditFinderTags( nil, frmMain );
end;
{$ENDIF}