From 7cf8eabb824ddb943368cc13aac7a3c2a46ff37b Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sun, 14 Apr 2024 15:49:22 -0400 Subject: [PATCH] Browser: Use LibWeb's history state for history navigation --- .../Applications/Browser/BrowserWindow.cpp | 2 +- .../Browser/History/HistoryModel.cpp | 4 +- .../Browser/History/HistoryModel.h | 13 ++- .../Browser/History/HistoryWidget.cpp | 4 +- .../Browser/History/HistoryWidget.h | 3 +- Userland/Applications/Browser/Tab.cpp | 96 +++++-------------- Userland/Applications/Browser/Tab.h | 18 ++-- 7 files changed, 48 insertions(+), 92 deletions(-) diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index b6573fb570..3bc591a1a9 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -377,7 +377,7 @@ void BrowserWindow::build_menus(StringView const man_file) }, this)); debug_menu->add_action(GUI::Action::create("Dump &History", { Mod_Ctrl, Key_H }, g_icon_bag.history, [this](auto&) { - active_tab().m_history.dump(); + active_tab().view().debug_request("dump-session-history"); })); debug_menu->add_action(GUI::Action::create("Dump C&ookies", g_icon_bag.cookie, [this](auto&) { m_cookie_jar.dump_cookies(); diff --git a/Userland/Applications/Browser/History/HistoryModel.cpp b/Userland/Applications/Browser/History/HistoryModel.cpp index 7894156606..fe92f035e0 100644 --- a/Userland/Applications/Browser/History/HistoryModel.cpp +++ b/Userland/Applications/Browser/History/HistoryModel.cpp @@ -9,10 +9,10 @@ namespace Browser { -void HistoryModel::set_items(AK::Vector items) +void HistoryModel::set_items(Vector items) { begin_insert_rows({}, m_entries.size(), m_entries.size()); - m_entries = items; + m_entries = move(items); end_insert_rows(); did_update(DontInvalidateIndices); diff --git a/Userland/Applications/Browser/History/HistoryModel.h b/Userland/Applications/Browser/History/HistoryModel.h index 30e64c5250..32647a3916 100644 --- a/Userland/Applications/Browser/History/HistoryModel.h +++ b/Userland/Applications/Browser/History/HistoryModel.h @@ -6,13 +6,20 @@ #pragma once +#include #include #include #include -#include +#include namespace Browser { +// FIXME: Reimplement viewing history entries using WebContent's history. +struct URLTitlePair { + URL::URL url; + ByteString title; +}; + class HistoryModel final : public GUI::Model { public: enum Column { @@ -21,7 +28,7 @@ public: __Count, }; - void set_items(AK::Vector items); + void set_items(Vector items); void clear_items(); virtual int row_count(GUI::ModelIndex const&) const override; virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return Column::__Count; } @@ -31,7 +38,7 @@ public: virtual GUI::Model::MatchResult data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override; private: - AK::Vector m_entries; + Vector m_entries; }; } diff --git a/Userland/Applications/Browser/History/HistoryWidget.cpp b/Userland/Applications/Browser/History/HistoryWidget.cpp index f3c594e4e0..bfb342a988 100644 --- a/Userland/Applications/Browser/History/HistoryWidget.cpp +++ b/Userland/Applications/Browser/History/HistoryWidget.cpp @@ -31,9 +31,9 @@ HistoryWidget::HistoryWidget() m_table_view->set_alternating_row_colors(true); } -void HistoryWidget::set_history_entries(Vector entries) +void HistoryWidget::set_history_entries(Vector entries) { - m_model->set_items(entries); + m_model->set_items(move(entries)); } void HistoryWidget::clear_history_entries() diff --git a/Userland/Applications/Browser/History/HistoryWidget.h b/Userland/Applications/Browser/History/HistoryWidget.h index 4f5c71649e..17309ceb12 100644 --- a/Userland/Applications/Browser/History/HistoryWidget.h +++ b/Userland/Applications/Browser/History/HistoryWidget.h @@ -10,7 +10,6 @@ #include #include #include -#include namespace Browser { @@ -20,7 +19,7 @@ class HistoryWidget final : public GUI::Widget { public: virtual ~HistoryWidget() override = default; - void set_history_entries(Vector entries); + void set_history_entries(Vector entries); void clear_history_entries(); private: diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index f5bfd63099..8e44b2eba0 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -43,7 +43,6 @@ #include #include #include -#include #include #include #include @@ -134,28 +133,18 @@ Tab::Tab(BrowserWindow& window) auto& go_back_button = toolbar.add_action(window.go_back_action()); go_back_button.on_context_menu_request = [&](auto&) { - if (!m_history.can_go_back()) + if (!m_can_navigate_back) return; - int i = 0; - m_go_back_context_menu = GUI::Menu::construct(); - for (auto& url : m_history.get_back_title_history()) { - i++; - m_go_back_context_menu->add_action(GUI::Action::create(url.to_byte_string(), g_icon_bag.filetype_html, [this, i](auto&) { go_back(i); })); - } - m_go_back_context_menu->popup(go_back_button.screen_relative_rect().bottom_left().moved_up(1)); + + // FIXME: Reimplement selecting a specific entry using WebContent's history. }; auto& go_forward_button = toolbar.add_action(window.go_forward_action()); go_forward_button.on_context_menu_request = [&](auto&) { - if (!m_history.can_go_forward()) + if (!m_can_navigate_forward) return; - int i = 0; - m_go_forward_context_menu = GUI::Menu::construct(); - for (auto& url : m_history.get_forward_title_history()) { - i++; - m_go_forward_context_menu->add_action(GUI::Action::create(url.to_byte_string(), g_icon_bag.filetype_html, [this, i](auto&) { go_forward(i); })); - } - m_go_forward_context_menu->popup(go_forward_button.screen_relative_rect().bottom_left().moved_up(1)); + + // FIXME: Reimplement selecting a specific entry using WebContent's history. }; auto& go_home_button = toolbar.add_action(window.go_home_action()); @@ -215,16 +204,10 @@ Tab::Tab(BrowserWindow& window) m_bookmark_button->set_icon(g_icon_bag.bookmark_contour); m_bookmark_button->set_fixed_size(22, 22); - view().on_load_start = [this](auto& url, bool is_redirect) { + view().on_load_start = [this](auto& url, bool) { m_navigating_url = url; m_loaded = false; - // If we are loading due to a redirect, we replace the current history entry - // with the loaded URL - if (is_redirect) { - m_history.replace_current(url, title()); - } - auto url_serialized = url.serialize(); m_title = url_serialized; @@ -235,17 +218,10 @@ Tab::Tab(BrowserWindow& window) if (on_favicon_change) on_favicon_change(*m_icon); - update_status(); - m_location_box->set_icon(nullptr); m_location_box->set_text(url_serialized); - // don't add to history if back or forward is pressed - if (!m_is_history_navigation) - m_history.push(url, title()); - m_is_history_navigation = false; - - update_actions(); + update_status(); update_bookmark_button(url_serialized); if (m_dom_inspector_widget) @@ -262,29 +238,25 @@ Tab::Tab(BrowserWindow& window) m_dom_inspector_widget->inspect(); }; - view().on_history_api_push_or_replace = [this](auto const& url, auto history_behavior) { - switch (history_behavior) { - case Web::HTML::HistoryHandlingBehavior::Push: - m_history.push(url, m_title); - break; - case Web::HTML::HistoryHandlingBehavior::Replace: - m_history.replace_current(url, m_title); - break; - } - + view().on_url_change = [this](auto const& url) { auto url_serialized = url.serialize(); m_location_box->set_text(url_serialized); - update_actions(); update_bookmark_button(url_serialized); }; + view().on_navigation_buttons_state_changed = [this](auto back_enabled, auto forward_enabled) { + m_can_navigate_back = back_enabled; + m_can_navigate_forward = forward_enabled; + update_actions(); + }; + view().on_navigate_back = [this]() { - go_back(1); + go_back(); }; view().on_navigate_forward = [this]() { - go_forward(1); + go_forward(); }; view().on_refresh = [this]() { @@ -499,7 +471,6 @@ Tab::Tab(BrowserWindow& window) }; view().on_title_change = [this](auto const& title) { - m_history.update_title(title); m_title = title; if (on_title_change) @@ -831,9 +802,8 @@ void Tab::update_reset_zoom_button() } } -void Tab::load(URL::URL const& url, LoadType load_type) +void Tab::load(URL::URL const& url) { - m_is_history_navigation = (load_type == LoadType::HistoryNavigation); m_web_content_view->load(url); m_location_box->set_focus(false); } @@ -845,30 +815,17 @@ URL::URL Tab::url() const void Tab::reload() { - if (m_history.is_empty()) - return; - - load(url()); + view().reload(); } -void Tab::go_back(int steps) +void Tab::go_back() { - if (!m_history.can_go_back(steps)) - return; - - m_history.go_back(steps); - update_actions(); - load(m_history.current().url, LoadType::HistoryNavigation); + view().traverse_the_history_by_delta(-1); } -void Tab::go_forward(int steps) +void Tab::go_forward() { - if (!m_history.can_go_forward(steps)) - return; - - m_history.go_forward(steps); - update_actions(); - load(m_history.current().url, LoadType::HistoryNavigation); + view().traverse_the_history_by_delta(1); } void Tab::update_actions() @@ -876,9 +833,8 @@ void Tab::update_actions() 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()); - window.reload_action().set_enabled(!m_history.is_empty()); + window.go_back_action().set_enabled(m_can_navigate_back); + window.go_forward_action().set_enabled(m_can_navigate_forward); } ErrorOr Tab::bookmark_current_url() @@ -1077,8 +1033,8 @@ void Tab::show_history_inspector() m_history_widget = history_window->set_main_widget(); } + // FIXME: Reimplement viewing history entries using WebContent's history. m_history_widget->clear_history_entries(); - m_history_widget->set_history_entries(m_history.get_all_history_entries()); auto* window = m_history_widget->window(); window->show(); diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index 6f68b7c24b..9726f1a279 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -14,7 +14,6 @@ #include #include #include -#include #include namespace WebView { @@ -40,16 +39,11 @@ public: URL::URL url() const; - enum class LoadType { - Normal, - HistoryNavigation, - }; - - void load(URL::URL const&, LoadType = LoadType::Normal); + void load(URL::URL const&); void reload(); - void go_back(int steps = 1); - void go_forward(int steps = 1); + void go_back(); + void go_forward(); void did_become_active(); void context_menu_requested(Gfx::IntPoint screen_position); @@ -109,8 +103,6 @@ private: void update_status(Optional text_override = {}, i32 count_waiting = 0); void close_sub_widgets(); - WebView::History m_history; - RefPtr m_web_content_view; RefPtr m_location_box; @@ -158,7 +150,9 @@ private: Optional m_navigating_url; bool m_loaded { false }; - bool m_is_history_navigation { false }; + + bool m_can_navigate_back { false }; + bool m_can_navigate_forward { false }; }; }