Skip to content

Commit

Permalink
fix(#32): default dark theme on windows matching desktop
Browse files Browse the repository at this point in the history
Qt doesn't select right theme that match desktop, and always select
native (light) on Windows. This add a new check to match dekstop
theme, on Windows only. Note that dark theme isn't native dark, but
since Qt doesn't offer one it should be ok.
  • Loading branch information
DorianBDev committed Aug 20, 2024
1 parent f7c66c5 commit 73c572b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
35 changes: 32 additions & 3 deletions src/GUI/Preferences/PreferencesHandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,34 @@

#include "PreferencesHandler.h"

#include <QStyleHints>
#include <fstream>

#define RECENT_PROJECTS_LIST_FILE "recent.config"

namespace degate
{
PreferencesHandler::PreferencesHandler() : settings(QString::fromStdString(DEGATE_IN_CONFIGURATION(DEGATE_CONFIGURATION_FILE_NAME)), QSettings::IniFormat)
PreferencesHandler::PreferencesHandler()
: settings(QString::fromStdString(DEGATE_IN_CONFIGURATION(DEGATE_CONFIGURATION_FILE_NAME)),
QSettings::IniFormat)
{
///////////
// Appearance
///////////

// Theme
QString theme = settings.value("theme", "native").toString();
auto default_theme = "native";
#ifdef SYS_WINDOWS
// Fix for Windows where Qt doesn't handle automatic dark theme regarding desktop theme state
// It will always stay native light
// Therefore if we detect manually that the desktop is in dark theme, we force default theme to dark
// NOTE: the dark theme isn't native dark, but Qt doesn't seem to offer one anyway
if (is_desktop_in_dark_mode())
{
default_theme = "dark";
}
#endif
QString theme = settings.value("theme", default_theme).toString();
preferences.theme = string_to_theme(theme.toStdString());

// Icon Theme
Expand Down Expand Up @@ -220,6 +234,21 @@ namespace degate
insert_recent_project(project->get_name(), project->get_project_directory());
}

bool PreferencesHandler::is_desktop_in_dark_mode()
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
const auto scheme = QGuiApplication::styleHints()->colorScheme();
return scheme == Qt::ColorScheme::Dark;
#else
// This is a workaround, not perfect but should work
const QPalette default_palette;
const auto text = default_palette.color(QPalette::WindowText);
const auto window = default_palette.color(QPalette::Window);
return text.lightness() > window.lightness();
#endif
}


void PreferencesHandler::remove_invalid_recent_projects()
{
std::vector<std::pair<std::string, std::string>> elements;
Expand Down Expand Up @@ -325,4 +354,4 @@ namespace degate

recent_projects.emplace_back(project_name, project_path);
}
}
} // namespace degate
15 changes: 9 additions & 6 deletions src/GUI/Preferences/PreferencesHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
#define __PREFERENCESHANDLER_H__

#include "Core/Primitive/SingletonBase.h"
#include "GUI/Preferences/ThemeManager.h"
#include "Core/Project/Project.h"
#include "Core/XML/XMLExporter.h"
#include "Core/XML/XMLImporter.h"
#include "Core/Project/Project.h"
#include "GUI/Preferences/ThemeManager.h"

#include <QObject>

Expand Down Expand Up @@ -75,7 +75,6 @@ namespace degate
unsigned int cache_size;
unsigned int image_importer_cache_size;
unsigned int max_concurrent_thread_count;

};

/**
Expand All @@ -89,7 +88,6 @@ namespace degate
Q_OBJECT

public:

/**
* Create the preferences handler.
*/
Expand Down Expand Up @@ -146,6 +144,11 @@ namespace degate
*/
void add_recent_project(const Project_shptr& project);

/**
* Returns true if the desktop theme is dark.
*/
bool is_desktop_in_dark_mode();

protected:
/**
* Remove invalid recent projects (that are not reachable).
Expand Down Expand Up @@ -194,11 +197,11 @@ namespace degate
std::shared_ptr<QTranslator> base_translator = nullptr;
std::vector<std::pair<std::string /* Project name */, std::string /* Project path */>> recent_projects;
};
}
} // namespace degate

/**
* Get the preferences handler instance.
*/
#define PREFERENCES_HANDLER PreferencesHandler::get_instance()

#endif
#endif

0 comments on commit 73c572b

Please sign in to comment.