diff --git a/crates/search/src/project_search.rs b/crates/search/src/project_search.rs index ef905fa8e7902e47249ceecc0b374ab1ee301d31..30bb76197b8c85e040d8133d58bc47e3f32a336e 100644 --- a/crates/search/src/project_search.rs +++ b/crates/search/src/project_search.rs @@ -2,6 +2,7 @@ use crate::{ elements::ButtonSide, history::SearchHistory, mode::{SearchMode, Side}, + search_bar::render_nav_button, ActivateRegexMode, CycleMode, NextHistoryQuery, PreviousHistoryQuery, SearchOptions, SelectNextMatch, SelectPrevMatch, ToggleCaseSensitive, ToggleWholeWord, }; @@ -1322,91 +1323,6 @@ impl ProjectSearchBar { } } - fn render_nav_button( - &self, - icon: &'static str, - direction: Direction, - cx: &mut ViewContext, - ) -> AnyElement { - let action: Box; - let tooltip; - - match direction { - Direction::Prev => { - action = Box::new(SelectPrevMatch); - tooltip = "Select Previous Match"; - } - Direction::Next => { - action = Box::new(SelectNextMatch); - tooltip = "Select Next Match"; - } - }; - let tooltip_style = theme::current(cx).tooltip.clone(); - - enum NavButton {} - MouseEventHandler::::new(direction as usize, cx, |state, cx| { - let theme = theme::current(cx); - let mut style = theme.search.nav_button.style_for(state).clone(); - - match direction { - Direction::Prev => style.container.border.left = false, - Direction::Next => style.container.border.right = false, - }; - let label = Label::new(icon, style.label.clone()) - .contained() - .with_style(style.container.clone()); - match direction { - Direction::Prev => Flex::row() - .with_child( - ButtonSide::left( - style - .clone() - .container - .background_color - .unwrap_or_else(gpui::color::Color::transparent_black), - ) - .with_border(style.container.border.width, style.container.border.color) - .contained() - .constrained() - .with_max_width(theme.search.mode_filling_width), - ) - .with_child(label) - .constrained() - .with_height(theme.workspace.toolbar.height), - Direction::Next => Flex::row() - .with_child(label) - .with_child( - ButtonSide::right( - style - .clone() - .container - .background_color - .unwrap_or_else(gpui::color::Color::transparent_black), - ) - .with_border(style.container.border.width, style.container.border.color) - .contained() - .constrained() - .with_max_width(theme.search.mode_filling_width), - ) - .constrained() - .with_height(theme.workspace.toolbar.height), - } - }) - .on_click(MouseButton::Left, move |_, this, cx| { - if let Some(search) = this.active_project_search.as_ref() { - search.update(cx, |search, cx| search.select_match(direction, cx)); - } - }) - .with_cursor_style(CursorStyle::PointingHand) - .with_tooltip::( - direction as usize, - tooltip.to_string(), - Some(action), - tooltip_style, - cx, - ) - .into_any() - } fn render_option_button_icon( &self, icon: &'static str, @@ -1747,6 +1663,18 @@ impl View for ProjectSearchBar { let semantic_index = SemanticIndex::enabled(cx) .then(|| self.render_search_mode_button(SearchMode::Semantic, cx)); + let mut nav_button_for_direction = |label, direction| { + render_nav_button( + label, + direction, + move |_, this, cx| { + if let Some(search) = this.active_project_search.as_ref() { + search.update(cx, |search, cx| search.select_match(direction, cx)); + } + }, + cx, + ) + }; Flex::row() .with_child( Flex::column() @@ -1755,16 +1683,8 @@ impl View for ProjectSearchBar { .align_children_center() .with_child( Flex::row() - .with_child(self.render_nav_button( - "<", - Direction::Prev, - cx, - )) - .with_child(self.render_nav_button( - ">", - Direction::Next, - cx, - )) + .with_child(nav_button_for_direction("<", Direction::Prev)) + .with_child(nav_button_for_direction(">", Direction::Next)) .aligned(), ) .with_children(matches) diff --git a/crates/search/src/search_bar.rs b/crates/search/src/search_bar.rs index 5119e7866e3adc909213aec73bb5fef7b67b7c56..236509b86fa867a2cfda22f515456ee535816bec 100644 --- a/crates/search/src/search_bar.rs +++ b/crates/search/src/search_bar.rs @@ -1,9 +1,12 @@ use gpui::{ - elements::{MouseEventHandler, Svg}, + elements::{Flex, Label, MouseEventHandler, ParentElement, Svg}, platform::{CursorStyle, MouseButton}, scene::MouseClick, Action, AnyElement, Element, EventContext, View, ViewContext, }; +use workspace::searchable::Direction; + +use crate::{elements::ButtonSide, SelectNextMatch, SelectPrevMatch}; pub(super) fn render_close_button( theme: &theme::Search, @@ -32,3 +35,91 @@ pub(super) fn render_close_button( .with_tooltip::(0, tooltip.to_string(), dismiss_action, tooltip_style, cx) .into_any() } + +pub(super) fn render_nav_button( + icon: &'static str, + direction: Direction, + on_click: impl Fn(MouseClick, &mut V, &mut EventContext) + 'static, + cx: &mut ViewContext, +) -> AnyElement { + let action: Box; + let tooltip; + + match direction { + Direction::Prev => { + action = Box::new(SelectPrevMatch); + tooltip = "Select Previous Match"; + } + Direction::Next => { + action = Box::new(SelectNextMatch); + tooltip = "Select Next Match"; + } + }; + let tooltip_style = theme::current(cx).tooltip.clone(); + + enum NavButton {} + MouseEventHandler::::new(direction as usize, cx, |state, cx| { + let theme = theme::current(cx); + let mut style = theme.search.nav_button.style_for(state).clone(); + + match direction { + Direction::Prev => style.container.border.left = false, + Direction::Next => style.container.border.right = false, + }; + let label = Label::new(icon, style.label.clone()) + .contained() + .with_style(style.container.clone()); + match direction { + Direction::Prev => Flex::row() + .with_child( + ButtonSide::left( + style + .clone() + .container + .background_color + .unwrap_or_else(gpui::color::Color::transparent_black), + ) + .with_border(style.container.border.width, style.container.border.color) + .contained() + .constrained() + .with_max_width(theme.search.mode_filling_width), + ) + .with_child(label) + .constrained() + .with_height(theme.workspace.toolbar.height), + Direction::Next => Flex::row() + .with_child(label) + .with_child( + ButtonSide::right( + style + .clone() + .container + .background_color + .unwrap_or_else(gpui::color::Color::transparent_black), + ) + .with_border(style.container.border.width, style.container.border.color) + .contained() + .constrained() + .with_max_width(theme.search.mode_filling_width), + ) + .constrained() + .with_height(theme.workspace.toolbar.height), + } + }) + .on_click( + MouseButton::Left, + on_click, /*move |_, this, cx| { + if let Some(search) = this.active_project_search.as_ref() { + search.update(cx, |search, cx| search.select_match(direction, cx)); + }*/ + ) + .with_cursor_style(CursorStyle::PointingHand) + .with_tooltip::( + direction as usize, + tooltip.to_string(), + Some(action), + tooltip_style, + cx, + ) + .into_any() +}