From 71d92c72988375e7b6bd73b561379900093c3586 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 16 Apr 2026 23:20:18 +0200 Subject: [PATCH] UI/AppKit: Restore focus when activating a tab Tabs opened from links on AppKit can be created before their URL is loaded. That left background open-url tabs with the location field as their stored responder, so switching to them later restored address-bar focus instead of web content focus. Track each tab's preferred responder, restore it when AppKit makes the tab key, and mark page-backed open-url tabs to prefer the web view. Blank new-tab pages still keep the location field focused. --- UI/AppKit/Application/Application.mm | 4 ++++ UI/AppKit/Application/ApplicationDelegate.mm | 5 +++++ UI/AppKit/Interface/LadybirdWebViewWindow.h | 1 + UI/AppKit/Interface/LadybirdWebViewWindow.mm | 8 ++++++++ UI/AppKit/Interface/TabController.h | 2 ++ UI/AppKit/Interface/TabController.mm | 17 ++++++++++++++--- 6 files changed, 34 insertions(+), 3 deletions(-) diff --git a/UI/AppKit/Application/Application.mm b/UI/AppKit/Application/Application.mm index 30759182cc2..e46ad6e769a 100644 --- a/UI/AppKit/Application/Application.mm +++ b/UI/AppKit/Application/Application.mm @@ -51,6 +51,10 @@ Optional Application::open_blank_new_tab(Web::HTML ApplicationDelegate* delegate = [NSApp delegate]; auto* controller = [delegate createNewTab:activate_tab fromTab:[delegate activeTab]]; + if (activate_tab == Web::HTML::ActivateTab::Yes) + [controller focusWebView]; + else + [controller focusWebViewWhenActivated]; auto* tab = (Tab*)[controller window]; return [[tab web_view] view]; diff --git a/UI/AppKit/Application/ApplicationDelegate.mm b/UI/AppKit/Application/ApplicationDelegate.mm index 92141f09776..eb66f7ff99a 100644 --- a/UI/AppKit/Application/ApplicationDelegate.mm +++ b/UI/AppKit/Application/ApplicationDelegate.mm @@ -88,6 +88,9 @@ if (url.has_value()) { [controller loadURL:*url]; + + if (*url != WebView::Application::settings().new_tab_page_url()) + [controller focusWebView]; } return controller; @@ -104,6 +107,8 @@ [controller loadURL:*url]; } + [controller focusWebView]; + return controller; } diff --git a/UI/AppKit/Interface/LadybirdWebViewWindow.h b/UI/AppKit/Interface/LadybirdWebViewWindow.h index 771ce21fce8..53ab2880a47 100644 --- a/UI/AppKit/Interface/LadybirdWebViewWindow.h +++ b/UI/AppKit/Interface/LadybirdWebViewWindow.h @@ -16,5 +16,6 @@ windowRect:(NSRect)window_rect; @property (nonatomic, strong) LadybirdWebView* web_view; +@property (nonatomic, weak) NSResponder* preferred_first_responder; @end diff --git a/UI/AppKit/Interface/LadybirdWebViewWindow.mm b/UI/AppKit/Interface/LadybirdWebViewWindow.mm index b6dedb9c517..818c322bf00 100644 --- a/UI/AppKit/Interface/LadybirdWebViewWindow.mm +++ b/UI/AppKit/Interface/LadybirdWebViewWindow.mm @@ -52,4 +52,12 @@ [super setIsMiniaturized:flag]; } +- (void)becomeKeyWindow +{ + [super becomeKeyWindow]; + + if (self.preferred_first_responder && [self firstResponder] != self.preferred_first_responder) + [self makeFirstResponder:self.preferred_first_responder]; +} + @end diff --git a/UI/AppKit/Interface/TabController.h b/UI/AppKit/Interface/TabController.h index 705b81c8493..ac3e1192074 100644 --- a/UI/AppKit/Interface/TabController.h +++ b/UI/AppKit/Interface/TabController.h @@ -28,6 +28,8 @@ - (void)onEnterFullscreenWindow; - (void)onExitFullscreenWindow; +- (void)focusWebViewWhenActivated; +- (void)focusWebView; - (void)focusLocationToolbarItem; @end diff --git a/UI/AppKit/Interface/TabController.mm b/UI/AppKit/Interface/TabController.mm index 92eee870f04..112c2b94f63 100644 --- a/UI/AppKit/Interface/TabController.mm +++ b/UI/AppKit/Interface/TabController.mm @@ -329,9 +329,8 @@ static NSInteger autocomplete_suggestion_index(NSString* suggestion_text, Vector [self setLocationFieldText:url.serialize()]; // Don't steal focus from the location bar when loading the new tab page - if (url != WebView::Application::settings().new_tab_page_url()) { - [self.window makeFirstResponder:[self tab].web_view]; - } + if (url != WebView::Application::settings().new_tab_page_url()) + [self focusWebView]; } - (void)onEnterFullscreenWindow @@ -353,9 +352,21 @@ static NSInteger autocomplete_suggestion_index(NSString* suggestion_text, Vector - (void)focusLocationToolbarItem { + [self tab].preferred_first_responder = self.location_toolbar_item.view; [self.window makeFirstResponder:self.location_toolbar_item.view]; } +- (void)focusWebViewWhenActivated +{ + [self tab].preferred_first_responder = [self tab].web_view; +} + +- (void)focusWebView +{ + [self tab].preferred_first_responder = [self tab].web_view; + [self.window makeFirstResponder:[self tab].web_view]; +} + #pragma mark - Private methods - (Tab*)tab