Skip to content

Commit

Permalink
Qt: Use "fusion" style when dark mode is activated on Windows 10/11
Browse files Browse the repository at this point in the history
  • Loading branch information
Cacodemon345 authored Jan 12, 2024
1 parent 6e546bb commit 1e02589
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/qt/qt_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,45 @@ main_thread_fn()

static std::thread *main_thread;

#ifdef Q_OS_WINDOWS
static bool is_light_theme() {
// based on https://stackoverflow.com/questions/51334674/how-to-detect-windows-10-light-dark-mode-in-win32-application

// The value is expected to be a REG_DWORD, which is a signed 32-bit little-endian
auto buffer = std::vector<char>(4);
auto cbData = static_cast<DWORD>(buffer.size() * sizeof(char));
auto res = RegGetValueW(
HKEY_CURRENT_USER,
L"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
L"AppsUseLightTheme",
RRF_RT_REG_DWORD, // expected value type
nullptr,
buffer.data(),
&cbData);

if (res != ERROR_SUCCESS) {
return 1;
}

// convert bytes written to our buffer to an int, assuming little-endian
auto i = int(buffer[3] << 24 |
buffer[2] << 16 |
buffer[1] << 8 |
buffer[0]);

return i == 1;
}
#endif

int
main(int argc, char *argv[])
{
#ifdef Q_OS_WINDOWS
qputenv("QT_QPA_PLATFORM", "windows:darkmode=2");
if (!is_light_theme()) {
qputenv("QT_STYLE_OVERRIDE", "fusion");
}
#endif
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QApplication::setAttribute(Qt::AA_DisableHighDpiScaling, false);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
Expand Down

0 comments on commit 1e02589

Please sign in to comment.