Truly independent web browser
UI/Qt: Give each window one toolbar
Shannon Booth900d7bb5eef0
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cppindex deea559eff5..da42f110188 100644--- a/UI/Qt/BrowserWindow.cpp+++ b/UI/Qt/BrowserWindow.cpp@@ -311,6 +311,9 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow, m_tabs_container(new TabWidget(is_private, this)), m_is_popup_window(is_popup_window){+ // NB: The toolbar is made before the rest of the window, so it learns of the session's downloads once it is all here.+ toolbar().update_downloads_button();+auto const& browser_options = WebView::Application::browser_options();// A restored window keeps the session row the store re-adopted for it.@@ -519,8 +522,7 @@ void BrowserWindow::initialize_ladybird_menu()#endifdownloads_action->setShortcutContext(Qt::WidgetShortcut);QObject::connect(downloads_action, &QAction::triggered, this, [this] {- if (m_current_tab)- m_current_tab->show_downloads_popover();+ toolbar().show_downloads_popover();});m_ladybird_menu->addSeparator();@@ -1012,9 +1014,7 @@ void BrowserWindow::set_failed_download_count(size_t count)void BrowserWindow::acknowledge_failed_downloads(){m_acknowledged_failed_download_count = m_failed_download_count;- for_each_tab([](Tab& tab) {- tab.update_downloads_button();- });+ toolbar().update_downloads_button();}void BrowserWindow::set_current_tab(Tab* tab)diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.hindex 6313a6986c4..6972ae9263e 100644--- a/UI/Qt/BrowserWindow.h+++ b/UI/Qt/BrowserWindow.h@@ -160,6 +160,7 @@ public:int tab_index(Tab*);Tab* tab_at(int index) const { return m_tabs_container->tab(index); }TabWidget& tab_widget() const { return *m_tabs_container; }+ WindowToolbar& toolbar() const { return m_tabs_container->toolbar(); }// Closes a tab that isn't worth reopening, such as an empty tab that other tabs took the place of.void close_tab_without_keeping_it(int index);diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txtindex e87646ad600..347198db5c0 100644--- a/UI/Qt/CMakeLists.txt+++ b/UI/Qt/CMakeLists.txt@@ -71,6 +71,7 @@ target_sources(ladybird PRIVATEWindowCloseCoordinator.cppWindowControlButton.cppWindowScreenObserver.cpp+ WindowToolbar.cppladybird.qrc)if (NOT APPLE AND NOT WIN32 AND NOT ANDROID)diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex 66f4b13a622..89eea9de877 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -14,14 +14,12 @@#include <LibWakeLock/DisplaySleepInhibitor.h>#include <LibWebCommon/HTML/SelectedFile.h>#include <LibWebView/Application.h>-#include <LibWebView/DownloadPresentation.h>#include <LibWebView/URL.h>#include <LibWebView/Utilities.h>#include <LibWebView/WebContentClient.h>#include <UI/Qt/Application.h>#include <UI/Qt/BookmarkPopover.h>#include <UI/Qt/BrowserWindow.h>-#include <UI/Qt/ChromeLayout.h>#include <UI/Qt/ChromeStyle.h>#include <UI/Qt/Icon.h>#include <UI/Qt/JavaScriptDialog.h>@@ -38,9 +36,7 @@#include <UI/Qt/StringUtils.h>#include <UI/Qt/SystemAppearance.h>#include <UI/Qt/Theme.h>-#include <UI/Qt/WindowControlButton.h>-#include <LibWebView/TabPerformanceMonitor.h>#include <QApplication>#include <QCalendarWidget>#include <QClipboard>@@ -51,7 +47,6 @@#include <QFileDialog>#include <QFont>#include <QFontMetrics>-#include <QFrame>#include <QHBoxLayout>#include <QImage>#include <QLocale>@@ -62,18 +57,12 @@#include <QPainter>#include <QPainterPath>#include <QPixmap>-#include <QProgressBar>#include <QPushButton>#include <QResizeEvent>-#include <QScrollArea>-#include <QStyleOptionToolButton>-#include <QStylePainter>#include <QTimeEdit>#include <QTimer>#include <QToolButton>#include <QVBoxLayout>-#include <QVariantAnimation>-#include <UI/Qt/PerformanceMonitorWidget.h>namespace Ladybird {@@ -109,759 +98,9 @@ static QIcon calendar_month_chevron_icon(QWidget const& widget, ChevronDirectionreturn QIcon(pixmap);}-static Web::HTML::ActivateTab activate_tab_for_modifiers(Qt::KeyboardModifiers modifiers)-{- return modifiers.testFlag(Qt::ShiftModifier) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No;-}--class ToolbarButton : public QToolButton {-public:- AK_ALLOC_WITH_KMALLOC;-- explicit ToolbarButton(QWidget* parent)- : QToolButton(parent)- {- QObject::connect(&m_icon_motion_animation, &QVariantAnimation::valueChanged, this, [this] { update(); });- QObject::connect(&m_icon_motion_animation, &QVariantAnimation::finished, this, [this] { update(); });- }-- Function<void(Web::HTML::ActivateTab)> on_open_in_new_tab;-- bool click_opens_in_new_tab() const- {- return can_open_in_new_tab() && QApplication::keyboardModifiers().testFlag(Qt::ControlModifier);- }-- // A click or a change of state moves the icon a little: back and forward nudge their way, and the downloads arrow- // drops into place as the button appears and bobs once as a download finishes.- enum class IconMotion : u8 {- NudgeLeft,- NudgeRight,- DropIn,- Bob,- };-- void play_icon_motion(IconMotion motion)- {- if (SystemAppearance::prefers_reduced_motion())- return;- m_icon_motion = motion;- // NB: A click can disable its button, as going back to the first page does with the back button. The motion- // plays out as the button looked when it started.- m_icon_motion_is_hovered = underMouse();- m_icon_motion_animation.stop();- m_icon_motion_animation.setStartValue(0.0);- m_icon_motion_animation.setEndValue(1.0);- switch (motion) {- case IconMotion::NudgeLeft:- case IconMotion::NudgeRight:- m_icon_motion_animation.setDuration(NUDGE_DURATION_MS);- break;- case IconMotion::DropIn:- m_icon_motion_animation.setDuration(DROP_IN_DURATION_MS);- break;- case IconMotion::Bob:- m_icon_motion_animation.setDuration(BOB_DURATION_MS);- break;- }- m_icon_motion_animation.start();- }--protected:- virtual bool can_open_in_new_tab() const { return static_cast<bool>(on_open_in_new_tab); }-- QRect icon_rect() const- {- return QRect(QPoint((width() - iconSize().width()) / 2, (height() - iconSize().height()) / 2), iconSize());- }-- QIcon::Mode icon_mode() const- {- return isEnabled() ? (underMouse() ? QIcon::Active : QIcon::Normal) : QIcon::Disabled;- }-- virtual void paintEvent(QPaintEvent*) override- {- if (m_icon_motion_animation.state() == QAbstractAnimation::Running) {- QPainter painter(this);- if (isEnabled()) {- ChromeStyle::paint_control_frame(painter, *this);- } else if (m_icon_motion_is_hovered && underMouse()) {- painter.setRenderHint(QPainter::Antialiasing, true);- painter.setPen(Qt::NoPen);- painter.setBrush(Theme::chrome(palette()).hover);- painter.drawRoundedRect(QRectF(rect()), ChromeStyle::CONTROL_FRAME_RADIUS, ChromeStyle::CONTROL_FRAME_RADIUS);- painter.setRenderHint(QPainter::Antialiasing, false);- }- auto mode = isEnabled() ? icon_mode() : (m_icon_motion_is_hovered && underMouse() ? QIcon::Active : QIcon::Normal);- auto progress = m_icon_motion_animation.currentValue().toReal();- switch (m_icon_motion) {- case IconMotion::NudgeLeft:- painter.translate(-NUDGE_DISTANCE * Motion::there_and_back(progress, NUDGE_PEAK_AT), 0);- break;- case IconMotion::NudgeRight:- painter.translate(NUDGE_DISTANCE * Motion::there_and_back(progress, NUDGE_PEAK_AT), 0);- break;- case IconMotion::DropIn: {- auto eased = Motion::enter_easing().valueForProgress(progress);- painter.translate(0, -DROP_IN_DISTANCE * (1 - eased));- painter.setOpacity(eased);- break;- }- case IconMotion::Bob:- painter.translate(0, BOB_DISTANCE * Motion::there_and_back(progress, BOB_PEAK_AT));- break;- }- icon().paint(&painter, icon_rect(), Qt::AlignCenter, mode);- return;- }-- QStyleOptionToolButton option;- initStyleOption(&option);-- QStylePainter painter(this);- ChromeStyle::paint_control_frame(painter, *this);-- painter.drawComplexControl(QStyle::CC_ToolButton, option);- }-- virtual void mousePressEvent(QMouseEvent* event) override- {- if (event->button() == Qt::MiddleButton && can_open_in_new_tab())- event->accept();- else- QToolButton::mousePressEvent(event);- }-- virtual void mouseReleaseEvent(QMouseEvent* event) override- {- if (event->button() == Qt::MiddleButton && can_open_in_new_tab()) {- if (rect().contains(event->position().toPoint()))- on_open_in_new_tab(activate_tab_for_modifiers(event->modifiers()));- event->accept();- } else {- QToolButton::mouseReleaseEvent(event);- }- }-- // QToolButton triggers its default action from here, once a click has completed.- virtual void nextCheckState() override- {- if (click_opens_in_new_tab()) {- on_open_in_new_tab(activate_tab_for_modifiers(QApplication::keyboardModifiers()));- return;- }- QToolButton::nextCheckState();- }--private:- static constexpr int NUDGE_DURATION_MS = 180;- static constexpr qreal NUDGE_DISTANCE = 2;- static constexpr qreal NUDGE_PEAK_AT = 0.4;- static constexpr int DROP_IN_DURATION_MS = 220;- static constexpr qreal DROP_IN_DISTANCE = 4;- static constexpr int BOB_DURATION_MS = 260;- static constexpr qreal BOB_DISTANCE = 2;- static constexpr qreal BOB_PEAK_AT = 0.45;-- IconMotion m_icon_motion { IconMotion::NudgeLeft };- bool m_icon_motion_is_hovered { false };- QVariantAnimation m_icon_motion_animation;-};--// The reload button becomes a stop button while the page loads. So that one click does not do what the user did not aim-// at, a stop button under the pointer stays until the pointer leaves or a short delay passes, and a click on stop within-// the double-click interval of a reload click is ignored, so a double-click does not stop the reload it started. The-// reload action stays attached to the button while it shows stop, so its shortcuts still work during a load.-class ReloadButton final : public ToolbarButton {-public:- AK_ALLOC_WITH_KMALLOC;-- explicit ReloadButton(QWidget* parent)- : ToolbarButton(parent)- {- m_icon_fade.setStartValue(0.0);- m_icon_fade.setEndValue(1.0);- QObject::connect(&m_icon_fade, &QVariantAnimation::valueChanged, this, [this] { update(); });-- m_double_click_timer.setSingleShot(true);- m_double_click_timer.setInterval(QApplication::doubleClickInterval());-- m_stop_to_reload_timer.setSingleShot(true);- m_stop_to_reload_timer.setInterval(STOP_TO_RELOAD_DELAY_MS);- QObject::connect(&m_stop_to_reload_timer, &QTimer::timeout, this, [this] {- change_mode(m_intended_mode, ForceChange::Yes);- });-- QObject::connect(this, &QToolButton::pressed, this, [this] {- if (m_visible_mode == Mode::Reload)- m_double_click_timer.start();- });-- // NB: A stop click that arrives after the load ended has nothing to stop, so show the reload button right away.- QObject::connect(this, &QToolButton::clicked, this, [this] {- if (m_visible_mode == Mode::Stop)- change_mode(m_intended_mode, ForceChange::Yes);- });- }-- void set_actions(QAction& reload_action, QAction& reload_bypassing_cache_action, QAction& stop_action)- {- m_reload_action = &reload_action;- m_reload_bypassing_cache_action = &reload_bypassing_cache_action;- m_stop_action = &stop_action;- }-- void set_loading(bool is_loading)- {- change_mode(is_loading ? Mode::Stop : Mode::Reload, ForceChange::No);- }--protected:- virtual bool can_open_in_new_tab() const override- {- return m_visible_mode == Mode::Reload && ToolbarButton::can_open_in_new_tab();- }-- virtual void leaveEvent(QEvent* event) override- {- ToolbarButton::leaveEvent(event);- change_mode(m_intended_mode, ForceChange::Yes);- }-- // A Shift-click on the reload button reloads bypassing the HTTP cache, and one on the stop button stops the load as- // any other click there does.- virtual void nextCheckState() override- {- if (m_visible_mode == Mode::Stop && m_double_click_timer.isActive())- return;- if (m_visible_mode == Mode::Reload && !click_opens_in_new_tab() && QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier)) {- m_reload_bypassing_cache_action->trigger();- return;- }- ToolbarButton::nextCheckState();- }--private:- static constexpr int STOP_TO_RELOAD_DELAY_MS = 1000;- static constexpr int ARROW_TURN_DURATION_MS = 340;- static constexpr int TURN_CROSS_FADE_DELAY_MS = 200;- static constexpr int TURN_CROSS_FADE_DURATION_MS = 140;- static constexpr int STOP_TURN_DELAY_MS = 180;- static constexpr int STOP_TURN_DURATION_MS = 200;- static constexpr int RELOAD_TO_STOP_DURATION_MS = STOP_TURN_DELAY_MS + STOP_TURN_DURATION_MS;- static constexpr int ICON_CROSS_FADE_DURATION_MS = 160;-- enum class Mode {- Reload,- Stop,- };-- enum class ForceChange {- No,- Yes,- };-- void change_mode(Mode mode, ForceChange force_change)- {- m_intended_mode = mode;-- auto change_is_safe = mode == Mode::Stop || m_visible_mode != Mode::Stop;- if (force_change == ForceChange::Yes || !underMouse() || change_is_safe) {- m_stop_to_reload_timer.stop();- if (m_visible_mode != mode && defaultAction() && !SystemAppearance::prefers_reduced_motion()) {- m_previous_icon = defaultAction()->icon();- // NB: The arrow turns into the stop icon only as the reload button is clicked. A load started otherwise- // cross-fades it, as does the end of a load.- m_turns_icon = mode == Mode::Stop && m_double_click_timer.isActive();- m_icon_fade.stop();- m_icon_fade.setDuration(m_turns_icon ? RELOAD_TO_STOP_DURATION_MS : ICON_CROSS_FADE_DURATION_MS);- m_icon_fade.start();- }- m_visible_mode = mode;- setDefaultAction(mode == Mode::Stop ? m_stop_action : m_reload_action);- return;- }-- if (m_visible_mode == Mode::Stop && !m_stop_to_reload_timer.isActive())- m_stop_to_reload_timer.start();- }-- // As a click on it starts a load, the reload arrow turns half a turn, and over its second half cross-fades into the- // stop icon, which turns into place. Otherwise, as when the load ends, the two icons only cross-fade.- virtual void paintEvent(QPaintEvent* event) override- {- if (m_icon_fade.state() != QAbstractAnimation::Running) {- ToolbarButton::paintEvent(event);- return;- }-- QPainter painter(this);- painter.setRenderHint(QPainter::SmoothPixmapTransform, true);- ChromeStyle::paint_control_frame(painter, *this);- auto elapsed = m_icon_fade.currentValue().toReal() * m_icon_fade.duration();- auto part = [&](qreal delay, qreal duration) { return clamp((elapsed - delay) / duration, 0.0, 1.0); };- auto center = QRectF(icon_rect()).center();- auto paint_turned = [&](QIcon const& icon, qreal opacity, qreal degrees) {- painter.save();- painter.setOpacity(opacity);- painter.translate(center);- painter.rotate(degrees);- painter.translate(-center);- icon.paint(&painter, icon_rect(), Qt::AlignCenter, icon_mode());- painter.restore();- };- if (!m_turns_icon) {- auto fade = part(0, ICON_CROSS_FADE_DURATION_MS);- paint_turned(m_previous_icon, 1 - fade, 0);- paint_turned(icon(), fade, 0);- return;- }- static auto const easing = Motion::enter_easing();- auto fade = part(TURN_CROSS_FADE_DELAY_MS, TURN_CROSS_FADE_DURATION_MS);- paint_turned(m_previous_icon, 1 - fade, 180 * easing.valueForProgress(part(0, ARROW_TURN_DURATION_MS)));- paint_turned(icon(), fade, -90 * (1 - easing.valueForProgress(part(STOP_TURN_DELAY_MS, STOP_TURN_DURATION_MS))));- }-- QAction* m_reload_action { nullptr };- QAction* m_reload_bypassing_cache_action { nullptr };- QAction* m_stop_action { nullptr };- QIcon m_previous_icon;- QVariantAnimation m_icon_fade;- bool m_turns_icon { false };- Mode m_visible_mode { Mode::Reload };- Mode m_intended_mode { Mode::Reload };- QTimer m_double_click_timer;- QTimer m_stop_to_reload_timer;-};--static constexpr QSize TOOLBAR_BUTTON_SIZE { 32, 28 };--// The downloads button appears at the end of the toolbar once there are downloads, and opens the list of them. While-// they are under way, a ring around it fills as they progress, or turns while their progress is not known. A failed-// download marks it with a dot until the list is looked at.-class DownloadsButton final : public ToolbarButton {-public:- AK_ALLOC_WITH_KMALLOC;-- explicit DownloadsButton(QWidget* parent)- : ToolbarButton(parent)- {- setObjectName("LadybirdDownloadsButton");- setAccessibleName("Downloads");- // NB: The button stays checked while the list of downloads is open.- setCheckable(true);- setAutoRaise(true);- setFocusPolicy(Qt::NoFocus);- setIconSize({ TOOLBAR_ICON_SIZE, TOOLBAR_ICON_SIZE });- setFixedSize(TOOLBAR_BUTTON_SIZE);- setAttribute(Qt::WA_LayoutUsesWidgetRect);- hide();-- m_ring_animation.setDuration(ChromeStyle::LOADING_RING_PERIOD_MS);- m_ring_animation.setStartValue(0.0);- m_ring_animation.setEndValue(1.0);- m_ring_animation.setLoopCount(-1);- QObject::connect(&m_ring_animation, &QVariantAnimation::valueChanged, this, [this] { update(); });-- m_ring_fade.setDuration(RING_FADE_DURATION_MS);- QObject::connect(&m_ring_fade, &QVariantAnimation::valueChanged, this, [this] { update(); });-- m_progress_animation.setDuration(PROGRESS_GLIDE_DURATION_MS);- QObject::connect(&m_progress_animation, &QVariantAnimation::valueChanged, this, [this](QVariant const& value) {- m_shown_progress = value.toReal();- update();- });- }-- void set_progress(bool downloading, Optional<double> progress)- {- if (m_downloading == downloading && m_progress == progress)- return;- // The ring fades in as downloads start, and out as they end, showing how far they had got.- if (m_downloading != downloading) {- m_ring_fade.stop();- if (!SystemAppearance::prefers_reduced_motion() && isVisible()) {- m_ring_fade.setStartValue(downloading ? 0.0 : 1.0);- m_ring_fade.setEndValue(downloading ? 1.0 : 0.0);- m_ring_fade.start();- }- }- if (m_downloading && !downloading)- m_progress_at_end = m_shown_progress;- // The ring glides to each new amount rather than jumping to it.- m_progress_animation.stop();- if (progress.has_value() && downloading) {- if (!m_downloading || !m_progress.has_value() || SystemAppearance::prefers_reduced_motion()) {- m_shown_progress = *progress;- } else {- m_progress_animation.setStartValue(m_shown_progress);- m_progress_animation.setEndValue(*progress);- m_progress_animation.start();- }- }- m_downloading = downloading;- m_progress = progress;- auto should_turn = downloading && !progress.has_value() && isVisible();- if (should_turn && m_ring_animation.state() != QAbstractAnimation::Running)- m_ring_animation.start();- else if (!should_turn)- m_ring_animation.stop();- update();- }-- void set_needs_attention(bool needs_attention)- {- if (m_needs_attention == needs_attention)- return;- m_needs_attention = needs_attention;- update();- }--private:- virtual void paintEvent(QPaintEvent* event) override- {- ToolbarButton::paintEvent(event);-- QPainter painter(this);- painter.setRenderHint(QPainter::Antialiasing, true);- auto palette = Theme::chrome_palette(*this);- auto center = QRectF(rect()).center();- auto ring_is_fading = m_ring_fade.state() == QAbstractAnimation::Running;- if (m_downloading || ring_is_fading) {- painter.save();- if (ring_is_fading)- painter.setOpacity(m_ring_fade.currentValue().toReal());- auto progress = m_downloading ? (m_progress.has_value() ? Optional<double> { m_shown_progress } : Optional<double> {}) : m_progress_at_end;- if (progress.has_value())- ChromeStyle::paint_progress_ring(painter, palette, center, *progress);- else- ChromeStyle::paint_loading_ring(painter, palette, center, ChromeStyle::LoadingRingSize::ToolbarButton);- painter.restore();- }- if (m_needs_attention) {- QPointF dot_center { width() - 7.0, 6.0 };- painter.setPen(Qt::NoPen);- painter.setBrush(Theme::chrome(palette).toolbar);- painter.drawEllipse(dot_center, 5.5, 5.5);- painter.setBrush(Theme::chrome(palette).warning);- painter.drawEllipse(dot_center, 3.5, 3.5);- }- }-- static constexpr int RING_FADE_DURATION_MS = 200;- static constexpr int PROGRESS_GLIDE_DURATION_MS = 300;-- bool m_downloading { false };- Optional<double> m_progress;- Optional<double> m_progress_at_end;- double m_shown_progress { 0 };- QVariantAnimation m_progress_animation;- bool m_needs_attention { false };- QVariantAnimation m_ring_animation;- QVariantAnimation m_ring_fade;-};--template<typename ButtonType = ToolbarButton>-static ButtonType* create_toolbar_button(QWidget& parent, QAction& action)-{- auto* button = new ButtonType(&parent);- button->setDefaultAction(&action);- button->setAutoRaise(true);- button->setFocusPolicy(Qt::NoFocus);- button->setIconSize({ TOOLBAR_ICON_SIZE, TOOLBAR_ICON_SIZE });- button->setFixedSize(TOOLBAR_BUTTON_SIZE);- // NB: The platform style pads tool buttons for its own frame, which would space them further apart than the design.- button->setAttribute(Qt::WA_LayoutUsesWidgetRect);-- // FIXME: In Menu.cpp, we set the initial visibility of the action before we've associated it with this QToolButton.- // It would be nicer if we didn't have to do this here.- button->setVisible(action.isVisible());-- return button;-}--QToolButton* Tab::create_navigation_history_toolbar_button(QWidget& parent, QAction& action, int direction)-{- auto& view = this->view();- auto* button = create_toolbar_button(parent, action);- button->on_open_in_new_tab = [this, direction](auto activate_tab) {- m_window->open_history_entry_in_new_tab(*this, direction, activate_tab);- };- auto* menu = new QMenu(button);- QObject::connect(menu, &QMenu::aboutToShow, button, [menu, &view, direction] {- populate_session_history_traversal_menu(*menu, view, direction);- });- QObject::connect(menu, &QMenu::triggered, button, [this, &view](QAction* action) {- auto step = action->data().toInt();- if (auto modifiers = QApplication::keyboardModifiers(); modifiers.testFlag(Qt::ControlModifier))- m_window->duplicate_tab(*this, activate_tab_for_modifiers(modifiers), step);- else- view.traverse_the_history_to_step(step);- });- button->setMenu(menu);- button->setPopupMode(QToolButton::DelayedPopup);- QObject::connect(button, &QToolButton::clicked, button, [button, direction] {- if (!button->click_opens_in_new_tab())- button->play_icon_motion(direction < 0 ? ToolbarButton::IconMotion::NudgeLeft : ToolbarButton::IconMotion::NudgeRight);- });- button->setContextMenuPolicy(Qt::CustomContextMenu);- QObject::connect(button, &QToolButton::customContextMenuRequested, button, [button, menu, &view, direction](QPoint const&) {- populate_session_history_traversal_menu(*menu, view, direction);- if (!menu->isEmpty())- button->showMenu();- });- return button;-}-static constexpr int TAB_FAVICON_SIZE = 16;-static constexpr int TOOLBAR_HORIZONTAL_MARGIN = 8;-// The buttons in the toolbar sit close together, while the address field keeps a wider gap on either side.-static constexpr int TOOLBAR_SPACING = 2;-// The separator after the collapsed sidebar's buttons keeps a little more room on either side.-static constexpr int SIDEBAR_SLOT_SEPARATOR_MARGIN = 6;-// NB: With the toolbar's spacing, the address field starts 8 px after the reload button.-static constexpr int TOOLBAR_LOCATION_EDIT_MARGIN = 6;-// The address field's focus ring reaches 3 px past its edge. With the toolbar's spacing, this room also puts the-// downloads button 6 px after the field.-static constexpr int TOOLBAR_LOCATION_FOCUS_RING_ROOM = 4;-static constexpr int TOOLBAR_WINDOW_CONTROLS_RIGHT_MARGIN = 4;--// NB: Unless window controls follow it, the address field ends the toolbar, and the room for its focus ring takes the-// place of some of the toolbar's margin.-static constexpr int toolbar_right_margin(bool has_custom_window_controls)-{- return has_custom_window_controls ? TOOLBAR_WINDOW_CONTROLS_RIGHT_MARGIN : TOOLBAR_HORIZONTAL_MARGIN - TOOLBAR_LOCATION_FOCUS_RING_ROOM;-}-static constexpr int DOWNLOADS_POPOVER_WIDTH = 380;-static constexpr int DOWNLOADS_POPOVER_MAX_HEIGHT = 360;static constexpr int PRIVATE_SESSION_POPOVER_WIDTH = 320;-class ElidedLabel final : public QLabel {-public:- AK_ALLOC_WITH_KMALLOC;-- explicit ElidedLabel(QString text, Qt::TextElideMode elide_mode, QWidget* parent = nullptr)- : QLabel(text, parent)- , m_elide_mode(elide_mode)- {- setMinimumWidth(0);- setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);- }-- virtual QSize sizeHint() const override- {- auto size = QLabel::sizeHint();- size.setWidth(0);- return size;- }-- virtual QSize minimumSizeHint() const override- {- return { 0, QLabel::minimumSizeHint().height() };- }--protected:- virtual void paintEvent(QPaintEvent*) override- {- QPainter painter(this);- painter.setFont(font());- painter.setPen(palette().color(foregroundRole()));- painter.drawText(rect(), alignment() | Qt::TextSingleLine, fontMetrics().elidedText(text(), m_elide_mode, width()));- }--private:- Qt::TextElideMode m_elide_mode { Qt::ElideRight };-};--// A small page with the file's extension on it.-class DownloadFileIcon final : public QWidget {-public:- AK_ALLOC_WITH_KMALLOC;-- explicit DownloadFileIcon(QWidget* parent)- : QWidget(parent)- {- setFixedSize(28, 34);- }-- void set_extension(QString extension)- {- m_extension = extension.toUpper().left(4);- update();- }--private:- virtual void paintEvent(QPaintEvent*) override- {- QPainter painter(this);- painter.setRenderHint(QPainter::Antialiasing, true);- auto const& panel = Theme::panel();- painter.setBrush(panel.page);- painter.setPen(QPen(panel.separator, 1));- painter.drawRoundedRect(QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5), 4, 4);-- auto font = this->font();- font.setPixelSize(8);- font.setWeight(QFont::Bold);- painter.setFont(font);- painter.setPen(panel.muted);- painter.drawText(rect().adjusted(0, 0, 0, -4), Qt::AlignHCenter | Qt::AlignBottom, m_extension);- }-- QString m_extension;-};--class DownloadRow final : public QFrame {-public:- AK_ALLOC_WITH_KMALLOC;-- explicit DownloadRow(WebView::FileDownloader::Download const& download, QWidget* parent)- : QFrame(parent)- , m_download_id(download.id)- {- setObjectName("LadybirdDownloadRow");- setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);-- auto* row_layout = new QHBoxLayout(this);- row_layout->setContentsMargins(10, 8, 6, 8);- row_layout->setSpacing(12);-- m_file_icon = new DownloadFileIcon(this);- row_layout->addWidget(m_file_icon, 0, Qt::AlignVCenter);-- auto* details_layout = new QVBoxLayout;- details_layout->setContentsMargins(0, 0, 0, 0);- details_layout->setSpacing(5);-- m_file_name_label = new ElidedLabel({}, Qt::ElideMiddle, this);- m_file_name_label->setObjectName("LadybirdDownloadFileName");- details_layout->addWidget(m_file_name_label);-- m_progress_bar = new QProgressBar(this);- m_progress_bar->setObjectName("LadybirdDownloadProgress");- m_progress_bar->setRange(0, 1000);- m_progress_bar->setTextVisible(false);- details_layout->addWidget(m_progress_bar);-- m_status_label = new ElidedLabel({}, Qt::ElideRight, this);- m_status_label->setObjectName("LadybirdDownloadStatus");- details_layout->addWidget(m_status_label);-- row_layout->addLayout(details_layout, 1);-- auto make_action = [&](ChromeIcon icon, QString const& tooltip, auto callback) {- auto* button = new ToolbarButton(this);- button->setObjectName("LadybirdDownloadAction");- button->setAutoRaise(true);- button->setIcon(create_chrome_icon(icon, Theme::panel().body));- button->setIconSize({ 16, 16 });- button->setFixedSize(28, 28);- button->setToolTip(tooltip);- button->setAccessibleName(tooltip);- button->setFocusPolicy(Qt::TabFocus);- QObject::connect(button, &QToolButton::clicked, this, [this, callback] { callback(*this); });- row_layout->addWidget(button, 0, Qt::AlignVCenter);- return button;- };-- m_pause_button = make_action(ChromeIcon::Pause, "Pause", [](DownloadRow& row) {- if (row.m_is_paused) {- if (row.on_resume_download)- row.on_resume_download(row.m_download_id);- } else if (row.on_pause_download) {- row.on_pause_download(row.m_download_id);- }- });- m_cancel_button = make_action(ChromeIcon::Close, "Cancel", [](DownloadRow& row) {- if (row.on_cancel_download)- row.on_cancel_download(row.m_download_id);- });- m_retry_button = make_action(ChromeIcon::Reload, "Retry", [](DownloadRow& row) {- if (row.on_resume_download)- row.on_resume_download(row.m_download_id);- });-#if defined(AK_OS_MACOS)- auto show_in_folder_name = "Show in Finder";-#elif defined(AK_OS_WINDOWS)- auto show_in_folder_name = "Show in Explorer";-#else- auto show_in_folder_name = "Show in Files";-#endif- m_show_in_folder_button = make_action(ChromeIcon::Folder, show_in_folder_name, [](DownloadRow& row) {- if (row.on_show_in_folder)- row.on_show_in_folder(row.m_download_id);- });-- (void)update_download(download);- }-- u64 id() const { return m_download_id; }-- bool update_download(WebView::FileDownloader::Download const& download)- {- VERIFY(download.id == m_download_id);-- auto basename = qstring_from_ak_string(download.destination.basename());- m_file_name_label->setText(basename);- m_file_name_label->setToolTip(qstring_from_ak_string(download.destination.string()));- m_file_icon->set_extension(qstring_from_ak_string(download.destination.extension()));-- auto status_text = qstring_from_ak_string(WebView::download_status_text(download));- m_status_label->setText(status_text);- m_status_label->setToolTip(status_text);- auto failed = download.status == WebView::FileDownloader::DownloadStatus::Failed;- if (m_status_label->property("failed").toBool() != failed) {- m_status_label->setProperty("failed", failed);- m_status_label->style()->unpolish(m_status_label);- m_status_label->style()->polish(m_status_label);- }-- bool geometry_changed = false;- auto set_visible = [&](QWidget& widget, bool visible) {- if (widget.isHidden() == visible) {- widget.setVisible(visible);- geometry_changed = true;- }- };-- auto is_active = WebView::FileDownloader::status_is_active(download.status);- auto progress = download.progress();- set_visible(*m_progress_bar, is_active && progress.has_value());- if (progress.has_value())- m_progress_bar->setValue(static_cast<int>(*progress * 1000.0));-- m_is_paused = download.status == WebView::FileDownloader::DownloadStatus::Paused;- m_pause_button->setIcon(create_chrome_icon(m_is_paused ? ChromeIcon::Download : ChromeIcon::Pause, Theme::chrome_palette(*this)));- m_pause_button->setToolTip(m_is_paused ? "Resume" : "Pause");- m_pause_button->setAccessibleName(m_pause_button->toolTip());-- set_visible(*m_pause_button, download.can_resume && is_active);- set_visible(*m_cancel_button, is_active);- set_visible(*m_retry_button, failed && download.can_resume);- set_visible(*m_show_in_folder_button, download.status == WebView::FileDownloader::DownloadStatus::Completed);-- return geometry_changed;- }-- Function<void(u64)> on_cancel_download;- Function<void(u64)> on_pause_download;- Function<void(u64)> on_resume_download;- Function<void(u64)> on_show_in_folder;--private:- u64 m_download_id { 0 };- bool m_is_paused { false };- DownloadFileIcon* m_file_icon { nullptr };- ElidedLabel* m_file_name_label { nullptr };- ElidedLabel* m_status_label { nullptr };- QProgressBar* m_progress_bar { nullptr };- QToolButton* m_pause_button { nullptr };- QToolButton* m_cancel_button { nullptr };- QToolButton* m_retry_button { nullptr };- QToolButton* m_show_in_folder_button { nullptr };-};-// The page's zoom, with buttons to change it and a way back to the default.class ZoomPopover final : public Popover {public:@@ -932,176 +171,6 @@ private:QLabel* m_level { nullptr };};-class DownloadsPopover final : public Popover {-public:- AK_ALLOC_WITH_KMALLOC;-- explicit DownloadsPopover(QWidget* parent)- : Popover(parent)- {- card().setObjectName("LadybirdDownloadsPopover");- card().setFixedWidth(DOWNLOADS_POPOVER_WIDTH);-- auto* layout = new QVBoxLayout(&card());- layout->setContentsMargins(6, 12, 6, 6);- layout->setSpacing(0);-- auto* header = new QHBoxLayout;- header->setContentsMargins(10, 0, 4, 6);- auto* title = new QLabel("Downloads", this);- title->setObjectName("LadybirdDownloadsPopoverTitle");- header->addWidget(title, 1);- auto* clear_button = new QPushButton("Clear finished", this);- clear_button->setObjectName("LadybirdPopoverLink");- clear_button->setCursor(Qt::PointingHandCursor);- QObject::connect(clear_button, &QPushButton::clicked, this, [this] {- if (on_clear_finished)- on_clear_finished();- });- header->addWidget(clear_button);- layout->addLayout(header);-- m_empty_label = new QLabel("No downloads", this);- m_empty_label->setObjectName("LadybirdDownloadsEmpty");- m_empty_label->setAlignment(Qt::AlignCenter);- m_empty_label->setMinimumHeight(80);- layout->addWidget(m_empty_label);-- m_scroll_area = new QScrollArea(this);- m_scroll_area->setObjectName("LadybirdDownloadsPopoverScroll");- m_scroll_area->setFrameShape(QFrame::NoFrame);- m_scroll_area->setWidgetResizable(true);- m_scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);- m_scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);-- m_rows_widget = new QWidget(m_scroll_area);- m_rows_widget->setObjectName("LadybirdDownloadsPopoverRows");- m_rows_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);- m_rows_layout = new QVBoxLayout(m_rows_widget);- m_rows_layout->setContentsMargins(0, 0, 0, 0);- m_rows_layout->setSpacing(0);- m_scroll_area->setWidget(m_rows_widget);- layout->addWidget(m_scroll_area);-- auto* separator = new QFrame(this);- separator->setObjectName("LadybirdPopoverSeparator");- separator->setFixedHeight(1);- auto* separator_row = new QHBoxLayout;- separator_row->setContentsMargins(10, 4, 10, 4);- separator_row->addWidget(separator);- layout->addLayout(separator_row);-- auto* show_all_button = new QPushButton("Show all downloads", this);- show_all_button->setObjectName("LadybirdPopoverRow");- QObject::connect(show_all_button, &QPushButton::clicked, this, [this] {- if (on_open_all_downloads)- on_open_all_downloads();- });- layout->addWidget(show_all_button);- }-- void update_chrome_style(QPalette const& palette)- {- setPalette(palette);- setStyleSheet(ChromeStyle::downloads_popover_style_sheet(palette));- }-- bool set_downloads(ReadonlySpan<WebView::FileDownloader::Download> downloads)- {- bool geometry_changed = false;- auto visible_downloads = WebView::recent_downloads_for_popover(downloads);-- if (!rows_match(visible_downloads)) {- rebuild_rows(visible_downloads);- geometry_changed = true;- } else {- for (size_t i = 0; i < visible_downloads.size(); ++i) {- if (m_download_rows[i]->update_download(*visible_downloads[i]))- geometry_changed = true;- }- }-- auto is_empty = visible_downloads.is_empty();- auto empty_label_is_visible = !m_empty_label->isHidden();- if (empty_label_is_visible != is_empty) {- m_empty_label->setVisible(is_empty);- geometry_changed = true;- }- auto scroll_area_is_visible = !m_scroll_area->isHidden();- if (scroll_area_is_visible == is_empty) {- m_scroll_area->setVisible(!is_empty);- geometry_changed = true;- }-- return geometry_changed;- }-- Function<void(u64)> on_cancel_download;- Function<void(u64)> on_pause_download;- Function<void(u64)> on_resume_download;- Function<void(u64)> on_show_in_folder;- Function<void()> on_clear_finished;- Function<void()> on_open_all_downloads;--private:- bool rows_match(Vector<WebView::FileDownloader::Download const*> const& downloads) const- {- if (m_download_rows.size() != downloads.size())- return false;-- for (size_t i = 0; i < downloads.size(); ++i) {- if (m_download_rows[i]->id() != downloads[i]->id)- return false;- }-- return true;- }-- void rebuild_rows(Vector<WebView::FileDownloader::Download const*> const& downloads)- {- clear_rows();- m_download_rows.ensure_capacity(downloads.size());-- for (auto* download : downloads) {- auto* row = new DownloadRow(*download, m_rows_widget);- row->on_cancel_download = [this](u64 id) {- if (on_cancel_download)- on_cancel_download(id);- };- row->on_pause_download = [this](u64 id) {- if (on_pause_download)- on_pause_download(id);- };- row->on_resume_download = [this](u64 id) {- if (on_resume_download)- on_resume_download(id);- };- row->on_show_in_folder = [this](u64 id) {- if (on_show_in_folder)- on_show_in_folder(id);- };- m_rows_layout->addWidget(row);- m_download_rows.append(row);- }- }-- void clear_rows()- {- while (auto* item = m_rows_layout->takeAt(0)) {- if (auto* widget = item->widget())- delete widget;- delete item;- }- m_download_rows.clear();- }-- QLabel* m_empty_label { nullptr };- QScrollArea* m_scroll_area { nullptr };- QWidget* m_rows_widget { nullptr };- QVBoxLayout* m_rows_layout { nullptr };- Vector<DownloadRow*> m_download_rows;-};-Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversable): QWidget(window), m_window(window)@@ -1127,18 +196,11 @@ void Tab::create_tab_shell()tab_layout->setSpacing(0);tab_layout->setContentsMargins(0, 0, 0, 0);- // TabWidget keeps every tab's toolbar container in a stack beside its pages, so even a tab that hasn't loaded needs- // one. initialize() fills it.- m_toolbar_container = new QWidget(this);- m_toolbar_container->setObjectName("LadybirdToolbarContainer");- m_toolbar_container->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);-create_tab_context_menu();}void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable){- auto& application = WebView::Application::the();auto* tab_layout = static_cast<QBoxLayout*>(layout());auto view_initial_state = WebContentViewInitialState {@@ -1159,29 +221,9 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)m_view->add_floating_surface(*m_find_in_page);m_find_in_page->setVisible(false);- m_toolbar = new QWidget(this);- m_toolbar->setObjectName("LadybirdNavigationToolbar");- m_toolbar->setFixedHeight(browser_chrome_layout_policy().toolbar_height);- m_toolbar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);-- auto* toolbar_container_layout = new QVBoxLayout(m_toolbar_container);- toolbar_container_layout->setSpacing(0);- // NB: The container draws the line under the toolbar along its bottom edge, below the toolbar's full height.- toolbar_container_layout->setContentsMargins(0, 0, 0, 1);-- auto* toolbar_layout = new QHBoxLayout(m_toolbar);- toolbar_layout->setSpacing(TOOLBAR_SPACING);- toolbar_layout->setContentsMargins(TOOLBAR_HORIZONTAL_MARGIN, 0, toolbar_right_margin(false), 0);-+ // The window's toolbar holds the address field while it shows this tab.m_location_edit = new LocationEdit(this, m_window->is_private());-- m_downloads_button = new DownloadsButton(m_toolbar);- m_downloads_button->setIcon(create_chrome_icon(ChromeIcon::Download, Theme::chrome_palette(*this)));- QObject::connect(m_downloads_button, &QToolButton::clicked, this, [this] {- // NB: The button only stays checked while the list is open, which showing it sees to.- m_downloads_button->setChecked(m_downloads_popover && m_downloads_popover->isVisible());- show_downloads_popover();- });+ m_location_edit->hide();m_bookmarks_bar = new BookmarksBar(this);m_bookmarks_bar_container = new QWidget(this);@@ -1216,7 +258,7 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)m_bookmarks_bar_hover_collapse_timer->setInterval(BOOKMARKS_BAR_HOVER_COLLAPSE_POLL_INTERVAL_MS);connect(m_bookmarks_bar_hover_collapse_timer, &QTimer::timeout, this, &Tab::update_bookmarks_bar_hover);- m_toolbar_container->installEventFilter(this);+ m_window->toolbar().installEventFilter(this);m_bookmarks_bar_container->installEventFilter(this);m_bookmarks_bar->installEventFilter(this);m_view->installEventFilter(this);@@ -1235,22 +277,9 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)focus_location_editor_action->setShortcuts({ QKeySequence("Ctrl+L"), QKeySequence("Alt+D") });addAction(focus_location_editor_action);- toolbar_container_layout->addWidget(m_toolbar);tab_layout->addWidget(m_view);update_bookmarks_bar_visibility();- m_navigate_back_action = create_application_action(*this, view().navigate_back_action());- m_navigate_forward_action = create_application_action(*this, view().navigate_forward_action());- m_reload_action = create_application_action(*this, application.reload_action());- m_reload_bypassing_cache_action = create_application_action(*this, application.reload_bypassing_cache_action());- addAction(m_reload_bypassing_cache_action);- m_stop_action = new QAction("Stop", this);- m_stop_action->setToolTip("Stop loading this page");- QObject::connect(m_stop_action, &QAction::triggered, this, [this] {- view().stop_loading();- });- m_open_downloads_page_action = create_application_action(*this, application.open_downloads_page_action());-// NB: A page can block many requests in a burst while it loads, so the open popover catches up with them at most// ten times a second rather than for each one.m_content_blocking_update_timer = new QTimer(this);@@ -1265,110 +294,20 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)m_content_blocking_update_timer->start();};- m_toolbar_window_controls_separator = new QWidget(m_toolbar);- m_toolbar_window_controls_separator->setObjectName("LadybirdToolbarWindowControlsSeparator");- m_toolbar_window_controls_separator->setFixedSize(1, 22);- m_toolbar_window_controls_separator->setAttribute(Qt::WA_TransparentForMouseEvents);-- if constexpr (use_native_macos_window_controls()) {- auto const native_controls_width = NATIVE_MACOS_WINDOW_CONTROLS_WIDTH + NATIVE_MACOS_WINDOW_CONTROLS_GAP;- m_toolbar_window_controls = create_window_controls_spacer(*m_toolbar, "LadybirdToolbarWindowControls", { native_controls_width, 38 });- } else {- auto window_control_buttons = create_window_control_buttons(*m_toolbar, "LadybirdToolbarWindowControls", { 16, 16 }, { 38, 38 });- m_toolbar_window_controls = window_control_buttons.container;- m_minimize_window_button = window_control_buttons.minimize;- m_maximize_window_button = window_control_buttons.maximize;- m_close_window_button = window_control_buttons.close;-- QObject::connect(m_minimize_window_button, &QToolButton::clicked, this, [this] {- m_window->showMinimized();- });- QObject::connect(m_maximize_window_button, &QToolButton::clicked, this, [this] {- if (m_window->isMaximized())- m_window->showNormal();- else- m_window->showMaximized();- });- QObject::connect(m_close_window_button, &QToolButton::clicked, this, [this] {- m_window->close();- });- }-- recreate_toolbar_icons();-- auto* navigation_button_cluster = new QWidget(m_toolbar);- navigation_button_cluster->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);- auto* navigation_button_layout = new QHBoxLayout(navigation_button_cluster);- navigation_button_layout->setSpacing(TOOLBAR_SPACING);- navigation_button_layout->setContentsMargins(0, 0, 0, 0);-- navigation_button_layout->addWidget(create_navigation_history_toolbar_button(*navigation_button_cluster, *m_navigate_back_action, -1));- navigation_button_layout->addWidget(create_navigation_history_toolbar_button(*navigation_button_cluster, *m_navigate_forward_action, 1));- m_reload_button = create_toolbar_button<ReloadButton>(*navigation_button_cluster, *m_reload_action);- m_reload_button->set_actions(*m_reload_action, *m_reload_bypassing_cache_action, *m_stop_action);- m_reload_button->on_open_in_new_tab = [this](auto activate_tab) {- m_window->duplicate_tab(*this, activate_tab);- };- navigation_button_layout->addWidget(m_reload_button);-- auto* location_edit_container = new QWidget(m_toolbar);- location_edit_container->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);- auto* location_edit_layout = new QHBoxLayout(location_edit_container);- location_edit_layout->setSpacing(0);- // NB: The field reaches the end of the toolbar, leaving room for its focus ring.- location_edit_layout->setContentsMargins(TOOLBAR_LOCATION_EDIT_MARGIN, 0, TOOLBAR_LOCATION_FOCUS_RING_ROOM, 0);-// The star bookmarks the page, then shows the bookmark to name it or file it, rather than toggling it.auto* bookmark_action = create_application_action(*m_location_edit, view().toggle_bookmark_action());QObject::disconnect(bookmark_action, &QAction::triggered, nullptr, nullptr);QObject::connect(bookmark_action, &QAction::triggered, this, [this] { show_bookmark_popover(); });m_location_edit->set_trailing_action(bookmark_action);m_location_edit->set_zoom_action(create_application_action(*m_location_edit, view().reset_zoom_action(), IncludeActionIcon::No));- location_edit_layout->addWidget(m_location_edit);- m_performance_monitor = new PerformanceMonitorWidget(m_toolbar);- (void)WebView::TabPerformanceMonitor::the();- m_performance_monitor->setVisible(WebView::Application::settings().config_variable_as_bool(WebView::ConfigVariableID::ShowTabPerformanceMonitor));view().on_performance_stats = [this](WebView::TabPerformanceStats const& stats) {- static_cast<PerformanceMonitorWidget*>(m_performance_monitor)->set_stats(stats);+ m_performance_stats = stats;+ emit performance_stats_changed();};- if constexpr (use_native_macos_window_controls())- toolbar_layout->addWidget(m_toolbar_window_controls, 0, Qt::AlignVCenter);-- m_toolbar_sidebar_slot = new QWidget(m_toolbar);- m_toolbar_sidebar_slot->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);- auto* sidebar_slot_layout = new QHBoxLayout(m_toolbar_sidebar_slot);- // The Ladybird button keeps as clear of the traffic lights as on the tab strip.- auto sidebar_slot_leading_margin = 0;- if constexpr (use_native_macos_window_controls())- sidebar_slot_leading_margin = NATIVE_MACOS_WINDOW_CONTROLS_INSET + NATIVE_MACOS_WINDOW_CONTROLS_WIDTH + LADYBIRD_BUTTON_TRAFFIC_LIGHTS_GAP - (TOOLBAR_HORIZONTAL_MARGIN + NATIVE_MACOS_WINDOW_CONTROLS_WIDTH + NATIVE_MACOS_WINDOW_CONTROLS_GAP + TOOLBAR_SPACING);- sidebar_slot_layout->setContentsMargins(sidebar_slot_leading_margin, 0, 0, 0);- sidebar_slot_layout->setSpacing(TOOLBAR_SPACING);- // NB: The separator sits where the open sidebar's edge does, so the toolbar's contents keep their place as it toggles.- sidebar_slot_layout->addSpacing(SIDEBAR_SLOT_SEPARATOR_MARGIN - 1);- auto* sidebar_slot_separator = new QWidget(m_toolbar_sidebar_slot);- sidebar_slot_separator->setObjectName("LadybirdToolbarWindowControlsSeparator");- sidebar_slot_separator->setFixedSize(1, 16);- sidebar_slot_separator->setAttribute(Qt::WA_TransparentForMouseEvents);- sidebar_slot_layout->addWidget(sidebar_slot_separator, 0, Qt::AlignVCenter);- sidebar_slot_layout->addSpacing(SIDEBAR_SLOT_SEPARATOR_MARGIN);- m_toolbar_sidebar_slot->hide();- toolbar_layout->addWidget(m_toolbar_sidebar_slot, 0, Qt::AlignVCenter);-- toolbar_layout->addWidget(navigation_button_cluster, 0, Qt::AlignVCenter);- toolbar_layout->addWidget(location_edit_container, 1);- toolbar_layout->addWidget(m_downloads_button, 0, Qt::AlignVCenter);- toolbar_layout->addWidget(m_performance_monitor, 0, Qt::AlignVCenter);-- if constexpr (!use_native_macos_window_controls()) {- toolbar_layout->addWidget(m_toolbar_window_controls_separator, 0, Qt::AlignVCenter);- toolbar_layout->addWidget(m_toolbar_window_controls, 0, Qt::AlignVCenter);- }-+ recreate_location_edit_icons();update_chrome_style();- update_downloads_button();- set_toolbar_window_controls_visible(false);m_location_edit->on_site_settings_requested = [this] {show_site_settings_popover();@@ -2045,7 +984,11 @@ void Tab::create_tab_context_menu()});}-Tab::~Tab() = default;+// The window's toolbar can hold the address field while the tab goes away with the window.+Tab::~Tab()+{+ delete m_location_edit;+}void Tab::park_location_editing(){@@ -2062,16 +1005,9 @@ void Tab::ensure_loaded()// Until initialize() has built the view, url() reads the deferred tab's URL, so the deferred tab stays until then.auto deferred_url = m_deferred_tab->url;- // initialize() hides the toolbar's window controls, which overwrites the visibility TabWidget last asked for.- auto toolbar_window_controls_visible = m_toolbar_window_controls_visible;initialize({});m_deferred_tab.clear();- // TabWidget pushed these while the tab had no toolbar to apply them to.- set_vertical_tabs_enabled(m_vertical_tabs_enabled);- recreate_toolbar_icons();- set_toolbar_window_controls_visible(toolbar_window_controls_visible);-if (m_session_tab_id.has_value())view().set_session_tab_id(*m_session_tab_id);emit loaded();@@ -2137,8 +1073,11 @@ void Tab::set_window(BrowserWindow& window)return;}+ // The bookmarks bar expands as the pointer comes over the toolbar of the window the tab is in.+ m_window->toolbar().removeEventFilter(this);m_window = &window;- recreate_toolbar_icons();+ m_window->toolbar().installEventFilter(this);+ recreate_location_edit_icons();// The tab's pages hold the geometry of the window that hosted them until now.m_view->set_window_position({ window.x(), window.y() });@@ -2151,104 +1090,6 @@ void Tab::set_pinned(bool pinned)m_pin_tab_action->setText(pinned ? "Un&pin Tab" : "&Pin Tab");}-void Tab::set_vertical_tabs_enabled(bool enabled)-{- m_vertical_tabs_enabled = enabled;-- // The page's separator supplies the bottom border, leaving the toolbar open above the sidebar.- m_toolbar_container->setProperty("verticalTabs", enabled);- m_toolbar_container->style()->unpolish(m_toolbar_container);- m_toolbar_container->style()->polish(m_toolbar_container);- m_toolbar_container->update();-- if (!is_loaded())- return;-- m_toolbar->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);- update_bookmarks_bar_visibility();- m_toolbar->layout()->invalidate();-}--void Tab::set_toolbar_container_in_tab_layout(bool in_tab_layout)-{- auto* tab_layout = static_cast<QBoxLayout*>(layout());- auto index = tab_layout->indexOf(m_toolbar_container);-- if (in_tab_layout) {- if (index == -1) {- m_toolbar_container->setParent(this);- tab_layout->insertWidget(0, m_toolbar_container);- }- m_toolbar_container->show();- return;- }-- if (index != -1) {- tab_layout->removeWidget(m_toolbar_container);- m_toolbar_container->hide();- }-}--int Tab::toolbar_height(bool)-{- return browser_chrome_layout_policy().toolbar_height;-}--void Tab::set_toolbar_window_controls_visible(bool visible)-{- m_toolbar_window_controls_visible = visible;- if (!is_loaded())- return;-- auto const has_native_window_controls = use_native_macos_window_controls() && visible;- auto const has_custom_window_controls = !use_native_macos_window_controls() && visible;-- m_toolbar_window_controls_separator->setVisible(has_custom_window_controls);- m_toolbar_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);- m_toolbar->layout()->invalidate();-}--// How far the navigation buttons sit past the toolbar's leading margin beside the collapsed sidebar, where the Ladybird-// button and the sidebar's toggle come before them.-int Tab::toolbar_lead_beside_collapsed_sidebar(QWidget const& ladybird_buttons, QSize sidebar_toggle_size) const-{- auto toolbar_spacing = m_toolbar->layout()->spacing();- auto lead = 0;- if constexpr (use_native_macos_window_controls())- lead += m_toolbar_window_controls->width() + toolbar_spacing;-- auto slot_width = m_toolbar_sidebar_slot->layout()->sizeHint().width();- if (ladybird_buttons.parentWidget() != m_toolbar_sidebar_slot)- slot_width += ladybird_buttons.sizeHint().width() + sidebar_toggle_size.width() + (2 * m_toolbar_sidebar_slot->layout()->spacing());- return lead + slot_width + toolbar_spacing;-}--// While the sidebar opens or closes, the toolbar's contents keep clear of where the collapsed sidebar's buttons sit.-void Tab::set_toolbar_leading_room(int room)-{- auto margins = m_toolbar->layout()->contentsMargins();- if (margins.left() == TOOLBAR_HORIZONTAL_MARGIN + room)- return;- margins.setLeft(TOOLBAR_HORIZONTAL_MARGIN + room);- 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();-}--void Tab::update_window_control_icons()-{- if (!m_minimize_window_button || !m_maximize_window_button || !m_close_window_button)- return;-- auto is_maximized = m_window->isMaximized();- m_minimize_window_button->setIcon(create_chrome_icon(ChromeIcon::WindowMinimize, Theme::chrome_palette(*this)));- m_maximize_window_button->setIcon(create_chrome_icon(is_maximized ? ChromeIcon::WindowRestore : ChromeIcon::WindowMaximize, Theme::chrome_palette(*this)));- m_maximize_window_button->setToolTip(is_maximized ? "Restore" : "Maximize");- m_close_window_button->setIcon(create_chrome_icon(ChromeIcon::WindowClose, Theme::chrome_palette(*this)));-}-void Tab::navigate(URL::URL const& url){view().load_from_user_input(url);@@ -2345,13 +1186,10 @@ void Tab::update_tab_title()void Tab::tab_settings_changed(){- auto const& tab_settings = WebView::Application::settings().tab_settings();- set_vertical_tabs_enabled(tab_settings.vertical_tabs_enabled);-if (!is_loaded())return;- recreate_toolbar_icons();+ recreate_location_edit_icons();}void Tab::config_variable_changed(WebView::ConfigVariableID variable)@@ -2359,8 +1197,6 @@ void Tab::config_variable_changed(WebView::ConfigVariableID variable)if (!is_loaded())return;- if (variable == WebView::ConfigVariableID::ShowTabPerformanceMonitor)- m_performance_monitor->setVisible(WebView::Application::settings().config_variable_as_bool(variable));if (variable == WebView::ConfigVariableID::ShowWebContentProcessIDInTabTitle)update_tab_title();}@@ -2395,8 +1231,6 @@ void Tab::set_loading(bool is_loading)elsem_load_finished_clock.start();emit loading_changed();-- m_reload_button->set_loading(m_is_loading);}void Tab::update_tab_icon()@@ -2558,11 +1392,11 @@ bool Tab::event(QEvent* event){// An inactive window's toolbar icons drop to the muted color.if ((event->type() == QEvent::WindowActivate || event->type() == QEvent::WindowDeactivate) && is_loaded())- recreate_toolbar_icons();+ recreate_location_edit_icons();if (event->type() == QEvent::PaletteChange && is_loaded()) {update_chrome_style();- recreate_toolbar_icons();+ recreate_location_edit_icons();if (m_is_loading)update_tab_icon();return QWidget::event(event);@@ -2594,12 +1428,12 @@ bool Tab::eventFilter(QObject* watched, QEvent* event)if (watched == m_bookmarks_bar && event->type() == QEvent::LayoutRequest)update_bookmarks_bar_geometry();- if (watched == m_toolbar_container || watched == m_bookmarks_bar_container) {+ if (watched == &m_window->toolbar() || watched == m_bookmarks_bar_container) {if (event->type() == QEvent::Enter || event->type() == QEvent::Show) {update_bookmarks_bar_hover();} else if (event->type() == QEvent::Leave) {QTimer::singleShot(0, this, &Tab::update_bookmarks_bar_hover);- } else if (event->type() == QEvent::Hide && watched == m_toolbar_container) {+ } else if (event->type() == QEvent::Hide && watched == &m_window->toolbar()) {set_bookmarks_bar_hover_expanded(false);}}@@ -2612,177 +1446,23 @@ void Tab::update_chrome_style()if (m_is_updating_chrome_style)return;m_is_updating_chrome_style = true;- m_toolbar_container->setStyleSheet(ChromeStyle::toolbar_container_style_sheet(Theme::chrome_palette(*this)));m_bookmarks_bar->update_chrome_style();auto hover_surface = ChromeStyle::style_sheet_color(ChromeStyle::chrome_surface(Theme::chrome_palette(*this)));auto hover_border = ChromeStyle::style_sheet_color(ChromeStyle::chrome_border(Theme::chrome_palette(*this)));auto hover_text = ChromeStyle::style_sheet_color(ChromeStyle::chrome_text(Theme::chrome_palette(*this)));m_hover_label->setStyleSheet(qformatted("background: {}; color: {}; border: 1px solid {}; border-top-right-radius: {}px;",hover_surface, hover_text, hover_border, HOVER_TEXT_CORNER_RADIUS));- if (m_downloads_popover)- m_downloads_popover->update_chrome_style(palette());m_is_updating_chrome_style = false;}-void Tab::recreate_toolbar_icons()+void Tab::recreate_location_edit_icons(){- auto activity = ChromeStyle::window_activity(*this);- m_navigate_back_action->setIcon(create_chrome_icon(ChromeIcon::Back, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));- m_navigate_forward_action->setIcon(create_chrome_icon(ChromeIcon::Forward, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));- m_reload_action->setIcon(create_chrome_icon(ChromeIcon::Reload, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));- m_stop_action->setIcon(create_chrome_icon(ChromeIcon::Stop, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));- m_downloads_button->setIcon(create_chrome_icon(ChromeIcon::Download, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));- update_window_control_icons();-if (auto* action = m_location_edit->trailing_action()) {auto engaged = view().toggle_bookmark_action().engaged();- action->setIcon(create_chrome_icon(engaged ? ChromeIcon::StarFilled : ChromeIcon::Star, Theme::chrome_palette(*this), activity));- }-}--void Tab::update_downloads_button()-{- auto downloads = WebView::Application::the().file_downloader().downloads_of_session(m_window->is_private());- auto button_state = WebView::downloads_button_state(downloads);-- if (!button_state.has_downloads && m_downloads_popover)- m_downloads_popover->close();-- // The arrow drops into place as the button appears, and bobs once as a download finishes.- auto was_visible = m_downloads_button->isVisible();- m_downloads_button->setVisible(button_state.has_downloads);- if (!was_visible && button_state.has_downloads)- m_downloads_button->play_icon_motion(ToolbarButton::IconMotion::DropIn);- else if (button_state.completed_download_count > m_completed_download_count)- m_downloads_button->play_icon_motion(ToolbarButton::IconMotion::Bob);- m_completed_download_count = button_state.completed_download_count;- m_downloads_button->set_progress(button_state.active_download_count > 0, button_state.active_download_progress);- m_window->set_failed_download_count(button_state.failed_download_count);- m_downloads_button->set_needs_attention(m_window->has_unseen_failed_downloads());-- auto tooltip = qstring_from_ak_string(button_state.tooltip);- if (m_downloads_button->toolTip() != tooltip) {- m_downloads_button->setToolTip(tooltip);- m_downloads_button->setAccessibleName(tooltip);+ action->setIcon(create_chrome_icon(engaged ? ChromeIcon::StarFilled : ChromeIcon::Star, Theme::chrome_palette(*this), ChromeStyle::window_activity(*this)));}}-void Tab::update_downloads_popover()-{- if (!m_downloads_popover || !m_downloads_popover->isVisible())- return;-- if (m_downloads_popover->set_downloads(WebView::Application::the().file_downloader().downloads_of_session(m_window->is_private())))- position_downloads_popover();-}--// The downloads hang from the downloads button. Without any, the downloads page opens instead.-void Tab::show_downloads_popover()-{- auto downloads = WebView::Application::the().file_downloader().downloads_of_session(m_window->is_private());- if (!WebView::downloads_button_state(downloads).has_downloads) {- m_open_downloads_page_action->trigger();- return;- }- m_window->acknowledge_failed_downloads();-- if (!m_downloads_popover) {- m_downloads_popover = new DownloadsPopover(this);- m_downloads_popover->on_visibility_change = [this](bool visible) {- m_downloads_button->setChecked(visible);- };- m_downloads_popover->on_cancel_download = [this](u64 id) {- auto& file_downloader = WebView::Application::the().file_downloader();- if (auto download = file_downloader.download(id); download.has_value() && WebView::FileDownloader::status_is_active(download->status))- file_downloader.cancel_download(id);- update_downloads_popover();- };- m_downloads_popover->on_pause_download = [this](u64 id) {- WebView::Application::the().file_downloader().pause_download(id);- update_downloads_popover();- };- m_downloads_popover->on_resume_download = [this](u64 id) {- WebView::Application::the().file_downloader().resume_download(id);- update_downloads_popover();- };- m_downloads_popover->on_show_in_folder = [](u64 id) {- if (auto download = WebView::Application::the().file_downloader().download(id); download.has_value())- WebView::Application::the().show_download_in_folder(*download);- };- m_downloads_popover->on_clear_finished = [this] {- (void)WebView::Application::the().file_downloader().prune_inactive_downloads(m_window->is_private());- update_downloads_popover();- };- m_downloads_popover->on_open_all_downloads = [this] {- if (m_downloads_popover)- m_downloads_popover->close();- m_open_downloads_page_action->trigger();- };- }-- m_downloads_popover->update_chrome_style(palette());- (void)m_downloads_popover->set_downloads(WebView::Application::the().file_downloader().downloads_of_session(m_window->is_private()));- position_downloads_popover();- m_downloads_popover->show();- position_downloads_popover();- m_downloads_popover->raise();-}--void Tab::position_downloads_popover()-{- if (!m_downloads_popover)- return;-- m_downloads_popover->adjustSize();- auto& card = m_downloads_popover->card();- auto size = card.sizeHint();- size.setWidth(DOWNLOADS_POPOVER_WIDTH);- if (size.height() > DOWNLOADS_POPOVER_MAX_HEIGHT)- size.setHeight(DOWNLOADS_POPOVER_MAX_HEIGHT);- card.setFixedSize(size);- m_downloads_popover->adjustSize();-- move_popover_below(*m_downloads_popover, *m_downloads_button, PopoverAlignment::Trailing);-}--void Tab::download_added(WebView::FileDownloader::Download const& download)-{- if (!is_loaded())- return;-- // NB: A window lists the downloads of its own browsing session only.- if (download.is_private != m_window->is_private())- return;- update_downloads_button();- if (Application::the().active_tab() == this && m_window->isActiveWindow()) {- show_downloads_popover();- return;- }- update_downloads_popover();-}--void Tab::download_updated(WebView::FileDownloader::Download const& download)-{- if (!is_loaded())- return;-- if (download.is_private != m_window->is_private())- return;- update_downloads_button();- update_downloads_popover();-}--void Tab::download_removed(u64, WebView::IsPrivate is_private)-{- if (!is_loaded())- return;-- if (is_private != m_window->is_private())- return;- update_downloads_button();- update_downloads_popover();-}-void Tab::show_zoom_popover(){if (!m_zoom_popover) {@@ -2841,7 +1521,7 @@ void Tab::show_bookmark_popover()void Tab::close_popovers(){- for (auto* popover : { static_cast<QWidget*>(m_downloads_popover), static_cast<QWidget*>(m_private_session_popover), static_cast<QWidget*>(m_site_settings_popover), static_cast<QWidget*>(m_zoom_popover), static_cast<QWidget*>(m_bookmark_popover) }) {+ for (auto* popover : { static_cast<QWidget*>(m_private_session_popover), static_cast<QWidget*>(m_site_settings_popover), static_cast<QWidget*>(m_zoom_popover), static_cast<QWidget*>(m_bookmark_popover) }) {if (popover)popover->close();}@@ -2978,17 +1658,14 @@ void Tab::find_next()void Tab::update_bookmarks_bar_visibility(){auto show_bookmarks_bar = Application::settings().appearance().show_bookmarks_bar;- auto* toolbar_layout = static_cast<QBoxLayout*>(m_toolbar_container->layout());if (show_bookmarks_bar == WebView::ShowBookmarksBar::OnHover) {// Hovering must not resize the page or move the sidebar. Keep the bar out of both layouts and overlay the view.- toolbar_layout->removeWidget(m_bookmarks_bar_container);static_cast<QBoxLayout*>(layout())->removeWidget(m_bookmarks_bar_container);if (m_bookmarks_bar_container->parentWidget() != this)m_bookmarks_bar_container->setParent(this);} else if (auto* tab_layout = static_cast<QBoxLayout*>(layout()); tab_layout->indexOf(m_bookmarks_bar_container) == -1) {// An always-visible bar sits above the page, so that it is beside a collapsed sidebar rather than above it.- toolbar_layout->removeWidget(m_bookmarks_bar_container);m_bookmarks_bar_container->setParent(this);tab_layout->insertWidget(tab_layout->indexOf(m_view), m_bookmarks_bar_container);}@@ -3008,7 +1685,7 @@ bool Tab::has_bookmarks_bar_in_page() constbool Tab::can_expand_bookmarks_bar_on_hover() const{return Application::settings().appearance().show_bookmarks_bar == WebView::ShowBookmarksBar::OnHover- && isVisible() && m_toolbar->isVisible() && !m_window->isFullScreen()+ && isVisible() && m_window->toolbar().isVisible() && !m_window->isFullScreen()&& !m_location_edit->property("autocompleteOpen").toBool();}@@ -3029,7 +1706,7 @@ bool Tab::cursor_is_over_bookmarks_bar_hover_region() constauto contains_cursor = [&](QWidget const& widget) {return widget.isVisible() && QRect { widget.mapToGlobal(QPoint {}), widget.size() }.contains(cursor_position);};- return contains_cursor(*m_toolbar_container) || contains_cursor(*m_bookmarks_bar_container);+ return contains_cursor(m_window->toolbar()) || contains_cursor(*m_bookmarks_bar_container);}void Tab::update_bookmarks_bar_hover()@@ -3100,13 +1777,8 @@ void Tab::update_bookmarks_bar_geometry()ensure_appkit_window_is_exposed(*m_bookmarks_bar_container);#endif- // NB: The bar below the toolbar draws the line under both.- if (m_toolbar_container->property("bookmarksBarBelow").toBool() != visible) {- m_toolbar_container->setProperty("bookmarksBarBelow", visible);- m_toolbar_container->style()->unpolish(m_toolbar_container);- m_toolbar_container->style()->polish(m_toolbar_container);- m_toolbar_container->update();- }+ if (m_window->current_tab() == this)+ m_window->toolbar().set_bookmarks_bar_below(visible);}void Tab::request_close()diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.hindex 82ceebb3626..8f76c16ded4 100644--- a/UI/Qt/Tab.h+++ b/UI/Qt/Tab.h@@ -20,6 +20,7 @@#include <LibWebView/FileDownloader.h>#include <LibWebView/SessionStore.h>#include <LibWebView/Settings.h>+#include <LibWebView/TabPerformanceStats.h>#include <UI/Qt/BookmarksBar.h>#include <UI/Qt/FindInPageWidget.h>#include <UI/Qt/LocationEdit.h>@@ -47,13 +48,9 @@ namespace Ladybird {class BookmarkPopover;class BrowserWindow;enum class ChromeIcon;-class DownloadsButton;-class DownloadsPopover;-class ReloadButton;class RestoreSessionCard;class JavaScriptDialog;class MessagePopover;-class WindowControlButton;class HyperlinkLabel final : public QLabel {Q_OBJECT@@ -89,7 +86,6 @@ class Popover;class SiteSettingsPopover;class Tab final: public QWidget- , public WebView::FileDownloaderObserver, public WebView::SettingsObserver {Q_OBJECT@@ -162,19 +158,10 @@ public:QMenu* context_menu() const { return m_context_menu; }- QWidget* toolbar_container() const { return m_toolbar_container; }+ LocationEdit& location_edit() { return *m_location_edit; }+ WebView::TabPerformanceStats const& performance_stats() const { return m_performance_stats; }- void set_vertical_tabs_enabled(bool);void set_window(BrowserWindow&);- void set_toolbar_container_in_tab_layout(bool);- void set_toolbar_window_controls_visible(bool);- // While the sidebar is collapsed, the toolbar holds the Ladybird button and the sidebar's toggle, after the window- // controls, here.- QWidget& toolbar_sidebar_slot() { return *m_toolbar_sidebar_slot; }- int toolbar_lead_beside_collapsed_sidebar(QWidget const& ladybird_buttons, QSize sidebar_toggle_size) const;- void set_toolbar_leading_room(int);- static int toolbar_height(bool window_controls_visible);- void update_window_control_icons();void update_hover_label();void enter_report_review(Tab* tab_to_activate_on_close, Optional<QString> title = {});@@ -191,8 +178,6 @@ public:FadeIn,Immediate,};- void show_downloads_popover();- void update_downloads_button();void show_private_session_popover();void show_site_settings_popover();void refresh_site_settings_popover();@@ -210,6 +195,7 @@ signals:void favicon_changed(int id, QIcon const&);void audibility_changed(int id, Audio::Audibility);void loading_changed();+ void performance_stats_changed();void loaded();private:@@ -227,13 +213,10 @@ private:virtual void tab_settings_changed() override;virtual void config_variable_changed(WebView::ConfigVariableID) override;- void recreate_toolbar_icons();- QToolButton* create_navigation_history_toolbar_button(QWidget& parent, QAction&, int direction);+ void recreate_location_edit_icons();void update_chrome_style();void update_tab_title();void leave_report_review();- void update_downloads_popover();- void position_downloads_popover();void position_private_session_popover();void hide_hover_label();QImage render_hover_text(QString const&) const;@@ -248,20 +231,6 @@ private:void set_bookmarks_bar_hover_expanded(bool);void update_bookmarks_bar_geometry();- virtual void download_added(WebView::FileDownloader::Download const&) override;- virtual void download_updated(WebView::FileDownloader::Download const&) override;- virtual void download_removed(u64, WebView::IsPrivate) override;-- QWidget* m_toolbar_container { nullptr };- QWidget* m_toolbar { nullptr };- QWidget* m_performance_monitor { nullptr };- QWidget* m_toolbar_window_controls_separator { nullptr };- QWidget* m_toolbar_sidebar_slot { nullptr };- QWidget* m_toolbar_window_controls { nullptr };- WindowControlButton* m_minimize_window_button { nullptr };- WindowControlButton* m_maximize_window_button { nullptr };- WindowControlButton* m_close_window_button { nullptr };-BookmarksBar* m_bookmarks_bar { nullptr };QWidget* m_bookmarks_bar_container { nullptr };QTimer* m_bookmarks_bar_hover_expand_timer { nullptr };@@ -270,7 +239,6 @@ private:bool m_bookmarks_bar_hover_expanded { false };qreal m_bookmarks_bar_hover_expand_progress { 0.0 };- QPointer<DownloadsPopover> m_downloads_popover;QPointer<MessagePopover> m_private_session_popover;QPointer<SiteSettingsPopover> m_site_settings_popover;// The site the open popover shows, which its changes apply to whatever the tab shows by then.@@ -279,7 +247,7 @@ private:QPointer<Popover> m_zoom_popover;QPointer<BookmarkPopover> m_bookmark_popover;QPointer<Tab> m_tab_to_activate_on_close;- LocationEdit* m_location_edit { nullptr };+ QPointer<LocationEdit> m_location_edit;WebContentView* m_view { nullptr };FindInPageWidget* m_find_in_page { nullptr };QPointer<RestoreSessionCard> m_restore_session_card;@@ -303,21 +271,12 @@ private:QElapsedTimer m_load_finished_clock;bool m_has_had_real_size { false };bool m_is_updating_chrome_style { false };- bool m_vertical_tabs_enabled { false };QMenu* m_context_menu { nullptr };QMenu* m_select_dropdown { nullptr };- QAction* m_navigate_back_action { nullptr };- QAction* m_navigate_forward_action { nullptr };- QAction* m_reload_action { nullptr };- QAction* m_reload_bypassing_cache_action { nullptr };- QAction* m_stop_action { nullptr };- ReloadButton* m_reload_button { nullptr };- QAction* m_open_downloads_page_action { nullptr };QAction* m_pin_tab_action { nullptr };- DownloadsButton* m_downloads_button { nullptr };- size_t m_completed_download_count { 0 };+ WebView::TabPerformanceStats m_performance_stats;JavaScriptDialog* m_javascript_dialog { nullptr };Function<void(bool)> m_before_unload_completion;@@ -336,7 +295,6 @@ private:bool m_window_is_closing { false };bool m_is_pinned { false };- bool m_toolbar_window_controls_visible { false };Optional<WebView::SessionTabId> m_session_tab_id;Optional<DeferredTab> m_deferred_tab;};diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cppindex f53465332c9..6be49eed292 100644--- a/UI/Qt/TabBar.cpp+++ b/UI/Qt/TabBar.cpp@@ -3173,9 +3173,8 @@ TabWidget::TabWidget(WebView::IsPrivate is_private, QWidget* parent)m_tab_bar->setDrawBase(false);m_tab_bar->installEventFilter(this);- m_toolbar_container = new QStackedWidget(this);- m_toolbar_container->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);- m_toolbar_container->installEventFilter(this);+ m_toolbar = new WindowToolbar(is_private, this);+ m_toolbar->installEventFilter(this);m_stacked_widget = new QStackedWidget(this);m_page_column = new QWidget(this);m_page_column->installEventFilter(this);@@ -3321,17 +3320,19 @@ TabWidget::TabWidget(WebView::IsPrivate is_private, QWidget* parent)if (auto* previous_tab = qobject_cast<Tab*>(m_stacked_widget->currentWidget()); previous_tab && m_stacked_widget->indexOf(previous_tab) != index)previous_tab->park_location_editing();+ // Showing a tab shows its waiting permission prompt, which is placed below the tab's location field, so the+ // field moves into the toolbar first.+ m_toolbar->bind(as_if<Tab>(m_stacked_widget->widget(index)));+if (index >= 0 && index < m_stacked_widget->count())m_stacked_widget->setCurrentIndex(index);- m_toolbar_container->setCurrentIndex(index);emit current_tab_changed(index);// The all-tabs list closes as another tab is selected, such as a new tab opened in front of it. Tabs opened or// closed behind it only change its rows.if (m_all_tabs_popover && m_all_tabs_popover->isVisible())m_all_tabs_popover->close();- place_collapsed_sidebar_buttons();update_vertical_tabs_content_separator();update_vertical_tabs_overlay_geometry();update_vertical_tabs_resize_handle();@@ -3347,12 +3348,6 @@ TabWidget::TabWidget(WebView::IsPrivate is_private, QWidget* parent)auto* widget = m_stacked_widget->widget(from);m_stacked_widget->removeWidget(widget);m_stacked_widget->insertWidget(to, widget);-- auto* toolbar = as<Tab>(widget)->toolbar_container();- if (auto toolbar_index = m_toolbar_container->indexOf(toolbar); toolbar_index != -1) {- m_toolbar_container->removeWidget(toolbar);- m_toolbar_container->insertWidget(to, toolbar);- }m_stacked_widget->setCurrentIndex(m_tab_bar->currentIndex());});@@ -3409,25 +3404,18 @@ void TabWidget::insert_tab(int index, Tab* widget, QString const& label)widget->view().installEventFilter(this);widget->bookmarks_bar().installEventFilter(this);} else {- // A tab that hasn't loaded gets the filters for its view and bookmarks bar once it has them, and the collapsed- // sidebar's buttons once it has a toolbar to hold them.+ // A tab that hasn't loaded gets the filters for its view and bookmarks bar once it has them.auto did_load = [this, widget] {widget->view().installEventFilter(this);widget->bookmarks_bar().installEventFilter(this);- place_collapsed_sidebar_buttons();};connect(widget, &Tab::loaded, this, did_load, Qt::SingleShotConnection);}m_stacked_widget->insertWidget(index, widget);m_tab_bar->insertTab(index, label);- widget->set_toolbar_container_in_tab_layout(false);- m_toolbar_container->insertWidget(index, widget->toolbar_container());place_toolbar_over_page_column();- widget->set_vertical_tabs_enabled(m_vertical_tabs_enabled);-- update_toolbar_placement();update_tab_layout();update_tab_button_visibility();@@ -3448,17 +3436,6 @@ Tab* TabWidget::take_tab(int index)update_all_tabs_entries_soon();- // NB: Beside the collapsed sidebar, the Ladybird and all-tabs buttons and the sidebar's toggle sit in the current- // tab's toolbar, which the tab owns. They belong to this widget, so they don't go along with a tab that leaves- // it, or is deleted.- auto* tab_toolbar = as<Tab>(widget)->toolbar_container();- for (QWidget* button : { m_ladybird_buttons, static_cast<QWidget*>(m_vertical_tabs_toggle_button) }) {- if (button && (widget->isAncestorOf(button) || tab_toolbar->isAncestorOf(button))) {- button->hide();- button->setParent(this);- }- }-m_tab_bar->prepare_for_tab_removal(index);m_stacked_widget->removeWidget(widget);if (as<Tab>(widget)->is_loaded()) {@@ -3466,10 +3443,7 @@ Tab* TabWidget::take_tab(int index)as<Tab>(widget)->bookmarks_bar().removeEventFilter(this);}disconnect(as<Tab>(widget), &Tab::loaded, this, nullptr);- auto* toolbar = as<Tab>(widget)->toolbar_container();- if (m_toolbar_container->indexOf(toolbar) != -1)- m_toolbar_container->removeWidget(toolbar);- as<Tab>(widget)->set_toolbar_container_in_tab_layout(true);+ m_toolbar->release_location_edit(*as<Tab>(widget));m_tab_bar->removeTab(index);@@ -3478,7 +3452,6 @@ Tab* TabWidget::take_tab(int index)update_tab_layout();update_tab_button_visibility();- place_collapsed_sidebar_buttons();return as<Tab>(widget);}@@ -3520,7 +3493,7 @@ void TabWidget::set_tab_bar_visible(bool visible)void TabWidget::set_window_controls_visible(bool visible){m_window_controls_visible = visible;- update_tab_toolbar_window_controls_visibility();+ update_toolbar_window_controls_visibility();update_tab_chrome_visibility();update_tab_layout();}@@ -3533,9 +3506,7 @@ void TabWidget::set_vertical_tabs_enabled(bool enabled)m_vertical_tabs_enabled = enabled;if (!m_vertical_tabs_enabled)set_vertical_tabs_hover_expanded(false);- for (int index = 0; index < m_stacked_widget->count(); ++index) {- tab(index)->set_vertical_tabs_enabled(enabled);- }+ m_toolbar->set_vertical_tabs_enabled(enabled);rebuild_layout();}@@ -3614,8 +3585,7 @@ void TabWidget::set_vertical_tabs_expanded(bool expanded)// all-tabs buttons keep their place, and the toggle rides the sidebar's edge.void TabWidget::float_sidebar_buttons_while_toggling(){- auto* current = as_if<Tab>(m_stacked_widget->currentWidget());- auto floats = m_vertical_tabs_toggling && !vertical_tabs_are_on_right() && current && current->is_loaded();+ auto floats = m_vertical_tabs_toggling && !vertical_tabs_are_on_right();if (!floats) {for (auto* image : { m_floating_ladybird_buttons, m_floating_sidebar_toggle }) {if (image)@@ -3647,13 +3617,13 @@ void TabWidget::float_sidebar_buttons_while_toggling()make_image(m_floating_sidebar_toggle, *m_vertical_tabs_toggle_button);auto column_origin = m_vertical_tab_bar_column->mapTo(this, QPoint());- auto toolbar_height = Tab::toolbar_height(should_show_window_controls_in_tab_toolbar());+ auto toolbar_height = WindowToolbar::toolbar_height(should_show_window_controls_in_tab_toolbar());auto center_y = [&](QWidget const& image) { return column_origin.y() + ((toolbar_height - image.height()) / 2); };auto& ladybird_buttons = *m_floating_ladybird_buttons;auto& toggle = *m_floating_sidebar_toggle;auto ladybird_x = column_origin.x() + (vertical_tabs_hold_window_controls() ? NATIVE_MACOS_WINDOW_CONTROLS_INSET + NATIVE_MACOS_WINDOW_CONTROLS_WIDTH + LADYBIRD_BUTTON_TRAFFIC_LIGHTS_GAP : vertical_tabs_side_margin(true));- auto collapsed_toggle_x = ladybird_x + ladybird_buttons.width() + current->toolbar_sidebar_slot().layout()->spacing();+ auto collapsed_toggle_x = ladybird_x + ladybird_buttons.width() + m_toolbar->sidebar_slot().layout()->spacing();auto at_edge_x = column_origin.x() + current_vertical_tabs_width() - vertical_tabs_side_margin(true) - SIDEBAR_TOGGLE_PAGE_SIDE_MARGIN - toggle.width();toggle.move(max(collapsed_toggle_x, at_edge_x), center_y(toggle));ladybird_buttons.move(ladybird_x, center_y(ladybird_buttons));@@ -3672,15 +3642,12 @@ bool TabWidget::slides_page_with_toggling_sidebar() constvoid TabWidget::slide_page_with_toggling_sidebar(){- auto* current = as_if<Tab>(m_stacked_widget->currentWidget());if (!slides_page_with_toggling_sidebar()) {- if (current)- current->set_toolbar_leading_room(0);+ m_toolbar->set_leading_room(0);return;}// NB: The toolbar's contents start where they sit beside the collapsed sidebar, until the sidebar's edge reaches them.- if (current)- current->set_toolbar_leading_room(max(0, current->toolbar_lead_beside_collapsed_sidebar(*m_ladybird_buttons, COLLAPSED_VERTICAL_TABS_TOGGLE_BUTTON_SIZE) - current_vertical_tabs_width()));+ m_toolbar->set_leading_room(max(0, m_toolbar->lead_beside_collapsed_sidebar(*m_ladybird_buttons, COLLAPSED_VERTICAL_TABS_TOGGLE_BUTTON_SIZE) - current_vertical_tabs_width()));auto page_width = m_vertical_tabs_content->width() - min(m_vertical_tabs_toggle_from_width, m_vertical_tabs_toggle_to_width);m_page_column->setGeometry(current_vertical_tabs_width(), 0, page_width, m_vertical_tabs_content->height());}@@ -3861,7 +3828,7 @@ bool TabWidget::eventFilter(QObject* watched, QEvent* event)if ((watched == m_page_column || watched == m_vertical_tabs_content) && (event->type() == QEvent::Move || event->type() == QEvent::Resize))place_toolbar_over_page_column();- if (watched == m_toolbar_container && event->type() == QEvent::Resize) {+ if (watched == m_toolbar && event->type() == QEvent::Resize) {update_vertical_tabs_column_margins();update_tab_layout();}@@ -4054,7 +4021,7 @@ bool TabWidget::vertical_tabs_hold_window_controls() const// under it. The open sidebar's tabs start as far down as the collapsed sidebar's, so they stay in place as it toggles.int TabWidget::vertical_tabs_header_spacing() const{- auto toolbar_line_height = max(0, m_toolbar_container->height() - Tab::toolbar_height(should_show_window_controls_in_tab_toolbar()));+ auto toolbar_line_height = max(0, m_toolbar->height() - WindowToolbar::toolbar_height(should_show_window_controls_in_tab_toolbar()));return VERTICAL_TABS_TOP_MARGIN - VERTICAL_TAB_BAR_END_ROOM + toolbar_line_height;}@@ -4075,17 +4042,9 @@ qreal TabWidget::vertical_tabs_toggle_collapsedness() constreturn m_vertical_tabs_collapse_pending ? m_vertical_tabs_toggle_progress : 1 - m_vertical_tabs_toggle_progress;}-void TabWidget::update_toolbar_placement()-{- m_toolbar_container->setCurrentIndex(m_tab_bar->currentIndex());- update_tab_toolbar_window_controls_visibility();-}--void TabWidget::update_tab_toolbar_window_controls_visibility()+void TabWidget::update_toolbar_window_controls_visibility(){- auto show_controls = should_show_window_controls_in_tab_toolbar();- for (int index = 0; index < m_stacked_widget->count(); ++index)- tab(index)->set_toolbar_window_controls_visible(show_controls);+ m_toolbar->set_window_controls_visible(should_show_window_controls_in_tab_toolbar());}void TabWidget::rebuild_layout()@@ -4124,7 +4083,7 @@ void TabWidget::rebuild_layout()m_tab_bar->set_vertical_tabs_position(m_vertical_tabs_position);m_tab_bar->set_pinned_tabs_in_rows(m_vertical_tabs_hover_expanded && !m_vertical_tabs_expanded);m_tab_bar->set_tab_layout(current_tab_layout());- update_toolbar_placement();+ update_toolbar_window_controls_visibility();if (m_tab_bar->tab_layout() != TabLayout::Horizontal) {rebuild_layout_for_vertical_tabs();@@ -4139,7 +4098,7 @@ void TabWidget::rebuild_layout()} else {m_toolbar_follows_page_column = false;m_toolbar_placeholder->hide();- m_main_layout->addWidget(m_toolbar_container);+ m_main_layout->addWidget(m_toolbar);m_page_column_layout->addWidget(m_stacked_widget, 1);}m_page_column->show();@@ -4367,16 +4326,12 @@ void TabWidget::update_all_tabs_entries()void TabWidget::place_collapsed_sidebar_buttons(){auto collapsed = m_tab_bar_visible && m_tab_bar->tab_layout() != TabLayout::Horizontal && !m_vertical_tabs_expanded;- auto* current = as_if<Tab>(m_stacked_widget->currentWidget());- // A tab that hasn't loaded has no toolbar yet, so there's no slot of its own to hide or to fill.- for (int index = 0; index < m_stacked_widget->count(); ++index) {- if (auto* tab = this->tab(index); tab && tab->is_loaded() && (tab != current || !collapsed))- tab->toolbar_sidebar_slot().hide();- }- if (!current || !current->is_loaded() || !collapsed)+ auto& slot = m_toolbar->sidebar_slot();+ if (!collapsed) {+ slot.hide();return;+ }- auto& slot = current->toolbar_sidebar_slot();auto* slot_layout = static_cast<QBoxLayout*>(slot.layout());if (m_ladybird_buttons->parentWidget() != &slot) {m_ladybird_buttons->setParent(&slot);@@ -4417,7 +4372,7 @@ void TabWidget::rebuild_layout_for_vertical_tabs()// collapsed sidebar, and the sidebar's toggle at the edge toward the page.// NB: On the right, the all-tabs button keeps as clear of the window's edge as the toggle is of the page.top_row_layout->setContentsMargins(vertical_tabs_are_on_right() ? SIDEBAR_TOGGLE_PAGE_SIDE_MARGIN : 0, 0, SIDEBAR_TOGGLE_PAGE_SIDE_MARGIN, 0);- top_row_layout->addStrut(Tab::toolbar_height(should_show_window_controls_in_tab_toolbar()));+ top_row_layout->addStrut(WindowToolbar::toolbar_height(should_show_window_controls_in_tab_toolbar()));m_vertical_tabs_toggle_button->setParent(m_vertical_tab_bar_column);// NB: The button was smaller while it sat in the collapsed sidebar's slot.m_vertical_tabs_toggle_button->setFixedSize(VERTICAL_TABS_TOGGLE_BUTTON_SIZE, VERTICAL_TABS_TOGGLE_BUTTON_SIZE);@@ -4474,7 +4429,7 @@ void TabWidget::paint_vertical_tabs_column()auto top_row_height = 0;if (m_vertical_tabs_toggling) {auto collapsedness = vertical_tabs_toggle_collapsedness();- top_row_height = Tab::toolbar_height(should_show_window_controls_in_tab_toolbar());+ top_row_height = WindowToolbar::toolbar_height(should_show_window_controls_in_tab_toolbar());painter.fillRect(QRect(0, 0, column_rect.width(), top_row_height), ChromeStyle::mix(chrome.sidebar, chrome.toolbar, collapsedness));auto faded_divider = chrome.divider;faded_divider.setAlphaF(faded_divider.alphaF() * (1 - collapsedness));@@ -4516,18 +4471,18 @@ void TabWidget::place_toolbar_over_page_column()return;// NB: The stand-in is as tall as the toolbar, which is only known once a tab's toolbar is in it.- if (auto toolbar_height = m_toolbar_container->sizeHint().height(); m_toolbar_placeholder->height() != toolbar_height) {+ if (auto toolbar_height = m_toolbar->sizeHint().height(); m_toolbar_placeholder->height() != toolbar_height) {m_toolbar_placeholder->setFixedHeight(toolbar_height);m_page_column_layout->activate();}auto position = m_page_column->mapTo(this, QPoint());- m_toolbar_container->setGeometry(position.x(), position.y(), m_page_column->width(), m_toolbar_placeholder->height());- m_toolbar_container->stackUnder(m_vertical_tab_bar_column);+ m_toolbar->setGeometry(position.x(), position.y(), m_page_column->width(), m_toolbar_placeholder->height());+ m_toolbar->stackUnder(m_vertical_tab_bar_column);// Reparenting the page column when switching tab layouts can put its placeholder above the toolbar.if (m_page_column->parentWidget() == this)- m_page_column->stackUnder(m_toolbar_container);+ m_page_column->stackUnder(m_toolbar);if (m_vertical_tabs_content_separator->isVisible())m_vertical_tabs_content_separator->raise();@@ -4621,7 +4576,7 @@ void TabWidget::update_vertical_tabs_content_separator()return;auto separator_x = vertical_tabs_are_on_right() ? 0 : max(0, current_vertical_tabs_width() - 1);- auto separator_y = max(0, m_toolbar_container->height() - 1);+ auto separator_y = max(0, m_toolbar->height() - 1);auto separator_width = vertical_tabs_are_on_right() ? max(0, width() - current_vertical_tabs_width()) : max(0, width() - separator_x);m_vertical_tabs_content_separator->setGeometry(separator_x, separator_y, separator_width, 1);m_vertical_tabs_content_separator->raise();@@ -4644,7 +4599,7 @@ void TabWidget::update_vertical_tabs_hover_layout()QRect TabWidget::vertical_tabs_chrome_rect() const{// NB: The open sidebar runs the full height of the window, and the collapsed one starts below the toolbar.- auto chrome_height = m_vertical_tabs_expanded ? 0 : m_toolbar_container->height();+ auto chrome_height = m_vertical_tabs_expanded ? 0 : m_toolbar->height();auto chrome_width = current_vertical_tabs_width();return {vertical_tabs_are_on_right() ? max(0, width() - chrome_width) : 0,@@ -4741,7 +4696,7 @@ void TabWidget::update_tab_chrome_visibility()auto is_horizontal = tab_layout == TabLayout::Horizontal;auto show_top_row = m_tab_bar_visible && is_horizontal;m_tab_bar_row->setVisible(show_top_row);- m_toolbar_container->setVisible(m_tab_bar_visible);+ m_toolbar->setVisible(m_tab_bar_visible);m_toolbar_placeholder->setVisible(m_tab_bar_visible && m_toolbar_follows_page_column);m_vertical_tab_bar_column->setVisible(m_tab_bar_visible && !is_horizontal);update_vertical_tabs_content_separator();@@ -4874,8 +4829,7 @@ void TabWidget::update_window_button_icons()m_close_window_button->setIcon(create_chrome_icon(ChromeIcon::WindowClose, Theme::chrome_palette(*this)));}- for (int index = 0; index < m_stacked_widget->count(); ++index)- tab(index)->update_window_control_icons();+ m_toolbar->update_window_control_icons();}void TabWidget::toggle_window_maximized()diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.hindex c3d3cf8d579..2e776100dd7 100644--- a/UI/Qt/TabBar.h+++ b/UI/Qt/TabBar.h@@ -17,6 +17,7 @@#include <LibWebView/BrowsingSession.h>#include <LibWebView/Settings.h>#include <UI/Qt/Tab.h>+#include <UI/Qt/WindowToolbar.h>#include <QHash>#include <QPixmap>@@ -309,6 +310,8 @@ public:Tab* tab(int index) const { return as<Tab>(m_stacked_widget->widget(index)); }void set_current_tab(Tab*);+ WindowToolbar& toolbar() const { return *m_toolbar; }+int index_of(Tab* widget) const;void set_tab_text(int index, QString const& text);@@ -378,8 +381,7 @@ private:void place_toolbar_over_page_column();void update_vertical_tabs_column_margins();void paint_vertical_tabs_column();- void update_toolbar_placement();- void update_tab_toolbar_window_controls_visibility();+ void update_toolbar_window_controls_visibility();int current_vertical_tabs_width() const;void apply_vertical_tabs_expanded_width(int width);void persist_vertical_tabs_expanded_width();@@ -421,7 +423,7 @@ private:QToolButton* m_maximize_window_button { nullptr };QToolButton* m_close_window_button { nullptr };QWidget* m_window_controls { nullptr };- QStackedWidget* m_toolbar_container { nullptr };+ WindowToolbar* m_toolbar { nullptr };QWidget* m_page_column { nullptr };QWidget* m_toolbar_placeholder { nullptr };bool m_toolbar_follows_page_column { false };diff --git a/UI/Qt/WindowToolbar.cpp b/UI/Qt/WindowToolbar.cppnew file mode 100644index 00000000000..136aa53e509--- /dev/null+++ b/UI/Qt/WindowToolbar.cpp@@ -0,0 +1,1461 @@+/*+ * Copyright (c) 2022, Andreas Kling <[email protected]>+ * Copyright (c) 2022, Matthew Costa <[email protected]>+ * Copyright (c) 2024, Jamie Mansfield <[email protected]>+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/kmalloc.h>+#include <LibWebView/Application.h>+#include <LibWebView/DownloadPresentation.h>+#include <LibWebView/TabPerformanceMonitor.h>+#include <UI/Qt/Application.h>+#include <UI/Qt/BrowserWindow.h>+#include <UI/Qt/ChromeLayout.h>+#include <UI/Qt/ChromeStyle.h>+#include <UI/Qt/Icon.h>+#include <UI/Qt/LadybirdButton.h>+#include <UI/Qt/Menu.h>+#include <UI/Qt/Motion.h>+#include <UI/Qt/PerformanceMonitorWidget.h>+#include <UI/Qt/Popover.h>+#include <UI/Qt/StringUtils.h>+#include <UI/Qt/SystemAppearance.h>+#include <UI/Qt/Theme.h>+#include <UI/Qt/WindowControlButton.h>+#include <UI/Qt/WindowToolbar.h>++#include <QApplication>+#include <QFont>+#include <QFontMetrics>+#include <QFrame>+#include <QHBoxLayout>+#include <QMenu>+#include <QMouseEvent>+#include <QPainter>+#include <QPixmap>+#include <QProgressBar>+#include <QPushButton>+#include <QScrollArea>+#include <QStyleOptionToolButton>+#include <QStylePainter>+#include <QTimer>+#include <QToolButton>+#include <QVBoxLayout>+#include <QVariantAnimation>++namespace Ladybird {++static Web::HTML::ActivateTab activate_tab_for_modifiers(Qt::KeyboardModifiers modifiers)+{+ return modifiers.testFlag(Qt::ShiftModifier) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No;+}++class ToolbarButton : public QToolButton {+public:+ AK_ALLOC_WITH_KMALLOC;++ explicit ToolbarButton(QWidget* parent)+ : QToolButton(parent)+ {+ QObject::connect(&m_icon_motion_animation, &QVariantAnimation::valueChanged, this, [this] { update(); });+ QObject::connect(&m_icon_motion_animation, &QVariantAnimation::finished, this, [this] { update(); });+ }++ Function<void(Web::HTML::ActivateTab)> on_open_in_new_tab;++ bool click_opens_in_new_tab() const+ {+ return can_open_in_new_tab() && QApplication::keyboardModifiers().testFlag(Qt::ControlModifier);+ }++ // A click or a change of state moves the icon a little: back and forward nudge their way, and the downloads arrow+ // drops into place as the button appears and bobs once as a download finishes.+ enum class IconMotion : u8 {+ NudgeLeft,+ NudgeRight,+ DropIn,+ Bob,+ };++ void play_icon_motion(IconMotion motion)+ {+ if (SystemAppearance::prefers_reduced_motion())+ return;+ m_icon_motion = motion;+ // NB: A click can disable its button, as going back to the first page does with the back button. The motion+ // plays out as the button looked when it started.+ m_icon_motion_is_hovered = underMouse();+ m_icon_motion_animation.stop();+ m_icon_motion_animation.setStartValue(0.0);+ m_icon_motion_animation.setEndValue(1.0);+ switch (motion) {+ case IconMotion::NudgeLeft:+ case IconMotion::NudgeRight:+ m_icon_motion_animation.setDuration(NUDGE_DURATION_MS);+ break;+ case IconMotion::DropIn:+ m_icon_motion_animation.setDuration(DROP_IN_DURATION_MS);+ break;+ case IconMotion::Bob:+ m_icon_motion_animation.setDuration(BOB_DURATION_MS);+ break;+ }+ m_icon_motion_animation.start();+ }++protected:+ virtual bool can_open_in_new_tab() const { return static_cast<bool>(on_open_in_new_tab); }++ QRect icon_rect() const+ {+ return QRect(QPoint((width() - iconSize().width()) / 2, (height() - iconSize().height()) / 2), iconSize());+ }++ QIcon::Mode icon_mode() const+ {+ return isEnabled() ? (underMouse() ? QIcon::Active : QIcon::Normal) : QIcon::Disabled;+ }++ virtual void paintEvent(QPaintEvent*) override+ {+ if (m_icon_motion_animation.state() == QAbstractAnimation::Running) {+ QPainter painter(this);+ if (isEnabled()) {+ ChromeStyle::paint_control_frame(painter, *this);+ } else if (m_icon_motion_is_hovered && underMouse()) {+ painter.setRenderHint(QPainter::Antialiasing, true);+ painter.setPen(Qt::NoPen);+ painter.setBrush(Theme::chrome(palette()).hover);+ painter.drawRoundedRect(QRectF(rect()), ChromeStyle::CONTROL_FRAME_RADIUS, ChromeStyle::CONTROL_FRAME_RADIUS);+ painter.setRenderHint(QPainter::Antialiasing, false);+ }+ auto mode = isEnabled() ? icon_mode() : (m_icon_motion_is_hovered && underMouse() ? QIcon::Active : QIcon::Normal);+ auto progress = m_icon_motion_animation.currentValue().toReal();+ switch (m_icon_motion) {+ case IconMotion::NudgeLeft:+ painter.translate(-NUDGE_DISTANCE * Motion::there_and_back(progress, NUDGE_PEAK_AT), 0);+ break;+ case IconMotion::NudgeRight:+ painter.translate(NUDGE_DISTANCE * Motion::there_and_back(progress, NUDGE_PEAK_AT), 0);+ break;+ case IconMotion::DropIn: {+ auto eased = Motion::enter_easing().valueForProgress(progress);+ painter.translate(0, -DROP_IN_DISTANCE * (1 - eased));+ painter.setOpacity(eased);+ break;+ }+ case IconMotion::Bob:+ painter.translate(0, BOB_DISTANCE * Motion::there_and_back(progress, BOB_PEAK_AT));+ break;+ }+ icon().paint(&painter, icon_rect(), Qt::AlignCenter, mode);+ return;+ }++ QStyleOptionToolButton option;+ initStyleOption(&option);++ QStylePainter painter(this);+ ChromeStyle::paint_control_frame(painter, *this);++ painter.drawComplexControl(QStyle::CC_ToolButton, option);+ }++ virtual void mousePressEvent(QMouseEvent* event) override+ {+ if (event->button() == Qt::MiddleButton && can_open_in_new_tab())+ event->accept();+ else+ QToolButton::mousePressEvent(event);+ }++ virtual void mouseReleaseEvent(QMouseEvent* event) override+ {+ if (event->button() == Qt::MiddleButton && can_open_in_new_tab()) {+ if (rect().contains(event->position().toPoint()))+ on_open_in_new_tab(activate_tab_for_modifiers(event->modifiers()));+ event->accept();+ } else {+ QToolButton::mouseReleaseEvent(event);+ }+ }++ // QToolButton triggers its default action from here, once a click has completed.+ virtual void nextCheckState() override+ {+ if (click_opens_in_new_tab()) {+ on_open_in_new_tab(activate_tab_for_modifiers(QApplication::keyboardModifiers()));+ return;+ }+ QToolButton::nextCheckState();+ }++private:+ static constexpr int NUDGE_DURATION_MS = 180;+ static constexpr qreal NUDGE_DISTANCE = 2;+ static constexpr qreal NUDGE_PEAK_AT = 0.4;+ static constexpr int DROP_IN_DURATION_MS = 220;+ static constexpr qreal DROP_IN_DISTANCE = 4;+ static constexpr int BOB_DURATION_MS = 260;+ static constexpr qreal BOB_DISTANCE = 2;+ static constexpr qreal BOB_PEAK_AT = 0.45;++ IconMotion m_icon_motion { IconMotion::NudgeLeft };+ bool m_icon_motion_is_hovered { false };+ QVariantAnimation m_icon_motion_animation;+};++// The reload button becomes a stop button while the page loads. So that one click does not do what the user did not aim+// at, a stop button under the pointer stays until the pointer leaves or a short delay passes, and a click on stop within+// the double-click interval of a reload click is ignored, so a double-click does not stop the reload it started. The+// reload action stays attached to the button while it shows stop, so its shortcuts still work during a load.+class ReloadButton final : public ToolbarButton {+public:+ AK_ALLOC_WITH_KMALLOC;++ explicit ReloadButton(QWidget* parent)+ : ToolbarButton(parent)+ {+ m_icon_fade.setStartValue(0.0);+ m_icon_fade.setEndValue(1.0);+ QObject::connect(&m_icon_fade, &QVariantAnimation::valueChanged, this, [this] { update(); });++ m_double_click_timer.setSingleShot(true);+ m_double_click_timer.setInterval(QApplication::doubleClickInterval());++ m_stop_to_reload_timer.setSingleShot(true);+ m_stop_to_reload_timer.setInterval(STOP_TO_RELOAD_DELAY_MS);+ QObject::connect(&m_stop_to_reload_timer, &QTimer::timeout, this, [this] {+ change_mode(m_intended_mode, ForceChange::Yes);+ });++ QObject::connect(this, &QToolButton::pressed, this, [this] {+ if (m_visible_mode == Mode::Reload)+ m_double_click_timer.start();+ });++ // NB: A stop click that arrives after the load ended has nothing to stop, so show the reload button right away.+ QObject::connect(this, &QToolButton::clicked, this, [this] {+ if (m_visible_mode == Mode::Stop)+ change_mode(m_intended_mode, ForceChange::Yes);+ });+ }++ void set_actions(QAction& reload_action, QAction& reload_bypassing_cache_action, QAction& stop_action)+ {+ m_reload_action = &reload_action;+ m_reload_bypassing_cache_action = &reload_bypassing_cache_action;+ m_stop_action = &stop_action;+ }++ void set_loading(bool is_loading)+ {+ change_mode(is_loading ? Mode::Stop : Mode::Reload, ForceChange::No);+ }++ // Another tab's state is shown as it stands, without the motion of a change.+ void show_loading_at_once(bool is_loading)+ {+ m_icon_fade.stop();+ m_stop_to_reload_timer.stop();+ m_intended_mode = m_visible_mode = is_loading ? Mode::Stop : Mode::Reload;+ setDefaultAction(is_loading ? m_stop_action : m_reload_action);+ update();+ }++protected:+ virtual bool can_open_in_new_tab() const override+ {+ return m_visible_mode == Mode::Reload && ToolbarButton::can_open_in_new_tab();+ }++ virtual void leaveEvent(QEvent* event) override+ {+ ToolbarButton::leaveEvent(event);+ change_mode(m_intended_mode, ForceChange::Yes);+ }++ // A Shift-click on the reload button reloads bypassing the HTTP cache, and one on the stop button stops the load as+ // any other click there does.+ virtual void nextCheckState() override+ {+ if (m_visible_mode == Mode::Stop && m_double_click_timer.isActive())+ return;+ if (m_visible_mode == Mode::Reload && !click_opens_in_new_tab() && QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier)) {+ m_reload_bypassing_cache_action->trigger();+ return;+ }+ ToolbarButton::nextCheckState();+ }++private:+ static constexpr int STOP_TO_RELOAD_DELAY_MS = 1000;+ static constexpr int ARROW_TURN_DURATION_MS = 340;+ static constexpr int TURN_CROSS_FADE_DELAY_MS = 200;+ static constexpr int TURN_CROSS_FADE_DURATION_MS = 140;+ static constexpr int STOP_TURN_DELAY_MS = 180;+ static constexpr int STOP_TURN_DURATION_MS = 200;+ static constexpr int RELOAD_TO_STOP_DURATION_MS = STOP_TURN_DELAY_MS + STOP_TURN_DURATION_MS;+ static constexpr int ICON_CROSS_FADE_DURATION_MS = 160;++ enum class Mode {+ Reload,+ Stop,+ };++ enum class ForceChange {+ No,+ Yes,+ };++ void change_mode(Mode mode, ForceChange force_change)+ {+ m_intended_mode = mode;++ auto change_is_safe = mode == Mode::Stop || m_visible_mode != Mode::Stop;+ if (force_change == ForceChange::Yes || !underMouse() || change_is_safe) {+ m_stop_to_reload_timer.stop();+ if (m_visible_mode != mode && defaultAction() && !SystemAppearance::prefers_reduced_motion()) {+ m_previous_icon = defaultAction()->icon();+ // NB: The arrow turns into the stop icon only as the reload button is clicked. A load started otherwise+ // cross-fades it, as does the end of a load.+ m_turns_icon = mode == Mode::Stop && m_double_click_timer.isActive();+ m_icon_fade.stop();+ m_icon_fade.setDuration(m_turns_icon ? RELOAD_TO_STOP_DURATION_MS : ICON_CROSS_FADE_DURATION_MS);+ m_icon_fade.start();+ }+ m_visible_mode = mode;+ setDefaultAction(mode == Mode::Stop ? m_stop_action : m_reload_action);+ return;+ }++ if (m_visible_mode == Mode::Stop && !m_stop_to_reload_timer.isActive())+ m_stop_to_reload_timer.start();+ }++ // As a click on it starts a load, the reload arrow turns half a turn, and over its second half cross-fades into the+ // stop icon, which turns into place. Otherwise, as when the load ends, the two icons only cross-fade.+ virtual void paintEvent(QPaintEvent* event) override+ {+ if (m_icon_fade.state() != QAbstractAnimation::Running) {+ ToolbarButton::paintEvent(event);+ return;+ }++ QPainter painter(this);+ painter.setRenderHint(QPainter::SmoothPixmapTransform, true);+ ChromeStyle::paint_control_frame(painter, *this);+ auto elapsed = m_icon_fade.currentValue().toReal() * m_icon_fade.duration();+ auto part = [&](qreal delay, qreal duration) { return clamp((elapsed - delay) / duration, 0.0, 1.0); };+ auto center = QRectF(icon_rect()).center();+ auto paint_turned = [&](QIcon const& icon, qreal opacity, qreal degrees) {+ painter.save();+ painter.setOpacity(opacity);+ painter.translate(center);+ painter.rotate(degrees);+ painter.translate(-center);+ icon.paint(&painter, icon_rect(), Qt::AlignCenter, icon_mode());+ painter.restore();+ };+ if (!m_turns_icon) {+ auto fade = part(0, ICON_CROSS_FADE_DURATION_MS);+ paint_turned(m_previous_icon, 1 - fade, 0);+ paint_turned(icon(), fade, 0);+ return;+ }+ static auto const easing = Motion::enter_easing();+ auto fade = part(TURN_CROSS_FADE_DELAY_MS, TURN_CROSS_FADE_DURATION_MS);+ paint_turned(m_previous_icon, 1 - fade, 180 * easing.valueForProgress(part(0, ARROW_TURN_DURATION_MS)));+ paint_turned(icon(), fade, -90 * (1 - easing.valueForProgress(part(STOP_TURN_DELAY_MS, STOP_TURN_DURATION_MS))));+ }++ QAction* m_reload_action { nullptr };+ QAction* m_reload_bypassing_cache_action { nullptr };+ QAction* m_stop_action { nullptr };+ QIcon m_previous_icon;+ QVariantAnimation m_icon_fade;+ bool m_turns_icon { false };+ Mode m_visible_mode { Mode::Reload };+ Mode m_intended_mode { Mode::Reload };+ QTimer m_double_click_timer;+ QTimer m_stop_to_reload_timer;+};++static constexpr QSize TOOLBAR_BUTTON_SIZE { 32, 28 };++// The downloads button appears at the end of the toolbar once there are downloads, and opens the list of them. While+// they are under way, a ring around it fills as they progress, or turns while their progress is not known. A failed+// download marks it with a dot until the list is looked at.+class DownloadsButton final : public ToolbarButton {+public:+ AK_ALLOC_WITH_KMALLOC;++ explicit DownloadsButton(QWidget* parent)+ : ToolbarButton(parent)+ {+ setObjectName("LadybirdDownloadsButton");+ setAccessibleName("Downloads");+ // NB: The button stays checked while the list of downloads is open.+ setCheckable(true);+ setAutoRaise(true);+ setFocusPolicy(Qt::NoFocus);+ setIconSize({ TOOLBAR_ICON_SIZE, TOOLBAR_ICON_SIZE });+ setFixedSize(TOOLBAR_BUTTON_SIZE);+ setAttribute(Qt::WA_LayoutUsesWidgetRect);+ hide();++ m_ring_animation.setDuration(ChromeStyle::LOADING_RING_PERIOD_MS);+ m_ring_animation.setStartValue(0.0);+ m_ring_animation.setEndValue(1.0);+ m_ring_animation.setLoopCount(-1);+ QObject::connect(&m_ring_animation, &QVariantAnimation::valueChanged, this, [this] { update(); });++ m_ring_fade.setDuration(RING_FADE_DURATION_MS);+ QObject::connect(&m_ring_fade, &QVariantAnimation::valueChanged, this, [this] { update(); });++ m_progress_animation.setDuration(PROGRESS_GLIDE_DURATION_MS);+ QObject::connect(&m_progress_animation, &QVariantAnimation::valueChanged, this, [this](QVariant const& value) {+ m_shown_progress = value.toReal();+ update();+ });+ }++ void set_progress(bool downloading, Optional<double> progress)+ {+ if (m_downloading == downloading && m_progress == progress)+ return;+ // The ring fades in as downloads start, and out as they end, showing how far they had got.+ if (m_downloading != downloading) {+ m_ring_fade.stop();+ if (!SystemAppearance::prefers_reduced_motion() && isVisible()) {+ m_ring_fade.setStartValue(downloading ? 0.0 : 1.0);+ m_ring_fade.setEndValue(downloading ? 1.0 : 0.0);+ m_ring_fade.start();+ }+ }+ if (m_downloading && !downloading)+ m_progress_at_end = m_shown_progress;+ // The ring glides to each new amount rather than jumping to it.+ m_progress_animation.stop();+ if (progress.has_value() && downloading) {+ if (!m_downloading || !m_progress.has_value() || SystemAppearance::prefers_reduced_motion()) {+ m_shown_progress = *progress;+ } else {+ m_progress_animation.setStartValue(m_shown_progress);+ m_progress_animation.setEndValue(*progress);+ m_progress_animation.start();+ }+ }+ m_downloading = downloading;+ m_progress = progress;+ auto should_turn = downloading && !progress.has_value() && isVisible();+ if (should_turn && m_ring_animation.state() != QAbstractAnimation::Running)+ m_ring_animation.start();+ else if (!should_turn)+ m_ring_animation.stop();+ update();+ }++ void set_needs_attention(bool needs_attention)+ {+ if (m_needs_attention == needs_attention)+ return;+ m_needs_attention = needs_attention;+ update();+ }++private:+ virtual void paintEvent(QPaintEvent* event) override+ {+ ToolbarButton::paintEvent(event);++ QPainter painter(this);+ painter.setRenderHint(QPainter::Antialiasing, true);+ auto palette = Theme::chrome_palette(*this);+ auto center = QRectF(rect()).center();+ auto ring_is_fading = m_ring_fade.state() == QAbstractAnimation::Running;+ if (m_downloading || ring_is_fading) {+ painter.save();+ if (ring_is_fading)+ painter.setOpacity(m_ring_fade.currentValue().toReal());+ auto progress = m_downloading ? (m_progress.has_value() ? Optional<double> { m_shown_progress } : Optional<double> {}) : m_progress_at_end;+ if (progress.has_value())+ ChromeStyle::paint_progress_ring(painter, palette, center, *progress);+ else+ ChromeStyle::paint_loading_ring(painter, palette, center, ChromeStyle::LoadingRingSize::ToolbarButton);+ painter.restore();+ }+ if (m_needs_attention) {+ QPointF dot_center { width() - 7.0, 6.0 };+ painter.setPen(Qt::NoPen);+ painter.setBrush(Theme::chrome(palette).toolbar);+ painter.drawEllipse(dot_center, 5.5, 5.5);+ painter.setBrush(Theme::chrome(palette).warning);+ painter.drawEllipse(dot_center, 3.5, 3.5);+ }+ }++ static constexpr int RING_FADE_DURATION_MS = 200;+ static constexpr int PROGRESS_GLIDE_DURATION_MS = 300;++ bool m_downloading { false };+ Optional<double> m_progress;+ Optional<double> m_progress_at_end;+ double m_shown_progress { 0 };+ QVariantAnimation m_progress_animation;+ bool m_needs_attention { false };+ QVariantAnimation m_ring_animation;+ QVariantAnimation m_ring_fade;+};++template<typename ButtonType = ToolbarButton>+static ButtonType* create_toolbar_button(QWidget& parent, QAction& action)+{+ auto* button = new ButtonType(&parent);+ button->setDefaultAction(&action);+ button->setAutoRaise(true);+ button->setFocusPolicy(Qt::NoFocus);+ button->setIconSize({ TOOLBAR_ICON_SIZE, TOOLBAR_ICON_SIZE });+ button->setFixedSize(TOOLBAR_BUTTON_SIZE);+ // NB: The platform style pads tool buttons for its own frame, which would space them further apart than the design.+ button->setAttribute(Qt::WA_LayoutUsesWidgetRect);++ // FIXME: In Menu.cpp, we set the initial visibility of the action before we've associated it with this QToolButton.+ // It would be nicer if we didn't have to do this here.+ button->setVisible(action.isVisible());++ return button;+}++static constexpr int TOOLBAR_HORIZONTAL_MARGIN = 8;+// The buttons in the toolbar sit close together, while the address field keeps a wider gap on either side.+static constexpr int TOOLBAR_SPACING = 2;+// The separator after the collapsed sidebar's buttons keeps a little more room on either side.+static constexpr int SIDEBAR_SLOT_SEPARATOR_MARGIN = 6;+// NB: With the toolbar's spacing, the address field starts 8 px after the reload button.+static constexpr int TOOLBAR_LOCATION_EDIT_MARGIN = 6;+// The address field's focus ring reaches 3 px past its edge. With the toolbar's spacing, this room also puts the+// downloads button 6 px after the field.+static constexpr int TOOLBAR_LOCATION_FOCUS_RING_ROOM = 4;+static constexpr int TOOLBAR_WINDOW_CONTROLS_RIGHT_MARGIN = 4;++// NB: Unless window controls follow it, the address field ends the toolbar, and the room for its focus ring takes the+// place of some of the toolbar's margin.+static constexpr int toolbar_right_margin(bool has_custom_window_controls)+{+ return has_custom_window_controls ? TOOLBAR_WINDOW_CONTROLS_RIGHT_MARGIN : TOOLBAR_HORIZONTAL_MARGIN - TOOLBAR_LOCATION_FOCUS_RING_ROOM;+}+static constexpr int DOWNLOADS_POPOVER_WIDTH = 380;+static constexpr int DOWNLOADS_POPOVER_MAX_HEIGHT = 360;+class ElidedLabel final : public QLabel {+public:+ AK_ALLOC_WITH_KMALLOC;++ explicit ElidedLabel(QString text, Qt::TextElideMode elide_mode, QWidget* parent = nullptr)+ : QLabel(text, parent)+ , m_elide_mode(elide_mode)+ {+ setMinimumWidth(0);+ setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);+ }++ virtual QSize sizeHint() const override+ {+ auto size = QLabel::sizeHint();+ size.setWidth(0);+ return size;+ }++ virtual QSize minimumSizeHint() const override+ {+ return { 0, QLabel::minimumSizeHint().height() };+ }++protected:+ virtual void paintEvent(QPaintEvent*) override+ {+ QPainter painter(this);+ painter.setFont(font());+ painter.setPen(palette().color(foregroundRole()));+ painter.drawText(rect(), alignment() | Qt::TextSingleLine, fontMetrics().elidedText(text(), m_elide_mode, width()));+ }++private:+ Qt::TextElideMode m_elide_mode { Qt::ElideRight };+};++// A small page with the file's extension on it.+class DownloadFileIcon final : public QWidget {+public:+ AK_ALLOC_WITH_KMALLOC;++ explicit DownloadFileIcon(QWidget* parent)+ : QWidget(parent)+ {+ setFixedSize(28, 34);+ }++ void set_extension(QString extension)+ {+ m_extension = extension.toUpper().left(4);+ update();+ }++private:+ virtual void paintEvent(QPaintEvent*) override+ {+ QPainter painter(this);+ painter.setRenderHint(QPainter::Antialiasing, true);+ auto const& panel = Theme::panel();+ painter.setBrush(panel.page);+ painter.setPen(QPen(panel.separator, 1));+ painter.drawRoundedRect(QRectF(rect()).adjusted(0.5, 0.5, -0.5, -0.5), 4, 4);++ auto font = this->font();+ font.setPixelSize(8);+ font.setWeight(QFont::Bold);+ painter.setFont(font);+ painter.setPen(panel.muted);+ painter.drawText(rect().adjusted(0, 0, 0, -4), Qt::AlignHCenter | Qt::AlignBottom, m_extension);+ }++ QString m_extension;+};++class DownloadRow final : public QFrame {+public:+ AK_ALLOC_WITH_KMALLOC;++ explicit DownloadRow(WebView::FileDownloader::Download const& download, QWidget* parent)+ : QFrame(parent)+ , m_download_id(download.id)+ {+ setObjectName("LadybirdDownloadRow");+ setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);++ auto* row_layout = new QHBoxLayout(this);+ row_layout->setContentsMargins(10, 8, 6, 8);+ row_layout->setSpacing(12);++ m_file_icon = new DownloadFileIcon(this);+ row_layout->addWidget(m_file_icon, 0, Qt::AlignVCenter);++ auto* details_layout = new QVBoxLayout;+ details_layout->setContentsMargins(0, 0, 0, 0);+ details_layout->setSpacing(5);++ m_file_name_label = new ElidedLabel({}, Qt::ElideMiddle, this);+ m_file_name_label->setObjectName("LadybirdDownloadFileName");+ details_layout->addWidget(m_file_name_label);++ m_progress_bar = new QProgressBar(this);+ m_progress_bar->setObjectName("LadybirdDownloadProgress");+ m_progress_bar->setRange(0, 1000);+ m_progress_bar->setTextVisible(false);+ details_layout->addWidget(m_progress_bar);++ m_status_label = new ElidedLabel({}, Qt::ElideRight, this);+ m_status_label->setObjectName("LadybirdDownloadStatus");+ details_layout->addWidget(m_status_label);++ row_layout->addLayout(details_layout, 1);++ auto make_action = [&](ChromeIcon icon, QString const& tooltip, auto callback) {+ auto* button = new ToolbarButton(this);+ button->setObjectName("LadybirdDownloadAction");+ button->setAutoRaise(true);+ button->setIcon(create_chrome_icon(icon, Theme::panel().body));+ button->setIconSize({ 16, 16 });+ button->setFixedSize(28, 28);+ button->setToolTip(tooltip);+ button->setAccessibleName(tooltip);+ button->setFocusPolicy(Qt::TabFocus);+ QObject::connect(button, &QToolButton::clicked, this, [this, callback] { callback(*this); });+ row_layout->addWidget(button, 0, Qt::AlignVCenter);+ return button;+ };++ m_pause_button = make_action(ChromeIcon::Pause, "Pause", [](DownloadRow& row) {+ if (row.m_is_paused) {+ if (row.on_resume_download)+ row.on_resume_download(row.m_download_id);+ } else if (row.on_pause_download) {+ row.on_pause_download(row.m_download_id);+ }+ });+ m_cancel_button = make_action(ChromeIcon::Close, "Cancel", [](DownloadRow& row) {+ if (row.on_cancel_download)+ row.on_cancel_download(row.m_download_id);+ });+ m_retry_button = make_action(ChromeIcon::Reload, "Retry", [](DownloadRow& row) {+ if (row.on_resume_download)+ row.on_resume_download(row.m_download_id);+ });+#if defined(AK_OS_MACOS)+ auto show_in_folder_name = "Show in Finder";+#elif defined(AK_OS_WINDOWS)+ auto show_in_folder_name = "Show in Explorer";+#else+ auto show_in_folder_name = "Show in Files";+#endif+ m_show_in_folder_button = make_action(ChromeIcon::Folder, show_in_folder_name, [](DownloadRow& row) {+ if (row.on_show_in_folder)+ row.on_show_in_folder(row.m_download_id);+ });++ (void)update_download(download);+ }++ u64 id() const { return m_download_id; }++ bool update_download(WebView::FileDownloader::Download const& download)+ {+ VERIFY(download.id == m_download_id);++ auto basename = qstring_from_ak_string(download.destination.basename());+ m_file_name_label->setText(basename);+ m_file_name_label->setToolTip(qstring_from_ak_string(download.destination.string()));+ m_file_icon->set_extension(qstring_from_ak_string(download.destination.extension()));++ auto status_text = qstring_from_ak_string(WebView::download_status_text(download));+ m_status_label->setText(status_text);+ m_status_label->setToolTip(status_text);+ auto failed = download.status == WebView::FileDownloader::DownloadStatus::Failed;+ if (m_status_label->property("failed").toBool() != failed) {+ m_status_label->setProperty("failed", failed);+ m_status_label->style()->unpolish(m_status_label);+ m_status_label->style()->polish(m_status_label);+ }++ bool geometry_changed = false;+ auto set_visible = [&](QWidget& widget, bool visible) {+ if (widget.isHidden() == visible) {+ widget.setVisible(visible);+ geometry_changed = true;+ }+ };++ auto is_active = WebView::FileDownloader::status_is_active(download.status);+ auto progress = download.progress();+ set_visible(*m_progress_bar, is_active && progress.has_value());+ if (progress.has_value())+ m_progress_bar->setValue(static_cast<int>(*progress * 1000.0));++ m_is_paused = download.status == WebView::FileDownloader::DownloadStatus::Paused;+ m_pause_button->setIcon(create_chrome_icon(m_is_paused ? ChromeIcon::Download : ChromeIcon::Pause, Theme::chrome_palette(*this)));+ m_pause_button->setToolTip(m_is_paused ? "Resume" : "Pause");+ m_pause_button->setAccessibleName(m_pause_button->toolTip());++ set_visible(*m_pause_button, download.can_resume && is_active);+ set_visible(*m_cancel_button, is_active);+ set_visible(*m_retry_button, failed && download.can_resume);+ set_visible(*m_show_in_folder_button, download.status == WebView::FileDownloader::DownloadStatus::Completed);++ return geometry_changed;+ }++ Function<void(u64)> on_cancel_download;+ Function<void(u64)> on_pause_download;+ Function<void(u64)> on_resume_download;+ Function<void(u64)> on_show_in_folder;++private:+ u64 m_download_id { 0 };+ bool m_is_paused { false };+ DownloadFileIcon* m_file_icon { nullptr };+ ElidedLabel* m_file_name_label { nullptr };+ ElidedLabel* m_status_label { nullptr };+ QProgressBar* m_progress_bar { nullptr };+ QToolButton* m_pause_button { nullptr };+ QToolButton* m_cancel_button { nullptr };+ QToolButton* m_retry_button { nullptr };+ QToolButton* m_show_in_folder_button { nullptr };+};++class DownloadsPopover final : public Popover {+public:+ AK_ALLOC_WITH_KMALLOC;++ explicit DownloadsPopover(QWidget* parent)+ : Popover(parent)+ {+ card().setObjectName("LadybirdDownloadsPopover");+ card().setFixedWidth(DOWNLOADS_POPOVER_WIDTH);++ auto* layout = new QVBoxLayout(&card());+ layout->setContentsMargins(6, 12, 6, 6);+ layout->setSpacing(0);++ auto* header = new QHBoxLayout;+ header->setContentsMargins(10, 0, 4, 6);+ auto* title = new QLabel("Downloads", this);+ title->setObjectName("LadybirdDownloadsPopoverTitle");+ header->addWidget(title, 1);+ auto* clear_button = new QPushButton("Clear finished", this);+ clear_button->setObjectName("LadybirdPopoverLink");+ clear_button->setCursor(Qt::PointingHandCursor);+ QObject::connect(clear_button, &QPushButton::clicked, this, [this] {+ if (on_clear_finished)+ on_clear_finished();+ });+ header->addWidget(clear_button);+ layout->addLayout(header);++ m_empty_label = new QLabel("No downloads", this);+ m_empty_label->setObjectName("LadybirdDownloadsEmpty");+ m_empty_label->setAlignment(Qt::AlignCenter);+ m_empty_label->setMinimumHeight(80);+ layout->addWidget(m_empty_label);++ m_scroll_area = new QScrollArea(this);+ m_scroll_area->setObjectName("LadybirdDownloadsPopoverScroll");+ m_scroll_area->setFrameShape(QFrame::NoFrame);+ m_scroll_area->setWidgetResizable(true);+ m_scroll_area->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);+ m_scroll_area->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);++ m_rows_widget = new QWidget(m_scroll_area);+ m_rows_widget->setObjectName("LadybirdDownloadsPopoverRows");+ m_rows_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);+ m_rows_layout = new QVBoxLayout(m_rows_widget);+ m_rows_layout->setContentsMargins(0, 0, 0, 0);+ m_rows_layout->setSpacing(0);+ m_scroll_area->setWidget(m_rows_widget);+ layout->addWidget(m_scroll_area);++ auto* separator = new QFrame(this);+ separator->setObjectName("LadybirdPopoverSeparator");+ separator->setFixedHeight(1);+ auto* separator_row = new QHBoxLayout;+ separator_row->setContentsMargins(10, 4, 10, 4);+ separator_row->addWidget(separator);+ layout->addLayout(separator_row);++ auto* show_all_button = new QPushButton("Show all downloads", this);+ show_all_button->setObjectName("LadybirdPopoverRow");+ QObject::connect(show_all_button, &QPushButton::clicked, this, [this] {+ if (on_open_all_downloads)+ on_open_all_downloads();+ });+ layout->addWidget(show_all_button);+ }++ void update_chrome_style(QPalette const& palette)+ {+ setPalette(palette);+ setStyleSheet(ChromeStyle::downloads_popover_style_sheet(palette));+ }++ bool set_downloads(ReadonlySpan<WebView::FileDownloader::Download> downloads)+ {+ bool geometry_changed = false;+ auto visible_downloads = WebView::recent_downloads_for_popover(downloads);++ if (!rows_match(visible_downloads)) {+ rebuild_rows(visible_downloads);+ geometry_changed = true;+ } else {+ for (size_t i = 0; i < visible_downloads.size(); ++i) {+ if (m_download_rows[i]->update_download(*visible_downloads[i]))+ geometry_changed = true;+ }+ }++ auto is_empty = visible_downloads.is_empty();+ auto empty_label_is_visible = !m_empty_label->isHidden();+ if (empty_label_is_visible != is_empty) {+ m_empty_label->setVisible(is_empty);+ geometry_changed = true;+ }+ auto scroll_area_is_visible = !m_scroll_area->isHidden();+ if (scroll_area_is_visible == is_empty) {+ m_scroll_area->setVisible(!is_empty);+ geometry_changed = true;+ }++ return geometry_changed;+ }++ Function<void(u64)> on_cancel_download;+ Function<void(u64)> on_pause_download;+ Function<void(u64)> on_resume_download;+ Function<void(u64)> on_show_in_folder;+ Function<void()> on_clear_finished;+ Function<void()> on_open_all_downloads;++private:+ bool rows_match(Vector<WebView::FileDownloader::Download const*> const& downloads) const+ {+ if (m_download_rows.size() != downloads.size())+ return false;++ for (size_t i = 0; i < downloads.size(); ++i) {+ if (m_download_rows[i]->id() != downloads[i]->id)+ return false;+ }++ return true;+ }++ void rebuild_rows(Vector<WebView::FileDownloader::Download const*> const& downloads)+ {+ clear_rows();+ m_download_rows.ensure_capacity(downloads.size());++ for (auto* download : downloads) {+ auto* row = new DownloadRow(*download, m_rows_widget);+ row->on_cancel_download = [this](u64 id) {+ if (on_cancel_download)+ on_cancel_download(id);+ };+ row->on_pause_download = [this](u64 id) {+ if (on_pause_download)+ on_pause_download(id);+ };+ row->on_resume_download = [this](u64 id) {+ if (on_resume_download)+ on_resume_download(id);+ };+ row->on_show_in_folder = [this](u64 id) {+ if (on_show_in_folder)+ on_show_in_folder(id);+ };+ m_rows_layout->addWidget(row);+ m_download_rows.append(row);+ }+ }++ void clear_rows()+ {+ while (auto* item = m_rows_layout->takeAt(0)) {+ if (auto* widget = item->widget())+ delete widget;+ delete item;+ }+ m_download_rows.clear();+ }++ QLabel* m_empty_label { nullptr };+ QScrollArea* m_scroll_area { nullptr };+ QWidget* m_rows_widget { nullptr };+ QVBoxLayout* m_rows_layout { nullptr };+ Vector<DownloadRow*> m_download_rows;+};++WindowToolbar::WindowToolbar(WebView::IsPrivate is_private, QWidget* parent)+ : QWidget(parent)+ , m_is_private(is_private)+{+ auto& application = WebView::Application::the();++ setObjectName("LadybirdToolbarContainer");+ // NB: The style sheet paints the toolbar's background, which a QWidget subclass only draws when asked to.+ setAttribute(Qt::WA_StyledBackground);+ setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);++ m_toolbar = new QWidget(this);+ m_toolbar->setObjectName("LadybirdNavigationToolbar");+ m_toolbar->setFixedHeight(browser_chrome_layout_policy().toolbar_height);+ m_toolbar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);++ auto* toolbar_container_layout = new QVBoxLayout(this);+ toolbar_container_layout->setSpacing(0);+ // NB: The container draws the line under the toolbar along its bottom edge, below the toolbar's full height.+ toolbar_container_layout->setContentsMargins(0, 0, 0, 1);++ auto* toolbar_layout = new QHBoxLayout(m_toolbar);+ toolbar_layout->setSpacing(TOOLBAR_SPACING);+ toolbar_layout->setContentsMargins(TOOLBAR_HORIZONTAL_MARGIN, 0, toolbar_right_margin(false), 0);++ m_downloads_button = new DownloadsButton(m_toolbar);+ QObject::connect(m_downloads_button, &QToolButton::clicked, this, [this] {+ // NB: The button only stays checked while the list is open, which showing it sees to.+ m_downloads_button->setChecked(m_downloads_popover && m_downloads_popover->isVisible());+ show_downloads_popover();+ });++ m_navigate_back_action.action().setEnabled(false);+ m_navigate_forward_action.action().setEnabled(false);+ m_reload_action = create_application_action(*this, application.reload_action());+ // NB: Reloading without the cache has no button of its own, so its shortcut belongs to what holds the toolbar,+ // which stays shown when the toolbar does not.+ m_reload_bypassing_cache_action = create_application_action(*parent, application.reload_bypassing_cache_action());+ parent->addAction(m_reload_bypassing_cache_action);+ m_stop_action = new QAction("Stop", this);+ m_stop_action->setToolTip("Stop loading this page");+ QObject::connect(m_stop_action, &QAction::triggered, this, [this] {+ if (m_tab && m_tab->is_loaded())+ m_tab->view().stop_loading();+ });+ m_open_downloads_page_action = create_application_action(*this, application.open_downloads_page_action());++ m_window_controls_separator = new QWidget(m_toolbar);+ m_window_controls_separator->setObjectName("LadybirdToolbarWindowControlsSeparator");+ m_window_controls_separator->setFixedSize(1, 22);+ m_window_controls_separator->setAttribute(Qt::WA_TransparentForMouseEvents);++ if constexpr (use_native_macos_window_controls()) {+ auto const native_controls_width = NATIVE_MACOS_WINDOW_CONTROLS_WIDTH + NATIVE_MACOS_WINDOW_CONTROLS_GAP;+ m_window_controls = create_window_controls_spacer(*m_toolbar, "LadybirdToolbarWindowControls", { native_controls_width, 38 });+ } else {+ auto window_control_buttons = create_window_control_buttons(*m_toolbar, "LadybirdToolbarWindowControls", { 16, 16 }, { 38, 38 });+ m_window_controls = window_control_buttons.container;+ m_minimize_window_button = window_control_buttons.minimize;+ m_maximize_window_button = window_control_buttons.maximize;+ m_close_window_button = window_control_buttons.close;++ QObject::connect(m_minimize_window_button, &QToolButton::clicked, this, [this] {+ window()->showMinimized();+ });+ QObject::connect(m_maximize_window_button, &QToolButton::clicked, this, [this] {+ if (window()->isMaximized())+ window()->showNormal();+ else+ window()->showMaximized();+ });+ QObject::connect(m_close_window_button, &QToolButton::clicked, this, [this] {+ window()->close();+ });+ }++ auto* navigation_button_cluster = new QWidget(m_toolbar);+ navigation_button_cluster->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);+ auto* navigation_button_layout = new QHBoxLayout(navigation_button_cluster);+ navigation_button_layout->setSpacing(TOOLBAR_SPACING);+ navigation_button_layout->setContentsMargins(0, 0, 0, 0);++ navigation_button_layout->addWidget(create_navigation_history_toolbar_button(*navigation_button_cluster, m_navigate_back_action.action(), -1));+ navigation_button_layout->addWidget(create_navigation_history_toolbar_button(*navigation_button_cluster, m_navigate_forward_action.action(), 1));+ m_reload_button = create_toolbar_button<ReloadButton>(*navigation_button_cluster, *m_reload_action);+ m_reload_button->set_actions(*m_reload_action, *m_reload_bypassing_cache_action, *m_stop_action);+ m_reload_button->on_open_in_new_tab = [this](auto activate_tab) {+ if (m_tab)+ browser_window().duplicate_tab(*m_tab, activate_tab);+ };+ navigation_button_layout->addWidget(m_reload_button);++ auto* location_edit_container = new QWidget(m_toolbar);+ location_edit_container->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);+ m_location_edit_layout = new QHBoxLayout(location_edit_container);+ m_location_edit_layout->setSpacing(0);+ // NB: The field reaches the end of the toolbar, leaving room for its focus ring.+ m_location_edit_layout->setContentsMargins(TOOLBAR_LOCATION_EDIT_MARGIN, 0, TOOLBAR_LOCATION_FOCUS_RING_ROOM, 0);++ m_performance_monitor = new PerformanceMonitorWidget(m_toolbar);+ (void)WebView::TabPerformanceMonitor::the();+ m_performance_monitor->setVisible(WebView::Application::settings().config_variable_as_bool(WebView::ConfigVariableID::ShowTabPerformanceMonitor));++ if constexpr (use_native_macos_window_controls())+ toolbar_layout->addWidget(m_window_controls, 0, Qt::AlignVCenter);++ m_sidebar_slot = new QWidget(m_toolbar);+ m_sidebar_slot->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);+ auto* sidebar_slot_layout = new QHBoxLayout(m_sidebar_slot);+ // The Ladybird button keeps as clear of the traffic lights as on the tab strip.+ auto sidebar_slot_leading_margin = 0;+ if constexpr (use_native_macos_window_controls())+ sidebar_slot_leading_margin = NATIVE_MACOS_WINDOW_CONTROLS_INSET + NATIVE_MACOS_WINDOW_CONTROLS_WIDTH + LADYBIRD_BUTTON_TRAFFIC_LIGHTS_GAP - (TOOLBAR_HORIZONTAL_MARGIN + NATIVE_MACOS_WINDOW_CONTROLS_WIDTH + NATIVE_MACOS_WINDOW_CONTROLS_GAP + TOOLBAR_SPACING);+ sidebar_slot_layout->setContentsMargins(sidebar_slot_leading_margin, 0, 0, 0);+ sidebar_slot_layout->setSpacing(TOOLBAR_SPACING);+ // NB: The separator sits where the open sidebar's edge does, so the toolbar's contents keep their place as it toggles.+ sidebar_slot_layout->addSpacing(SIDEBAR_SLOT_SEPARATOR_MARGIN - 1);+ auto* sidebar_slot_separator = new QWidget(m_sidebar_slot);+ sidebar_slot_separator->setObjectName("LadybirdToolbarWindowControlsSeparator");+ sidebar_slot_separator->setFixedSize(1, 16);+ sidebar_slot_separator->setAttribute(Qt::WA_TransparentForMouseEvents);+ sidebar_slot_layout->addWidget(sidebar_slot_separator, 0, Qt::AlignVCenter);+ sidebar_slot_layout->addSpacing(SIDEBAR_SLOT_SEPARATOR_MARGIN);+ m_sidebar_slot->hide();+ toolbar_layout->addWidget(m_sidebar_slot, 0, Qt::AlignVCenter);++ toolbar_layout->addWidget(navigation_button_cluster, 0, Qt::AlignVCenter);+ toolbar_layout->addWidget(location_edit_container, 1);+ toolbar_layout->addWidget(m_downloads_button, 0, Qt::AlignVCenter);+ toolbar_layout->addWidget(m_performance_monitor, 0, Qt::AlignVCenter);++ if constexpr (!use_native_macos_window_controls()) {+ toolbar_layout->addWidget(m_window_controls_separator, 0, Qt::AlignVCenter);+ toolbar_layout->addWidget(m_window_controls, 0, Qt::AlignVCenter);+ }++ toolbar_container_layout->addWidget(m_toolbar);++ recreate_icons();+ update_chrome_style();+ set_window_controls_visible(false);+}++WindowToolbar::~WindowToolbar() = default;++int WindowToolbar::toolbar_height(bool)+{+ return browser_chrome_layout_policy().toolbar_height;+}++BrowserWindow& WindowToolbar::browser_window() const+{+ return as<BrowserWindow>(*window());+}++void WindowToolbar::bind(Tab* tab)+{+ if (m_tab)+ QObject::disconnect(m_tab, nullptr, this, nullptr);+ m_tab = tab;++ // A tab that hasn't loaded has no page yet, so it is shown once it has one.+ if (!tab || !tab->is_loaded()) {+ show_location_edit(nullptr);+ if (tab)+ QObject::connect(tab, &Tab::loaded, this, [this, tab] { show_tab(*tab); }, Qt::SingleShotConnection);+ return;+ }+ show_tab(*tab);+}++void WindowToolbar::show_tab(Tab& tab)+{+ auto& location_edit = tab.location_edit();+ if (m_location_edit_layout->indexOf(&location_edit) == -1)+ m_location_edit_layout->addWidget(&location_edit);+ show_location_edit(&location_edit);++ m_navigate_back_action.bind(tab.view().navigate_back_action());+ m_navigate_forward_action.bind(tab.view().navigate_forward_action());+ m_reload_button->show_loading_at_once(tab.is_loading());+ m_performance_monitor->set_stats(tab.performance_stats());+ set_bookmarks_bar_below(tab.has_bookmarks_bar_in_page());++ QObject::connect(&tab, &Tab::loading_changed, this, [this] {+ m_reload_button->set_loading(m_tab->is_loading());+ });+ QObject::connect(&tab, &Tab::performance_stats_changed, this, [this] {+ m_performance_monitor->set_stats(m_tab->performance_stats());+ });+}++void WindowToolbar::show_location_edit(LocationEdit* location_edit)+{+ if (m_location_edit && m_location_edit != location_edit)+ m_location_edit->hide();+ m_location_edit = location_edit;+ if (location_edit)+ location_edit->show();+}++// A tab that leaves the window takes its address field along.+void WindowToolbar::release_location_edit(Tab& tab)+{+ if (!tab.is_loaded())+ return;++ auto& location_edit = tab.location_edit();+ if (m_location_edit_layout->indexOf(&location_edit) == -1)+ return;+ if (m_location_edit == &location_edit)+ m_location_edit = nullptr;+ m_location_edit_layout->removeWidget(&location_edit);+ location_edit.hide();+ location_edit.setParent(&tab);+}++QToolButton* WindowToolbar::create_navigation_history_toolbar_button(QWidget& parent, QAction& action, int direction)+{+ auto* button = create_toolbar_button(parent, action);+ button->on_open_in_new_tab = [this, direction](auto activate_tab) {+ if (m_tab)+ browser_window().open_history_entry_in_new_tab(*m_tab, direction, activate_tab);+ };+ auto* menu = new QMenu(button);+ auto populate_menu = [this, menu, direction] {+ m_history_menu_tab = m_tab;+ if (m_tab && m_tab->is_loaded())+ populate_session_history_traversal_menu(*menu, m_tab->view(), direction);+ else+ menu->clear();+ };+ QObject::connect(menu, &QMenu::aboutToShow, button, populate_menu);+ QObject::connect(menu, &QMenu::triggered, button, [this](QAction* action) {+ auto tab = m_history_menu_tab;+ if (!tab || !tab->is_loaded())+ return;+ auto step = action->data().toInt();+ if (auto modifiers = QApplication::keyboardModifiers(); modifiers.testFlag(Qt::ControlModifier))+ browser_window().duplicate_tab(*tab, activate_tab_for_modifiers(modifiers), step);+ else+ tab->view().traverse_the_history_to_step(step);+ });+ button->setMenu(menu);+ button->setPopupMode(QToolButton::DelayedPopup);+ QObject::connect(button, &QToolButton::clicked, button, [button, direction] {+ if (!button->click_opens_in_new_tab())+ button->play_icon_motion(direction < 0 ? ToolbarButton::IconMotion::NudgeLeft : ToolbarButton::IconMotion::NudgeRight);+ });+ button->setContextMenuPolicy(Qt::CustomContextMenu);+ QObject::connect(button, &QToolButton::customContextMenuRequested, button, [button, menu, populate_menu](QPoint const&) {+ populate_menu();+ if (!menu->isEmpty())+ button->showMenu();+ });+ return button;+}++void WindowToolbar::set_window_controls_visible(bool visible)+{+ auto const has_native_window_controls = use_native_macos_window_controls() && visible;+ auto const has_custom_window_controls = !use_native_macos_window_controls() && 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);+ m_toolbar->layout()->invalidate();+}++// How far the navigation buttons sit past the toolbar's leading margin beside the collapsed sidebar, where the Ladybird+// button and the sidebar's toggle come before them.+int WindowToolbar::lead_beside_collapsed_sidebar(QWidget const& ladybird_buttons, QSize sidebar_toggle_size) const+{+ auto toolbar_spacing = m_toolbar->layout()->spacing();+ auto lead = 0;+ if constexpr (use_native_macos_window_controls())+ lead += m_window_controls->width() + toolbar_spacing;++ auto slot_width = m_sidebar_slot->layout()->sizeHint().width();+ if (ladybird_buttons.parentWidget() != m_sidebar_slot)+ slot_width += ladybird_buttons.sizeHint().width() + sidebar_toggle_size.width() + (2 * m_sidebar_slot->layout()->spacing());+ return lead + slot_width + toolbar_spacing;+}++// While the sidebar opens or closes, the toolbar's contents keep clear of where the collapsed sidebar's buttons sit.+void WindowToolbar::set_leading_room(int room)+{+ auto margins = m_toolbar->layout()->contentsMargins();+ if (margins.left() == TOOLBAR_HORIZONTAL_MARGIN + room)+ return;+ margins.setLeft(TOOLBAR_HORIZONTAL_MARGIN + room);+ 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();+}++void WindowToolbar::update_window_control_icons()+{+ if (!m_minimize_window_button || !m_maximize_window_button || !m_close_window_button)+ return;++ auto is_maximized = window()->isMaximized();+ m_minimize_window_button->setIcon(create_chrome_icon(ChromeIcon::WindowMinimize, Theme::chrome_palette(*this)));+ m_maximize_window_button->setIcon(create_chrome_icon(is_maximized ? ChromeIcon::WindowRestore : ChromeIcon::WindowMaximize, Theme::chrome_palette(*this)));+ m_maximize_window_button->setToolTip(is_maximized ? "Restore" : "Maximize");+ m_close_window_button->setIcon(create_chrome_icon(ChromeIcon::WindowClose, Theme::chrome_palette(*this)));+}++void WindowToolbar::set_vertical_tabs_enabled(bool enabled)+{+ // The page's separator supplies the bottom border, leaving the toolbar open above the sidebar.+ setProperty("verticalTabs", enabled);+ style()->unpolish(this);+ style()->polish(this);+ update();++ m_toolbar->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);+ m_toolbar->layout()->invalidate();+}++// NB: The bar below the toolbar draws the line under both.+void WindowToolbar::set_bookmarks_bar_below(bool below)+{+ if (property("bookmarksBarBelow").toBool() == below)+ return;+ setProperty("bookmarksBarBelow", below);+ style()->unpolish(this);+ style()->polish(this);+ update();+}++bool WindowToolbar::event(QEvent* event)+{+ // An inactive window's toolbar icons drop to the muted color.+ if (event->type() == QEvent::WindowActivate || event->type() == QEvent::WindowDeactivate)+ recreate_icons();++ if (event->type() == QEvent::PaletteChange) {+ update_chrome_style();+ recreate_icons();+ }++ return QWidget::event(event);+}++void WindowToolbar::tab_settings_changed()+{+ recreate_icons();+}++void WindowToolbar::config_variable_changed(WebView::ConfigVariableID variable)+{+ if (variable == WebView::ConfigVariableID::ShowTabPerformanceMonitor)+ m_performance_monitor->setVisible(WebView::Application::settings().config_variable_as_bool(variable));+}++void WindowToolbar::update_chrome_style()+{+ if (m_is_updating_chrome_style)+ return;+ m_is_updating_chrome_style = true;+ setStyleSheet(ChromeStyle::toolbar_container_style_sheet(Theme::chrome_palette(*this)));+ if (m_downloads_popover)+ m_downloads_popover->update_chrome_style(palette());+ m_is_updating_chrome_style = false;+}++void WindowToolbar::recreate_icons()+{+ auto activity = ChromeStyle::window_activity(*this);+ m_navigate_back_action.action().setIcon(create_chrome_icon(ChromeIcon::Back, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));+ m_navigate_forward_action.action().setIcon(create_chrome_icon(ChromeIcon::Forward, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));+ m_reload_action->setIcon(create_chrome_icon(ChromeIcon::Reload, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));+ m_stop_action->setIcon(create_chrome_icon(ChromeIcon::Stop, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));+ m_downloads_button->setIcon(create_chrome_icon(ChromeIcon::Download, Theme::chrome_palette(*this), activity, TOOLBAR_ICON_SIZE));+ update_window_control_icons();+}++void WindowToolbar::update_downloads_button()+{+ auto downloads = WebView::Application::the().file_downloader().downloads_of_session(m_is_private);+ auto button_state = WebView::downloads_button_state(downloads);++ if (!button_state.has_downloads && m_downloads_popover)+ m_downloads_popover->close();++ // The arrow drops into place as the button appears, and bobs once as a download finishes.+ auto was_visible = m_downloads_button->isVisible();+ m_downloads_button->setVisible(button_state.has_downloads);+ if (!was_visible && button_state.has_downloads)+ m_downloads_button->play_icon_motion(ToolbarButton::IconMotion::DropIn);+ else if (button_state.completed_download_count > m_completed_download_count)+ m_downloads_button->play_icon_motion(ToolbarButton::IconMotion::Bob);+ m_completed_download_count = button_state.completed_download_count;+ m_downloads_button->set_progress(button_state.active_download_count > 0, button_state.active_download_progress);+ browser_window().set_failed_download_count(button_state.failed_download_count);+ m_downloads_button->set_needs_attention(browser_window().has_unseen_failed_downloads());++ auto tooltip = qstring_from_ak_string(button_state.tooltip);+ if (m_downloads_button->toolTip() != tooltip) {+ m_downloads_button->setToolTip(tooltip);+ m_downloads_button->setAccessibleName(tooltip);+ }+}++void WindowToolbar::update_downloads_popover()+{+ if (!m_downloads_popover || !m_downloads_popover->isVisible())+ return;++ if (m_downloads_popover->set_downloads(WebView::Application::the().file_downloader().downloads_of_session(m_is_private)))+ position_downloads_popover();+}++// The downloads hang from the downloads button. Without any, the downloads page opens instead.+void WindowToolbar::show_downloads_popover()+{+ auto downloads = WebView::Application::the().file_downloader().downloads_of_session(m_is_private);+ if (!WebView::downloads_button_state(downloads).has_downloads) {+ m_open_downloads_page_action->trigger();+ return;+ }+ browser_window().acknowledge_failed_downloads();++ if (!m_downloads_popover) {+ m_downloads_popover = new DownloadsPopover(this);+ m_downloads_popover->on_visibility_change = [this](bool visible) {+ m_downloads_button->setChecked(visible);+ };+ m_downloads_popover->on_cancel_download = [this](u64 id) {+ auto& file_downloader = WebView::Application::the().file_downloader();+ if (auto download = file_downloader.download(id); download.has_value() && WebView::FileDownloader::status_is_active(download->status))+ file_downloader.cancel_download(id);+ update_downloads_popover();+ };+ m_downloads_popover->on_pause_download = [this](u64 id) {+ WebView::Application::the().file_downloader().pause_download(id);+ update_downloads_popover();+ };+ m_downloads_popover->on_resume_download = [this](u64 id) {+ WebView::Application::the().file_downloader().resume_download(id);+ update_downloads_popover();+ };+ m_downloads_popover->on_show_in_folder = [](u64 id) {+ if (auto download = WebView::Application::the().file_downloader().download(id); download.has_value())+ WebView::Application::the().show_download_in_folder(*download);+ };+ m_downloads_popover->on_clear_finished = [this] {+ (void)WebView::Application::the().file_downloader().prune_inactive_downloads(m_is_private);+ update_downloads_popover();+ };+ m_downloads_popover->on_open_all_downloads = [this] {+ if (m_downloads_popover)+ m_downloads_popover->close();+ m_open_downloads_page_action->trigger();+ };+ }++ m_downloads_popover->update_chrome_style(palette());+ (void)m_downloads_popover->set_downloads(WebView::Application::the().file_downloader().downloads_of_session(m_is_private));+ position_downloads_popover();+ m_downloads_popover->show();+ position_downloads_popover();+ m_downloads_popover->raise();+}++void WindowToolbar::position_downloads_popover()+{+ if (!m_downloads_popover)+ return;++ m_downloads_popover->adjustSize();+ auto& card = m_downloads_popover->card();+ auto size = card.sizeHint();+ size.setWidth(DOWNLOADS_POPOVER_WIDTH);+ if (size.height() > DOWNLOADS_POPOVER_MAX_HEIGHT)+ size.setHeight(DOWNLOADS_POPOVER_MAX_HEIGHT);+ card.setFixedSize(size);+ m_downloads_popover->adjustSize();++ move_popover_below(*m_downloads_popover, *m_downloads_button, PopoverAlignment::Trailing);+}++void WindowToolbar::download_added(WebView::FileDownloader::Download const& download)+{+ // NB: A window lists the downloads of its own browsing session only.+ if (download.is_private != m_is_private)+ return;+ update_downloads_button();+ if (browser_window().isActiveWindow()) {+ show_downloads_popover();+ return;+ }+ update_downloads_popover();+}++void WindowToolbar::download_updated(WebView::FileDownloader::Download const& download)+{+ if (download.is_private != m_is_private)+ return;+ update_downloads_button();+ update_downloads_popover();+}++void WindowToolbar::download_removed(u64, WebView::IsPrivate is_private)+{+ if (is_private != m_is_private)+ return;+ update_downloads_button();+ update_downloads_popover();+}++}diff --git a/UI/Qt/WindowToolbar.h b/UI/Qt/WindowToolbar.hnew file mode 100644index 00000000000..d3c968b4cbc--- /dev/null+++ b/UI/Qt/WindowToolbar.h@@ -0,0 +1,115 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/kmalloc.h>+#include <LibWebView/FileDownloader.h>+#include <LibWebView/Forward.h>+#include <LibWebView/Settings.h>+#include <UI/Qt/Menu.h>++#include <QPointer>+#include <QWidget>++class QAction;+class QHBoxLayout;+class QToolButton;++namespace Ladybird {++class BrowserWindow;+class DownloadsButton;+class DownloadsPopover;+class LocationEdit;+class PerformanceMonitorWidget;+class ReloadButton;+class Tab;++// A window has one toolbar, which shows the tab that is selected. Each tab keeps its own address field, which the+// toolbar holds while that tab is shown.+class WindowToolbar final+ : public QWidget+ , public WebView::FileDownloaderObserver+ , public WebView::SettingsObserver {+ Q_OBJECT++public:+ AK_ALLOC_WITH_KMALLOC;++ WindowToolbar(WebView::IsPrivate, QWidget* parent);+ virtual ~WindowToolbar() override;++ static int toolbar_height(bool window_controls_visible);++ void bind(Tab*);+ void release_location_edit(Tab&);++ // While the sidebar is collapsed, the toolbar holds the Ladybird button and the sidebar's toggle, after the window+ // controls, here.+ QWidget& sidebar_slot() { return *m_sidebar_slot; }+ void set_window_controls_visible(bool);+ int lead_beside_collapsed_sidebar(QWidget const& ladybird_buttons, QSize sidebar_toggle_size) const;+ void set_leading_room(int);+ void update_window_control_icons();+ void set_vertical_tabs_enabled(bool);+ void set_bookmarks_bar_below(bool);++ void show_downloads_popover();+ void update_downloads_button();++private:+ virtual bool event(QEvent*) override;++ virtual void tab_settings_changed() override;+ virtual void config_variable_changed(WebView::ConfigVariableID) override;++ virtual void download_added(WebView::FileDownloader::Download const&) override;+ virtual void download_updated(WebView::FileDownloader::Download const&) override;+ virtual void download_removed(u64, WebView::IsPrivate) override;++ BrowserWindow& browser_window() const;+ void show_tab(Tab&);+ void show_location_edit(LocationEdit*);+ QToolButton* create_navigation_history_toolbar_button(QWidget& parent, QAction&, int direction);+ void recreate_icons();+ void update_chrome_style();+ void update_downloads_popover();+ void position_downloads_popover();++ WebView::IsPrivate m_is_private { WebView::IsPrivate::No };+ QPointer<Tab> m_tab;+ // The tab whose history a back or forward button's menu lists, which stays the same while the menu is open.+ QPointer<Tab> m_history_menu_tab;++ QWidget* m_toolbar { nullptr };+ // NB: The fields of the tabs shown so far share this layout, which leaves room around them for the focus ring.+ QHBoxLayout* m_location_edit_layout { nullptr };+ QPointer<LocationEdit> m_location_edit;+ PerformanceMonitorWidget* m_performance_monitor { nullptr };+ QWidget* m_window_controls_separator { nullptr };+ QWidget* m_sidebar_slot { nullptr };+ QWidget* m_window_controls { nullptr };+ QToolButton* m_minimize_window_button { nullptr };+ QToolButton* m_maximize_window_button { nullptr };+ QToolButton* m_close_window_button { nullptr };++ ActionBinding m_navigate_back_action { *this, IncludeActionIcon::No };+ ActionBinding m_navigate_forward_action { *this, IncludeActionIcon::No };+ QAction* m_reload_action { nullptr };+ QAction* m_reload_bypassing_cache_action { nullptr };+ QAction* m_stop_action { nullptr };+ QAction* m_open_downloads_page_action { nullptr };+ ReloadButton* m_reload_button { nullptr };++ DownloadsButton* m_downloads_button { nullptr };+ QPointer<DownloadsPopover> m_downloads_popover;+ size_t m_completed_download_count { 0 };++ bool m_is_updating_chrome_style { false };+};++}