浏览代码

Browser: Move actions from Tab to BrowserWindow

Navigation actions (back/forward/home/reload) now live in BrowserWindow
instead of being duplicated in every new Tab instance.
Andreas Kling 4 年之前
父节点
当前提交
a743075b9f

+ 17 - 17
Userland/Applications/Browser/BrowserWindow.cpp

@@ -153,21 +153,21 @@ void BrowserWindow::build_menus()
         },
         this));
 
-    auto go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) { active_tab().go_back(); }, this);
-    auto go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) { active_tab().go_forward(); }, this);
-    auto go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) { active_tab().load(g_home_url); }, this);
-    go_home_action->set_status_tip("Go to home page");
-    auto reload_action = GUI::CommonActions::make_reload_action([this](auto&) { active_tab().reload(); }, this);
-    reload_action->set_status_tip("Reload current page");
+    m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) { active_tab().go_back(); }, this);
+    m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) { active_tab().go_forward(); }, this);
+    m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) { active_tab().load(g_home_url); }, this);
+    m_go_home_action->set_status_tip("Go to home page");
+    m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) { active_tab().reload(); }, this);
+    m_reload_action->set_status_tip("Reload current page");
 
     auto& go_menu = menubar->add_menu("&Go");
-    go_menu.add_action(*go_back_action);
-    go_menu.add_action(*go_forward_action);
-    go_menu.add_action(*go_home_action);
+    go_menu.add_action(*m_go_back_action);
+    go_menu.add_action(*m_go_forward_action);
+    go_menu.add_action(*m_go_home_action);
     go_menu.add_separator();
-    go_menu.add_action(*reload_action);
+    go_menu.add_action(*m_reload_action);
 
-    auto view_source_action = GUI::Action::create(
+    m_view_source_action = GUI::Action::create(
         "View &Source", { Mod_Ctrl, Key_U }, [this](auto&) {
             auto& tab = active_tab();
             if (tab.m_type == Tab::Type::InProcessWebView) {
@@ -180,9 +180,9 @@ void BrowserWindow::build_menus()
             }
         },
         this);
-    view_source_action->set_status_tip("View source code of the current page");
+    m_view_source_action->set_status_tip("View source code of the current page");
 
-    auto inspect_dom_tree_action = GUI::Action::create(
+    m_inspect_dom_tree_action = GUI::Action::create(
         "Inspect &DOM Tree", { Mod_None, Key_F12 }, [this](auto&) {
             auto& tab = active_tab();
             if (tab.m_type == Tab::Type::InProcessWebView) {
@@ -202,11 +202,11 @@ void BrowserWindow::build_menus()
             }
         },
         this);
-    inspect_dom_tree_action->set_status_tip("Open DOM inspector window for this page");
+    m_inspect_dom_tree_action->set_status_tip("Open DOM inspector window for this page");
 
     auto& inspect_menu = menubar->add_menu("&Inspect");
-    inspect_menu.add_action(*view_source_action);
-    inspect_menu.add_action(*inspect_dom_tree_action);
+    inspect_menu.add_action(*m_view_source_action);
+    inspect_menu.add_action(*m_inspect_dom_tree_action);
 
     auto js_console_action = GUI::Action::create(
         "Open &JS Console", { Mod_Ctrl, Key_I }, [this](auto&) {
@@ -440,7 +440,7 @@ void BrowserWindow::set_window_title_for_tab(Tab const& tab)
 void BrowserWindow::create_new_tab(URL url, bool activate)
 {
     auto type = Browser::g_single_process ? Browser::Tab::Type::InProcessWebView : Browser::Tab::Type::OutOfProcessWebView;
-    auto& new_tab = m_tab_widget->add_tab<Browser::Tab>("New tab", type);
+    auto& new_tab = m_tab_widget->add_tab<Browser::Tab>("New tab", *this, type);
 
     m_tab_widget->set_bar_visible(!is_fullscreen() && m_tab_widget->children().size() > 1);
 

+ 14 - 0
Userland/Applications/Browser/BrowserWindow.h

@@ -26,12 +26,26 @@ public:
     Tab& active_tab();
     void create_new_tab(URL, bool activate);
 
+    GUI::Action& go_back_action() { return *m_go_back_action; }
+    GUI::Action& go_forward_action() { return *m_go_forward_action; }
+    GUI::Action& go_home_action() { return *m_go_home_action; }
+    GUI::Action& reload_action() { return *m_reload_action; }
+    GUI::Action& view_source_action() { return *m_view_source_action; }
+    GUI::Action& inspect_dom_tree_action() { return *m_inspect_dom_tree_action; }
+
 private:
     explicit BrowserWindow(CookieJar&, URL);
 
     void build_menus();
     void set_window_title_for_tab(Tab const&);
 
+    RefPtr<GUI::Action> m_go_back_action;
+    RefPtr<GUI::Action> m_go_forward_action;
+    RefPtr<GUI::Action> m_go_home_action;
+    RefPtr<GUI::Action> m_reload_action;
+    RefPtr<GUI::Action> m_view_source_action;
+    RefPtr<GUI::Action> m_inspect_dom_tree_action;
+
     CookieJar& m_cookie_jar;
     WindowActions m_window_actions;
     RefPtr<GUI::TabWidget> m_tab_widget;

+ 32 - 25
Userland/Applications/Browser/Tab.cpp

@@ -8,6 +8,7 @@
 #include "Tab.h"
 #include "BookmarksBarWidget.h"
 #include "Browser.h"
+#include "BrowserWindow.h"
 #include "ConsoleWidget.h"
 #include "DownloadWidget.h"
 #include <AK/StringBuilder.h>
@@ -51,7 +52,7 @@ URL url_from_user_input(const String& input)
 
 void Tab::start_download(const URL& url)
 {
-    auto window = GUI::Window::construct(this->window());
+    auto window = GUI::Window::construct(&this->window());
     window->resize(300, 150);
     window->set_title(String::formatted("0% of {}", url.basename()));
     window->set_resizable(false);
@@ -62,7 +63,7 @@ void Tab::start_download(const URL& url)
 
 void Tab::view_source(const URL& url, const String& source)
 {
-    auto window = GUI::Window::construct(this->window());
+    auto window = GUI::Window::construct(&this->window());
     auto& editor = window->set_main_widget<GUI::TextEditor>();
     editor.set_text(source);
     editor.set_mode(GUI::TextEditor::ReadOnly);
@@ -74,7 +75,7 @@ void Tab::view_source(const URL& url, const String& source)
     [[maybe_unused]] auto& unused = window.leak_ref();
 }
 
-Tab::Tab(Type type)
+Tab::Tab(BrowserWindow& window, Type type)
     : m_type(type)
 {
     load_from_gml(tab_gml);
@@ -89,19 +90,10 @@ Tab::Tab(Type type)
     else
         m_web_content_view = webview_container.add<Web::OutOfProcessWebView>();
 
-    m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) { go_back(); }, this);
-    m_go_forward_action = GUI::CommonActions::make_go_forward_action([this](auto&) { go_forward(); }, this);
-    m_go_home_action = GUI::CommonActions::make_go_home_action([this](auto&) { load(g_home_url); }, this);
-    m_go_home_action->set_status_tip("Go to home page");
-
-    toolbar.add_action(*m_go_back_action);
-    toolbar.add_action(*m_go_forward_action);
-    toolbar.add_action(*m_go_home_action);
-
-    m_reload_action = GUI::CommonActions::make_reload_action([this](auto&) { reload(); }, this);
-    m_reload_action->set_status_tip("Reload current page");
-
-    toolbar.add_action(*m_reload_action);
+    toolbar.add_action(window.go_back_action());
+    toolbar.add_action(window.go_forward_action());
+    toolbar.add_action(window.go_home_action());
+    toolbar.add_action(window.reload_action());
 
     m_location_box = toolbar.add<GUI::TextBox>();
     m_location_box->set_placeholder("Address");
@@ -277,19 +269,19 @@ Tab::Tab(Type type)
 
     m_tab_context_menu = GUI::Menu::construct();
     m_tab_context_menu->add_action(GUI::Action::create("&Reload Tab", [this](auto&) {
-        m_reload_action->activate();
+        this->window().reload_action().activate();
     }));
     m_tab_context_menu->add_action(GUI::Action::create("&Close Tab", [this](auto&) {
         on_tab_close_request(*this);
     }));
 
     m_page_context_menu = GUI::Menu::construct();
-    m_page_context_menu->add_action(*m_go_back_action);
-    m_page_context_menu->add_action(*m_go_forward_action);
-    m_page_context_menu->add_action(*m_reload_action);
+    m_page_context_menu->add_action(window.go_back_action());
+    m_page_context_menu->add_action(window.go_forward_action());
+    m_page_context_menu->add_action(window.reload_action());
     m_page_context_menu->add_separator();
-    //m_page_context_menu->add_action(*view_source_action);
-    //m_page_context_menu->add_action(*inspect_dom_tree_action);
+    m_page_context_menu->add_action(window.view_source_action());
+    m_page_context_menu->add_action(window.inspect_dom_tree_action());
     hooks().on_context_menu_request = [&](auto& screen_position) {
         m_page_context_menu->popup(screen_position);
     };
@@ -337,8 +329,11 @@ void Tab::go_forward()
 
 void Tab::update_actions()
 {
-    m_go_back_action->set_enabled(m_history.can_go_back());
-    m_go_forward_action->set_enabled(m_history.can_go_forward());
+    auto& window = this->window();
+    if (this != &window.active_tab())
+        return;
+    window.go_back_action().set_enabled(m_history.can_go_back());
+    window.go_forward_action().set_enabled(m_history.can_go_forward());
 }
 
 void Tab::update_bookmark_button(const String& url)
@@ -378,9 +373,11 @@ void Tab::did_become_active()
     BookmarksBarWidget::the().remove_from_parent();
     m_toolbar_container->add_child(BookmarksBarWidget::the());
 
-    auto is_fullscreen = window()->is_fullscreen();
+    auto is_fullscreen = window().is_fullscreen();
     m_toolbar_container->set_visible(!is_fullscreen);
     m_statusbar->set_visible(!is_fullscreen);
+
+    update_actions();
 }
 
 void Tab::context_menu_requested(const Gfx::IntPoint& screen_position)
@@ -412,4 +409,14 @@ void Tab::action_left(GUI::Action&)
     m_statusbar->set_override_text({});
 }
 
+BrowserWindow const& Tab::window() const
+{
+    return static_cast<BrowserWindow const&>(*Widget::window());
+}
+
+BrowserWindow& Tab::window()
+{
+    return static_cast<BrowserWindow&>(*Widget::window());
+}
+
 }

+ 6 - 5
Userland/Applications/Browser/Tab.h

@@ -21,6 +21,8 @@ class WebViewHooks;
 
 namespace Browser {
 
+class BrowserWindow;
+
 class Tab final : public GUI::Widget {
     C_OBJECT(Tab);
 
@@ -67,7 +69,10 @@ public:
     GUI::AbstractScrollableWidget& view();
 
 private:
-    explicit Tab(Type);
+    explicit Tab(BrowserWindow&, Type);
+
+    BrowserWindow const& window() const;
+    BrowserWindow& window();
 
     Web::WebViewHooks& hooks();
     void update_actions();
@@ -82,10 +87,6 @@ private:
     RefPtr<Web::InProcessWebView> m_page_view;
     RefPtr<Web::OutOfProcessWebView> m_web_content_view;
 
-    RefPtr<GUI::Action> m_go_back_action;
-    RefPtr<GUI::Action> m_go_forward_action;
-    RefPtr<GUI::Action> m_go_home_action;
-    RefPtr<GUI::Action> m_reload_action;
     RefPtr<GUI::TextBox> m_location_box;
     RefPtr<GUI::Button> m_bookmark_button;
     RefPtr<GUI::Window> m_dom_inspector_window;