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.
This commit is contained in:
Andreas Kling
2026-04-16 23:20:18 +02:00
committed by Andreas Kling
parent 1572429541
commit 71d92c7298
Notes: github-actions[bot] 2026-04-17 06:07:22 +00:00
6 changed files with 34 additions and 3 deletions

View File

@@ -51,6 +51,10 @@ Optional<WebView::ViewImplementation&> 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];

View File

@@ -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;
}

View File

@@ -16,5 +16,6 @@
windowRect:(NSRect)window_rect;
@property (nonatomic, strong) LadybirdWebView* web_view;
@property (nonatomic, weak) NSResponder* preferred_first_responder;
@end

View File

@@ -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

View File

@@ -28,6 +28,8 @@
- (void)onEnterFullscreenWindow;
- (void)onExitFullscreenWindow;
- (void)focusWebViewWhenActivated;
- (void)focusWebView;
- (void)focusLocationToolbarItem;
@end

View File

@@ -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