diff --git a/Ladybird/AppKit/Application/ApplicationDelegate.h b/Ladybird/AppKit/Application/ApplicationDelegate.h index 80fbf1ca4de..2012c907ac6 100644 --- a/Ladybird/AppKit/Application/ApplicationDelegate.h +++ b/Ladybird/AppKit/Application/ApplicationDelegate.h @@ -40,5 +40,6 @@ - (WebView::CookieJar&)cookieJar; - (Optional const&)webdriverContentIPCPath; - (Web::CSS::PreferredColorScheme)preferredColorScheme; +- (WebView::SearchEngine const&)searchEngine; @end diff --git a/Ladybird/AppKit/Application/ApplicationDelegate.mm b/Ladybird/AppKit/Application/ApplicationDelegate.mm index 2e1a4f5f2d3..6eca9e3df98 100644 --- a/Ladybird/AppKit/Application/ApplicationDelegate.mm +++ b/Ladybird/AppKit/Application/ApplicationDelegate.mm @@ -4,6 +4,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include + #import #import #import @@ -26,6 +28,8 @@ Optional m_webdriver_content_ipc_path; Web::CSS::PreferredColorScheme m_preferred_color_scheme; + + WebView::SearchEngine m_search_engine; } @property (nonatomic, strong) NSMutableArray* 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; diff --git a/Ladybird/AppKit/UI/TabController.mm b/Ladybird/AppKit/UI/TabController.mm index 5b2b48f340d..09505158625 100644 --- a/Ladybird/AppKit/UI/TabController.mm +++ b/Ladybird/AppKit/UI/TabController.mm @@ -5,6 +5,7 @@ */ #include +#include #include #import @@ -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]; }