Skip to content

Commit

Permalink
UI/Qt: Set dark theme as reported by Qt
Browse files Browse the repository at this point in the history
Previously, we did a bit of guessing.
That approach was hacky and less reliable.
  • Loading branch information
shlyakpavel committed Nov 23, 2024
1 parent e565e3c commit f29b91b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
12 changes: 9 additions & 3 deletions UI/Qt/InspectorWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@
#include <QCloseEvent>
#include <QGuiApplication>
#include <QMenu>
#include <QStyleHints>
#include <QVBoxLayout>
#include <QWindow>

namespace Ladybird {

extern bool is_using_dark_system_theme(QWidget&);

InspectorWidget::InspectorWidget(QWidget* tab, WebContentView& content_view)
: QWidget(tab, Qt::Window)
{
m_inspector_view = new WebContentView(this);

if (is_using_dark_system_theme(*this))
auto color_scheme = QGuiApplication::styleHints()->colorScheme();
switch (color_scheme) {
case Qt::ColorScheme::Dark:
m_inspector_view->update_palette(WebContentView::PaletteMode::Dark);
break;
case Qt::ColorScheme::Light:
case Qt::ColorScheme::Unknown:
m_inspector_view->update_palette(WebContentView::PaletteMode::Default);
}

m_inspector_client = make<WebView::InspectorClient>(content_view, *m_inspector_view);

Expand Down
5 changes: 2 additions & 3 deletions UI/Qt/WebContentView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@
#include <QPainter>
#include <QPalette>
#include <QScrollBar>
#include <QStyleHints>
#include <QTextEdit>
#include <QTimer>
#include <QToolTip>

namespace Ladybird {

bool is_using_dark_system_theme(QWidget&);

WebContentView::WebContentView(QWidget* window, RefPtr<WebView::WebContentClient> parent_client, size_t page_index)
: QWidget(window)
{
Expand Down Expand Up @@ -584,7 +583,7 @@ static Core::AnonymousBuffer make_system_theme_from_qt_palette(QWidget& widget,
translate(Gfx::ColorRole::Selection, QPalette::ColorRole::Highlight);
translate(Gfx::ColorRole::SelectionText, QPalette::ColorRole::HighlightedText);

palette.set_flag(Gfx::FlagRole::IsDark, is_using_dark_system_theme(widget));
palette.set_flag(Gfx::FlagRole::IsDark, QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark);

return theme;
}
Expand Down
19 changes: 0 additions & 19 deletions UI/Qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,6 @@
# include <LibWebView/MachPortServer.h>
#endif

namespace Ladybird {

// FIXME: Find a place to put this declaration (and other helper functions).
bool is_using_dark_system_theme(QWidget&);
bool is_using_dark_system_theme(QWidget& widget)
{
// FIXME: Qt does not provide any method to query if the system is using a dark theme. We will have to implement
// platform-specific methods if we wish to have better detection. For now, this inspects if Qt is using a
// dark color for widget backgrounds using Rec. 709 luma coefficients.
// https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients

auto color = widget.palette().color(widget.backgroundRole());
auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF();

return luma <= 0.5f;
}

}

static ErrorOr<void> handle_attached_debugger()
{
#ifdef AK_OS_LINUX
Expand Down

0 comments on commit f29b91b

Please sign in to comment.