diff --git a/crates/ui2/src/components/buffer_search.rs b/crates/ui2/src/components/buffer_search.rs index 642942e537..a0c60fd0ab 100644 --- a/crates/ui2/src/components/buffer_search.rs +++ b/crates/ui2/src/components/buffer_search.rs @@ -1,20 +1,32 @@ -use crate::prelude::*; -use crate::{h_stack, EditorPane, Icon, IconButton, Input}; +use gpui3::{view, Context, View}; -#[derive(Element)] -#[element(view_state = "EditorPane")] -pub struct BufferSearch {} +use crate::prelude::*; +use crate::{h_stack, Icon, IconButton, IconColor, Input}; + +pub struct BufferSearch { + is_replace_open: bool, +} impl BufferSearch { pub fn new() -> Self { - Self {} + Self { + is_replace_open: false, + } } - fn render( - &mut self, - _view: &mut EditorPane, - cx: &mut ViewContext, - ) -> impl Element { + fn toggle_replace(&mut self, cx: &mut ViewContext) { + self.is_replace_open = !self.is_replace_open; + + cx.notify(); + } + + pub fn view(cx: &mut WindowContext) -> View { + let theme = theme(cx); + + view(cx.entity(|cx| Self::new()), Self::render) + } + + fn render(&mut self, cx: &mut ViewContext) -> impl Element { let theme = theme(cx); h_stack() @@ -23,7 +35,13 @@ impl BufferSearch { .child( h_stack() .child(Input::new("Search (↑/↓ for previous/next query)")) - .child(IconButton::new(Icon::Replace)), + .child( + IconButton::::new(Icon::Replace) + .when(self.is_replace_open, |this| this.color(IconColor::Accent)) + .on_click(|buffer_search, cx| { + buffer_search.toggle_replace(cx); + }), + ), ) } } diff --git a/crates/ui2/src/components/editor_pane.rs b/crates/ui2/src/components/editor_pane.rs index eb40b373e4..59dc2619fa 100644 --- a/crates/ui2/src/components/editor_pane.rs +++ b/crates/ui2/src/components/editor_pane.rs @@ -14,11 +14,13 @@ pub struct EditorPane { path: PathBuf, symbols: Vec, buffer: Buffer, + buffer_search: View, is_buffer_search_open: bool, } impl EditorPane { pub fn new( + cx: &mut WindowContext, tabs: Vec>, path: PathBuf, symbols: Vec, @@ -29,6 +31,7 @@ impl EditorPane { path, symbols, buffer, + buffer_search: BufferSearch::view(cx), is_buffer_search_open: false, } } @@ -43,7 +46,7 @@ impl EditorPane { let theme = theme(cx); view( - cx.entity(|cx| hello_world_rust_editor_with_status_example(&theme)), + cx.entity(|cx| hello_world_rust_editor_with_status_example(cx)), Self::render, ) } @@ -69,7 +72,7 @@ impl EditorPane { IconButton::new(Icon::MagicWand), ]), ) - .children(Some(BufferSearch::new()).filter(|_| self.is_buffer_search_open)) + .children(Some(self.buffer_search.clone()).filter(|_| self.is_buffer_search_open)) .child(self.buffer.clone()) } } diff --git a/crates/ui2/src/static_data.rs b/crates/ui2/src/static_data.rs index 3909f3848b..c9b389c923 100644 --- a/crates/ui2/src/static_data.rs +++ b/crates/ui2/src/static_data.rs @@ -1,10 +1,11 @@ use std::path::PathBuf; use std::str::FromStr; +use gpui3::WindowContext; use rand::Rng; use crate::{ - Buffer, BufferRow, BufferRows, EditorPane, FileSystemStatus, GitStatus, HighlightColor, + theme, 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,8 +598,9 @@ pub fn example_editor_actions() -> Vec EditorPane { +pub fn empty_editor_example(cx: &mut WindowContext) -> EditorPane { EditorPane::new( + cx, static_tabs_example(), PathBuf::from_str("crates/ui/src/static_data.rs").unwrap(), vec![], @@ -610,8 +612,11 @@ pub fn empty_buffer_example() -> Buffer { Buffer::new().set_rows(Some(BufferRows::default())) } -pub fn hello_world_rust_editor_example(theme: &Theme) -> EditorPane { +pub fn hello_world_rust_editor_example(cx: &mut WindowContext) -> EditorPane { + let theme = theme(cx); + EditorPane::new( + cx, static_tabs_example(), PathBuf::from_str("crates/ui/src/static_data.rs").unwrap(), vec![Symbol(vec![ @@ -624,7 +629,7 @@ pub fn hello_world_rust_editor_example(theme: &Theme) -> EditorPane { color: HighlightColor::Function.hsla(&theme), }, ])], - hello_world_rust_buffer_example(theme), + hello_world_rust_buffer_example(&theme), ) } @@ -748,8 +753,11 @@ pub fn hello_world_rust_buffer_rows(theme: &Theme) -> Vec { ] } -pub fn hello_world_rust_editor_with_status_example(theme: &Theme) -> EditorPane { +pub fn hello_world_rust_editor_with_status_example(cx: &mut WindowContext) -> EditorPane { + let theme = theme(cx); + EditorPane::new( + cx, static_tabs_example(), PathBuf::from_str("crates/ui/src/static_data.rs").unwrap(), vec![Symbol(vec![ @@ -762,7 +770,7 @@ pub fn hello_world_rust_editor_with_status_example(theme: &Theme) -> EditorPane color: HighlightColor::Function.hsla(&theme), }, ])], - hello_world_rust_buffer_with_status_example(theme), + hello_world_rust_buffer_with_status_example(&theme), ) }