From 89aaae82a1aa06ba2d8946e92354eaaeafc58772 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 17 Oct 2019 21:25:38 +0200 Subject: [PATCH] Browser: Add basic back/forward history --- Applications/Browser/History.h | 61 ++++++++++++++++++++++++++++++++++ Applications/Browser/main.cpp | 37 +++++++++++++++++---- 2 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 Applications/Browser/History.h diff --git a/Applications/Browser/History.h b/Applications/Browser/History.h new file mode 100644 index 0000000000..97d1edd8ab --- /dev/null +++ b/Applications/Browser/History.h @@ -0,0 +1,61 @@ +#pragma once + +#include +#include +#include + +template +class History final { +public: + void push(const T& item); + T current() const; + + void go_back(); + void go_forward(); + + bool can_go_back() { return m_current > 0; } + bool can_go_forward() { return m_current + 1 < m_items.size(); } + + void clear(); + +private: + Vector m_items; + int m_current { -1 }; +}; + +template +inline void History::push(const T& item) +{ + m_items.shrink(m_current + 1); + m_items.append(item); + m_current++; +} + +template +inline T History::current() const +{ + if (m_current == -1) + return {}; + return m_items[m_current]; +} + +template +inline void History::go_back() +{ + ASSERT(can_go_back()); + m_current--; +} + +template +inline void History::go_forward() +{ + ASSERT(can_go_forward()); + m_current++; +} + +template +inline void History::clear() +{ + m_items = {}; + m_current = -1; +} diff --git a/Applications/Browser/main.cpp b/Applications/Browser/main.cpp index 149dab1ff5..25365cfde1 100644 --- a/Applications/Browser/main.cpp +++ b/Applications/Browser/main.cpp @@ -1,3 +1,4 @@ +#include "History.h" #include #include #include @@ -39,13 +40,34 @@ int main(int argc, char** argv) auto toolbar = GToolBar::construct(widget); auto html_widget = HtmlView::construct(widget); - toolbar->add_action(GCommonActions::make_go_back_action([&](auto&) { - // FIXME: Implement back action - })); + History history; - toolbar->add_action(GCommonActions::make_go_forward_action([&](auto&) { - // FIXME: Implement forward action - })); + RefPtr go_back_action; + RefPtr go_forward_action; + + auto update_actions = [&]() { + go_back_action->set_enabled(history.can_go_back()); + go_forward_action->set_enabled(history.can_go_forward()); + }; + + bool should_push_loads_to_history = true; + + go_back_action = GCommonActions::make_go_back_action([&](auto&) { + history.go_back(); + update_actions(); + TemporaryChange change(should_push_loads_to_history, false); + html_widget->load(history.current()); + }); + + go_forward_action = GCommonActions::make_go_forward_action([&](auto&) { + history.go_forward(); + update_actions(); + TemporaryChange change(should_push_loads_to_history, false); + html_widget->load(history.current()); + }); + + toolbar->add_action(*go_back_action); + toolbar->add_action(*go_forward_action); toolbar->add_action(GCommonActions::make_go_home_action([&](auto&) { html_widget->load(home_url); @@ -63,6 +85,9 @@ int main(int argc, char** argv) html_widget->on_load_start = [&](auto& url) { location_box->set_text(url.to_string()); + if (should_push_loads_to_history) + history.push(url); + update_actions(); }; html_widget->on_link_click = [&](auto& url) {