Selaa lähdekoodia

Ladybird: Add common handy actions to context menu

This commit adds the common actions you'd expect to the Ladybird context
menu, arranged like so:

	┌──────────────────────────────┐
	│  Go Back           Alt+Left  │
	│  Go Forward        Alt+Right │
	│  Reload            Ctrl+R    │
	│ ──────────────────────────── │
	│  Copy              Ctrl+C    │
	│  Select All        Ctrl+A    │
	│ ──────────────────────────── │
	│  View Source       Ctrl+U    │
	│  Inspect Element             │
	└──────────────────────────────┘
MacDue 2 vuotta sitten
vanhempi
commit
404804db36
4 muutettua tiedostoa jossa 65 lisäystä ja 35 poistoa
  1. 37 10
      Ladybird/BrowserWindow.cpp
  2. 20 0
      Ladybird/BrowserWindow.h
  3. 8 21
      Ladybird/Tab.cpp
  4. 0 4
      Ladybird/Tab.h

+ 37 - 10
Ladybird/BrowserWindow.cpp

@@ -333,6 +333,42 @@ BrowserWindow::BrowserWindow(Browser::CookieJar& cookie_jar, StringView webdrive
     setContextMenuPolicy(Qt::CustomContextMenu);
     QObject::connect(this, &QWidget::customContextMenuRequested, this, &BrowserWindow::show_context_menu);
 
+    m_context_menu = make<QMenu>("Context menu", this);
+    auto* inspect_element_action = new QAction("&Inspect Element", this);
+    connect(inspect_element_action, &QAction::triggered, this, [this] {
+        if (m_current_tab)
+            m_current_tab->view().show_inspector(WebContentView::InspectorTarget::HoveredElement);
+    });
+    m_go_back_action = make<QAction>("Go Back");
+    connect(m_go_back_action, &QAction::triggered, this, [this] {
+        if (m_current_tab)
+            m_current_tab->back();
+    });
+    m_go_forward_action = make<QAction>("Go Forward");
+    connect(m_go_forward_action, &QAction::triggered, this, [this] {
+        if (m_current_tab)
+            m_current_tab->forward();
+    });
+    m_reload_action = make<QAction>("&Reload");
+    connect(m_reload_action, &QAction::triggered, this, [this] {
+        if (m_current_tab)
+            m_current_tab->reload();
+    });
+    m_context_menu->addAction(m_go_back_action);
+    m_context_menu->addAction(m_go_forward_action);
+    m_context_menu->addAction(m_reload_action);
+    m_context_menu->addSeparator();
+    m_context_menu->addAction(copy_action);
+    m_context_menu->addAction(select_all_action);
+    m_context_menu->addSeparator();
+    m_context_menu->addAction(view_source_action);
+    m_context_menu->addAction(inspect_element_action);
+    m_go_back_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Back));
+    m_go_forward_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Forward));
+    m_reload_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Refresh));
+    m_go_back_action->setEnabled(false);
+    m_go_forward_action->setEnabled(false);
+
     new_tab(s_settings->new_tab_page(), Web::HTML::ActivateTab::Yes);
 
     setCentralWidget(m_tabs_container);
@@ -340,16 +376,7 @@ BrowserWindow::BrowserWindow(Browser::CookieJar& cookie_jar, StringView webdrive
 
 void BrowserWindow::show_context_menu(QPoint const& point)
 {
-    QMenu contextMenu("Context menu", this);
-
-    QAction inspect_action("&Inspect Element", this);
-    connect(&inspect_action, &QAction::triggered, this, [this] {
-        if (!m_current_tab)
-            return;
-        m_current_tab->view().show_inspector(WebContentView::InspectorTarget::HoveredElement);
-    });
-    contextMenu.addAction(&inspect_action);
-    contextMenu.exec(mapToGlobal(point));
+    m_context_menu->exec(mapToGlobal(point));
 }
 
 void BrowserWindow::set_current_tab(Tab* tab)

+ 20 - 0
Ladybird/BrowserWindow.h

@@ -32,6 +32,21 @@ public:
 
     int tab_index(Tab*);
 
+    QAction& go_back_action()
+    {
+        return *m_go_back_action;
+    }
+
+    QAction& go_forward_action()
+    {
+        return *m_go_forward_action;
+    }
+
+    QAction& reload_action()
+    {
+        return *m_reload_action;
+    }
+
 public slots:
     void tab_title_changed(int index, QString const&);
     void tab_favicon_changed(int index, QIcon icon);
@@ -68,6 +83,11 @@ private:
     Tab* m_current_tab { nullptr };
     QMenu* m_zoom_menu { nullptr };
 
+    OwnPtr<QMenu> m_context_menu {};
+    OwnPtr<QAction> m_go_back_action {};
+    OwnPtr<QAction> m_go_forward_action {};
+    OwnPtr<QAction> m_reload_action {};
+
     Browser::CookieJar& m_cookie_jar;
 
     StringView m_webdriver_content_ipc_path;

+ 8 - 21
Ladybird/Tab.cpp

@@ -74,20 +74,11 @@ Tab::Tab(BrowserWindow* window, StringView webdriver_content_ipc_path, WebView::
     m_layout->addWidget(m_toolbar);
     m_layout->addWidget(m_view);
 
-    m_back_action = make<QAction>("Back");
-    m_back_action->setEnabled(false);
-    m_back_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Back));
-    m_forward_action = make<QAction>("Forward");
-    m_forward_action->setEnabled(false);
-    m_forward_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Forward));
-    m_reload_action = make<QAction>("Reload");
-    m_reload_action->setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Refresh));
-
     rerender_toolbar_icons();
 
-    m_toolbar->addAction(m_back_action);
-    m_toolbar->addAction(m_forward_action);
-    m_toolbar->addAction(m_reload_action);
+    m_toolbar->addAction(&m_window->go_back_action());
+    m_toolbar->addAction(&m_window->go_forward_action());
+    m_toolbar->addAction(&m_window->reload_action());
     m_toolbar->addWidget(m_location_edit);
     m_reset_zoom_button->setToolTip("Reset zoom level");
     m_reset_zoom_button_action = m_toolbar->addWidget(m_reset_zoom_button);
@@ -143,16 +134,12 @@ Tab::Tab(BrowserWindow* window, StringView webdriver_content_ipc_path, WebView::
         }
         m_is_history_navigation = false;
 
-        m_back_action->setEnabled(m_history.can_go_back());
-        m_forward_action->setEnabled(m_history.can_go_forward());
+        m_window->go_back_action().setEnabled(m_history.can_go_back());
+        m_window->go_forward_action().setEnabled(m_history.can_go_forward());
     });
     QObject::connect(m_location_edit, &QLineEdit::returnPressed, this, &Tab::location_edit_return_pressed);
     QObject::connect(m_view, &WebContentView::title_changed, this, &Tab::page_title_changed);
     QObject::connect(m_view, &WebContentView::favicon_changed, this, &Tab::page_favicon_changed);
-
-    QObject::connect(m_back_action, &QAction::triggered, this, &Tab::back);
-    QObject::connect(m_forward_action, &QAction::triggered, this, &Tab::forward);
-    QObject::connect(m_reload_action, &QAction::triggered, this, &Tab::reload);
     QObject::connect(focus_location_editor_action, &QAction::triggered, this, &Tab::focus_location_editor);
 
     QObject::connect(m_view, &WebContentView::got_source, this, [this](AK::URL, QString const& source) {
@@ -300,7 +287,7 @@ bool Tab::event(QEvent* event)
 
 void Tab::rerender_toolbar_icons()
 {
-    m_back_action->setIcon(render_svg_icon_with_theme_colors("back", palette()));
-    m_forward_action->setIcon(render_svg_icon_with_theme_colors("forward", palette()));
-    m_reload_action->setIcon(render_svg_icon_with_theme_colors("reload", palette()));
+    m_window->go_back_action().setIcon(render_svg_icon_with_theme_colors("back", palette()));
+    m_window->go_forward_action().setIcon(render_svg_icon_with_theme_colors("forward", palette()));
+    m_window->reload_action().setIcon(render_svg_icon_with_theme_colors("reload", palette()));
 }

+ 0 - 4
Ladybird/Tab.h

@@ -67,10 +67,6 @@ private:
     QString m_title;
     QLabel* m_hover_label { nullptr };
 
-    OwnPtr<QAction> m_back_action;
-    OwnPtr<QAction> m_forward_action;
-    OwnPtr<QAction> m_reload_action;
-
     int tab_index();
 
     bool m_is_history_navigation { false };