Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#32): default dark theme on windows matching desktop #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading