Skip to content

Commit

Permalink
Ladybird/Qt: Apply selected color scheme for new tabs
Browse files Browse the repository at this point in the history
Previously, new tabs always had the `auto` color scheme, regardless of
what the user has selected before.
Replace the 3 individual slots with a `set_preferred_color_scheme`
method.
  • Loading branch information
simonkrauter authored and tcl3 committed Jul 6, 2024
1 parent 172e080 commit 82915e1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
43 changes: 18 additions & 25 deletions Ladybird/Qt/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,24 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, WebView::Cook
auto_color_scheme->setCheckable(true);
color_scheme_group->addAction(auto_color_scheme);
color_scheme_menu->addAction(auto_color_scheme);
QObject::connect(auto_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_auto_color_scheme);

QObject::connect(auto_color_scheme, &QAction::triggered, this, [this] {
set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Auto);
});
auto* light_color_scheme = new QAction("&Light", this);
light_color_scheme->setCheckable(true);
color_scheme_group->addAction(light_color_scheme);
color_scheme_menu->addAction(light_color_scheme);
QObject::connect(light_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_light_color_scheme);
QObject::connect(light_color_scheme, &QAction::triggered, this, [this] {
set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Light);
});

auto* dark_color_scheme = new QAction("&Dark", this);
dark_color_scheme->setCheckable(true);
color_scheme_group->addAction(dark_color_scheme);
color_scheme_menu->addAction(dark_color_scheme);
QObject::connect(dark_color_scheme, &QAction::triggered, this, &BrowserWindow::enable_dark_color_scheme);
QObject::connect(dark_color_scheme, &QAction::triggered, this, [this] {
set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Dark);
});

auto_color_scheme->setChecked(true);

Expand Down Expand Up @@ -808,6 +813,7 @@ void BrowserWindow::initialize_tab(Tab* tab)
tab->set_user_agent_string(user_agent_string());
tab->set_navigator_compatibility_mode(navigator_compatibility_mode());
tab->set_enable_do_not_track(Settings::the()->enable_do_not_track());
tab->view().set_preferred_color_scheme(m_preferred_color_scheme);
}

void BrowserWindow::activate_tab(int index)
Expand Down Expand Up @@ -982,27 +988,6 @@ void BrowserWindow::open_previous_tab()
m_tabs_container->setCurrentIndex(next_index);
}

void BrowserWindow::enable_auto_color_scheme()
{
for_each_tab([](auto& tab) {
tab.view().set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Auto);
});
}

void BrowserWindow::enable_light_color_scheme()
{
for_each_tab([](auto& tab) {
tab.view().set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Light);
});
}

void BrowserWindow::enable_dark_color_scheme()
{
for_each_tab([](auto& tab) {
tab.view().set_preferred_color_scheme(Web::CSS::PreferredColorScheme::Dark);
});
}

void BrowserWindow::enable_auto_contrast()
{
for_each_tab([](auto& tab) {
Expand Down Expand Up @@ -1127,6 +1112,14 @@ void BrowserWindow::set_window_rect(Optional<Web::DevicePixels> x, Optional<Web:
setGeometry(x.value().value(), y.value().value(), width.value().value(), height.value().value());
}

void BrowserWindow::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
{
m_preferred_color_scheme = color_scheme;
for_each_tab([color_scheme](auto& tab) {
tab.view().set_preferred_color_scheme(color_scheme);
});
}

void BrowserWindow::copy_selected_text()
{
if (!m_current_tab)
Expand Down
6 changes: 3 additions & 3 deletions Ladybird/Qt/BrowserWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ public slots:
void open_next_tab();
void open_previous_tab();
void open_file();
void enable_auto_color_scheme();
void enable_light_color_scheme();
void enable_dark_color_scheme();
void enable_auto_contrast();
void enable_less_contrast();
void enable_more_contrast();
Expand Down Expand Up @@ -179,6 +176,9 @@ public slots:
QScreen* m_current_screen;
double m_device_pixel_ratio { 0 };

Web::CSS::PreferredColorScheme m_preferred_color_scheme;
void set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme);

QTabWidget* m_tabs_container { nullptr };
Tab* m_current_tab { nullptr };
QMenu* m_zoom_menu { nullptr };
Expand Down

0 comments on commit 82915e1

Please sign in to comment.