Wire up buffer search toggle for `EditorPane`

Marshall Bowers created

Change summary

crates/storybook2/src/storybook2.rs      | 10 ++-
crates/ui2/src/components/editor_pane.rs | 77 ++++++++++++++++++-------
crates/ui2/src/components/workspace.rs   | 21 +++---
crates/ui2/src/static_data.rs            | 48 +++++++--------
4 files changed, 93 insertions(+), 63 deletions(-)

Detailed changes

crates/storybook2/src/storybook2.rs 🔗

@@ -12,8 +12,8 @@ use std::sync::Arc;
 
 use clap::Parser;
 use gpui3::{
-    div, px, size, view, AnyView, Bounds, Context, Element, ViewContext, WindowBounds,
-    WindowOptions,
+    div, px, size, view, AnyView, BorrowAppContext, Bounds, Context, Element, ViewContext,
+    WindowBounds, WindowOptions,
 };
 use log::LevelFilter;
 use simplelog::SimpleLogger;
@@ -68,7 +68,11 @@ fn main() {
                     },
                     move |cx| {
                         view(
-                            cx.entity(|cx| StoryWrapper::new(selector.story(cx), theme)),
+                            cx.entity(|cx| {
+                                cx.with_global(theme.clone(), |cx| {
+                                    StoryWrapper::new(selector.story(cx), theme)
+                                })
+                            }),
                             StoryWrapper::render,
                         )
                     },

crates/ui2/src/components/editor_pane.rs 🔗

@@ -1,43 +1,74 @@
 use std::path::PathBuf;
 
+use gpui3::{view, Context, View};
+
 use crate::prelude::*;
-use crate::{v_stack, Breadcrumb, Buffer, Icon, IconButton, Symbol, Tab, TabBar, Toolbar};
+use crate::{
+    hello_world_rust_editor_with_status_example, v_stack, Breadcrumb, Buffer, Icon, IconButton,
+    IconColor, Symbol, Tab, TabBar, Toolbar,
+};
 
-pub struct Editor<S: 'static + Send + Sync + Clone> {
-    pub tabs: Vec<Tab<S>>,
-    pub path: PathBuf,
-    pub symbols: Vec<Symbol>,
-    pub buffer: Buffer<S>,
+#[derive(Clone)]
+pub struct EditorPane {
+    tabs: Vec<Tab<Self>>,
+    path: PathBuf,
+    symbols: Vec<Symbol>,
+    buffer: Buffer<Self>,
+    is_buffer_search_open: bool,
 }
 
-#[derive(Element)]
-pub struct EditorPane<S: 'static + Send + Sync + Clone> {
-    editor: Editor<S>,
-}
+impl EditorPane {
+    pub fn new(
+        tabs: Vec<Tab<Self>>,
+        path: PathBuf,
+        symbols: Vec<Symbol>,
+        buffer: Buffer<Self>,
+    ) -> Self {
+        Self {
+            tabs,
+            path,
+            symbols,
+            buffer,
+            is_buffer_search_open: false,
+        }
+    }
+
+    pub fn toggle_buffer_search(&mut self, cx: &mut ViewContext<Self>) {
+        self.is_buffer_search_open = !self.is_buffer_search_open;
+
+        cx.notify();
+    }
+
+    pub fn view(cx: &mut WindowContext) -> View<Self> {
+        let theme = theme(cx);
 
-impl<S: 'static + Send + Sync + Clone> EditorPane<S> {
-    pub fn new(editor: Editor<S>) -> Self {
-        Self { editor }
+        view(
+            cx.entity(|cx| hello_world_rust_editor_with_status_example(&theme)),
+            Self::render,
+        )
     }
 
-    fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
+    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Element<ViewState = Self> {
         v_stack()
             .w_full()
             .h_full()
             .flex_1()
-            .child(TabBar::new(self.editor.tabs.clone()))
+            .child(TabBar::new(self.tabs.clone()))
             .child(
                 Toolbar::new()
-                    .left_item(Breadcrumb::new(
-                        self.editor.path.clone(),
-                        self.editor.symbols.clone(),
-                    ))
+                    .left_item(Breadcrumb::new(self.path.clone(), self.symbols.clone()))
                     .right_items(vec![
-                        IconButton::new(Icon::InlayHint).into_any(),
-                        IconButton::new(Icon::MagnifyingGlass).into_any(),
-                        IconButton::new(Icon::MagicWand).into_any(),
+                        IconButton::new(Icon::InlayHint),
+                        IconButton::<Self>::new(Icon::MagnifyingGlass)
+                            .when(self.is_buffer_search_open, |this| {
+                                this.color(IconColor::Accent)
+                            })
+                            .on_click(|editor, cx| {
+                                editor.toggle_buffer_search(cx);
+                            }),
+                        IconButton::new(Icon::MagicWand),
                     ]),
             )
-            .child(self.editor.buffer.clone())
+            .child(self.buffer.clone())
     }
 }

crates/ui2/src/components/workspace.rs 🔗

@@ -1,17 +1,18 @@
 use chrono::DateTime;
 use gpui3::{px, relative, rems, view, Context, Size, View};
 
+use crate::prelude::*;
 use crate::{
-    hello_world_rust_editor_with_status_example, theme, v_stack, AssistantPanel, Button,
-    ChatMessage, ChatPanel, CollabPanel, EditorPane, Label, LanguageSelector, Pane, PaneGroup,
-    Panel, PanelAllowedSides, PanelSide, ProjectPanel, SplitDirection, StatusBar, Terminal,
-    TitleBar, Toast, ToastOrigin,
+    theme, v_stack, AssistantPanel, Button, ChatMessage, ChatPanel, CollabPanel, EditorPane, Label,
+    LanguageSelector, NotificationToast, Pane, PaneGroup, Panel, PanelAllowedSides, PanelSide,
+    ProjectPanel, SplitDirection, StatusBar, Terminal, TitleBar, Toast, ToastOrigin,
 };
-use crate::{prelude::*, NotificationToast};
 
 #[derive(Clone)]
 pub struct Workspace {
     title_bar: View<TitleBar>,
+    editor_1: View<EditorPane>,
+    editor_2: View<EditorPane>,
     show_project_panel: bool,
     show_collab_panel: bool,
     show_chat_panel: bool,
@@ -28,6 +29,8 @@ impl Workspace {
     pub fn new(cx: &mut ViewContext<Self>) -> Self {
         Self {
             title_bar: TitleBar::view(cx),
+            editor_1: EditorPane::view(cx),
+            editor_2: EditorPane::view(cx),
             show_project_panel: true,
             show_collab_panel: false,
             show_chat_panel: true,
@@ -127,9 +130,7 @@ impl Workspace {
                                 height: temp_size,
                             },
                         )
-                        .child(EditorPane::new(
-                            hello_world_rust_editor_with_status_example(&theme),
-                        )),
+                        .child(self.editor_1.clone()),
                         Pane::new(
                             ScrollState::default(),
                             Size {
@@ -149,9 +150,7 @@ impl Workspace {
                             height: relative(1.).into(),
                         },
                     )
-                    .child(EditorPane::new(
-                        hello_world_rust_editor_with_status_example(&theme),
-                    ))],
+                    .child(self.editor_2.clone())],
                     SplitDirection::Vertical,
                 ),
             ],

crates/ui2/src/static_data.rs 🔗

@@ -4,7 +4,7 @@ use std::str::FromStr;
 use rand::Rng;
 
 use crate::{
-    Buffer, BufferRow, BufferRows, Editor, FileSystemStatus, GitStatus, HighlightColor,
+    Buffer, BufferRow, BufferRows, EditorPane, FileSystemStatus, GitStatus, HighlightColor,
     HighlightedLine, HighlightedText, Icon, Keybinding, Label, LabelColor, ListEntry,
     ListEntrySize, ListItem, Livestream, MicStatus, ModifierKeys, PaletteItem, Player,
     PlayerCallStatus, PlayerWithCallStatus, ScreenShareStatus, Symbol, Tab, Theme, ToggleState,
@@ -597,26 +597,24 @@ pub fn example_editor_actions<S: 'static + Send + Sync + Clone>() -> Vec<Palette
     ]
 }
 
-pub fn empty_editor_example<S: 'static + Send + Sync + Clone>() -> Editor<S> {
-    Editor {
-        tabs: static_tabs_example(),
-        path: PathBuf::from_str("crates/ui/src/static_data.rs").unwrap(),
-        symbols: vec![],
-        buffer: empty_buffer_example(),
-    }
+pub fn empty_editor_example() -> EditorPane {
+    EditorPane::new(
+        static_tabs_example(),
+        PathBuf::from_str("crates/ui/src/static_data.rs").unwrap(),
+        vec![],
+        empty_buffer_example(),
+    )
 }
 
 pub fn empty_buffer_example<S: 'static + Send + Sync + Clone>() -> Buffer<S> {
     Buffer::new().set_rows(Some(BufferRows::default()))
 }
 
-pub fn hello_world_rust_editor_example<S: 'static + Send + Sync + Clone>(
-    theme: &Theme,
-) -> Editor<S> {
-    Editor {
-        tabs: static_tabs_example(),
-        path: PathBuf::from_str("crates/ui/src/static_data.rs").unwrap(),
-        symbols: vec![Symbol(vec![
+pub fn hello_world_rust_editor_example(theme: &Theme) -> EditorPane {
+    EditorPane::new(
+        static_tabs_example(),
+        PathBuf::from_str("crates/ui/src/static_data.rs").unwrap(),
+        vec![Symbol(vec![
             HighlightedText {
                 text: "fn ".to_string(),
                 color: HighlightColor::Keyword.hsla(&theme),
@@ -626,8 +624,8 @@ pub fn hello_world_rust_editor_example<S: 'static + Send + Sync + Clone>(
                 color: HighlightColor::Function.hsla(&theme),
             },
         ])],
-        buffer: hello_world_rust_buffer_example(theme),
-    }
+        hello_world_rust_buffer_example(theme),
+    )
 }
 
 pub fn hello_world_rust_buffer_example<S: 'static + Send + Sync + Clone>(
@@ -750,13 +748,11 @@ pub fn hello_world_rust_buffer_rows(theme: &Theme) -> Vec<BufferRow> {
     ]
 }
 
-pub fn hello_world_rust_editor_with_status_example<S: 'static + Send + Sync + Clone>(
-    theme: &Theme,
-) -> Editor<S> {
-    Editor {
-        tabs: static_tabs_example(),
-        path: PathBuf::from_str("crates/ui/src/static_data.rs").unwrap(),
-        symbols: vec![Symbol(vec![
+pub fn hello_world_rust_editor_with_status_example(theme: &Theme) -> EditorPane {
+    EditorPane::new(
+        static_tabs_example(),
+        PathBuf::from_str("crates/ui/src/static_data.rs").unwrap(),
+        vec![Symbol(vec![
             HighlightedText {
                 text: "fn ".to_string(),
                 color: HighlightColor::Keyword.hsla(&theme),
@@ -766,8 +762,8 @@ pub fn hello_world_rust_editor_with_status_example<S: 'static + Send + Sync + Cl
                 color: HighlightColor::Function.hsla(&theme),
             },
         ])],
-        buffer: hello_world_rust_buffer_with_status_example(theme),
-    }
+        hello_world_rust_buffer_with_status_example(theme),
+    )
 }
 
 pub fn hello_world_rust_buffer_with_status_example<S: 'static + Send + Sync + Clone>(