Ladybird/AppKit: Use LibWeb's history state for history navigation

This commit is contained in:
Timothy Flynn 2024-04-14 16:54:30 -04:00 committed by Alexander Kalenik
commit e5c5dcca6a
Notes: sideshowbarker 2024-07-17 08:38:37 +09:00
5 changed files with 68 additions and 106 deletions

View file

@ -12,7 +12,6 @@
#include <LibWeb/CSS/PreferredColorScheme.h> #include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWeb/HTML/ActivateTab.h> #include <LibWeb/HTML/ActivateTab.h>
#include <LibWeb/HTML/AudioPlayState.h> #include <LibWeb/HTML/AudioPlayState.h>
#include <LibWeb/HTML/HistoryHandlingBehavior.h>
#include <LibWebView/Forward.h> #include <LibWebView/Forward.h>
#import <System/Cocoa.h> #import <System/Cocoa.h>
@ -29,17 +28,15 @@
- (void)loadURL:(URL::URL const&)url; - (void)loadURL:(URL::URL const&)url;
- (void)onLoadStart:(URL::URL const&)url isRedirect:(BOOL)is_redirect; - (void)onLoadStart:(URL::URL const&)url isRedirect:(BOOL)is_redirect;
- (void)onLoadFinish:(URL::URL const&)url; - (void)onLoadFinish:(URL::URL const&)url;
- (void)onURLUpdated:(URL::URL const&)url
historyBehavior:(Web::HTML::HistoryHandlingBehavior)history_behavior; - (void)onURLChange:(URL::URL const&)url;
- (void)onBackNavigationEnabled:(BOOL)back_enabled
forwardNavigationEnabled:(BOOL)forward_enabled;
- (void)onTitleChange:(ByteString const&)title; - (void)onTitleChange:(ByteString const&)title;
- (void)onFaviconChange:(Gfx::Bitmap const&)bitmap; - (void)onFaviconChange:(Gfx::Bitmap const&)bitmap;
- (void)onAudioPlayStateChange:(Web::HTML::AudioPlayState)play_state; - (void)onAudioPlayStateChange:(Web::HTML::AudioPlayState)play_state;
- (void)onNavigateBack;
- (void)onNavigateForward;
- (void)onReload;
@end @end
@interface LadybirdWebView : NSClipView <NSMenuDelegate> @interface LadybirdWebView : NSClipView <NSMenuDelegate>
@ -49,6 +46,10 @@
- (void)loadURL:(URL::URL const&)url; - (void)loadURL:(URL::URL const&)url;
- (void)loadHTML:(StringView)html; - (void)loadHTML:(StringView)html;
- (void)navigateBack;
- (void)navigateForward;
- (void)reload;
- (WebView::ViewImplementation&)view; - (WebView::ViewImplementation&)view;
- (String const&)handle; - (String const&)handle;

View file

@ -128,6 +128,21 @@ struct HideCursor {
m_web_view_bridge->load_html(html); m_web_view_bridge->load_html(html);
} }
- (void)navigateBack
{
m_web_view_bridge->traverse_the_history_by_delta(-1);
}
- (void)navigateForward
{
m_web_view_bridge->traverse_the_history_by_delta(1);
}
- (void)reload
{
m_web_view_bridge->reload();
}
- (WebView::ViewImplementation&)view - (WebView::ViewImplementation&)view
{ {
return *m_web_view_bridge; return *m_web_view_bridge;
@ -282,8 +297,13 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
[self.observer onLoadFinish:url]; [self.observer onLoadFinish:url];
}; };
m_web_view_bridge->on_history_api_push_or_replace = [self](auto const& url, auto history_behavior) { m_web_view_bridge->on_url_change = [self](auto const& url) {
[self.observer onURLUpdated:url historyBehavior:history_behavior]; [self.observer onURLChange:url];
};
m_web_view_bridge->on_navigation_buttons_state_changed = [self](auto back_enabled, auto forward_enabled) {
[self.observer onBackNavigationEnabled:back_enabled
forwardNavigationEnabled:forward_enabled];
}; };
m_web_view_bridge->on_title_change = [self](auto const& title) { m_web_view_bridge->on_title_change = [self](auto const& title) {
@ -402,15 +422,15 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
}; };
m_web_view_bridge->on_navigate_back = [self]() { m_web_view_bridge->on_navigate_back = [self]() {
[self.observer onNavigateBack]; [self navigateBack];
}; };
m_web_view_bridge->on_navigate_forward = [self]() { m_web_view_bridge->on_navigate_forward = [self]() {
[self.observer onNavigateForward]; [self navigateForward];
}; };
m_web_view_bridge->on_refresh = [self]() { m_web_view_bridge->on_refresh = [self]() {
[self.observer onReload]; [self reload];
}; };
m_web_view_bridge->on_enter_tooltip_area = [self](auto, auto const& tooltip) { m_web_view_bridge->on_enter_tooltip_area = [self](auto, auto const& tooltip) {

View file

@ -287,10 +287,16 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
} }
} }
- (void)onURLUpdated:(URL::URL const&)url - (void)onURLChange:(URL::URL const&)url
historyBehavior:(Web::HTML::HistoryHandlingBehavior)history_behavior
{ {
[[self tabController] onURLUpdated:url historyBehavior:history_behavior]; [[self tabController] onURLChange:url];
}
- (void)onBackNavigationEnabled:(BOOL)back_enabled
forwardNavigationEnabled:(BOOL)forward_enabled
{
[[self tabController] onBackNavigationEnabled:back_enabled
forwardNavigationEnabled:forward_enabled];
} }
- (void)onTitleChange:(ByteString const&)title - (void)onTitleChange:(ByteString const&)title
@ -343,21 +349,6 @@ static constexpr CGFloat const WINDOW_HEIGHT = 800;
} }
} }
- (void)onNavigateBack
{
[[self tabController] navigateBack:nil];
}
- (void)onNavigateForward
{
[[self tabController] navigateForward:nil];
}
- (void)onReload
{
[[self tabController] reload:nil];
}
#pragma mark - NSWindow #pragma mark - NSWindow
- (void)setIsVisible:(BOOL)flag - (void)setIsVisible:(BOOL)flag

View file

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

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibWebView/History.h>
#include <LibWebView/SearchEngine.h> #include <LibWebView/SearchEngine.h>
#include <LibWebView/URL.h> #include <LibWebView/URL.h>
@ -29,11 +28,6 @@ static NSString* const TOOLBAR_ZOOM_IDENTIFIER = @"ToolbarZoomIdentifier";
static NSString* const TOOLBAR_NEW_TAB_IDENTIFIER = @"ToolbarNewTabIdentifier"; static NSString* const TOOLBAR_NEW_TAB_IDENTIFIER = @"ToolbarNewTabIdentifier";
static NSString* const TOOLBAR_TAB_OVERVIEW_IDENTIFIER = @"ToolbarTabOverviewIdentifer"; static NSString* const TOOLBAR_TAB_OVERVIEW_IDENTIFIER = @"ToolbarTabOverviewIdentifer";
enum class IsHistoryNavigation {
Yes,
No,
};
@interface LocationSearchField : NSSearchField @interface LocationSearchField : NSSearchField
- (BOOL)becomeFirstResponder; - (BOOL)becomeFirstResponder;
@ -56,10 +50,10 @@ enum class IsHistoryNavigation {
{ {
ByteString m_title; ByteString m_title;
WebView::History m_history;
IsHistoryNavigation m_is_history_navigation;
TabSettings m_settings; TabSettings m_settings;
bool m_can_navigate_back;
bool m_can_navigate_forward;
} }
@property (nonatomic, strong) NSToolbar* toolbar; @property (nonatomic, strong) NSToolbar* toolbar;
@ -97,8 +91,9 @@ enum class IsHistoryNavigation {
[self.toolbar setAllowsUserCustomization:NO]; [self.toolbar setAllowsUserCustomization:NO];
[self.toolbar setSizeMode:NSToolbarSizeModeRegular]; [self.toolbar setSizeMode:NSToolbarSizeModeRegular];
m_is_history_navigation = IsHistoryNavigation::No;
m_settings = {}; m_settings = {};
m_can_navigate_back = false;
m_can_navigate_forward = false;
} }
return self; return self;
@ -118,41 +113,25 @@ enum class IsHistoryNavigation {
- (void)onLoadStart:(URL::URL const&)url isRedirect:(BOOL)isRedirect - (void)onLoadStart:(URL::URL const&)url isRedirect:(BOOL)isRedirect
{ {
if (isRedirect) {
m_history.replace_current(url, m_title);
}
[self setLocationFieldText:url.serialize()]; [self setLocationFieldText:url.serialize()];
if (m_is_history_navigation == IsHistoryNavigation::Yes) {
m_is_history_navigation = IsHistoryNavigation::No;
} else {
m_history.push(url, m_title);
}
[self updateNavigationButtonStates];
} }
- (void)onURLUpdated:(URL::URL const&)url - (void)onURLChange:(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 setLocationFieldText:url.serialize()];
}
- (void)onBackNavigationEnabled:(BOOL)back_enabled
forwardNavigationEnabled:(BOOL)forward_enabled
{
m_can_navigate_back = back_enabled;
m_can_navigate_forward = forward_enabled;
[self updateNavigationButtonStates]; [self updateNavigationButtonStates];
} }
- (void)onTitleChange:(ByteString const&)title - (void)onTitleChange:(ByteString const&)title
{ {
m_title = title; m_title = title;
m_history.update_title(m_title);
} }
- (void)zoomIn:(id)sender - (void)zoomIn:(id)sender
@ -175,55 +154,27 @@ enum class IsHistoryNavigation {
- (void)navigateBack:(id)sender - (void)navigateBack:(id)sender
{ {
if (!m_history.can_go_back()) { [[[self tab] web_view] navigateBack];
return;
}
m_is_history_navigation = IsHistoryNavigation::Yes;
m_history.go_back();
auto url = m_history.current().url;
[self loadURL:url];
} }
- (void)navigateForward:(id)sender - (void)navigateForward:(id)sender
{ {
if (!m_history.can_go_forward()) { [[[self tab] web_view] navigateForward];
return;
}
m_is_history_navigation = IsHistoryNavigation::Yes;
m_history.go_forward();
auto url = m_history.current().url;
[self loadURL:url];
} }
- (void)reload:(id)sender - (void)reload:(id)sender
{ {
if (m_history.is_empty()) { [[[self tab] web_view] reload];
return;
}
m_is_history_navigation = IsHistoryNavigation::Yes;
auto url = m_history.current().url;
[self loadURL:url];
} }
- (void)clearHistory - (void)clearHistory
{ {
m_history.clear(); // FIXME: Reimplement clearing history using WebContent's history.
[self updateNavigationButtonStates];
} }
- (void)debugRequest:(ByteString const&)request argument:(ByteString const&)argument - (void)debugRequest:(ByteString const&)request argument:(ByteString const&)argument
{ {
if (request == "dump-history") { [[[self tab] web_view] debugRequest:request argument:argument];
m_history.dump();
} else {
[[[self tab] web_view] debugRequest:request argument:argument];
}
} }
- (void)viewSource:(id)sender - (void)viewSource:(id)sender
@ -298,13 +249,10 @@ enum class IsHistoryNavigation {
- (void)updateNavigationButtonStates - (void)updateNavigationButtonStates
{ {
auto* navigate_back_button = (NSButton*)[[self navigate_back_toolbar_item] view]; auto* navigate_back_button = (NSButton*)[[self navigate_back_toolbar_item] view];
[navigate_back_button setEnabled:m_history.can_go_back()]; [navigate_back_button setEnabled:m_can_navigate_back];
auto* navigate_forward_button = (NSButton*)[[self navigate_forward_toolbar_item] view]; auto* navigate_forward_button = (NSButton*)[[self navigate_forward_toolbar_item] view];
[navigate_forward_button setEnabled:m_history.can_go_forward()]; [navigate_forward_button setEnabled:m_can_navigate_forward];
auto* reload_button = (NSButton*)[[self reload_toolbar_item] view];
[reload_button setEnabled:!m_history.is_empty()];
} }
- (void)showTabOverview:(id)sender - (void)showTabOverview:(id)sender
@ -357,7 +305,7 @@ enum class IsHistoryNavigation {
- (void)dumpHistory:(id)sender - (void)dumpHistory:(id)sender
{ {
[self debugRequest:"dump-history" argument:""]; [self debugRequest:"dump-session-history" argument:""];
} }
- (void)dumpLocalStorage:(id)sender - (void)dumpLocalStorage:(id)sender