Skip to content

Commit 67be0e9

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

File tree

1 file changed

+22
-36
lines changed

1 file changed

+22
-36
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

0 commit comments

Comments
 (0)