Selaa lähdekoodia

Browser: Add basic back/forward history

Andreas Kling 5 vuotta sitten
vanhempi
commit
89aaae82a1
2 muutettua tiedostoa jossa 92 lisäystä ja 6 poistoa
  1. 61 0
      Applications/Browser/History.h
  2. 31 6
      Applications/Browser/main.cpp

+ 61 - 0
Applications/Browser/History.h

@@ -0,0 +1,61 @@
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/String.h>
+#include <AK/Vector.h>
+
+template<typename T>
+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<T> m_items;
+    int m_current { -1 };
+};
+
+template<typename T>
+inline void History<T>::push(const T& item)
+{
+    m_items.shrink(m_current + 1);
+    m_items.append(item);
+    m_current++;
+}
+
+template<typename T>
+inline T History<T>::current() const
+{
+    if (m_current == -1)
+        return {};
+    return m_items[m_current];
+}
+
+template<typename T>
+inline void History<T>::go_back()
+{
+    ASSERT(can_go_back());
+    m_current--;
+}
+
+template<typename T>
+inline void History<T>::go_forward()
+{
+    ASSERT(can_go_forward());
+    m_current++;
+}
+
+template<typename T>
+inline void History<T>::clear()
+{
+    m_items = {};
+    m_current = -1;
+}

+ 31 - 6
Applications/Browser/main.cpp

@@ -1,3 +1,4 @@
+#include "History.h"
 #include <LibCore/CFile.h>
 #include <LibCore/CFile.h>
 #include <LibGUI/GAboutDialog.h>
 #include <LibGUI/GAboutDialog.h>
 #include <LibGUI/GAction.h>
 #include <LibGUI/GAction.h>
@@ -39,13 +40,34 @@ int main(int argc, char** argv)
     auto toolbar = GToolBar::construct(widget);
     auto toolbar = GToolBar::construct(widget);
     auto html_widget = HtmlView::construct(widget);
     auto html_widget = HtmlView::construct(widget);
 
 
-    toolbar->add_action(GCommonActions::make_go_back_action([&](auto&) {
-        // FIXME: Implement back action
-    }));
+    History<URL> history;
 
 
-    toolbar->add_action(GCommonActions::make_go_forward_action([&](auto&) {
-        // FIXME: Implement forward action
-    }));
+    RefPtr<GAction> go_back_action;
+    RefPtr<GAction> 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<bool> 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<bool> 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&) {
     toolbar->add_action(GCommonActions::make_go_home_action([&](auto&) {
         html_widget->load(home_url);
         html_widget->load(home_url);
@@ -63,6 +85,9 @@ int main(int argc, char** argv)
 
 
     html_widget->on_load_start = [&](auto& url) {
     html_widget->on_load_start = [&](auto& url) {
         location_box->set_text(url.to_string());
         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) {
     html_widget->on_link_click = [&](auto& url) {