From 3b56be61dc7472f40668ddbdbea211d6d6b036d6 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 31 May 2024 16:33:52 -0400 Subject: [PATCH] Ladybird/AppKit: Add a checkbox to enable case-sensitive find-in-page --- Ladybird/AppKit/UI/LadybirdWebView.h | 3 ++- Ladybird/AppKit/UI/LadybirdWebView.mm | 3 ++- Ladybird/AppKit/UI/SearchPanel.mm | 26 +++++++++++++++++++++----- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Ladybird/AppKit/UI/LadybirdWebView.h b/Ladybird/AppKit/UI/LadybirdWebView.h index 2de55459d41..491acdf7e80 100644 --- a/Ladybird/AppKit/UI/LadybirdWebView.h +++ b/Ladybird/AppKit/UI/LadybirdWebView.h @@ -60,7 +60,8 @@ - (void)setPreferredColorScheme:(Web::CSS::PreferredColorScheme)color_scheme; -- (void)findInPage:(NSString*)query; +- (void)findInPage:(NSString*)query + caseSensitivity:(CaseSensitivity)case_sensitivity; - (void)findInPageNextMatch; - (void)findInPagePreviousMatch; diff --git a/Ladybird/AppKit/UI/LadybirdWebView.mm b/Ladybird/AppKit/UI/LadybirdWebView.mm index abeeff77db9..6fff82a6d75 100644 --- a/Ladybird/AppKit/UI/LadybirdWebView.mm +++ b/Ladybird/AppKit/UI/LadybirdWebView.mm @@ -181,8 +181,9 @@ struct HideCursor { } - (void)findInPage:(NSString*)query + caseSensitivity:(CaseSensitivity)case_sensitivity { - m_web_view_bridge->find_in_page(Ladybird::ns_string_to_string(query)); + m_web_view_bridge->find_in_page(Ladybird::ns_string_to_string(query), case_sensitivity); } - (void)findInPageNextMatch diff --git a/Ladybird/AppKit/UI/SearchPanel.mm b/Ladybird/AppKit/UI/SearchPanel.mm index a16e7babd9d..1c408fea7d9 100644 --- a/Ladybird/AppKit/UI/SearchPanel.mm +++ b/Ladybird/AppKit/UI/SearchPanel.mm @@ -19,8 +19,12 @@ static constexpr CGFloat const SEARCH_FIELD_HEIGHT = 30; static constexpr CGFloat const SEARCH_FIELD_WIDTH = 300; @interface SearchPanel () +{ + CaseSensitivity m_case_sensitivity; +} @property (nonatomic, strong) NSSearchField* search_field; +@property (nonatomic, strong) NSButton* search_match_case; @end @@ -45,6 +49,12 @@ static constexpr CGFloat const SEARCH_FIELD_WIDTH = 300; [search_next setToolTip:@"Find Next Match"]; [search_next setBordered:NO]; + self.search_match_case = [NSButton checkboxWithTitle:@"Match Case" + target:self + action:@selector(find:)]; + [self.search_match_case setState:NSControlStateValueOff]; + m_case_sensitivity = CaseSensitivity::CaseInsensitive; + auto* search_done = [NSButton buttonWithTitle:@"Done" target:self action:@selector(cancelSearch:)]; @@ -54,6 +64,7 @@ static constexpr CGFloat const SEARCH_FIELD_WIDTH = 300; [self addView:self.search_field inGravity:NSStackViewGravityLeading]; [self addView:search_previous inGravity:NSStackViewGravityLeading]; [self addView:search_next inGravity:NSStackViewGravityLeading]; + [self addView:self.search_match_case inGravity:NSStackViewGravityLeading]; [self addView:search_done inGravity:NSStackViewGravityTrailing]; [self setOrientation:NSUserInterfaceLayoutOrientationHorizontal]; @@ -103,7 +114,7 @@ static constexpr CGFloat const SEARCH_FIELD_WIDTH = 300; if (![self isHidden]) { [self.search_field setStringValue:query]; - [[[self tab] web_view] findInPage:query]; + [[[self tab] web_view] findInPage:query caseSensitivity:m_case_sensitivity]; [self.window makeFirstResponder:self.search_field]; } @@ -129,10 +140,15 @@ static constexpr CGFloat const SEARCH_FIELD_WIDTH = 300; auto* query = [paste_board stringForType:NSPasteboardTypeString]; if (query) { - if (![[self.search_field stringValue] isEqual:query]) { - [self.search_field setStringValue:query]; - [[[self tab] web_view] findInPage:query]; + auto case_sensitivity = [self.search_match_case state] == NSControlStateValueOff + ? CaseSensitivity::CaseInsensitive + : CaseSensitivity::CaseSensitive; + if (case_sensitivity != m_case_sensitivity || ![[self.search_field stringValue] isEqual:query]) { + [self.search_field setStringValue:query]; + m_case_sensitivity = case_sensitivity; + + [[[self tab] web_view] findInPage:query caseSensitivity:m_case_sensitivity]; return YES; } } @@ -150,7 +166,7 @@ static constexpr CGFloat const SEARCH_FIELD_WIDTH = 300; - (void)controlTextDidChange:(NSNotification*)notification { auto* query = [self.search_field stringValue]; - [[[self tab] web_view] findInPage:query]; + [[[self tab] web_view] findInPage:query caseSensitivity:m_case_sensitivity]; [self setPasteBoardContents:query]; }