Move option button rendering to search_bar

Piotr Osiewicz created

Change summary

crates/search/src/project_search.rs | 69 ++++++++----------------------
crates/search/src/search_bar.rs     | 35 +++++++++++++++
2 files changed, 52 insertions(+), 52 deletions(-)

Detailed changes

crates/search/src/project_search.rs 🔗

@@ -1312,41 +1312,6 @@ impl ProjectSearchBar {
         }
     }
 
-    fn render_option_button_icon(
-        &self,
-        icon: &'static str,
-        option: SearchOptions,
-        cx: &mut ViewContext<Self>,
-    ) -> AnyElement<Self> {
-        let tooltip_style = theme::current(cx).tooltip.clone();
-        let is_active = self.is_option_enabled(option, cx);
-        MouseEventHandler::<Self, _>::new(option.bits as usize, cx, |state, cx| {
-            let theme = theme::current(cx);
-            let style = theme
-                .search
-                .option_button
-                .in_state(is_active)
-                .style_for(state);
-            Svg::new(icon)
-                .with_color(style.text.color.clone())
-                .contained()
-                .with_style(style.container)
-                .constrained()
-        })
-        .on_click(MouseButton::Left, move |_, this, cx| {
-            this.toggle_search_option(option, cx);
-        })
-        .with_cursor_style(CursorStyle::PointingHand)
-        .with_tooltip::<Self>(
-            option.bits as usize,
-            format!("Toggle {}", option.label()),
-            Some(option.to_toggle_action()),
-            tooltip_style,
-            cx,
-        )
-        .into_any()
-    }
-
     fn activate_search_mode(&self, mode: SearchMode, cx: &mut ViewContext<Self>) {
         // Update Current Mode
         if let Some(search_view) = self.active_project_search.as_ref() {
@@ -1478,26 +1443,28 @@ impl View for ProjectSearchBar {
             };
             let search = _search.read(cx);
             let is_semantic_disabled = search.semantic_state.is_none();
-
-            let case_sensitive = if is_semantic_disabled {
-                Some(self.render_option_button_icon(
+            let render_option_button_icon = |path, option, cx: &mut ViewContext<Self>| {
+                crate::search_bar::render_option_button_icon(
+                    self.is_option_enabled(option, cx),
+                    path,
+                    option,
+                    move |_, this, cx| {
+                        this.toggle_search_option(option, cx);
+                    },
+                    cx,
+                )
+            };
+            let case_sensitive = is_semantic_disabled.then(|| {
+                render_option_button_icon(
                     "icons/case_insensitive_12.svg",
                     SearchOptions::CASE_SENSITIVE,
                     cx,
-                ))
-            } else {
-                None
-            };
+                )
+            });
 
-            let whole_word = if is_semantic_disabled {
-                Some(self.render_option_button_icon(
-                    "icons/word_search_12.svg",
-                    SearchOptions::WHOLE_WORD,
-                    cx,
-                ))
-            } else {
-                None
-            };
+            let whole_word = is_semantic_disabled.then(|| {
+                render_option_button_icon("icons/word_search_12.svg", SearchOptions::WHOLE_WORD, cx)
+            });
 
             let search = _search.read(cx);
             let icon_style = theme.search.editor_icon.clone();

crates/search/src/search_bar.rs 🔗

@@ -9,7 +9,7 @@ use workspace::searchable::Direction;
 use crate::{
     elements::ButtonSide,
     mode::{SearchMode, Side},
-    SelectNextMatch, SelectPrevMatch,
+    SearchOptions, SelectNextMatch, SelectPrevMatch,
 };
 
 pub(super) fn render_close_button<V: View>(
@@ -210,3 +210,36 @@ pub(crate) fn render_search_mode_button<V: View>(
     )
     .into_any()
 }
+
+pub(crate) fn render_option_button_icon<V: View>(
+    is_active: bool,
+    icon: &'static str,
+    option: SearchOptions,
+    on_click: impl Fn(MouseClick, &mut V, &mut EventContext<V>) + 'static,
+    cx: &mut ViewContext<V>,
+) -> AnyElement<V> {
+    let tooltip_style = theme::current(cx).tooltip.clone();
+    MouseEventHandler::<V, _>::new(option.bits as usize, cx, |state, cx| {
+        let theme = theme::current(cx);
+        let style = theme
+            .search
+            .option_button
+            .in_state(is_active)
+            .style_for(state);
+        Svg::new(icon)
+            .with_color(style.text.color.clone())
+            .contained()
+            .with_style(style.container)
+            .constrained()
+    })
+    .on_click(MouseButton::Left, on_click)
+    .with_cursor_style(CursorStyle::PointingHand)
+    .with_tooltip::<V>(
+        option.bits as usize,
+        format!("Toggle {}", option.label()),
+        Some(option.to_toggle_action()),
+        tooltip_style,
+        cx,
+    )
+    .into_any()
+}