Bladeren bron

Ladybird/AppKit: Implement history state change mechanics

We now appropriately update the current history item or create a new
history item in the chrome process.
Timothy Flynn 1 jaar geleden
bovenliggende
commit
45c4e1c446

+ 3 - 0
Ladybird/AppKit/UI/LadybirdWebView.h

@@ -12,6 +12,7 @@
 #include <LibWeb/CSS/PreferredColorScheme.h>
 #include <LibWeb/HTML/ActivateTab.h>
 #include <LibWeb/HTML/AudioPlayState.h>
+#include <LibWeb/HTML/HistoryHandlingBehavior.h>
 #include <LibWebView/Forward.h>
 
 #import <System/Cocoa.h>
@@ -28,6 +29,8 @@
 - (void)loadURL:(URL::URL const&)url;
 - (void)onLoadStart:(URL::URL const&)url isRedirect:(BOOL)is_redirect;
 - (void)onLoadFinish:(URL::URL const&)url;
+- (void)onURLUpdated:(URL::URL const&)url
+     historyBehavior:(Web::HTML::HistoryHandlingBehavior)history_behavior;
 
 - (void)onTitleChange:(ByteString const&)title;
 - (void)onFaviconChange:(Gfx::Bitmap const&)bitmap;

+ 4 - 0
Ladybird/AppKit/UI/LadybirdWebView.mm

@@ -282,6 +282,10 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
         [self.observer onLoadFinish:url];
     };
 
+    m_web_view_bridge->on_url_updated = [self](auto const& url, auto history_behavior) {
+        [self.observer onURLUpdated:url historyBehavior:history_behavior];
+    };
+
     m_web_view_bridge->on_title_change = [self](auto const& title) {
         [self.observer onTitleChange:title];
     };

+ 8 - 2
Ladybird/AppKit/UI/Tab.mm

@@ -221,12 +221,12 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
 
 - (void)onLoadStart:(URL::URL const&)url isRedirect:(BOOL)is_redirect
 {
-    [[self tabController] onLoadStart:url isRedirect:is_redirect];
-
     self.title = Ladybird::string_to_ns_string(url.serialize());
     self.favicon = [Tab defaultFavicon];
     [self updateTabTitleAndFavicon];
 
+    [[self tabController] onLoadStart:url isRedirect:is_redirect];
+
     if (self.inspector_controller != nil) {
         auto* inspector = (Inspector*)[self.inspector_controller window];
         [inspector reset];
@@ -241,6 +241,12 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
     }
 }
 
+- (void)onURLUpdated:(URL::URL const&)url
+     historyBehavior:(Web::HTML::HistoryHandlingBehavior)history_behavior
+{
+    [[self tabController] onURLUpdated:url historyBehavior:history_behavior];
+}
+
 - (void)onTitleChange:(ByteString const&)title
 {
     [[self tabController] onTitleChange:title];

+ 3 - 0
Ladybird/AppKit/UI/TabController.h

@@ -8,6 +8,7 @@
 
 #include <AK/Forward.h>
 #include <LibURL/URL.h>
+#include <LibWeb/HTML/HistoryHandlingBehavior.h>
 
 #import <System/Cocoa.h>
 
@@ -27,6 +28,8 @@ struct TabSettings {
 - (void)loadHTML:(StringView)html url:(URL::URL const&)url;
 
 - (void)onLoadStart:(URL::URL const&)url isRedirect:(BOOL)isRedirect;
+- (void)onURLUpdated:(URL::URL const&)url
+     historyBehavior:(Web::HTML::HistoryHandlingBehavior)history_behavior;
 - (void)onTitleChange:(ByteString const&)title;
 
 - (void)navigateBack:(id)sender;

+ 16 - 0
Ladybird/AppKit/UI/TabController.mm

@@ -133,6 +133,22 @@ enum class IsHistoryNavigation {
     [self updateNavigationButtonStates];
 }
 
+- (void)onURLUpdated:(URL::URL const&)url
+     historyBehavior:(Web::HTML::HistoryHandlingBehavior)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;
+    }
+
+    [self setLocationFieldText:url.serialize()];
+    [self updateNavigationButtonStates];
+}
+
 - (void)onTitleChange:(ByteString const&)title
 {
     m_title = title;