vcl/inc/osx/salframe.h       |    2 +
 vcl/osx/salframe.cxx         |   49 +++++++++++++++++++++++++++++++++----------
 vcl/osx/salnativewidgets.cxx |   19 +++++++++++++++-
 3 files changed, 58 insertions(+), 12 deletions(-)

New commits:
commit 48125efbd7bf370981d48e4a8d0ce7fbaf1857e1
Author:     Patrick Luby <guibmac...@gmail.com>
AuthorDate: Sun Feb 23 22:28:56 2025 -0500
Commit:     Patrick Luby <guibomac...@gmail.com>
CommitDate: Mon Feb 24 16:00:29 2025 +0100

    tdf#165266 Force NSColor and native controls to use effective appearance
    
    +[NSAppearance setCurrentAppearance:] is deprecated and calling that
    appears to do less and less with each new version of macos and/or Xcode
    so run all system +[NSColor ...] calls and drawing of native controls
    in a block passed to -[NSAppearance performAsCurrentDrawingAppearance:].
    
    Change-Id: I857bfb2504ad2896579c7b2da4fdd2d591fab3b8
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/182074
    Reviewed-by: Sahil Gautam <sahil.gautam.ext...@allotropia.de>
    Tested-by: Jenkins
    Reviewed-by: Patrick Luby <guibomac...@gmail.com>

diff --git a/vcl/inc/osx/salframe.h b/vcl/inc/osx/salframe.h
index ee7afe053cab..beb5bff493fe 100644
--- a/vcl/inc/osx/salframe.h
+++ b/vcl/inc/osx/salframe.h
@@ -229,6 +229,8 @@ private: // methods
 
     bool doFlush();
 
+    void doUpdateSettings( AllSettings& rSettings );
+
 private: // data
     static AquaSalFrame*       s_pCaptureFrame;
 
diff --git a/vcl/osx/salframe.cxx b/vcl/osx/salframe.cxx
index 6314c198e06a..b6d0625b0670 100644
--- a/vcl/osx/salframe.cxx
+++ b/vcl/osx/salframe.cxx
@@ -1445,21 +1445,23 @@ void AquaSalFrame::getResolution( sal_Int32& o_rDPIX, 
sal_Int32& o_rDPIY )
 
 void AquaSalFrame::UpdateDarkMode()
 {
-    if (@available(macOS 10.14, iOS 13, *))
+    NSAppearance *pCurrentAppearance = [NSApp appearance];
+
+    switch (MiscSettings::GetDarkMode())
     {
-        switch (MiscSettings::GetDarkMode())
-        {
-            case 0: // auto
-            default:
+        case 0: // auto
+        default:
+            if (pCurrentAppearance)
                 [NSApp setAppearance: nil];
-                break;
-            case 1: // light
+            break;
+        case 1: // light
+            if (!pCurrentAppearance || [NSAppearanceNameAqua isEqualToString: 
[pCurrentAppearance name]])
                 [NSApp setAppearance: [NSAppearance appearanceNamed: 
NSAppearanceNameAqua]];
-                break;
-            case 2: // dark
+            break;
+        case 2: // dark
+            if (!pCurrentAppearance || [NSAppearanceNameDarkAqua 
isEqualToString: [pCurrentAppearance name]])
                 [NSApp setAppearance: [NSAppearance appearanceNamed: 
NSAppearanceNameDarkAqua]];
-                break;
-        }
+            break;
     }
 }
 
@@ -1547,6 +1549,31 @@ void AquaSalFrame::UpdateSettings( AllSettings& 
rSettings )
 
     OSX_SALDATA_RUNINMAIN( UpdateSettings( rSettings ) )
 
+    // tdf#165266 Force NSColor to use current effective appearance
+    // +[NSAppearance setCurrentAppearance:] is deprecated and calling
+    // that appears to do less and less with each new version of macos
+    // or Xcode so run all system +[NSColor ...] calls in a block passed
+    // to -[NSAppearance performAsCurrentDrawingAppearance:].
+    UpdateDarkMode();
+    if (@available(macOS 11, *))
+    {
+        assert(mpNSView);
+        [mpNSView.effectiveAppearance performAsCurrentDrawingAppearance:^() {
+            doUpdateSettings( rSettings );
+            return;
+        }];
+    }
+    else
+    {
+        doUpdateSettings( rSettings );
+    }
+}
+
+void AquaSalFrame::doUpdateSettings( AllSettings& rSettings )
+{
+    assert(mpNSWindow);
+    assert(mpNSView);
+
 SAL_WNODEPRECATED_DECLARATIONS_PUSH
         // "'lockFocus' is deprecated: first deprecated in macOS 10.14 - To 
draw, subclass NSView
         // and implement -drawRect:; AppKit's automatic deferred display 
mechanism will call
diff --git a/vcl/osx/salnativewidgets.cxx b/vcl/osx/salnativewidgets.cxx
index cfbb154eef74..b5b7ba7789b6 100644
--- a/vcl/osx/salnativewidgets.cxx
+++ b/vcl/osx/salnativewidgets.cxx
@@ -256,7 +256,24 @@ bool AquaSalGraphics::drawNativeControl(ControlType nType,
                                         const OUString &,
                                         const Color&)
 {
-    return mpBackend->drawNativeControl(nType, nPart, rControlRegion, nState, 
aValue);
+    // tdf#165266 Force native controls to use current effective appearance
+    // +[NSAppearance setCurrentAppearance:] is deprecated and calling
+    // that appears to do less and less with each new version of macos
+    // and/or Xcode so run all drawing of native controls in a block passed
+    // to -[NSAppearance performAsCurrentDrawingAppearance:].
+    __block bool bRet = false;
+    if (@available(macOS 11, *))
+    {
+        [[NSApp effectiveAppearance] performAsCurrentDrawingAppearance:^() {
+            bRet = mpBackend->drawNativeControl(nType, nPart, rControlRegion, 
nState, aValue);
+        }];
+    }
+    else
+    {
+        bRet = mpBackend->drawNativeControl(nType, nPart, rControlRegion, 
nState, aValue);
+    }
+
+    return bRet;
 }
 
 static NSColor* colorFromRGB(const Color& rColor)

Reply via email to