Skip to content

Truly independent web browser

UI/Qt: Honor KDE window control placement

Timothy Flynn811140ff011d
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index 97460d9d176..bfb556cbd0f 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -1420,13 +1420,20 @@ void BrowserWindow::create_menu_bar_window_controls()
m_menu_bar_minimize_window_button = window_control_buttons.minimize;
m_menu_bar_maximize_window_button = window_control_buttons.maximize;
m_menu_bar_close_window_button = window_control_buttons.close;
- menuBar()->setCornerWidget(m_menu_bar_window_controls, Qt::TopRightCorner);
+ auto on_left = window_controls_on_left();
+ if (!on_left)
+ menuBar()->setCornerWidget(m_menu_bar_window_controls, Qt::TopRightCorner);
// NB: A menu bar insets its corner widgets by its leading padding, so the room before the first menu comes from
// a corner widget of its own, and the window controls can reach the window's edge.
auto* leading_room = new QWidget(menuBar());
auto* leading_room_layout = new QHBoxLayout(leading_room);
- leading_room_layout->setContentsMargins(MENU_BAR_LEADING_ROOM, 0, 0, 0);
+ leading_room_layout->setContentsMargins(on_left ? 0 : MENU_BAR_LEADING_ROOM, 0, 0, 0);
+ if (on_left) {
+ leading_room_layout->setSpacing(0);
+ leading_room_layout->addWidget(m_menu_bar_window_controls);
+ leading_room_layout->addSpacing(MENU_BAR_LEADING_ROOM);
+ }
menuBar()->setCornerWidget(leading_room, Qt::TopLeftCorner);
connect(m_menu_bar_minimize_window_button, &QToolButton::clicked, this, [this] {
@@ -1757,12 +1764,12 @@ bool BrowserWindow::eventFilter(QObject* object, QEvent* event)
auto is_button = qobject_cast<QAbstractButton*>(object) != nullptr;
- // The window controls run to the window's right edge. While the window can be resized, that edge still resizes it.
+ // The window controls run to the window's edge. While the window can be resized, that edge still resizes it.
if (is_button && uses_client_side_decorations() && !isMaximized() && !isFullScreen()) {
auto is_press = event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick;
auto is_mouse_event = is_press || event->type() == QEvent::MouseMove;
auto position = is_mouse_event ? widget->mapTo(this, static_cast<QMouseEvent*>(event)->position().toPoint()) : mapFromGlobal(QCursor::pos());
- if (auto edges = resize_edges_for_position(position); edges.testFlag(Qt::RightEdge)) {
+ if (auto edges = resize_edges_for_position(position); edges.testFlag(window_controls_on_left() ? Qt::LeftEdge : Qt::RightEdge)) {
// NB: Even if the resize does not start, a press here must not reach the button and close the window.
if (is_press) {
auto* handle = windowHandle();
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index 3aca7cb80bb..cce834d2553 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -3687,8 +3687,8 @@ void TabWidget::set_window_controls_visible(bool visible)
update_toolbar_window_controls_visibility();
update_tab_chrome_visibility();
update_tab_layout();
- // NB: The sidebar's top row is built around the window controls, so it's built again when they come or go.
- if (changed && m_tab_bar->tab_layout() != TabLayout::Horizontal)
+ // NB: The sidebar's top row and a strip with controls on the left keep room for the controls when they are shown.
+ if (changed && (m_tab_bar->tab_layout() != TabLayout::Horizontal || (!use_native_macos_window_controls() && window_controls_on_left())))
rebuild_layout();
}
@@ -4532,7 +4532,10 @@ void TabWidget::rebuild_layout_for_horizontal_tabs()
// NB: Without the traffic lights, the Ladybird button starts the strip, and its room for its focus ring takes part of
// the strip's margin.
auto leading_margin = HORIZONTAL_TAB_STRIP_LEADING_MARGIN - (use_native_macos_window_controls() ? 0 : LADYBIRD_BUTTON_FOCUS_RING_ROOM);
- m_tab_bar_row_layout->setContentsMargins(leading_margin, HORIZONTAL_TAB_STRIP_HEIGHT - HORIZONTAL_TAB_HEIGHT, HORIZONTAL_TAB_STRIP_TRAILING_MARGIN, 0);
+ auto trailing_margin = HORIZONTAL_TAB_STRIP_TRAILING_MARGIN;
+ if (!use_native_macos_window_controls() && window_controls_on_left() && m_window_controls_visible)
+ swap(leading_margin, trailing_margin);
+ m_tab_bar_row_layout->setContentsMargins(leading_margin, HORIZONTAL_TAB_STRIP_HEIGHT - HORIZONTAL_TAB_HEIGHT, trailing_margin, 0);
set_dynamic_property_if_needed(*m_new_tab_button, VERTICAL_TABS_EXPANDED_PROPERTY, false);
set_dynamic_property_if_needed(*m_new_tab_button, VERTICAL_TABS_BUTTON_PROPERTY, true);
@@ -4557,19 +4560,14 @@ void TabWidget::rebuild_layout_for_horizontal_tabs()
m_tab_bar_row_layout->addWidget(m_ladybird_buttons, 0, Qt::AlignBottom);
m_tab_bar_row_layout->addSpacing(LADYBIRD_BUTTONS_TRAILING_GAP - HORIZONTAL_TAB_END_ROOM);
};
- if constexpr (use_native_macos_window_controls()) {
- m_tab_bar_row_layout->addWidget(m_window_controls, 0, Qt::AlignVCenter);
- add_ladybird_buttons();
- m_tab_bar_row_layout->addWidget(m_tab_bar, 0, Qt::AlignBottom);
- m_tab_bar_row_layout->addSpacing(new_tab_button_room);
- m_tab_bar_row_layout->addStretch(1);
- } else {
- add_ladybird_buttons();
- m_tab_bar_row_layout->addWidget(m_tab_bar, 0, Qt::AlignBottom);
- m_tab_bar_row_layout->addSpacing(new_tab_button_room);
- m_tab_bar_row_layout->addStretch(1);
+ if (window_controls_on_left())
+ m_tab_bar_row_layout->addWidget(m_window_controls, 0, use_native_macos_window_controls() ? Qt::AlignVCenter : Qt::AlignTop);
+ add_ladybird_buttons();
+ m_tab_bar_row_layout->addWidget(m_tab_bar, 0, Qt::AlignBottom);
+ m_tab_bar_row_layout->addSpacing(new_tab_button_room);
+ m_tab_bar_row_layout->addStretch(1);
+ if (!window_controls_on_left())
m_tab_bar_row_layout->addWidget(m_window_controls, 0, Qt::AlignTop);
- }
place_horizontal_new_tab_button();
}
@@ -4593,7 +4591,7 @@ void TabWidget::place_horizontal_new_tab_button()
// NB: Where the strip ends at the window's edge, its margin there is part of the free space.
int TabWidget::horizontal_tab_strip_drag_space() const
{
- if constexpr (use_native_macos_window_controls())
+ if (window_controls_on_left())
return HORIZONTAL_TAB_STRIP_DRAG_SPACE - m_tab_bar_row_layout->contentsMargins().right();
return HORIZONTAL_TAB_STRIP_DRAG_SPACE;
}
diff --git a/UI/Qt/WindowControlButton.cpp b/UI/Qt/WindowControlButton.cpp
index f6ca6f202fa..d85876a8741 100644
--- a/UI/Qt/WindowControlButton.cpp
+++ b/UI/Qt/WindowControlButton.cpp
@@ -4,11 +4,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <UI/Qt/ChromeLayout.h>
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/Theme.h>
#include <UI/Qt/WindowControlButton.h>
#include <AK/Assertions.h>
+#include <AK/Vector.h>
#include <QBoxLayout>
#include <QEnterEvent>
@@ -20,6 +22,10 @@
#if defined(AK_OS_LINUX)
# include <AK/StringView.h>
+# include <LibCore/Environment.h>
+
+# include <QSettings>
+# include <QStandardPaths>
// NB: GLib uses "signals" as a name, which Qt defines as a keyword.
# pragma push_macro("signals")
# undef signals
@@ -40,19 +46,19 @@ static constexpr qreal ROUND_BUTTON_GLYPH_STROKE_WIDTH = 1.5;
static constexpr auto pressed_outside_property = "pressedOutside";
-static char const* object_name_for_type(WindowControlButtonType type)
+static char const* object_name_for_type(ControlButton type)
{
switch (type) {
- case WindowControlButtonType::Minimize:
- case WindowControlButtonType::Maximize:
+ case ControlButton::Minimize:
+ case ControlButton::Maximize:
return "LadybirdWindowButton";
- case WindowControlButtonType::Close:
+ case ControlButton::Close:
return "LadybirdCloseWindowButton";
}
VERIFY_NOT_REACHED();
}
-WindowControlButton::WindowControlButton(WindowControlButtonType type, QString const& tool_tip, QSize icon_size, QSize button_size, QWidget* parent)
+WindowControlButton::WindowControlButton(ControlButton type, QString const& tool_tip, QSize icon_size, QSize button_size, QWidget* parent)
: QToolButton(parent)
, m_type(type)
{
@@ -95,14 +101,14 @@ void WindowControlButton::paintEvent(QPaintEvent* event)
QPainterPath glyph;
switch (m_type) {
- case WindowControlButtonType::Minimize:
+ case ControlButton::Minimize:
glyph.moveTo(1.5, 5);
glyph.lineTo(8.5, 5);
break;
- case WindowControlButtonType::Maximize:
+ case ControlButton::Maximize:
glyph.addRoundedRect(QRectF(1.5, 1.5, 7, 7), 1.5, 1.5);
break;
- case WindowControlButtonType::Close:
+ case ControlButton::Close:
glyph.moveTo(1.5, 1.5);
glyph.lineTo(8.5, 8.5);
glyph.moveTo(8.5, 1.5);
@@ -121,17 +127,59 @@ struct DesktopWindowButtons {
bool maximize { true };
bool close { true };
bool round { false };
+ bool on_left { false };
+ Vector<ControlButton, 3> order { ControlButton::Minimize, ControlButton::Maximize, ControlButton::Close };
};
-// The window controls follow the desktop's: on GNOME, the buttons its settings list, as circles.
-// FIXME: Place the window controls on the leading side when the desktop puts them there.
-static DesktopWindowButtons desktop_window_buttons()
+// The window controls follow the desktop's settings; GNOME's buttons are drawn as circles.
+static DesktopWindowButtons read_desktop_window_buttons()
{
DesktopWindowButtons buttons;
+
#if defined(AK_OS_LINUX)
- auto const* desktop = getenv("XDG_CURRENT_DESKTOP");
- if (!desktop || !StringView { desktop, strlen(desktop) }.contains("GNOME"sv))
+ auto desktop = Core::Environment::get("XDG_CURRENT_DESKTOP"sv);
+ if (!desktop.has_value())
+ return buttons;
+
+ if (auto desktops = desktop->split_view(Core::Environment::PATH_SEPARATOR); desktops.contains_slow("KDE"sv)) {
+ auto config_file = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, "kwinrc");
+ if (config_file.isEmpty())
+ return buttons;
+
+ QSettings settings(config_file, QSettings::IniFormat);
+ settings.beginGroup("org.kde.kdecoration2");
+ auto left = settings.value("ButtonsOnLeft", "MS").toString();
+ auto right = settings.value("ButtonsOnRight", "HIAX").toString();
+
+ // Keep the controls together on the side containing the close button.
+ buttons.on_left = left.contains('X');
+ buttons.order.clear();
+
+ for (auto button : left + right) {
+ switch (button.unicode()) {
+ case 'I':
+ buttons.order.append(ControlButton::Minimize);
+ break;
+ case 'A':
+ buttons.order.append(ControlButton::Maximize);
+ break;
+ case 'X':
+ buttons.order.append(ControlButton::Close);
+ break;
+ default:
+ break;
+ }
+ }
+
+ buttons.minimize = buttons.order.contains_slow(ControlButton::Minimize);
+ buttons.maximize = buttons.order.contains_slow(ControlButton::Maximize);
+ buttons.close = buttons.order.contains_slow(ControlButton::Close);
+
return buttons;
+ } else if (!desktops.contains_slow("GNOME"sv)) {
+ return buttons;
+ }
+
buttons.round = true;
auto* source = g_settings_schema_source_get_default();
@@ -150,9 +198,21 @@ static DesktopWindowButtons desktop_window_buttons()
g_free(layout);
g_object_unref(settings);
#endif
+
return buttons;
}
+static DesktopWindowButtons const& desktop_window_buttons()
+{
+ static auto const buttons = read_desktop_window_buttons();
+ return buttons;
+}
+
+bool window_controls_on_left()
+{
+ return use_native_macos_window_controls() || desktop_window_buttons().on_left;
+}
+
WindowControlButtons create_window_control_buttons(QWidget& parent, char const* object_name, QSize icon_size, QSize button_size)
{
auto* container = new QWidget(&parent);
@@ -162,14 +222,23 @@ WindowControlButtons create_window_control_buttons(QWidget& parent, char const*
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
- auto* minimize = new WindowControlButton(WindowControlButtonType::Minimize, "Minimize", icon_size, button_size, container);
- auto* maximize = new WindowControlButton(WindowControlButtonType::Maximize, "Maximize", icon_size, button_size, container);
- auto* close = new WindowControlButton(WindowControlButtonType::Close, "Close", icon_size, button_size, container);
- layout->addWidget(minimize);
- layout->addWidget(maximize);
- layout->addWidget(close);
-
- static auto const desktop_buttons = desktop_window_buttons();
+ auto* minimize = new WindowControlButton(ControlButton::Minimize, "Minimize", icon_size, button_size, container);
+ auto* maximize = new WindowControlButton(ControlButton::Maximize, "Maximize", icon_size, button_size, container);
+ auto* close = new WindowControlButton(ControlButton::Close, "Close", icon_size, button_size, container);
+ auto const& desktop_buttons = desktop_window_buttons();
+ for (auto button : desktop_buttons.order) {
+ switch (button) {
+ case ControlButton::Minimize:
+ layout->addWidget(minimize);
+ break;
+ case ControlButton::Maximize:
+ layout->addWidget(maximize);
+ break;
+ case ControlButton::Close:
+ layout->addWidget(close);
+ break;
+ }
+ }
minimize->setVisible(desktop_buttons.minimize);
maximize->setVisible(desktop_buttons.maximize);
close->setVisible(desktop_buttons.close);
diff --git a/UI/Qt/WindowControlButton.h b/UI/Qt/WindowControlButton.h
index fcd0804a91f..6ddbf728e11 100644
--- a/UI/Qt/WindowControlButton.h
+++ b/UI/Qt/WindowControlButton.h
@@ -19,7 +19,7 @@ class QWidget;
namespace Ladybird {
-enum class WindowControlButtonType {
+enum class ControlButton {
Minimize,
Maximize,
Close,
@@ -29,7 +29,7 @@ class WindowControlButton final : public QToolButton {
public:
AK_ALLOC_WITH_KMALLOC;
- WindowControlButton(WindowControlButtonType, QString const& tool_tip, QSize icon_size, QSize button_size, QWidget* parent = nullptr);
+ WindowControlButton(ControlButton, QString const& tool_tip, QSize icon_size, QSize button_size, QWidget* parent = nullptr);
// GNOME draws its window controls as small circles, at the trailing end of the button's room.
void set_round(bool);
@@ -47,7 +47,7 @@ private:
void set_pressed_outside(bool);
void update_pressed_position(QPoint const&);
- WindowControlButtonType m_type;
+ ControlButton m_type;
bool m_tracking_press { false };
bool m_is_round { false };
};
@@ -59,6 +59,7 @@ struct WindowControlButtons {
WindowControlButton* close { nullptr };
};
+bool window_controls_on_left();
WindowControlButtons create_window_control_buttons(QWidget& parent, char const* object_name, QSize icon_size, QSize button_size);
QWidget* create_window_controls_spacer(QWidget& parent, char const* object_name, QSize size);
diff --git a/UI/Qt/WindowControlsReveal.cpp b/UI/Qt/WindowControlsReveal.cpp
index 53e943fba57..1140d232afe 100644
--- a/UI/Qt/WindowControlsReveal.cpp
+++ b/UI/Qt/WindowControlsReveal.cpp
@@ -7,6 +7,7 @@
#include <UI/Qt/Motion.h>
#include <UI/Qt/SystemAppearance.h>
#include <UI/Qt/Theme.h>
+#include <UI/Qt/WindowControlButton.h>
#include <UI/Qt/WindowControlsReveal.h>
#include <QApplication>
@@ -119,8 +120,10 @@ bool WindowControlsReveal::eventFilter(QObject* watched, QEvent* event)
QRect WindowControlsReveal::rect_when_revealed() const
{
auto size = m_window_controls->sizeHint();
- auto top_right = m_page.mapTo(parentWidget(), QPoint(m_page.width(), 0));
- return { top_right - QPoint(size.width(), 0), size };
+ auto top_left = m_page.mapTo(parentWidget(), QPoint(0, 0));
+ if (!window_controls_on_left())
+ top_left.rx() += m_page.width() - size.width();
+ return { top_left, size };
}
void WindowControlsReveal::pointer_moved(QPointF global_position, bool button_is_down)
@@ -129,7 +132,9 @@ void WindowControlsReveal::pointer_moved(QPointF global_position, bool button_is
auto revealed_rect = rect_when_revealed();
if (m_revealed) {
- if (revealed_rect.adjusted(-CONCEAL_MARGIN, -WINDOW_EDGE_ROOM, WINDOW_EDGE_ROOM, CONCEAL_MARGIN).contains(position))
+ auto left_margin = window_controls_on_left() ? WINDOW_EDGE_ROOM : CONCEAL_MARGIN;
+ auto right_margin = window_controls_on_left() ? CONCEAL_MARGIN : WINDOW_EDGE_ROOM;
+ if (revealed_rect.adjusted(-left_margin, -WINDOW_EDGE_ROOM, right_margin, CONCEAL_MARGIN).contains(position))
m_conceal_timer->stop();
else if (!m_conceal_timer->isActive())
m_conceal_timer->start();
@@ -137,7 +142,8 @@ void WindowControlsReveal::pointer_moved(QPointF global_position, bool button_is
}
// NB: A drag that ends up in the corner, such as a text selection, isn't headed for the window controls.
- auto reveal_zone = QRect(revealed_rect.topLeft(), QSize(revealed_rect.width(), REVEAL_ZONE_HEIGHT)).adjusted(0, -WINDOW_EDGE_ROOM, WINDOW_EDGE_ROOM, 0);
+ auto reveal_zone = QRect(revealed_rect.topLeft(), QSize(revealed_rect.width(), REVEAL_ZONE_HEIGHT));
+ reveal_zone.adjust(window_controls_on_left() ? -WINDOW_EDGE_ROOM : 0, -WINDOW_EDGE_ROOM, window_controls_on_left() ? 0 : WINDOW_EDGE_ROOM, 0);
if (!button_is_down && reveal_zone.contains(position))
set_revealed(true);
}
@@ -190,7 +196,7 @@ void WindowControlsReveal::paintEvent(QPaintEvent*)
QPainter painter(this);
auto const& chrome = Theme::chrome(*this);
painter.fillRect(rect(), chrome.sidebar);
- painter.fillRect(0, 0, 1, height(), chrome.divider);
+ painter.fillRect(window_controls_on_left() ? width() - 1 : 0, 0, 1, height(), chrome.divider);
painter.fillRect(0, height() - 1, width(), 1, chrome.divider);
}
diff --git a/UI/Qt/WindowToolbar.cpp b/UI/Qt/WindowToolbar.cpp
index a2a81962c99..9a82e26d7bd 100644
--- a/UI/Qt/WindowToolbar.cpp
+++ b/UI/Qt/WindowToolbar.cpp
@@ -751,6 +751,11 @@ private:
// NB: Window controls run to the window's edge. Without them, the address field ends the toolbar, and the room for its
// focus ring takes the place of some of the toolbar's margin.
+static int toolbar_left_margin(bool has_custom_window_controls)
+{
+ return has_custom_window_controls && window_controls_on_left() ? 0 : TOOLBAR_HORIZONTAL_MARGIN;
+}
+
static constexpr int toolbar_right_margin(bool has_custom_window_controls)
{
return has_custom_window_controls ? 0 : TOOLBAR_HORIZONTAL_MARGIN - TOOLBAR_LOCATION_FOCUS_RING_ROOM;
@@ -1351,8 +1356,11 @@ void WindowToolbar::lay_out_as_row()
toolbar_layout->setSpacing(TOOLBAR_SPACING);
toolbar_layout->setContentsMargins(TOOLBAR_HORIZONTAL_MARGIN, 0, toolbar_right_margin(false), 0);
- if constexpr (use_native_macos_window_controls())
+ if (window_controls_on_left()) {
toolbar_layout->addWidget(m_window_controls, 0, Qt::AlignVCenter);
+ if constexpr (!use_native_macos_window_controls())
+ toolbar_layout->addWidget(m_window_controls_separator, 0, Qt::AlignVCenter);
+ }
toolbar_layout->addWidget(m_sidebar_slot, 0, Qt::AlignVCenter);
toolbar_layout->addWidget(m_navigation_buttons, 0, Qt::AlignVCenter);
m_navigation_buttons->show();
@@ -1360,7 +1368,7 @@ void WindowToolbar::lay_out_as_row()
toolbar_layout->addWidget(m_downloads_button, 0, Qt::AlignVCenter);
if (m_feedback_button)
toolbar_layout->addWidget(m_feedback_button, 0, Qt::AlignVCenter);
- if constexpr (!use_native_macos_window_controls()) {
+ if (!window_controls_on_left()) {
toolbar_layout->addWidget(m_window_controls_separator, 0, Qt::AlignVCenter);
toolbar_layout->addWidget(m_window_controls, 0, Qt::AlignVCenter);
}
@@ -1791,7 +1799,8 @@ void WindowToolbar::set_window_controls_visible(bool visible)
m_window_controls_separator->setVisible(has_custom_window_controls);
m_window_controls->setVisible(has_native_window_controls || has_custom_window_controls);
m_toolbar->setFixedHeight(toolbar_height(visible));
- m_toolbar->layout()->setContentsMargins(TOOLBAR_HORIZONTAL_MARGIN, 0, toolbar_right_margin(has_custom_window_controls), 0);
+ auto on_left = window_controls_on_left();
+ m_toolbar->layout()->setContentsMargins(toolbar_left_margin(has_custom_window_controls), 0, toolbar_right_margin(has_custom_window_controls && !on_left), 0);
m_toolbar->layout()->invalidate();
}
@@ -1801,8 +1810,11 @@ int WindowToolbar::lead_beside_collapsed_sidebar(QWidget const& ladybird_buttons
{
auto toolbar_spacing = m_toolbar->layout()->spacing();
auto lead = 0;
- if constexpr (use_native_macos_window_controls())
+ if (window_controls_on_left() && !m_window_controls->isHidden()) {
lead += m_window_controls->width() + toolbar_spacing;
+ if constexpr (!use_native_macos_window_controls())
+ lead += m_window_controls_separator->width() + toolbar_spacing;
+ }
auto slot_width = m_sidebar_slot->layout()->sizeHint().width();
auto slot_spacing = m_sidebar_slot->layout()->spacing();
@@ -1818,10 +1830,12 @@ void WindowToolbar::set_leading_room(int room)
{
if (m_arrangement == ToolbarArrangement::Sidebar)
return;
+ auto has_custom_window_controls = !use_native_macos_window_controls() && m_window_controls_visible;
+ auto left_margin = toolbar_left_margin(has_custom_window_controls) + room;
auto margins = m_toolbar->layout()->contentsMargins();
- if (margins.left() == TOOLBAR_HORIZONTAL_MARGIN + room)
+ if (margins.left() == left_margin)
return;
- margins.setLeft(TOOLBAR_HORIZONTAL_MARGIN + room);
+ margins.setLeft(left_margin);
m_toolbar->layout()->setContentsMargins(margins);
// NB: The toolbar moves along with the sidebar's edge at once, so its contents must too, or they lag a frame behind.
m_toolbar->layout()->activate();