Skip to content

Commit b9910e7

Browse files
committed
Support dark/light mode selection with env var TRIK_STUDIO_THEME
1 parent 61920f3 commit b9910e7

File tree

3 files changed

+26
-40
lines changed

3 files changed

+26
-40
lines changed

qrgui/mainWindow/mainWindow.cpp

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -713,45 +713,31 @@ bool MainWindow::windowsIsInDarkTheme()
713713
}
714714

715715
void MainWindow::initDarkPalette() {
716-
if (QSysInfo::productType() != "windows") {
717-
return;
718-
}
719-
720-
const auto &platform = QString::fromUtf8(qgetenv("TRIK_STUDIO_THEME"));
721-
constexpr auto lightThemeMessage = "A LIGHT THEME HAS BEEN SELECTED";
722-
QLOG_INFO() << "TRIK_STUDIO_THEME IS" << platform;
723-
724-
if (platform == "light") {
725-
QLOG_INFO() << lightThemeMessage;
726-
return;
727-
}
728-
729-
if (platform != "auto" && platform != "dark" && !platform.isEmpty()) {
730-
QLOG_INFO() << "INVALID VALUE" << platform << "FOR VARIABLE TRIK_STUDIO_THEME";
731-
QLOG_INFO() << "THE THEME WILL BE SELECTED AUTOMATICALLY";
732-
}
733-
734-
auto darkThemeAvailiable = windowsDarkThemeAvailiable();
735-
QLOG_INFO() << "windowsDarkThemeAvailiable RETURNS" << darkThemeAvailiable;
736-
737-
if (!darkThemeAvailiable) {
738-
QLOG_INFO() << lightThemeMessage;
739-
return;
716+
constexpr auto logPrefix = "Initializing color theme";
717+
const auto &explicitTheme = QProcessEnvironment::systemEnvironment().value("TRIK_STUDIO_THEME", "auto");
718+
QLOG_INFO() << logPrefix << "with" << explicitTheme;
719+
bool useDarkTheme;
720+
721+
if (explicitTheme == "light") {
722+
useDarkTheme = false;
723+
} else if (explicitTheme == "auto") {
724+
// It is hard to guess, but ...
725+
useDarkTheme = PlatformInfo::osType() == "windows" && windowsIsInDarkTheme();
726+
} else if (explicitTheme != "dark") {
727+
QLOG_INFO() << logPrefix << ":" << explicitTheme << "is not a correct option";
728+
useDarkTheme = false;
729+
} else {
730+
useDarkTheme = true;
740731
}
741732

742-
auto isInDarkTheme = windowsIsInDarkTheme();
743-
QLOG_INFO() << "windowsIsInDarkTheme RETURNS" << isInDarkTheme;
744-
745-
if (!isInDarkTheme) {
746-
QLOG_INFO() << lightThemeMessage;
747-
return;
733+
if (useDarkTheme) {
734+
QLOG_INFO() << logPrefix <<": enforcing dark mode";
735+
QApplication::setPalette(BrandManager::styles()->loadPalette(
736+
QCoreApplication::applicationDirPath() +
737+
"/palettes/darkWindowsPalette.ini")
738+
);
748739
}
749-
750-
QLOG_INFO() << "A DARK THEME HAS BEEN SELECTED";
751-
QApplication::setPalette(BrandManager::styles()->loadPalette(
752-
QCoreApplication::applicationDirPath() +
753-
"/palettes/darkWindowsPalette.ini")
754-
);
740+
// Light is the default one, just return
755741
}
756742

757743
// clazy:excludeall=function-args-by-value

qrtranslations/fr/qrgui_mainWindow_fr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ WARNING: The settings will be restored after application restart</source>
729729
<translation type="unfinished"></translation>
730730
</message>
731731
<message>
732-
<location line="+783"/>
732+
<location line="+769"/>
733733
<location line="+11"/>
734734
<source>Error</source>
735735
<translation>Erreur</translation>

qrtranslations/ru/qrgui_mainWindow_ru.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@
735735
<translation type="vanished">О QReal</translation>
736736
</message>
737737
<message>
738-
<location filename="../../qrgui/mainWindow/mainWindow.cpp" line="+982"/>
738+
<location filename="../../qrgui/mainWindow/mainWindow.cpp" line="+968"/>
739739
<location line="+11"/>
740740
<source>Error</source>
741741
<translation>Ошибка</translation>
@@ -788,7 +788,7 @@
788788
<translation>Создать диаграмму</translation>
789789
</message>
790790
<message>
791-
<location line="-1157"/>
791+
<location line="-1143"/>
792792
<source>Restore default settings</source>
793793
<translation>Восстановить настройки по-умолчанию</translation>
794794
</message>
@@ -807,7 +807,7 @@ WARNING: The settings will be restored after application restart</source>
807807
<translation>Не удалось сохранить файл, попробуйте сохранить его в другое место</translation>
808808
</message>
809809
<message>
810-
<location line="+693"/>
810+
<location line="+679"/>
811811
<source>Undo</source>
812812
<translation>Отменить</translation>
813813
</message>

0 commit comments

Comments
 (0)