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

Use Qt 6.5 styleHint colorScheme to detect dark theme #295

Merged
merged 1 commit into from
Mar 12, 2024
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
18 changes: 8 additions & 10 deletions src/controller/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <QPalette>
#include <QProcess>
#include <QSettings>
#include <QStyleHints>
#include <QTranslator>

inline CommandWithArguments::second_type parseArgumentJson(const QString& argumentStr)
Expand Down Expand Up @@ -74,19 +75,17 @@ Application::Application(int& argc, char** argv, const QString& name) :
#endif
}

#ifndef Q_OS_MAC
bool Application::isDarkTheme()
{
#ifdef Q_OS_WIN
QSettings settings(
"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize",
QSettings::NativeFormat);
return settings.value("AppsUseLightTheme", 1).toInt() == 0;
#else
// There is currently no straightforward way to detect dark mode in Linux, but this works for supported OS-s.
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
return styleHints()->colorScheme() == Qt::ColorScheme::Dark;
#elif defined(Q_OS_UNIX)
// There is currently no straightforward way to detect dark mode in Linux, but this works for
// supported OS-s.
static const bool isDarkTheme = [] {
QProcess p;
p.start(QStringLiteral("gsettings"), {"get", "org.gnome.desktop.interface", "color-scheme"});
p.start(QStringLiteral("gsettings"),
{"get", "org.gnome.desktop.interface", "color-scheme"});
if (p.waitForFinished()) {
return p.readAllStandardOutput().contains("dark");
}
Expand All @@ -97,7 +96,6 @@ bool Application::isDarkTheme()
return isDarkTheme;
#endif
}
#endif

void Application::loadTranslations(const QString& lang)
{
Expand Down
7 changes: 0 additions & 7 deletions src/controller/application.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@
#import <AppKit/AppKit.h>
#import <Cocoa/Cocoa.h>

bool Application::isDarkTheme()
{
auto appearance = [NSApp.effectiveAppearance bestMatchFromAppearancesWithNames:
@[ NSAppearanceNameAqua, NSAppearanceNameDarkAqua ]];
return [appearance isEqualToString:NSAppearanceNameDarkAqua];
}

void Application::showAbout()
{
NSLog(@"web-eid-app: starting app");
Expand Down
5 changes: 2 additions & 3 deletions src/controller/threads/commandhandlerconfirmthread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class CommandHandlerConfirmThread : public ControllerChildThread
public:
CommandHandlerConfirmThread(QObject* parent, CommandHandler& handler, WebEidUI* w,
const CardCertificateAndPinInfo& cardCertAndPin) :
ControllerChildThread(parent),
commandHandler(handler), cmdType(commandHandler.commandType()), window(w),
cardCertAndPinInfo(cardCertAndPin)
ControllerChildThread(parent), commandHandler(handler),
cmdType(commandHandler.commandType()), window(w), cardCertAndPinInfo(cardCertAndPin)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/controller/threads/commandhandlerrunthread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class CommandHandlerRunThread : public ControllerChildThread
public:
CommandHandlerRunThread(QObject* parent, CommandHandler& handler,
const std::vector<electronic_id::CardInfo::ptr>& cs) :
ControllerChildThread(parent),
commandHandler(handler), cmdType(commandHandler.commandType()), cards(cs)
ControllerChildThread(parent), commandHandler(handler),
cmdType(commandHandler.commandType()), cards(cs)
{
// Connect retry signal to retry signal to pass it up from the command handler.
connect(&commandHandler, &CommandHandler::retry, this, &ControllerChildThread::retry);
Expand Down
3 changes: 1 addition & 2 deletions src/ui/certificatewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ void CertificateWidget::paintEvent(QPaintEvent* /*event*/)

CertificateButton::CertificateButton(const CardCertificateAndPinInfo& cardCertPinInfo,
QWidget* parent) :
QAbstractButton(parent),
CertificateWidgetInfo(this)
QAbstractButton(parent), CertificateWidgetInfo(this)
{
setCheckable(true);
setAutoExclusive(true);
Expand Down
3 changes: 1 addition & 2 deletions src/ui/webeiddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,8 +653,7 @@ void WebEidDialog::setupOK(Func&& func, const std::function<QString()>& text, bo
connect(ui->okButton, &QPushButton::clicked, this, std::forward<Func>(func));
ui->okButton->show();
ui->okButton->setEnabled(enabled);
setTrText(
ui->okButton, text ? text : [] { return tr("Confirm"); });
setTrText(ui->okButton, text ? text : [] { return tr("Confirm"); });
ui->cancelButton->show();
ui->cancelButton->setEnabled(true);
ui->helpButton->hide();
Expand Down
Loading