Ladybird/AppKit: Support searching with a search engine in the URL bar

The setting for the search engine to use is currently ephemeral. Once we
have a settings dialog, we can implement this setting there, and persist
that setting.
This commit is contained in:
Timothy Flynn 2023-10-20 09:28:53 -04:00 committed by Tim Flynn
parent e7d977f58d
commit f885839ba5
Notes: sideshowbarker 2024-07-17 00:27:16 +09:00
3 changed files with 54 additions and 1 deletions

View file

@ -40,5 +40,6 @@
- (WebView::CookieJar&)cookieJar;
- (Optional<StringView> const&)webdriverContentIPCPath;
- (Web::CSS::PreferredColorScheme)preferredColorScheme;
- (WebView::SearchEngine const&)searchEngine;
@end

View file

@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWebView/SearchEngine.h>
#import <Application/ApplicationDelegate.h>
#import <LibWebView/UserAgent.h>
#import <UI/LadybirdWebView.h>
@ -26,6 +28,8 @@
Optional<StringView> m_webdriver_content_ipc_path;
Web::CSS::PreferredColorScheme m_preferred_color_scheme;
WebView::SearchEngine m_search_engine;
}
@property (nonatomic, strong) NSMutableArray<TabController*>* managed_tabs;
@ -34,6 +38,7 @@
- (NSMenuItem*)createFileMenu;
- (NSMenuItem*)createEditMenu;
- (NSMenuItem*)createViewMenu;
- (NSMenuItem*)createSettingsMenu;
- (NSMenuItem*)createHistoryMenu;
- (NSMenuItem*)createInspectMenu;
- (NSMenuItem*)createDebugMenu;
@ -56,6 +61,7 @@
[[NSApp mainMenu] addItem:[self createFileMenu]];
[[NSApp mainMenu] addItem:[self createEditMenu]];
[[NSApp mainMenu] addItem:[self createViewMenu]];
[[NSApp mainMenu] addItem:[self createSettingsMenu]];
[[NSApp mainMenu] addItem:[self createHistoryMenu]];
[[NSApp mainMenu] addItem:[self createInspectMenu]];
[[NSApp mainMenu] addItem:[self createDebugMenu]];
@ -74,6 +80,7 @@
}
m_preferred_color_scheme = Web::CSS::PreferredColorScheme::Auto;
m_search_engine = WebView::default_search_engine();
// Reduce the tooltip delay, as the default delay feels quite long.
[[NSUserDefaults standardUserDefaults] setObject:@100 forKey:@"NSInitialToolTipDelay"];
@ -125,6 +132,11 @@
return m_preferred_color_scheme;
}
- (WebView::SearchEngine const&)searchEngine
{
return m_search_engine;
}
#pragma mark - Private methods
- (nonnull TabController*)createNewTab:(Web::HTML::ActivateTab)activate_tab
@ -190,6 +202,17 @@
}
}
- (void)setSearchEngine:(id)sender
{
auto* item = (NSMenuItem*)sender;
auto title = Ladybird::ns_string_to_string([item title]);
if (auto search_engine = WebView::find_search_engine(title); search_engine.has_value())
m_search_engine = search_engine.release_value();
else
m_search_engine = WebView::default_search_engine();
}
- (void)clearHistory:(id)sender
{
for (TabController* controller in self.managed_tabs) {
@ -325,6 +348,30 @@
return menu;
}
- (NSMenuItem*)createSettingsMenu
{
auto* menu = [[NSMenuItem alloc] init];
auto* submenu = [[NSMenu alloc] initWithTitle:@"Settings"];
auto* search_engine_menu = [[NSMenu alloc] init];
for (auto const& search_engine : WebView::search_engines()) {
[search_engine_menu addItem:[[NSMenuItem alloc] initWithTitle:Ladybird::string_to_ns_string(search_engine.name)
action:@selector(setSearchEngine:)
keyEquivalent:@""]];
}
auto* search_engine_menu_item = [[NSMenuItem alloc] initWithTitle:@"Search Engine"
action:nil
keyEquivalent:@""];
[search_engine_menu_item setSubmenu:search_engine_menu];
[submenu addItem:search_engine_menu_item];
[menu setSubmenu:submenu];
return menu;
}
- (NSMenuItem*)createHistoryMenu
{
auto* menu = [[NSMenuItem alloc] init];
@ -513,6 +560,9 @@
[item setState:(m_preferred_color_scheme == Dark) ? NSControlStateValueOn : NSControlStateValueOff];
} else if ([item action] == @selector(setLightPreferredColorScheme:)) {
[item setState:(m_preferred_color_scheme == Light) ? NSControlStateValueOn : NSControlStateValueOff];
} else if ([item action] == @selector(setSearchEngine:)) {
auto title = Ladybird::ns_string_to_string([item title]);
[item setState:(m_search_engine.name == title) ? NSControlStateValueOn : NSControlStateValueOff];
}
return YES;

View file

@ -5,6 +5,7 @@
*/
#include <LibWebView/History.h>
#include <LibWebView/SearchEngine.h>
#include <LibWebView/URL.h>
#import <Application/ApplicationDelegate.h>
@ -638,8 +639,9 @@ enum class IsHistoryNavigation {
}
auto url_string = Ladybird::ns_string_to_string([[text_view textStorage] string]);
auto* delegate = (ApplicationDelegate*)[NSApp delegate];
if (auto url = WebView::sanitize_url(url_string); url.has_value()) {
if (auto url = WebView::sanitize_url(url_string, [delegate searchEngine].query_url); url.has_value()) {
[self loadURL:*url];
}