cui/source/options/appearance.cxx     |   28 ++++++++++++++--------------
 cui/source/options/appearance.hxx     |    9 +--------
 include/vcl/settings.hxx              |   11 ++++++-----
 include/vcl/themecolors.hxx           |   14 ++++++++++++++
 sfx2/source/appl/appserv.cxx          |   26 ++++++++++++++------------
 svtools/source/config/colorcfg.cxx    |   13 +++++++++----
 vcl/osx/salframe.cxx                  |    6 +++---
 vcl/osx/salinst.cxx                   |    6 +++---
 vcl/source/app/settings.cxx           |   30 +++++++++++++++++++++---------
 vcl/unx/gtk3/gtkframe.cxx             |    6 +++---
 vcl/win/gdi/salnativewidgets-luna.cxx |    6 +++---
 vcl/win/window/salframe.cxx           |    6 +++---
 12 files changed, 94 insertions(+), 67 deletions(-)

New commits:
commit ebbb3b979fb4b866947bb90addde59adff5fb267
Author:     Sahil Gautam <sahil.gautam.ext...@allotropia.de>
AuthorDate: Mon Apr 21 01:08:32 2025 +0530
Commit:     Sahil Gautam <sahil.gautam.ext...@allotropia.de>
CommitDate: Thu May 1 12:35:40 2025 +0200

    tdf#164970 use proper enums for ApplicationAppearance instead of raw ints
    
    ApplicationAppearance registry entry holds the appearance mode the
    application is in at any point. it is represented by three integers 0
    for "Automatic", 1 for "Light" and 2 for "Dark".
    
    this patch introduces an enum `AppearanceMode` which replaces the
    integer switch cases and comparisons in conditionals with more readible
    enums.
    
    Change-Id: Ibe90875cc8d2370476442f726ebbb4eb02cccc91
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/184393
    Reviewed-by: Michael Weghorn <m.wegh...@posteo.de>
    Tested-by: Jenkins

diff --git a/cui/source/options/appearance.cxx 
b/cui/source/options/appearance.cxx
index 1cae201a5b0b..225bfbd862ac 100644
--- a/cui/source/options/appearance.cxx
+++ b/cui/source/options/appearance.cxx
@@ -166,9 +166,9 @@ OUString SvxAppearanceTabPage::GetAllStrings()
 bool SvxAppearanceTabPage::FillItemSet(SfxItemSet* /* rSet */)
 {
     // commit appearance value if changed
-    if (eCurrentAppearanceMode != 
static_cast<Appearance>(MiscSettings::GetAppColorMode()))
+    if (eCurrentAppearanceMode != MiscSettings::GetAppColorMode())
     {
-        
MiscSettings::SetAppColorMode(static_cast<int>(eCurrentAppearanceMode));
+        MiscSettings::SetAppColorMode(eCurrentAppearanceMode);
         // if themes disabled then change the document colors as per the new 
appearance mode.
         if (ThemeColors::IsThemeDisabled())
             UpdateDocumentAppearance();
@@ -193,7 +193,7 @@ void SvxAppearanceTabPage::Reset(const SfxItemSet* /* rSet 
*/)
     UpdateRemoveBtnState();
 
     // reset appearance
-    eCurrentAppearanceMode = 
static_cast<Appearance>(MiscSettings::GetAppColorMode());
+    eCurrentAppearanceMode = MiscSettings::GetAppColorMode();
 
     // reset ColorConfig
     pColorConfig->ClearModified();
@@ -218,16 +218,16 @@ IMPL_LINK_NOARG(SvxAppearanceTabPage, ShowInDocumentHdl, 
weld::Toggleable&, void
 IMPL_LINK_NOARG(SvxAppearanceTabPage, AppearanceChangeHdl, weld::Toggleable&, 
void)
 {
     if (m_xAppearanceSystem->get_state() == TRISTATE_TRUE)
-        eCurrentAppearanceMode = Appearance::SYSTEM;
+        eCurrentAppearanceMode = AppearanceMode::AUTO;
     if (m_xAppearanceLight->get_state() == TRISTATE_TRUE)
-        eCurrentAppearanceMode = Appearance::LIGHT;
+        eCurrentAppearanceMode = AppearanceMode::LIGHT;
     if (m_xAppearanceDark->get_state() == TRISTATE_TRUE)
-        eCurrentAppearanceMode = Appearance::DARK;
+        eCurrentAppearanceMode = AppearanceMode::DARK;
     // set the extension theme on light/dark
 
     // restart iff appearance was toggled and theme was enabled
     m_bRestartRequired = false;
-    if (eCurrentAppearanceMode != 
static_cast<Appearance>(MiscSettings::GetAppColorMode())
+    if (eCurrentAppearanceMode != MiscSettings::GetAppColorMode()
         && !ThemeColors::IsThemeDisabled())
         m_bRestartRequired = true;
 
@@ -484,22 +484,22 @@ void SvxAppearanceTabPage::InitAppearance()
     m_xAppearanceSystem->connect_toggled(LINK(this, SvxAppearanceTabPage, 
AppearanceChangeHdl));
     m_xAppearanceDark->connect_toggled(LINK(this, SvxAppearanceTabPage, 
AppearanceChangeHdl));
 
-    Appearance nAppearance = 
static_cast<Appearance>(MiscSettings::GetAppColorMode());
+    AppearanceMode nAppearance = MiscSettings::GetAppColorMode();
     eCurrentAppearanceMode = nAppearance;
 
     switch (nAppearance)
     {
-        case Appearance::SYSTEM:
+        case AppearanceMode::AUTO:
             m_xAppearanceSystem->set_state(TRISTATE_TRUE);
             break;
-        case Appearance::LIGHT:
+        case AppearanceMode::LIGHT:
             m_xAppearanceLight->set_state(TRISTATE_TRUE);
             break;
-        case Appearance::DARK:
+        case AppearanceMode::DARK:
             m_xAppearanceDark->set_state(TRISTATE_TRUE);
             break;
         default:
-            eCurrentAppearanceMode = Appearance::SYSTEM;
+            eCurrentAppearanceMode = AppearanceMode::AUTO;
     }
 }
 
@@ -587,8 +587,8 @@ void SvxAppearanceTabPage::UpdateDocumentAppearance()
 
 bool SvxAppearanceTabPage::IsDarkModeEnabled()
 {
-    return eCurrentAppearanceMode == Appearance::DARK
-           || (eCurrentAppearanceMode == Appearance::SYSTEM && 
MiscSettings::GetUseDarkMode());
+    return eCurrentAppearanceMode == AppearanceMode::DARK
+           || (eCurrentAppearanceMode == AppearanceMode::AUTO && 
MiscSettings::GetUseDarkMode());
 }
 
 void SvxAppearanceTabPage::FillItemsList()
diff --git a/cui/source/options/appearance.hxx 
b/cui/source/options/appearance.hxx
index 45ba347be1d8..73a2a44f248e 100644
--- a/cui/source/options/appearance.hxx
+++ b/cui/source/options/appearance.hxx
@@ -33,15 +33,8 @@ using namespace svtools;
 class SvxAppearanceTabPage : public SfxTabPage
 {
 private:
-    enum class Appearance
-    {
-        SYSTEM,
-        LIGHT,
-        DARK,
-    };
-
     bool m_bRestartRequired;
-    Appearance eCurrentAppearanceMode;
+    AppearanceMode eCurrentAppearanceMode;
     std::unique_ptr<EditableColorConfig> pColorConfig;
 
     std::unique_ptr<weld::ComboBox> m_xSchemeList;
diff --git a/include/vcl/settings.hxx b/include/vcl/settings.hxx
index 29c4744670d3..4d2b0d55cf17 100644
--- a/include/vcl/settings.hxx
+++ b/include/vcl/settings.hxx
@@ -23,6 +23,7 @@
 #include <config_options.h>
 
 #include <vcl/dllapi.h>
+#include <vcl/themecolors.hxx>
 #include <tools/color.hxx>
 #include <tools/gen.hxx>
 #include <o3tl/cow_wrapper.hxx>
@@ -667,13 +668,13 @@ public:
     bool                            GetDisablePrinting() const;
     void                            SetEnableLocalizedDecimalSep( bool bEnable 
);
     bool                            GetEnableLocalizedDecimalSep() const;
-    // 0 auto, 1 light, 2, dark
-    static void                     SetDarkMode(int nMode);
-    static int                      GetDarkMode();
+
+    static void                     SetDarkMode(AppearanceMode nMode);
+    static AppearanceMode           GetDarkMode();
     // return if dark mode is active, resolving 'auto' to dark (true) or light 
(false)
     static bool                     GetUseDarkMode();
-    static void                     SetAppColorMode(int nMode);
-    static int                      GetAppColorMode();
+    static void                     SetAppColorMode(AppearanceMode nMode);
+    static AppearanceMode           GetAppColorMode();
     // return true if system preferences are set to use reduced animation
     static bool                     GetUseReducedAnimation();
     static bool                     IsAnimatedGraphicAllowed();
diff --git a/include/vcl/themecolors.hxx b/include/vcl/themecolors.hxx
index 80bc1308565d..e00e228d9d69 100644
--- a/include/vcl/themecolors.hxx
+++ b/include/vcl/themecolors.hxx
@@ -21,6 +21,20 @@ enum class ThemeState
     RESET = 2,
 };
 
+/*
+  AUTO          app colors follow os, light doc colors (default)
+  LIGHT         app colors follow os, light doc colors
+  DARK          app colors follow os, dark  doc colors
+  COUNT         app color modes count
+*/
+enum class AppearanceMode
+{
+    AUTO = 0,
+    LIGHT = 1,
+    DARK = 2,
+    COUNT,
+};
+
 class VCL_DLLPUBLIC ThemeColors
 {
     ThemeColors() {}
diff --git a/sfx2/source/appl/appserv.cxx b/sfx2/source/appl/appserv.cxx
index 040fe15a4357..2e21e6c496bb 100644
--- a/sfx2/source/appl/appserv.cxx
+++ b/sfx2/source/appl/appserv.cxx
@@ -692,31 +692,32 @@ void SfxApplication::MiscExec_Impl( SfxRequest& rReq )
                 // Since only gtk/osx/win support UI theme, toggle based on 
document colors
                 // Automatic in this case means "whatever GetUseDarkMode() 
says"
                 const bool bWasInDarkMode
-                    = MiscSettings::GetAppColorMode() == 2
-                      || (MiscSettings::GetAppColorMode() == 0 && 
MiscSettings::GetUseDarkMode());
+                    = MiscSettings::GetAppColorMode() == AppearanceMode::DARK
+                      || (MiscSettings::GetAppColorMode() == 
AppearanceMode::AUTO
+                          && MiscSettings::GetUseDarkMode());
 
                 // Set the UI theme. It would be nicest to use automatic 
whenever possible
-                sal_Int32 nUseMode = 0; // automatic
-                if (MiscSettings::GetDarkMode() != 0)
-                    MiscSettings::SetDarkMode(nUseMode);
+                AppearanceMode eUseMode = AppearanceMode::AUTO;
+                if (MiscSettings::GetDarkMode() != AppearanceMode::AUTO)
+                    MiscSettings::SetDarkMode(eUseMode);
 
                 if (MiscSettings::GetUseDarkMode() == bWasInDarkMode)
                 {
                     // automatic didn't toggle, so force the desired theme
-                    nUseMode = bWasInDarkMode ? 1 : 2;
-                    MiscSettings::SetDarkMode(nUseMode);
+                    eUseMode = bWasInDarkMode ? AppearanceMode::LIGHT : 
AppearanceMode::DARK;
+                    MiscSettings::SetDarkMode(eUseMode);
                 }
 
                 // Now set the document theme
                 // If the UI can be themed, then the document theme can always 
remain on automatic.
-                nUseMode = 0;
+                eUseMode = AppearanceMode::AUTO;
                 // NOTE: since SetDarkMode has run, GetUseDarkMode might 
return a different result.
                 if (MiscSettings::GetUseDarkMode() == bWasInDarkMode)
                 {
-                    nUseMode = bWasInDarkMode ? 1 : 2;
+                    eUseMode = bWasInDarkMode ? AppearanceMode::LIGHT : 
AppearanceMode::DARK;
                     sSchemeName = bWasInDarkMode ? u"Light" : u"Dark";
                 }
-                MiscSettings::SetAppColorMode(nUseMode);
+                MiscSettings::SetAppColorMode(eUseMode);
             }
             svtools::EditableColorConfig aEditableConfig;
             // kit explicitly ignores changes to the global color scheme, 
except for the current ViewShell,
@@ -1362,8 +1363,9 @@ void SfxApplication::MiscState_Impl(SfxItemSet &rSet)
                 case FN_CHANGE_THEME:
                 {
                     const bool bIsDarkMode
-                        = MiscSettings::GetAppColorMode() == 2
-                          || (!MiscSettings::GetAppColorMode() && 
MiscSettings::GetUseDarkMode());
+                        = MiscSettings::GetAppColorMode() == 
AppearanceMode::DARK
+                          || (MiscSettings::GetAppColorMode() == 
AppearanceMode::AUTO
+                              && MiscSettings::GetUseDarkMode());
                     rSet.Put(SfxBoolItem(FN_CHANGE_THEME, bIsDarkMode));
                     break;
                 }
diff --git a/svtools/source/config/colorcfg.cxx 
b/svtools/source/config/colorcfg.cxx
index b3e40141fc43..ce664ce40288 100644
--- a/svtools/source/config/colorcfg.cxx
+++ b/svtools/source/config/colorcfg.cxx
@@ -213,8 +213,8 @@ void ColorConfig_Impl::Load(const OUString& rScheme)
         ++nIndex;
 
         bool bIsDarkMode
-            = MiscSettings::GetAppColorMode() == 2
-              || (MiscSettings::GetAppColorMode() == 0 && 
MiscSettings::GetUseDarkMode());
+            = MiscSettings::GetAppColorMode() == AppearanceMode::DARK
+              || (MiscSettings::GetAppColorMode() == AppearanceMode::AUTO && 
MiscSettings::GetUseDarkMode());
 
         // based on the appearance (light/dark) cache the value of the 
appropriate color in nColor.
         // this way we don't have to add hundreds of function calls in the 
codebase and it will be fast.
@@ -710,14 +710,19 @@ Color ColorConfig::GetDefaultColor(ColorConfigEntry 
eEntry, int nMod)
             else
             {
                 switch (MiscSettings::GetAppColorMode()) {
+                    case AppearanceMode::LIGHT:
+                        nAppMod = clLight;
+                        break;
+                    case AppearanceMode::DARK:
+                        nAppMod = clDark;
+                        break;
+                    case AppearanceMode::AUTO:
                     default:
                         if (MiscSettings::GetUseDarkMode())
                             nAppMod = clDark;
                         else
                             nAppMod = clLight;
                         break;
-                    case 1: nAppMod = clLight; break;
-                    case 2: nAppMod = clDark; break;
                 }
             }
             aRet = cAutoColors[eEntry][nAppMod];
diff --git a/vcl/osx/salframe.cxx b/vcl/osx/salframe.cxx
index 483a5cb9c604..538d614985b5 100644
--- a/vcl/osx/salframe.cxx
+++ b/vcl/osx/salframe.cxx
@@ -1518,16 +1518,16 @@ void AquaSalFrame::UpdateDarkMode()
 
     switch (MiscSettings::GetDarkMode())
     {
-        case 0: // auto
+        case AppearanceMode::AUTO:
         default:
             if (pCurrentAppearance)
                 [NSApp setAppearance: nil];
             break;
-        case 1: // light
+        case AppearanceMode::LIGHT:
             if (!pCurrentAppearance || ![NSAppearanceNameAqua isEqualToString: 
[pCurrentAppearance name]])
                 [NSApp setAppearance: [NSAppearance appearanceNamed: 
NSAppearanceNameAqua]];
             break;
-        case 2: // dark
+        case AppearanceMode::DARK:
             if (!pCurrentAppearance || ![NSAppearanceNameDarkAqua 
isEqualToString: [pCurrentAppearance name]])
                 [NSApp setAppearance: [NSAppearance appearanceNamed: 
NSAppearanceNameDarkAqua]];
             break;
diff --git a/vcl/osx/salinst.cxx b/vcl/osx/salinst.cxx
index 8b9fed74c5b0..e3c654ff558a 100644
--- a/vcl/osx/salinst.cxx
+++ b/vcl/osx/salinst.cxx
@@ -108,9 +108,9 @@ public:
     virtual void Invoke() override
     {
         // Related: tdf#156855 force reload of both native and theme colors
-        int nMode = MiscSettings::GetAppColorMode();
-        if (!nMode)
-            MiscSettings::SetAppColorMode(nMode);
+        AppearanceMode eMode = MiscSettings::GetAppColorMode();
+        if (eMode == AppearanceMode::AUTO)
+            MiscSettings::SetAppColorMode(eMode);
 
         AquaSalInstance *pInst = GetSalData()->mpInstance;
         SalFrame *pAnyFrame = pInst->anyFrame();
diff --git a/vcl/source/app/settings.cxx b/vcl/source/app/settings.cxx
index 16bb4bb882f4..d5d846d0a250 100644
--- a/vcl/source/app/settings.cxx
+++ b/vcl/source/app/settings.cxx
@@ -17,7 +17,6 @@
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
  */
 
-#include <vcl/themecolors.hxx>
 #include <config_folders.h>
 
 #include <officecfg/Office/Common.hxx>
@@ -2308,16 +2307,16 @@ bool MiscSettings::GetEnableLocalizedDecimalSep() const
     return mxData->mbEnableLocalizedDecimalSep;
 }
 
-int MiscSettings::GetDarkMode()
+AppearanceMode MiscSettings::GetDarkMode()
 {
     // MiscSettings::GetAppColorMode() replaces MiscSettings::GetDarkMode()
     return MiscSettings::GetAppColorMode();
 }
 
-void MiscSettings::SetDarkMode(int nMode)
+void MiscSettings::SetDarkMode(AppearanceMode eMode)
 {
     // MiscSettings::SetAppColorMode() replaces MiscSettings::SetDarkMode()
-    MiscSettings::SetAppColorMode(nMode);
+    MiscSettings::SetAppColorMode(eMode);
 }
 
 bool MiscSettings::GetUseDarkMode()
@@ -2328,14 +2327,26 @@ bool MiscSettings::GetUseDarkMode()
     return pDefWindow->ImplGetFrame()->GetUseDarkMode();
 }
 
-int MiscSettings::GetAppColorMode()
+AppearanceMode MiscSettings::GetAppColorMode()
 {
     if (comphelper::IsFuzzing())
-        return 0;
-    return officecfg::Office::Common::Appearance::ApplicationAppearance::get();
+        return AppearanceMode::AUTO;
+
+    int nMode = 
officecfg::Office::Common::Appearance::ApplicationAppearance::get();
+
+    // check for invalid appearance mode, and if found, set it back to AUTO
+    if (nMode < static_cast<int>(AppearanceMode::AUTO)
+        || static_cast<int>(AppearanceMode::COUNT) <= nMode)
+    {
+        SAL_WARN("vcl.app", "invalid appearance mode! setting back to 
AppearanceMode::AUTO");
+        MiscSettings::SetAppColorMode(AppearanceMode::AUTO);
+        return AppearanceMode::AUTO;
+    }
+
+    return static_cast<AppearanceMode>(nMode);
 }
 
-void MiscSettings::SetAppColorMode(int nMode)
+void MiscSettings::SetAppColorMode(AppearanceMode eMode)
 {
     // Partial: tdf#156855 update native and LibreOffice dark mode states
     // Updating the dark mode state of everything all at once does not
@@ -2348,7 +2359,8 @@ void MiscSettings::SetAppColorMode(int nMode)
 
     // 1. Save the new mode.
     std::shared_ptr<comphelper::ConfigurationChanges> 
batch(comphelper::ConfigurationChanges::create());
-    officecfg::Office::Common::Appearance::ApplicationAppearance::set(nMode, 
batch);
+    
officecfg::Office::Common::Appearance::ApplicationAppearance::set(static_cast<int>(eMode),
+                                                                      batch);
     batch->commit();
 
     // 2. Force the native windows to update their dark mode state so
diff --git a/vcl/unx/gtk3/gtkframe.cxx b/vcl/unx/gtk3/gtkframe.cxx
index b4636ed28fe5..3c96f586f57f 100644
--- a/vcl/unx/gtk3/gtkframe.cxx
+++ b/vcl/unx/gtk3/gtkframe.cxx
@@ -1370,7 +1370,7 @@ void GtkSalFrame::SetColorScheme(GVariant* variant)
     switch (MiscSettings::GetAppColorMode())
     {
         default:
-        case 0: // Auto
+        case AppearanceMode::AUTO:
         {
             if (variant)
             {
@@ -1382,10 +1382,10 @@ void GtkSalFrame::SetColorScheme(GVariant* variant)
                 color_scheme = DEFAULT;
             break;
         }
-        case 1: // Light
+        case AppearanceMode::LIGHT:
             color_scheme = PREFER_LIGHT;
             break;
-        case 2: // Dark
+        case AppearanceMode::DARK:
             color_scheme = PREFER_DARK;
             break;
     }
diff --git a/vcl/win/gdi/salnativewidgets-luna.cxx 
b/vcl/win/gdi/salnativewidgets-luna.cxx
index 878a28003714..7c442ab8cda3 100644
--- a/vcl/win/gdi/salnativewidgets-luna.cxx
+++ b/vcl/win/gdi/salnativewidgets-luna.cxx
@@ -410,7 +410,7 @@ bool UseDarkMode()
     bool bRet(false);
     switch (MiscSettings::GetDarkMode())
     {
-        case 0: // auto
+        case AppearanceMode::AUTO:
         default:
         {
             HINSTANCE hUxthemeLib = LoadLibraryExW(L"uxtheme.dll", nullptr, 
LOAD_LIBRARY_SEARCH_SYSTEM32);
@@ -424,10 +424,10 @@ bool UseDarkMode()
             FreeLibrary(hUxthemeLib);
             break;
         }
-        case 1: // light
+        case AppearanceMode::LIGHT:
             bRet = false;
             break;
-        case 2: // dark
+        case AppearanceMode::DARK:
             bRet = true;
             break;
     }
diff --git a/vcl/win/window/salframe.cxx b/vcl/win/window/salframe.cxx
index c0db43ff227e..54e975e0e6a8 100644
--- a/vcl/win/window/salframe.cxx
+++ b/vcl/win/window/salframe.cxx
@@ -270,13 +270,13 @@ static void UpdateDarkMode(HWND hWnd)
     {
         switch (MiscSettings::GetDarkMode())
         {
-            case 0:
+            case AppearanceMode::AUTO:
                 SetPreferredAppMode(AllowDark);
                 break;
-            case 1:
+            case AppearanceMode::LIGHT:
                 SetPreferredAppMode(ForceLight);
                 break;
-            case 2:
+            case AppearanceMode::DARK:
                 SetPreferredAppMode(ForceDark);
                 break;
         }

Reply via email to