Browser: Use LibWeb's history state for history navigation

This commit is contained in:
Timothy Flynn 2024-04-14 15:49:22 -04:00 committed by Alexander Kalenik
parent 8e2b1a8a1d
commit 7cf8eabb82
7 changed files with 48 additions and 92 deletions

View file

@ -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();

View file

@ -9,10 +9,10 @@
namespace Browser {
void HistoryModel::set_items(AK::Vector<WebView::History::URLTitlePair> items)
void HistoryModel::set_items(Vector<URLTitlePair> items)
{
begin_insert_rows({}, m_entries.size(), m_entries.size());
m_entries = items;
m_entries = move(items);
end_insert_rows();
did_update(DontInvalidateIndices);

View file

@ -6,13 +6,20 @@
#pragma once
#include <AK/ByteString.h>
#include <AK/Vector.h>
#include <LibGUI/Model.h>
#include <LibGUI/Widget.h>
#include <LibWebView/History.h>
#include <LibURL/URL.h>
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<WebView::History::URLTitlePair> items);
void set_items(Vector<URLTitlePair> 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<WebView::History::URLTitlePair> m_entries;
Vector<URLTitlePair> m_entries;
};
}

View file

@ -31,9 +31,9 @@ HistoryWidget::HistoryWidget()
m_table_view->set_alternating_row_colors(true);
}
void HistoryWidget::set_history_entries(Vector<WebView::History::URLTitlePair> entries)
void HistoryWidget::set_history_entries(Vector<URLTitlePair> entries)
{
m_model->set_items(entries);
m_model->set_items(move(entries));
}
void HistoryWidget::clear_history_entries()

View file

@ -10,7 +10,6 @@
#include <LibGUI/FilteringProxyModel.h>
#include <LibGUI/TextBox.h>
#include <LibGUI/Widget.h>
#include <LibWebView/History.h>
namespace Browser {
@ -20,7 +19,7 @@ class HistoryWidget final : public GUI::Widget {
public:
virtual ~HistoryWidget() override = default;
void set_history_entries(Vector<WebView::History::URLTitlePair> entries);
void set_history_entries(Vector<URLTitlePair> entries);
void clear_history_entries();
private:

View file

@ -43,7 +43,6 @@
#include <LibGUI/Window.h>
#include <LibURL/URL.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/HistoryHandlingBehavior.h>
#include <LibWeb/HTML/SelectedFile.h>
#include <LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.h>
#include <LibWeb/Layout/BlockContainer.h>
@ -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<void> Tab::bookmark_current_url()
@ -1077,8 +1033,8 @@ void Tab::show_history_inspector()
m_history_widget = history_window->set_main_widget<HistoryWidget>();
}
// 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();

View file

@ -14,7 +14,6 @@
#include <LibHTTP/Job.h>
#include <LibURL/URL.h>
#include <LibWeb/Forward.h>
#include <LibWebView/History.h>
#include <LibWebView/ViewImplementation.h>
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<String> text_override = {}, i32 count_waiting = 0);
void close_sub_widgets();
WebView::History m_history;
RefPtr<WebView::OutOfProcessWebView> m_web_content_view;
RefPtr<URLBox> m_location_box;
@ -158,7 +150,9 @@ private:
Optional<URL::URL> 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 };
};
}