I've got it mostly working now. I couldn't figure out how to persist the option (In Tools->Preferences->Colors), so it resets to Same As System every startup. The patch is attached.
On Tue, Apr 13, 2021 at 12:51 PM Narahari Rao <narahari...@gmail.com> wrote: > Looks like bool ColorCache::isDarkmode() is setting the document colors on > startup, using QtPalette to get text/bg color and comparing them to guess > whether the user is in a dark theme or not. However, changing what this > function returns during runtime (via a variable) does not change the > document colors, so it looks like it needs a refresh of some sort. > > On Tue, Apr 13, 2021 at 8:33 AM Jean-Marc Lasgouttes <lasgout...@lyx.org> > wrote: > >> Le 13/04/2021 à 16:55, Richard Kimberly Heck a écrit : >> >> Not in the GUI. You can use the -style option when starting LyX. >> > >> > Does it seem worth having such an option? Windows users especially >> might >> > struggle with the command line. I'd be happy to write the code, if so, >> > if you can just point me at the dark-mode detection routine. >> >> Yes, I think a choice light/dark/system-default would be nice. >> >> JMarc >> -- >> lyx-devel mailing list >> lyx-devel@lists.lyx.org >> http://lists.lyx.org/mailman/listinfo/lyx-devel >> >
diff --git a/src/frontends/qt/ColorCache.cpp b/src/frontends/qt/ColorCache.cpp index 822f44a9d1..71e69b73dd 100644 --- a/src/frontends/qt/ColorCache.cpp +++ b/src/frontends/qt/ColorCache.cpp @@ -61,6 +61,9 @@ QPalette::ColorRole role(ColorCode col) void ColorCache::init() { + + darkMode = isSystemDarkMode(); + for (int col = 0; col <= Color_ignore; ++col) { lcolors_[col] = QColor(lcolor.getX11HexName(ColorCode(col), isDarkMode()).c_str()); } @@ -119,7 +122,7 @@ bool ColorCache::isSystem(ColorCode const color) const } -bool ColorCache::isDarkMode() const +bool ColorCache::isSystemDarkMode() const { QPalette palette = QPalette(); QColor text_color = palette.color(QPalette::Active, QPalette::WindowText); @@ -128,6 +131,21 @@ bool ColorCache::isDarkMode() const return (text_color.black() < bg_color.black()); } +bool ColorCache::isDarkMode() const +{ + return darkMode; +} + +void ColorCache::setDarkMode(bool newMode) +{ + if(newMode == darkMode) return; + + darkMode = newMode; + + for (int col = 0; col <= Color_ignore; ++col) { + lcolors_[col] = QColor(lcolor.getX11HexName(ColorCode(col), isDarkMode()).c_str()); + } +} QColor const rgb2qcolor(RGBColor const & rgb) { diff --git a/src/frontends/qt/ColorCache.h b/src/frontends/qt/ColorCache.h index 09a995d861..5fc471ca06 100644 --- a/src/frontends/qt/ColorCache.h +++ b/src/frontends/qt/ColorCache.h @@ -39,9 +39,15 @@ public: /// is this color replaced when LyXRC::use_system_color is true? bool isSystem(ColorCode color) const; - /// guess whether we are in dark mode + /// guess whether the system is in dark mode + bool isSystemDarkMode() const; + + /// are we in dark mode? bool isDarkMode() const; + // set dark mode + void setDarkMode(bool newMode); + /// change the undelying palette void setPalette(QPalette const pal) { pal_ = pal; clear(); } @@ -57,6 +63,8 @@ private: bool initialized_; /// QPalette pal_; + /// + bool darkMode; }; /// diff --git a/src/frontends/qt/GuiPrefs.cpp b/src/frontends/qt/GuiPrefs.cpp index 7d880856cf..d31a2a1564 100644 --- a/src/frontends/qt/GuiPrefs.cpp +++ b/src/frontends/qt/GuiPrefs.cpp @@ -1117,6 +1117,8 @@ PrefColors::PrefColors(GuiPreferences * form) } curcolors_.resize(lcolors_.size()); newcolors_.resize(lcolors_.size()); + + // End initialization connect(colorChangePB, SIGNAL(clicked()), @@ -1133,6 +1135,10 @@ PrefColors::PrefColors(GuiPreferences * form) this, SIGNAL(changed())); connect(syscolorsCB, SIGNAL(toggled(bool)), this, SLOT(changeSysColor())); + connect(darkModeSelect, SIGNAL(activated(int)), + this, SIGNAL(changed())); + connect(darkModeSelect, SIGNAL(activated(int)), + this, SLOT(setTheme())); } @@ -1147,6 +1153,47 @@ void PrefColors::applyRC(LyXRC & rc) const if (oldrc.use_system_colors != rc.use_system_colors) guiApp->colorCache().clear(); + + switch (darkModeSelect->currentIndex()) { + case 0: // Same As System + guiApp->colorCache().setDarkMode(guiApp->colorCache().isSystemDarkMode()); + break; + case 1: // Dark Mode + guiApp->colorCache().setDarkMode(true); + break; + case 2: // Light Mode + guiApp->colorCache().setDarkMode(false); + break; + } +} + +void PrefColors::setTheme() +{ + bool darkMode; + switch (darkModeSelect->currentIndex()) { + case 0: // Same As System + darkMode = guiApp->colorCache().isSystemDarkMode(); + break; + case 1: // Dark Mode + darkMode = true; + break; + case 2: // Light Mode + darkMode = false; + break; + } + + if(darkMode == guiApp->colorCache().isDarkMode()) return; + + ColorSet const defaultcolor; + for (size_type i = 0; i < lcolors_.size(); ++i) { + QColor color = defaultcolor.getX11HexName(lcolors_[size_t(i)], + darkMode).c_str(); + QPixmap coloritem(32, 32); + coloritem.fill(color); + lyxObjectsLW->item(int(i))->setIcon(QIcon(coloritem)); + newcolors_[i] = curcolors_[i] = color.name(); + } + } diff --git a/src/frontends/qt/GuiPrefs.h b/src/frontends/qt/GuiPrefs.h index 5fac6bc5e9..268a258c2a 100644 --- a/src/frontends/qt/GuiPrefs.h +++ b/src/frontends/qt/GuiPrefs.h @@ -254,6 +254,7 @@ private Q_SLOTS: void changeColor(); void resetColor(); void resetAllColor(); + void setTheme(); void changeSysColor(); void changeLyxObjectsSelection(); bool setColor(int const row, QColor const & new_color, diff --git a/src/frontends/qt/ui/PrefColorsUi.ui b/src/frontends/qt/ui/PrefColorsUi.ui index 10aa667a4b..093252c11d 100644 --- a/src/frontends/qt/ui/PrefColorsUi.ui +++ b/src/frontends/qt/ui/PrefColorsUi.ui @@ -6,7 +6,7 @@ <rect> <x>0</x> <y>0</y> - <width>397</width> + <width>400</width> <height>254</height> </rect> </property> @@ -20,18 +20,41 @@ <string/> </property> <layout class="QGridLayout" name="gridLayout_2"> - <item row="1" column="1"> - <spacer name="horizontalSpacer"> - <property name="orientation"> - <enum>Qt::Horizontal</enum> + <item row="2" column="1"> + <widget class="QComboBox" name="darkModeSelect"> + <item> + <property name="text"> + <string>Same As System</string> + </property> + </item> + <item> + <property name="text"> + <string>Dark Mode</string> + </property> + </item> + <item> + <property name="text"> + <string>Light Mode</string> + </property> + </item> + </widget> + </item> + <item row="1" column="0"> + <widget class="QCheckBox" name="syscolorsCB"> + <property name="toolTip"> + <string>Use the color scheme of your Operating System/Desktop Environment</string> </property> - <property name="sizeHint" stdset="0"> - <size> - <width>40</width> - <height>20</height> - </size> + <property name="text"> + <string>&Use system colors</string> </property> - </spacer> + </widget> + </item> + <item row="2" column="0"> + <widget class="QLabel" name="label"> + <property name="text"> + <string>Light/Dark Theme</string> + </property> + </widget> </item> <item row="0" column="0" colspan="2"> <layout class="QGridLayout" name="gridLayout"> @@ -99,15 +122,18 @@ </item> </layout> </item> - <item row="1" column="0"> - <widget class="QCheckBox" name="syscolorsCB"> - <property name="toolTip"> - <string>Use the color scheme of your Operating System/Desktop Environment</string> + <item row="1" column="1"> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> </property> - <property name="text"> - <string>&Use system colors</string> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> </property> - </widget> + </spacer> </item> </layout> </widget>
-- lyx-devel mailing list lyx-devel@lists.lyx.org http://lists.lyx.org/mailman/listinfo/lyx-devel