Skip to content

Truly independent web browser

UI/Qt: Add a context menu to the draggable parts of the window chrome

Andreas Klingf708a92f46a3
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 5f525f0697f..2d719c75b0f 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -313,7 +313,7 @@ public:
m_reopen_recently_closed_tab_action = make_action("&Reopen Recently Closed Tab");
m_reopen_recently_closed_tab_action->setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_T));
- QObject::connect(m_reopen_recently_closed_tab_action, &QAction::triggered, this, []() { Application::the().reopen_recently_closed_tab(); });
+ QObject::connect(m_reopen_recently_closed_tab_action, &QAction::triggered, this, []() { Application::the().reopen_recently_closed_tab(Application::the().active_window_if_any()); });
update_reopen_recently_closed_action();
m_close_current_tab_action = make_action("&Close Current Tab");
@@ -739,9 +739,7 @@ void Application::open_new_tab()
return;
}
- auto& tab = m_active_window->new_tab_from_url(WebView::Application::settings().new_tab_page_url(), Web::HTML::ActivateTab::Yes, BrowserWindow::TabLocation::end());
- tab.set_url_is_hidden(true);
- tab.focus_location_editor();
+ m_active_window->open_new_tab();
}
void Application::open_new_window(WebView::IsPrivate is_private)
@@ -842,10 +840,10 @@ void Application::focus_location_editor()
tab->focus_location_editor();
}
-void Application::reopen_recently_closed_tab()
+void Application::reopen_recently_closed_tab(BrowserWindow* target_window)
{
- auto is_private = m_active_window ? m_active_window->is_private() : WebView::IsPrivate::No;
- auto& session = m_active_window ? m_active_window->session() : WebView::Application::default_session();
+ auto is_private = target_window ? target_window->is_private() : WebView::IsPrivate::No;
+ auto& session = target_window ? target_window->session() : WebView::Application::default_session();
auto closed_unit = session.session_store->take_most_recently_closed();
if (closed_unit.is_error()) {
dbgln("Unable to reopen the most recently closed session unit: {}", closed_unit.error());
@@ -856,7 +854,7 @@ void Application::reopen_recently_closed_tab()
auto unit = closed_unit.value().release_value();
- auto* window = m_active_window;
+ auto* window = target_window;
bool restoring_to_source_window = false;
if (!unit.was_window && unit.source_window_id.has_value()) {
for (auto* widget : QApplication::topLevelWidgets()) {
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index f9385e0e322..fff89844ef7 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -55,7 +55,7 @@ public:
void open_new_tab();
void open_new_window(WebView::IsPrivate);
- void reopen_recently_closed_tab();
+ void reopen_recently_closed_tab(BrowserWindow* target_window);
void open_file();
void restart_private_browsing_session();
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index 7ff4e06cd81..9413116f1e2 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -21,6 +21,7 @@
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/DevToolsBanner.h>
#include <UI/Qt/Icon.h>
+#include <UI/Qt/Menu.h>
#include <UI/Qt/StringUtils.h>
#if defined(AK_OS_MACOS)
# include <UI/Qt/MacWindow.h>
@@ -31,6 +32,7 @@
#include <QAbstractButton>
#include <QApplication>
+#include <QContextMenuEvent>
#include <QCursor>
#include <QGuiApplication>
#include <QHBoxLayout>
@@ -63,6 +65,11 @@ static constexpr int NATIVE_WINDOW_CONTROL_X_OFFSET = 6;
static constexpr int NATIVE_WINDOW_CONTROL_Y_OFFSET = 4;
#endif
+static bool is_empty_window_drag_region(QWidget const& widget, QPoint position)
+{
+ return widget.property(WINDOW_DRAG_REGION_PROPERTY).toBool() && widget.childAt(position) == nullptr;
+}
+
static int visible_browser_window_count()
{
int count = 0;
@@ -229,6 +236,7 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow
initialize_application_actions();
initialize_application_menu();
initialize_hamburger_menu();
+ initialize_window_context_menu();
update_chrome_style();
m_exit_button = new ExitFullscreenButton { this };
@@ -427,6 +435,45 @@ QAction* BrowserWindow::create_hamburger_zoom_actions()
return action;
}
+void BrowserWindow::initialize_window_context_menu()
+{
+ m_window_context_menu = new QMenu(this);
+
+ auto* new_tab_action = m_window_context_menu->addAction("New &Tab");
+ QObject::connect(new_tab_action, &QAction::triggered, this, &BrowserWindow::open_new_tab);
+
+ m_reopen_closed_tab_action = m_window_context_menu->addAction("&Reopen Closed Tab");
+ QObject::connect(m_reopen_closed_tab_action, &QAction::triggered, this, [this] {
+ Application::the().reopen_recently_closed_tab(this);
+ });
+
+ m_window_context_menu->addSeparator();
+
+ m_always_on_top_action = m_window_context_menu->addAction("Always on T&op");
+ m_always_on_top_action->setCheckable(true);
+ QObject::connect(m_always_on_top_action, &QAction::triggered, this, &BrowserWindow::set_always_on_top);
+
+ m_window_context_menu->addSeparator();
+
+ m_toggle_vertical_tabs_action = m_window_context_menu->addAction(QString {});
+ QObject::connect(m_toggle_vertical_tabs_action, &QAction::triggered, this, [] {
+ auto tab_settings = Application::settings().tab_settings();
+ tab_settings.vertical_tabs_enabled = !tab_settings.vertical_tabs_enabled;
+ Application::settings().set_tab_settings(tab_settings);
+ });
+}
+
+void BrowserWindow::show_window_context_menu(QPoint global_position)
+{
+ auto vertical_tabs_enabled = Application::settings().tab_settings().vertical_tabs_enabled;
+
+ m_reopen_closed_tab_action->setEnabled(m_session->session_store->has_closed_units());
+ m_always_on_top_action->setChecked(windowFlags().testFlag(Qt::WindowStaysOnTopHint));
+ m_toggle_vertical_tabs_action->setText(vertical_tabs_enabled ? "Switch to &Horizontal Tabs" : "Switch to &Vertical Tabs");
+
+ execute_context_menu(*m_window_context_menu, global_position);
+}
+
void BrowserWindow::update_hamburger_zoom_label()
{
auto zoom_level = round_to<int>(m_current_tab->view().zoom_level() * 100);
@@ -467,6 +514,13 @@ Tab& BrowserWindow::new_tab_from_url(URL::URL const& url, Web::HTML::ActivateTab
return tab;
}
+void BrowserWindow::open_new_tab()
+{
+ auto& tab = new_tab_from_url(WebView::Application::settings().new_tab_page_url(), Web::HTML::ActivateTab::Yes, TabLocation::end());
+ tab.set_url_is_hidden(true);
+ tab.focus_location_editor();
+}
+
void BrowserWindow::duplicate_tab(Tab& source_tab)
{
auto history = source_tab.view().session_history_snapshot();
@@ -1001,6 +1055,17 @@ bool BrowserWindow::start_window_move()
return handle->startSystemMove();
}
+void BrowserWindow::set_always_on_top(bool always_on_top)
+{
+ auto flags = windowFlags();
+ flags.setFlag(Qt::WindowStaysOnTopHint, always_on_top);
+
+ // NB: setWindowFlags() hides the window, so update the flags of the existing native window instead.
+ overrideWindowFlags(flags);
+ if (auto* handle = windowHandle())
+ handle->setFlags(flags);
+}
+
void BrowserWindow::tab_audio_play_state_changed(int index, Web::HTML::AudioPlayState play_state)
{
auto* tab = m_tabs_container->tab(index);
@@ -1195,6 +1260,17 @@ bool BrowserWindow::eventFilter(QObject* object, QEvent* event)
if (!has_chrome_in_titlebar())
return QMainWindow::eventFilter(object, event);
+ if (event->type() == QEvent::ContextMenu) {
+ auto* context_menu_event = static_cast<QContextMenuEvent*>(event);
+ auto position = context_menu_event->pos();
+ auto is_empty_menu_bar_area = widget == menuBar() && menuBar()->actionAt(position) == nullptr;
+ if (!is_empty_window_drag_region(*widget, position) && !is_empty_menu_bar_area)
+ return QMainWindow::eventFilter(object, event);
+
+ show_window_context_menu(context_menu_event->globalPos());
+ return true;
+ }
+
auto const is_button = qobject_cast<QAbstractButton*>(object) != nullptr;
if (is_button && (event->type() == QEvent::Enter || event->type() == QEvent::MouseMove || event->type() == QEvent::Leave)) {
@@ -1234,9 +1310,7 @@ bool BrowserWindow::eventFilter(QObject* object, QEvent* event)
}
}
- auto is_empty_window_drag_region = widget->property(WINDOW_DRAG_REGION_PROPERTY).toBool()
- && widget->childAt(mouse_event->position().toPoint()) == nullptr;
- if (is_empty_window_drag_region && !isFullScreen()) {
+ if (is_empty_window_drag_region(*widget, mouse_event->position().toPoint()) && !isFullScreen()) {
if (event->type() == QEvent::MouseButtonDblClick) {
toggle_window_maximized();
return true;
diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.h
index 2d428f0f6e6..51e63b73e68 100644
--- a/UI/Qt/BrowserWindow.h
+++ b/UI/Qt/BrowserWindow.h
@@ -167,6 +167,8 @@ public:
QMenu& hamburger_menu() const { return *m_hamburger_menu; }
static bool has_chrome_in_titlebar();
+ void show_window_context_menu(QPoint global_position);
+
template<typename Callback>
void for_each_tab(Callback&& callback)
{
@@ -194,6 +196,7 @@ public slots:
void tab_favicon_changed(int index, QIcon const& icon);
void tab_audio_play_state_changed(int index, Web::HTML::AudioPlayState);
Tab& new_tab_from_url(URL::URL const&, Web::HTML::ActivateTab, TabLocation);
+ void open_new_tab();
void activate_tab(int index);
bool definitely_close_tab(int index);
void move_tab(int old_index, int new_index);
@@ -240,6 +243,7 @@ private:
void initialize_application_actions();
void initialize_application_menu();
void initialize_hamburger_menu();
+ void initialize_window_context_menu();
QAction* create_hamburger_zoom_actions();
void update_hamburger_zoom_label();
@@ -255,6 +259,7 @@ private:
static bool uses_client_side_decorations();
void toggle_window_maximized();
bool start_window_move();
+ void set_always_on_top(bool);
QIcon icon_for_page_mute_state(Tab&) const;
QString tool_tip_for_page_mute_state(Tab&) const;
@@ -274,6 +279,11 @@ private:
QToolButton* m_menu_bar_close_window_button { nullptr };
QMenu* m_hamburger_menu { nullptr };
+
+ QMenu* m_window_context_menu { nullptr };
+ QAction* m_reopen_closed_tab_action { nullptr };
+ QAction* m_always_on_top_action { nullptr };
+ QAction* m_toggle_vertical_tabs_action { nullptr };
QPushButton* m_zoom_level { nullptr };
IsPopupWindow m_is_popup_window { IsPopupWindow::No };
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index 530f1bccb06..a0515fa089b 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -130,6 +130,12 @@ static QPointer<Tab> s_active_tab_dragged_tab;
static QPointer<TabWidget> s_pending_tab_drop_target;
static int s_pending_tab_drop_index { -1 };
+static void show_window_context_menu(QWidget& widget, QPoint global_position)
+{
+ if (auto* top_level_window = as_if<BrowserWindow>(widget.window()))
+ top_level_window->show_window_context_menu(global_position);
+}
+
static bool window_has_chrome_in_titlebar(QWidget& widget)
{
auto* top_level_window = qobject_cast<BrowserWindow*>(widget.window());
@@ -974,8 +980,10 @@ void TabBar::contextMenuEvent(QContextMenuEvent* event)
return;
auto tab_index = tab_index_at(event->pos());
- if (tab_index < 0)
+ if (tab_index < 0) {
+ show_window_context_menu(*this, event->globalPos());
return;
+ }
m_pressed_tab = nullptr;
if (auto* tab = m_tab_widget->tab(tab_index))
@@ -2393,18 +2401,18 @@ bool TabWidget::eventFilter(QObject* watched, QEvent* event)
}
if ((watched == m_tab_bar_row || watched == m_vertical_tab_bar_column) && window_has_chrome_in_titlebar(*this)) {
- auto is_empty_chrome_area = [this, watched](QMouseEvent const& mouse_event) {
+ auto is_empty_chrome_area = [this, watched](QPoint position) {
if (watched == m_vertical_tab_bar_column) {
- auto* child = m_vertical_tab_bar_column->childAt(mouse_event.pos());
+ auto* child = m_vertical_tab_bar_column->childAt(position);
return child == nullptr;
}
- return m_tab_bar_row->childAt(mouse_event.pos()) == nullptr;
+ return m_tab_bar_row->childAt(position) == nullptr;
};
if (event->type() == QEvent::MouseButtonDblClick) {
auto* mouse_event = static_cast<QMouseEvent*>(event);
- if (mouse_event->button() == Qt::LeftButton && is_empty_chrome_area(*mouse_event)) {
+ if (mouse_event->button() == Qt::LeftButton && is_empty_chrome_area(mouse_event->pos())) {
toggle_window_maximized();
return true;
}
@@ -2412,9 +2420,17 @@ bool TabWidget::eventFilter(QObject* watched, QEvent* event)
if (event->type() == QEvent::MouseButtonPress) {
auto* mouse_event = static_cast<QMouseEvent*>(event);
- if (mouse_event->button() == Qt::LeftButton && is_empty_chrome_area(*mouse_event) && start_window_move())
+ if (mouse_event->button() == Qt::LeftButton && is_empty_chrome_area(mouse_event->pos()) && start_window_move())
return true;
}
+
+ if (event->type() == QEvent::ContextMenu) {
+ auto* context_menu_event = static_cast<QContextMenuEvent*>(event);
+ if (is_empty_chrome_area(context_menu_event->pos())) {
+ show_window_context_menu(*this, context_menu_event->globalPos());
+ return true;
+ }
+ }
}
return QWidget::eventFilter(watched, event);