cocoa: adding support of getting information IF dark theme is used

git-svn-id: trunk@61035 -
This commit is contained in:
dmitry 2019-04-22 05:31:54 +00:00
parent 2a63002bd2
commit 1f5d0de524
2 changed files with 98 additions and 1 deletions

View File

@ -56,10 +56,18 @@ type
procedure setEnabled_(aenabled: ObjCBool); message 'setEnabled:';
end;
NSAppearance = objcclass external(NSObject)
function name: NSString; message 'name';
class function currentAppearance: NSAppearance; message 'currentAppearance';
end;
NSApplicationFix = objccategory external (NSApplication)
procedure activateIgnoringOtherApps_(flag: ObjCBool); message 'activateIgnoringOtherApps:';
function nextEventMatchingMask_untilDate_inMode_dequeue_(mask: NSUInteger; expiration: NSDate; mode: NSString; deqFlag: ObjCBool): NSEvent; message 'nextEventMatchingMask:untilDate:inMode:dequeue:';
procedure postEvent_atStart_(event: NSEvent; flag: ObjCBool); message 'postEvent:atStart:';
function appearance: NSAppearance; message 'appearance'; // 10.14 (10.13)
function effectiveAppearance: NSAppearance; message 'effectiveAppearance'; // 10.14 (10.13)
end;
NSButtonFix = objccategory external(NSButton)
@ -151,6 +159,8 @@ type
procedure setHasShadow_(hasShadow_: ObjCBool); message 'setHasShadow:';
procedure setIgnoresMouseEvents_(flag: ObjCBool); message 'setIgnoresMouseEvents:';
{$endif}
// 10.14
function appearance: NSAppearance; message 'appearance'; // 10.14 (10.13)
end;
NSTableColumnFix = objccategory external (NSTableColumn)
@ -198,6 +208,8 @@ const
NSAppKitVersionNumber10_10 = 1343;
NSAppKitVersionNumber10_11 = 1404;
NSAppKitVersionNumber10_12 = 1504;
NSAppKitVersionNumber10_13 = 1561;
NSAppKitVersionNumber10_14 = 1641.10;
const

View File

@ -22,7 +22,7 @@ uses
LCLType, LCLProc, LCLIntf, Graphics, Themes, TmSchema,
customdrawndrawers,
// widgetset
CocoaUtils, CocoaGDIObjects;
CocoaUtils, CocoaGDIObjects, Cocoa_Extra;
type
{ TCocoaThemeServices }
@ -60,8 +60,93 @@ type
function GetOption(AOption: TThemeOption): Integer; override;
end;
// "dark" is not a good reference, as Apple might add more and more themes
function IsDarkPossible: Boolean; inline;
// returns if the application appearance is set to dark
function IsAppDark: Boolean;
// returns if the window appearance is set to dark
function IsWinDark(win: NSWindow): Boolean;
// returns true, if currently drawn (Painted) UI control is in Dark theme.
// The method would likely return FALSE outside of Paint event.
function IsPaintDark: Boolean;
// returns true, if Appear is assigned and bears name of Dark theme
function IsAppearDark(Appear: NSAppearance): Boolean; inline;
// weak-referenced NSAppearnceClass. Returns nil on any OS prior to 10.13
function NSAppearanceClass: pobjc_class;
implementation
var
_NSAppearanceClass : pobjc_class = nil;
_NSAppearanceClassRead: Boolean = false;
function NSAppearanceClass: pobjc_class;
begin
if not _NSAppearanceClassRead then
begin
_NSAppearanceClass := objc_getClass('NSAppearance');
_NSAppearanceClassRead := true;
end;
Result := _NSAppearanceClass;
end;
function IsAppearDark(Appear: NSAppearance): Boolean; inline;
begin
Result := Assigned(Appear)
and Appear.name.isEqualToString(NSSTR('NSAppearanceNameVibrantDark'))
end;
function IsDarkPossible: Boolean; inline;
begin
Result := NSAppKitVersionNumber > NSAppKitVersionNumber10_12;
end;
function IsAppDark: Boolean;
var
Appear: NSAppearance;
begin
if not isDarkPossible then
begin
Result := false;
Exit;
end;
if (not NSApplication(NSApp).respondsToSelector(ObjCSelector('appearance'))) then begin
Result := false;
Exit;
end;
Result := IsAppearDark(NSApplication(NSApp).appearance);
end;
function IsWinDark(win: NSWindow): Boolean;
begin
if not Assigned(win) or not isDarkPossible then
begin
Result := false;
Exit;
end;
if (not NSApplication(win).respondsToSelector(ObjCSelector('appearance'))) then begin
Result := false;
Exit;
end;
Result := IsAppearDark(win.appearance);
end;
function IsPaintDark: Boolean;
var
cls : pobjc_class;
begin
cls := NSAppearanceClass;
if not Assigned(cls) then Exit;
Result := IsAppearDark(objc_msgSend(cls, ObjCSelector('currentAppearance')));
end;
{ TCocoaThemeServices }
{------------------------------------------------------------------------------