Qt: Add a theme changed event and use it for hotkey rows

Fixes black text when window inactive on MacOS.
pull/3575/head
Stenzek 2 months ago
parent 05af0c1288
commit ea3c8a3654
No known key found for this signature in database

@ -4,6 +4,7 @@
#include "hotkeysettingswidget.h" #include "hotkeysettingswidget.h"
#include "controllersettingswindow.h" #include "controllersettingswindow.h"
#include "inputbindingwidgets.h" #include "inputbindingwidgets.h"
#include "mainwindow.h"
#include "qtutils.h" #include "qtutils.h"
#include "settingswindow.h" #include "settingswindow.h"
#include "settingwidgetbinder.h" #include "settingwidgetbinder.h"
@ -23,6 +24,7 @@ HotkeySettingsWidget::HotkeySettingsWidget(QWidget* parent, ControllerSettingsWi
: QWidget(parent), m_dialog(dialog) : QWidget(parent), m_dialog(dialog)
{ {
createUi(); createUi();
connect(g_main_window, &MainWindow::themeChanged, this, &HotkeySettingsWidget::onThemeChanged);
} }
HotkeySettingsWidget::~HotkeySettingsWidget() = default; HotkeySettingsWidget::~HotkeySettingsWidget() = default;
@ -51,6 +53,24 @@ void HotkeySettingsWidget::Container::repositionSearchBox()
m_search->setGeometry(x, box_padding, box_width, h); m_search->setGeometry(x, box_padding, box_width, h);
} }
QPalette HotkeySettingsWidget::getLabelPalette(bool is_dark_theme) const
{
QPalette pal = qApp->palette("QLabel");
const QColor label_default_color = pal.color(QPalette::Text);
const QColor label_color = is_dark_theme ? label_default_color.darker(120) : label_default_color.lighter();
pal.setColor(QPalette::Text, label_color);
return pal;
}
QPalette HotkeySettingsWidget::getRowPalette() const
{
// This is super jank. The native theme on MacOS does not set AlternateBase like the Windows/Fusion themes do, but
// instead overrides it in QAbstractItemView.
QPalette pal = qApp->palette("QWidget");
pal.setColor(QPalette::AlternateBase, qApp->palette("QAbstractItemView").color(QPalette::AlternateBase));
return pal;
}
void HotkeySettingsWidget::createUi() void HotkeySettingsWidget::createUi()
{ {
QGridLayout* layout = new QGridLayout(this); QGridLayout* layout = new QGridLayout(this);
@ -81,6 +101,8 @@ void HotkeySettingsWidget::createButtons()
static constexpr int LR_MARGIN = 8; static constexpr int LR_MARGIN = 8;
static constexpr int TB_MARGIN = 4; static constexpr int TB_MARGIN = 4;
const QPalette label_palette = getLabelPalette(QtHost::IsDarkApplicationTheme());
const QPalette row_palette = getRowPalette();
const std::vector<const HotkeyInfo*> hotkeys(InputManager::GetHotkeyList()); const std::vector<const HotkeyInfo*> hotkeys(InputManager::GetHotkeyList());
for (const HotkeyInfo* hotkey : hotkeys) for (const HotkeyInfo* hotkey : hotkeys)
{ {
@ -100,11 +122,6 @@ void HotkeySettingsWidget::createButtons()
label_font.setPixelSize(19); label_font.setPixelSize(19);
label_font.setBold(true); label_font.setBold(true);
cw.label->setFont(label_font); cw.label->setFont(label_font);
QPalette label_palette = cw.label->palette();
const QColor label_default_color = label_palette.color(QPalette::Text);
const QColor label_color =
QtHost::IsDarkApplicationTheme() ? label_default_color.darker(120) : label_default_color.lighter();
label_palette.setColor(QPalette::Text, label_color);
cw.label->setPalette(label_palette); cw.label->setPalette(label_palette);
row_layout->addWidget(cw.label); row_layout->addWidget(cw.label);
@ -124,6 +141,7 @@ void HotkeySettingsWidget::createButtons()
QWidget* const row = new QWidget(m_container); QWidget* const row = new QWidget(m_container);
row->setAutoFillBackground(true); row->setAutoFillBackground(true);
row->setBackgroundRole((((iter->layout->count()) % 2) == 0) ? QPalette::Base : QPalette::AlternateBase); row->setBackgroundRole((((iter->layout->count()) % 2) == 0) ? QPalette::Base : QPalette::AlternateBase);
row->setPalette(row_palette);
iter->layout->addWidget(row); iter->layout->addWidget(row);
QHBoxLayout* row_layout = new QHBoxLayout(row); QHBoxLayout* row_layout = new QHBoxLayout(row);
@ -163,3 +181,21 @@ void HotkeySettingsWidget::setFilter(const QString& filter)
cw.heading->setVisible(visible_row_count > 0); cw.heading->setVisible(visible_row_count > 0);
} }
} }
void HotkeySettingsWidget::onThemeChanged(bool is_dark_theme)
{
const QPalette label_palette = getLabelPalette(is_dark_theme);
const QPalette row_palette = getRowPalette();
for (const CategoryWidgets& cw : m_categories)
{
cw.label->setPalette(label_palette);
cw.line->setPalette(label_palette);
const int count = cw.layout->count();
for (int i = 0; i < count; i++)
{
if (QWidget* const row = qobject_cast<QWidget*>(cw.layout->itemAt(i)->widget()))
row->setPalette(row_palette);
}
}
}

@ -49,11 +49,16 @@ private:
QLineEdit* m_search; QLineEdit* m_search;
}; };
QPalette getLabelPalette(bool is_dark_theme) const;
QPalette getRowPalette() const;
void createUi(); void createUi();
void createButtons(); void createButtons();
void setFilter(const QString& filter); void setFilter(const QString& filter);
void onThemeChanged(bool is_dark_theme);
ControllerSettingsWindow* m_dialog; ControllerSettingsWindow* m_dialog;
QScrollArea* m_scroll_area = nullptr; QScrollArea* m_scroll_area = nullptr;
Container* m_container = nullptr; Container* m_container = nullptr;

@ -2828,6 +2828,7 @@ void MainWindow::changeEvent(QEvent* event)
{ {
QtHost::SetIconThemeFromStyle(); QtHost::SetIconThemeFromStyle();
reloadThemeSpecificImages(); reloadThemeSpecificImages();
emit themeChanged(QtHost::IsDarkApplicationTheme());
} }
QMainWindow::changeEvent(event); QMainWindow::changeEvent(event);

@ -140,6 +140,9 @@ public:
void onRAIntegrationMenuChanged(); void onRAIntegrationMenuChanged();
Q_SIGNALS:
void themeChanged(bool is_dark_theme);
protected: protected:
void closeEvent(QCloseEvent* event) override; void closeEvent(QCloseEvent* event) override;
void changeEvent(QEvent* event) override; void changeEvent(QEvent* event) override;

@ -406,15 +406,6 @@ void QtHost::SetStyleFromSettings()
qApp->setPalette(s_state.unthemed_palette); qApp->setPalette(s_state.unthemed_palette);
qApp->setStyleSheet(QString()); qApp->setStyleSheet(QString());
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Unknown); qApp->styleHints()->setColorScheme(Qt::ColorScheme::Unknown);
#ifdef __APPLE__
// This is super jank. The native theme on MacOS does not set AlternateBase like the Windows/Fusion themes do, but
// instead overrides it in QAbstractItemView. Since this is the only place that AlteranteBase is used, just copy it
// across. Why do we need this? Because we're using AlternateBase in hotkey bindings dialog to draw rows manually.
s_state.unthemed_palette.setColor(QPalette::AlternateBase,
qApp->palette("QAbstractItemView").color(QPalette::AlternateBase));
qApp->setPalette(s_state.unthemed_palette);
#endif
} }
} }

Loading…
Cancel
Save