From 3c1ec2e9ca70312584528c54731db2bc669260bf Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Fri, 13 Oct 2023 17:36:27 -0400 Subject: [PATCH 1/2] Add rudimentary UI for `BufferSearch` --- crates/ui2/src/components/buffer_search.rs | 13 +++++++++++-- crates/ui2/src/elements/button.rs | 2 +- crates/ui2/src/elements/icon.rs | 4 ++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/ui2/src/components/buffer_search.rs b/crates/ui2/src/components/buffer_search.rs index 95e328091c4499feed86a087510178fc5796f84a..642942e537673c3abf18f6530b3f24563f174c23 100644 --- a/crates/ui2/src/components/buffer_search.rs +++ b/crates/ui2/src/components/buffer_search.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use crate::EditorPane; +use crate::{h_stack, EditorPane, Icon, IconButton, Input}; #[derive(Element)] #[element(view_state = "EditorPane")] @@ -15,6 +15,15 @@ impl BufferSearch { _view: &mut EditorPane, cx: &mut ViewContext, ) -> impl Element { - div().child("This is where Buffer Search goes.") + let theme = theme(cx); + + h_stack() + .fill(theme.highest.base.default.background) + .p_2() + .child( + h_stack() + .child(Input::new("Search (↑/↓ for previous/next query)")) + .child(IconButton::new(Icon::Replace)), + ) } } diff --git a/crates/ui2/src/elements/button.rs b/crates/ui2/src/elements/button.rs index fc26f489f9993e4e920d3f0b46121b97358fef03..e1dac4466fd524432fa115e483f578de7d07abdd 100644 --- a/crates/ui2/src/elements/button.rs +++ b/crates/ui2/src/elements/button.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use gpui3::{DefiniteLength, Hsla, Interactive, MouseButton, WindowContext}; use crate::prelude::*; -use crate::{h_stack, theme, Icon, IconColor, IconElement, Label, LabelColor, LabelSize}; +use crate::{h_stack, Icon, IconColor, IconElement, Label, LabelColor, LabelSize}; #[derive(Default, PartialEq, Clone, Copy)] pub enum IconPosition { diff --git a/crates/ui2/src/elements/icon.rs b/crates/ui2/src/elements/icon.rs index 74ee43b0670731a499e12d21b85250eaaf301887..f38c8c339a664d86fb4104d24102af399a230a36 100644 --- a/crates/ui2/src/elements/icon.rs +++ b/crates/ui2/src/elements/icon.rs @@ -82,6 +82,8 @@ pub enum Icon { MicMute, Plus, Quote, + Replace, + ReplaceAll, Screen, SelectAll, Split, @@ -130,6 +132,8 @@ impl Icon { Icon::MicMute => "icons/mic-mute.svg", Icon::Plus => "icons/plus.svg", Icon::Quote => "icons/quote.svg", + Icon::Replace => "icons/replace.svg", + Icon::ReplaceAll => "icons/replace_all.svg", Icon::Screen => "icons/desktop.svg", Icon::SelectAll => "icons/select-all.svg", Icon::Split => "icons/split.svg", From 6891e8662137e1972cd7606513ec238199717018 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Fri, 13 Oct 2023 17:44:21 -0400 Subject: [PATCH 2/2] Add state to `BufferSearch` --- crates/ui2/src/components/buffer_search.rs | 40 ++++++++++++++++------ crates/ui2/src/components/editor_pane.rs | 7 ++-- crates/ui2/src/static_data.rs | 20 +++++++---- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/crates/ui2/src/components/buffer_search.rs b/crates/ui2/src/components/buffer_search.rs index 642942e537673c3abf18f6530b3f24563f174c23..a0c60fd0abe62d53134707087d5d7e1068bab6dd 100644 --- a/crates/ui2/src/components/buffer_search.rs +++ b/crates/ui2/src/components/buffer_search.rs @@ -1,20 +1,32 @@ +use gpui3::{view, Context, View}; + use crate::prelude::*; -use crate::{h_stack, EditorPane, Icon, IconButton, Input}; +use crate::{h_stack, Icon, IconButton, IconColor, Input}; -#[derive(Element)] -#[element(view_state = "EditorPane")] -pub struct BufferSearch {} +pub struct BufferSearch { + is_replace_open: bool, +} impl BufferSearch { pub fn new() -> Self { - Self {} + Self { + is_replace_open: false, + } + } + + 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, - _view: &mut EditorPane, - cx: &mut ViewContext, - ) -> impl Element { + 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 eb40b373e4219abf9d3205462a222951819ca8d0..59dc2619fa0a4341ac89d549d468ea85a7f63354 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 3909f3848b1a88019d34a4603c3aeef93551722b..c9b389c923d68bfb05e13904d0a6f4f067af99d3 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), ) }