Truly independent web browser
UI/Qt: Read the GNOME window button layout from the settings portal
Tim Ledbetter676e32d34554
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cppindex c9504aa0b8a..25636a38bd7 100644--- a/UI/Qt/BrowserWindow.cpp+++ b/UI/Qt/BrowserWindow.cpp@@ -1474,6 +1474,16 @@ void BrowserWindow::update_menu_bar_window_control_icons()m_menu_bar_close_window_button->setIcon(create_chrome_icon(ChromeIcon::WindowClose, Theme::chrome_palette(*this)));}+void BrowserWindow::window_control_buttons_changed()+{+ // A menu bar measures its corner widgets only when they are set, shown or hidden.+ if (m_menu_bar_window_controls) {+ for (auto corner : { Qt::TopLeftCorner, Qt::TopRightCorner })+ menuBar()->setCornerWidget(menuBar()->cornerWidget(corner), corner);+ }+ m_tabs_container->window_control_buttons_changed();+}+void BrowserWindow::update_window_decoration_state(){clear_resize_cursor();diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.hindex 1405012ef20..f430bfd0de7 100644--- a/UI/Qt/BrowserWindow.h+++ b/UI/Qt/BrowserWindow.h@@ -203,6 +203,7 @@ public:LadybirdButton& ladybird_button() const { return m_tabs_container->ladybird_button(); }static bool has_chrome_in_titlebar();+ void window_control_buttons_changed();void show_window_context_menu(QPoint global_position);diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cppindex 1e6e2ed906c..453720288b5 100644--- a/UI/Qt/TabBar.cpp+++ b/UI/Qt/TabBar.cpp@@ -332,6 +332,11 @@ static constexpr int vertical_tab_width(int available_width, TabLayout tab_layoureturn width - vertical_tabs_horizontal_margin_width(tab_layout);}+static int saved_vertical_tabs_expanded_width()+{+ return Application::settings().tab_settings().vertical_tabs_expanded_width.value_or(VERTICAL_TABS_DEFAULT_EXPANDED_WIDTH);+}+static char const* vertical_tabs_position_property_value(WebView::VerticalTabsPosition position){switch (position) {@@ -3395,7 +3400,7 @@ TabWidget::TabWidget(WebView::IsPrivate is_private, QWidget* parent)if (auto* top_level_window = window(); top_level_window != this)top_level_window->installEventFilter(this);- m_vertical_tabs_expanded_width = Application::settings().tab_settings().vertical_tabs_expanded_width.value_or(VERTICAL_TABS_DEFAULT_EXPANDED_WIDTH);+ m_vertical_tabs_expanded_width = saved_vertical_tabs_expanded_width();m_tab_bar = new TabBar(this);setAcceptDrops(true);@@ -3767,6 +3772,13 @@ void TabWidget::set_window_controls_visible(bool visible)rebuild_layout();}+void TabWidget::window_control_buttons_changed()+{+ // The open sidebar may have widened to fit the earlier window controls.+ m_vertical_tabs_expanded_width = saved_vertical_tabs_expanded_width();+ rebuild_layout();+}+void TabWidget::set_vertical_tabs_enabled(bool enabled){if (m_vertical_tabs_enabled == enabled)diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.hindex a30cf78b2e8..50ac8c1ea61 100644--- a/UI/Qt/TabBar.h+++ b/UI/Qt/TabBar.h@@ -344,6 +344,7 @@ public:void set_tab_bar_visible(bool visible);void set_new_tab_action(QAction* action);void set_window_controls_visible(bool);+ void window_control_buttons_changed();void update_window_button_icons();void set_vertical_tabs_enabled(bool);void set_everything_in_sidebar(bool);diff --git a/UI/Qt/WindowControlButton.cpp b/UI/Qt/WindowControlButton.cppindex 6ad5e50b4f4..d2c8975ad9c 100644--- a/UI/Qt/WindowControlButton.cpp+++ b/UI/Qt/WindowControlButton.cpp@@ -21,9 +21,14 @@#include <QWidget>#if defined(AK_OS_LINUX)+# include <AK/ByteString.h>+# include <AK/Optional.h># include <AK/StringView.h># include <LibCore/Environment.h>+# include <LibFileSystem/FileSystem.h>+# include <UI/Qt/BrowserWindow.h>+# include <QApplication># include <QSettings># include <QStandardPaths>// NB: GLib uses "signals" as a name, which Qt defines as a keyword.@@ -129,6 +134,21 @@ void WindowControlButton::paintEvent(QPaintEvent* event)}struct DesktopWindowButtons {+ bool shows(ControlButton button) const+ {+ switch (button) {+ case ControlButton::Minimize:+ return minimize;+ case ControlButton::Maximize:+ return maximize;+ case ControlButton::Close:+ return close;+ }+ VERIFY_NOT_REACHED();+ }++ bool operator==(DesktopWindowButtons const&) const = default;+bool minimize { true };bool maximize { true };bool close { true };@@ -137,6 +157,118 @@ struct DesktopWindowButtons {Vector<ControlButton, 3> order { ControlButton::Minimize, ControlButton::Maximize, ControlButton::Close };};+#if defined(AK_OS_LINUX)+static constexpr auto WM_PREFERENCES_SCHEMA = "org.gnome.desktop.wm.preferences";+static constexpr auto BUTTON_LAYOUT_KEY = "button-layout";++// https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Settings.html+static constexpr auto PORTAL_BUS_NAME = "org.freedesktop.portal.Desktop";+static constexpr auto PORTAL_OBJECT_PATH = "/org/freedesktop/portal/desktop";+static constexpr auto SETTINGS_INTERFACE = "org.freedesktop.portal.Settings";++static bool is_inside_flatpak()+{+ return FileSystem::exists("/.flatpak-info"sv);+}++static DesktopWindowButtons& desktop_window_buttons();++static void set_gnome_button_layout(DesktopWindowButtons& buttons, StringView layout)+{+ // The layout lists the buttons on the left of a colon and those on the right after it, such as "appmenu:close".+ buttons.minimize = layout.contains("minimize"sv);+ buttons.maximize = layout.contains("maximize"sv);+ buttons.close = layout.contains("close"sv);+}++static void apply_portal_button_layout(GVariant* reply)+{+ GVariant* value = nullptr;+ g_variant_get(reply, "(v)", &value);+ g_variant_unref(reply);+ if (g_variant_is_of_type(value, G_VARIANT_TYPE_VARIANT)) {+ auto* inner_value = g_variant_get_variant(value);+ g_variant_unref(value);+ value = inner_value;+ }+ if (!g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {+ g_variant_unref(value);+ return;+ }++ auto& buttons = desktop_window_buttons();+ auto previous_buttons = buttons;+ gsize layout_length = 0;+ auto const* layout = g_variant_get_string(value, &layout_length);+ set_gnome_button_layout(buttons, StringView { layout, layout_length });+ g_variant_unref(value);+ if (buttons == previous_buttons)+ return;++ for (auto* widget : QApplication::allWidgets()) {+ if (auto* button = as_if<WindowControlButton>(widget))+ button->setVisible(buttons.shows(button->type()));+ }+ for (auto* widget : QApplication::topLevelWidgets()) {+ if (auto* window = as_if<BrowserWindow>(widget))+ window->window_control_buttons_changed();+ }+}++static void call_settings_method(GDBusConnection* connection, char const* method, GAsyncReadyCallback callback)+{+ g_dbus_connection_call(connection, PORTAL_BUS_NAME, PORTAL_OBJECT_PATH, SETTINGS_INTERFACE, method,+ g_variant_new("(ss)", WM_PREFERENCES_SCHEMA, BUTTON_LAYOUT_KEY), G_VARIANT_TYPE("(v)"), G_DBUS_CALL_FLAGS_NONE, -1,+ nullptr, callback, nullptr);+}++static void did_read_portal_button_layout(GObject* source, GAsyncResult* result, gpointer)+{+ if (auto* reply = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), result, nullptr))+ apply_portal_button_layout(reply);+}++static void did_read_one_portal_button_layout(GObject* source, GAsyncResult* result, gpointer)+{+ auto* connection = G_DBUS_CONNECTION(source);+ GError* error = nullptr;+ auto* reply = g_dbus_connection_call_finish(connection, result, &error);+ // Portals before version 2 have only Settings.Read, which wraps the value in one more variant.+ if (!reply && g_error_matches(error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD))+ call_settings_method(connection, "Read", did_read_portal_button_layout);+ g_clear_error(&error);+ if (reply)+ apply_portal_button_layout(reply);+}++static void read_portal_button_layout()+{+ // Connecting synchronously sends the read while the first window is being built, so its answer is usually in+ // before that window is painted.+ auto* connection = g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, nullptr);+ if (!connection)+ return;+ call_settings_method(connection, "ReadOne", did_read_one_portal_button_layout);+ g_object_unref(connection);+}++static Optional<ByteString> read_gsettings_button_layout()+{+ auto* source = g_settings_schema_source_get_default();+ auto* schema = source ? g_settings_schema_source_lookup(source, WM_PREFERENCES_SCHEMA, TRUE) : nullptr;+ if (!schema)+ return {};+ g_settings_schema_unref(schema);++ auto* settings = g_settings_new(WM_PREFERENCES_SCHEMA);+ auto* layout = g_settings_get_string(settings, BUTTON_LAYOUT_KEY);+ ByteString result { layout };+ g_free(layout);+ g_object_unref(settings);+ return result;+}+#endif+// The window controls follow the desktop's settings; GNOME's buttons are drawn as circles.static DesktopWindowButtons read_desktop_window_buttons(){@@ -186,21 +318,12 @@ static DesktopWindowButtons read_desktop_window_buttons()return buttons;}- auto* source = g_settings_schema_source_get_default();- auto* schema = source ? g_settings_schema_source_lookup(source, "org.gnome.desktop.wm.preferences", TRUE) : nullptr;- if (!schema)- return buttons;- g_settings_schema_unref(schema);-- auto* settings = g_settings_new("org.gnome.desktop.wm.preferences");- auto* layout = g_settings_get_string(settings, "button-layout");- // The layout lists the buttons on the left of a colon and those on the right after it, such as "appmenu:close".- auto layout_view = StringView { layout, strlen(layout) };- buttons.minimize = layout_view.contains("minimize"sv);- buttons.maximize = layout_view.contains("maximize"sv);- buttons.close = layout_view.contains("close"sv);- g_free(layout);- g_object_unref(settings);+ // Inside a Flatpak, GSettings only sees the defaults of the runtime's own schemas.+ if (is_inside_flatpak()) {+ read_portal_button_layout();+ } else if (auto layout = read_gsettings_button_layout(); layout.has_value()) {+ set_gnome_button_layout(buttons, *layout);+ }#elif defined(AK_OS_WINDOWS)buttons.round = false;#endif@@ -208,9 +331,9 @@ static DesktopWindowButtons read_desktop_window_buttons()return buttons;}-static DesktopWindowButtons const& desktop_window_buttons()+static DesktopWindowButtons& desktop_window_buttons(){- static auto const buttons = read_desktop_window_buttons();+ static auto buttons = read_desktop_window_buttons();return buttons;}@@ -253,11 +376,10 @@ WindowControlButtons create_window_control_buttons(QWidget& parent, char const*break;}}- minimize->setVisible(desktop_buttons.minimize);- maximize->setVisible(desktop_buttons.maximize);- close->setVisible(desktop_buttons.close);- for (auto* button : { minimize, maximize, close })+ for (auto* button : { minimize, maximize, close }) {+ button->setVisible(desktop_buttons.shows(button->type()));button->set_round(desktop_buttons.round);+ }if (desktop_buttons.round)layout->setContentsMargins(0, 0, ROUND_BUTTONS_TRAILING_GAP, 0);diff --git a/UI/Qt/WindowControlButton.h b/UI/Qt/WindowControlButton.hindex c637da57719..d4bd310792c 100644--- a/UI/Qt/WindowControlButton.h+++ b/UI/Qt/WindowControlButton.h@@ -31,6 +31,8 @@ public:WindowControlButton(ControlButton, QString const& tool_tip, QSize icon_size, QSize button_size, QWidget* parent = nullptr);+ ControlButton type() const { return m_type; }+// GNOME draws its window controls as small circles, at the trailing end of the button's room.void set_round(bool);