UPD: simplify theme related based on the new Lazarus/Cocoa version

This commit is contained in:
rich2014 2025-12-18 00:50:21 +08:00
commit c97c74fb02
3 changed files with 17 additions and 71 deletions

View file

@ -901,7 +901,6 @@ type
procedure OnNSServiceOpenWithNewTab( filenames:TStringList );
function NSServiceMenuIsReady(): boolean;
function NSServiceMenuGetFilenames(): TStringArray;
procedure NSThemeChangedHandler();
{$ENDIF}
procedure LoadWindowState;
procedure SaveWindowState;
@ -1263,7 +1262,6 @@ begin
{$IF DEFINED(DARWIN)}
TDarwinApplicationUtil.initServiceProvider( @OnNSServiceOpenWithNewTab, @NSServiceMenuIsReady, @NSServiceMenuGetFilenames );
TDarwinApplicationUtil.addThemeObserver( @NSThemeChangedHandler );
TDarwinFileViewUtil.init( @ActiveNotebook, @ActiveFrame );
{$ENDIF}
end;
@ -6424,11 +6422,6 @@ begin
FreeAndNil( files );
Result:= filenames;
end;
procedure TfrmMain.NSThemeChangedHandler;
begin
ThemeServices.IntfDoOnThemeChange;
end;
{$ENDIF}
procedure TfrmMain.LoadWindowState;

View file

@ -6,9 +6,9 @@ unit uDarwinApplication;
interface
uses
Classes, SysUtils, fgl, Menus, uLng,
Classes, SysUtils, Menus, uLng,
MacOSAll, CocoaAll,
CocoaInt, CocoaPrivate, Cocoa_Extra, CocoaMenus, CocoaUtils, CocoaConst,
CocoaInt, CocoaPrivate, CocoaThemes, Cocoa_Extra, CocoaMenus, CocoaUtils, CocoaConst,
uDarwinUtil, uDarwinFinder;
const
@ -20,9 +20,6 @@ type
TDarwinServiceMenuIsReadyFunc = Function(): Boolean of object;
TDarwinServiceMenuGetFilenamesFunc = Function(): TStringArray of object;
TDarwinThemeObserver = Procedure() of object;
TDarwinThemeObservers = specialize TFPGList<TDarwinThemeObserver>;
{ TDarwinServiceProvider }
TDarwinServiceProvider = objcclass(NSObject)
@ -37,8 +34,6 @@ 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;
private
_serviceProvider: TDarwinServiceProvider;
public
@ -49,12 +44,6 @@ type
{ TDarwinApplicationUtil }
TDarwinApplicationUtil = class
private
class var _themeObservers: TDarwinThemeObservers;
private
class procedure init;
class procedure deinit;
class procedure themeNotify;
public
class procedure initServiceProvider(
const serveCallback: TDarwinServiceProviderCallBack;
@ -64,7 +53,6 @@ type
class procedure performService( const serviceName: String );
class procedure openSystemSecurityPreferences_PrivacyAllFiles;
public
class procedure addThemeObserver( const observer: TDarwinThemeObserver );
class procedure setTheme( const mode: Integer );
class function isDarkTheme: Boolean;
public
@ -101,20 +89,6 @@ begin
Result:= true;
end;
procedure TDCCocoaApplication.observeValueForKeyPath_ofObject_change_context(
keyPath: NSString; object_: id; change: NSDictionary; context_: pointer);
var
prior: NSNumber;
begin
Inherited observeValueForKeyPath_ofObject_change_context( keyPath, object_, change, context_ );
if keyPath.isEqualToString(NSSTR('effectiveAppearance')) then begin
prior:= NSNumber( change.valueForKey( NSSTR('notificationIsPrior') ) );
if prior.intValue > 0 then
Exit;
TDarwinApplicationUtil.themeNotify;
end;
end;
{ TDarwinServiceProvider }
procedure TDarwinServiceProvider.openWithNewTab( pboard:NSPasteboard; userData:NSString; error:NSStringPtr );
@ -136,32 +110,6 @@ end;
{ TDarwinApplicationUtil }
class procedure TDarwinApplicationUtil.init;
begin
_themeObservers:= TDarwinThemeObservers.Create;
end;
class procedure TDarwinApplicationUtil.deinit;
begin
FreeAndNil( _themeObservers );
end;
class procedure TDarwinApplicationUtil.themeNotify;
var
i: Integer;
begin
if _themeObservers = nil then
Exit;
for i:= 0 to _themeObservers.Count-1 do begin
_themeObservers[i]();
end;
end;
class procedure TDarwinApplicationUtil.addThemeObserver( const observer: TDarwinThemeObserver );
begin
_themeObservers.Add( observer );
end;
class procedure TDarwinApplicationUtil.setTheme(const mode: Integer);
var
appearance: NSAppearance;
@ -178,7 +126,11 @@ begin
appearance:= NSAppearance.appearanceNamed( NSAppearanceNameAqua );
end;
NSApp.setAppearance( appearance );
TDarwinApplicationUtil.themeNotify;
if appearance = nil then
appearance:= NSApp.effectiveAppearance;
NSAppearance.setCurrentAppearance( appearance );
TCocoaThemeServices.darwinThemeChangedNotify;
end;
class function TDarwinApplicationUtil.isDarkTheme: Boolean;
@ -363,11 +315,5 @@ begin
AttachEditMenu( menu, menu.numberOfItems, CocoaConst.NSSTR_EDIT_MENU );
end;
initialization
TDarwinApplicationUtil.init;
finalization
TDarwinApplicationUtil.deinit;
end.

View file

@ -15,7 +15,7 @@ uses
uDCUtils, uLng, uGlobs,
uDarwinApplication, uDarwinFSWatch, uDarwinSimpleFSWatch,
uDarwinFile, uDarwinImage,
CocoaAll, CocoaUtils;
CocoaAll, CocoaUtils, CocoaThemes;
type
@ -105,12 +105,13 @@ type
{ TiCloudDriveUIHandler }
TiCloudDriveUIHandler = class( TFileSourceUIHandler )
TiCloudDriveUIHandler = class( TFileSourceUIHandler, ICocoaThemeObserver )
private
_downloadImage: NSImage;
private
procedure createImages;
procedure releaseImages;
procedure onThemeChanged;
public
constructor Create;
destructor Destroy; override;
@ -556,14 +557,20 @@ begin
_downloadImage:= nil;
end;
procedure TiCloudDriveUIHandler.onThemeChanged;
begin
self.releaseImages;
end;
constructor TiCloudDriveUIHandler.Create;
begin
Inherited;
TDarwinApplicationUtil.addThemeObserver( @self.releaseImages );
TCocoaThemeServices.addObserver( self );
end;
destructor TiCloudDriveUIHandler.Destroy;
begin
TCocoaThemeServices.removeObserver( self );
self.releaseImages;
Inherited;
end;