From 47f979d457e13982bcc0d39052dd84a385457aa6 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Tue, 24 Oct 2023 11:20:31 +0200 Subject: [PATCH] Pass IDs to `IconButton`s instead of generating them --- crates/ui2/src/components/assistant_panel.rs | 12 ++++++------ crates/ui2/src/components/buffer_search.rs | 2 +- crates/ui2/src/components/chat_panel.rs | 4 ++-- crates/ui2/src/components/editor_pane.rs | 6 +++--- crates/ui2/src/components/icon_button.rs | 7 ++++--- crates/ui2/src/components/multi_buffer.rs | 2 +- crates/ui2/src/components/status_bar.rs | 16 ++++++++-------- crates/ui2/src/components/tab_bar.rs | 8 ++++---- crates/ui2/src/components/terminal.rs | 4 ++-- crates/ui2/src/components/title_bar.rs | 12 ++++++------ crates/ui2/src/components/toolbar.rs | 6 +++--- 11 files changed, 40 insertions(+), 39 deletions(-) diff --git a/crates/ui2/src/components/assistant_panel.rs b/crates/ui2/src/components/assistant_panel.rs index 2b32f332a48ff30678f01dd3064bc611c680f037..5f38fefb34aa7d2befc21439a39dcf82654e3e57 100644 --- a/crates/ui2/src/components/assistant_panel.rs +++ b/crates/ui2/src/components/assistant_panel.rs @@ -45,7 +45,7 @@ impl AssistantPanel { .child( div() .flex() - .child(IconButton::new(Icon::Menu)) + .child(IconButton::new(Icon::Menu, "menu")) .child(Label::new("New Conversation")), ) .child( @@ -53,11 +53,11 @@ impl AssistantPanel { .flex() .items_center() .gap_px() - .child(IconButton::new(Icon::SplitMessage)) - .child(IconButton::new(Icon::Quote)) - .child(IconButton::new(Icon::MagicWand)) - .child(IconButton::new(Icon::Plus)) - .child(IconButton::new(Icon::Maximize)), + .child(IconButton::new(Icon::SplitMessage, "split_message")) + .child(IconButton::new(Icon::Quote, "quote")) + .child(IconButton::new(Icon::MagicWand, "magic_wand")) + .child(IconButton::new(Icon::Plus, "plus")) + .child(IconButton::new(Icon::Maximize, "maximize")), ), ) // Chat Body diff --git a/crates/ui2/src/components/buffer_search.rs b/crates/ui2/src/components/buffer_search.rs index 06acd468cd9eb13fe770816e56eb5ac0ca4f83af..8e1b9b3e979bf31da09fcb60b7549158d62b5b43 100644 --- a/crates/ui2/src/components/buffer_search.rs +++ b/crates/ui2/src/components/buffer_search.rs @@ -32,7 +32,7 @@ impl BufferSearch { h_stack().bg(color.toolbar).p_2().child( h_stack().child(Input::new("Search")).child( - IconButton::::new(Icon::Replace) + IconButton::::new(Icon::Replace, "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/chat_panel.rs b/crates/ui2/src/components/chat_panel.rs index e8f535bda9e743eec83a700853c213cf974faed6..fda07f4718a8e19582c6945d818a3ff13d25656c 100644 --- a/crates/ui2/src/components/chat_panel.rs +++ b/crates/ui2/src/components/chat_panel.rs @@ -45,8 +45,8 @@ impl ChatPanel { .flex() .items_center() .gap_px() - .child(IconButton::new(Icon::File)) - .child(IconButton::new(Icon::AudioOn)), + .child(IconButton::new(Icon::File, "file")) + .child(IconButton::new(Icon::AudioOn, "audio_on")), ), ) .child( diff --git a/crates/ui2/src/components/editor_pane.rs b/crates/ui2/src/components/editor_pane.rs index 770273e7d1ab49921faa422ed8e49246915599fa..a1da0da258de7908723ea63f391102f7d41878ff 100644 --- a/crates/ui2/src/components/editor_pane.rs +++ b/crates/ui2/src/components/editor_pane.rs @@ -61,15 +61,15 @@ impl EditorPane { Toolbar::new() .left_item(Breadcrumb::new(self.path.clone(), self.symbols.clone())) .right_items(vec![ - IconButton::new(Icon::InlayHint), - IconButton::::new(Icon::MagnifyingGlass) + IconButton::new(Icon::InlayHint, "toggle_inlay_hints"), + IconButton::::new(Icon::MagnifyingGlass, "buffer_search") .when(self.is_buffer_search_open, |this| { this.color(IconColor::Accent) }) .on_click(|editor, cx| { editor.toggle_buffer_search(cx); }), - IconButton::new(Icon::MagicWand), + IconButton::new(Icon::MagicWand, "inline_assist"), ]), ) .children(Some(self.buffer_search.clone()).filter(|_| self.is_buffer_search_open)) diff --git a/crates/ui2/src/components/icon_button.rs b/crates/ui2/src/components/icon_button.rs index dd7e9457e15e5f00eead24f15ee37434edabd7a4..a5edfc9c73ffaf4160548035f375c282f070ec02 100644 --- a/crates/ui2/src/components/icon_button.rs +++ b/crates/ui2/src/components/icon_button.rs @@ -19,6 +19,7 @@ impl Default for IconButtonHandlers { #[derive(Element)] pub struct IconButton { state_type: PhantomData, + id: ElementId, icon: Icon, color: IconColor, variant: ButtonVariant, @@ -27,9 +28,10 @@ pub struct IconButton { } impl IconButton { - pub fn new(icon: Icon) -> Self { + pub fn new(icon: Icon, id: impl Into) -> Self { Self { state_type: PhantomData, + id: id.into(), icon, color: IconColor::default(), variant: ButtonVariant::default(), @@ -88,8 +90,7 @@ impl IconButton { }; let mut button = h_stack() - // TODO: We probably need a more robust method for differentiating `IconButton`s from one another. - .id(SharedString::from(format!("{:?}", self.icon))) + .id(self.id.clone()) .justify_center() .rounded_md() .py(ui_size(cx, 0.25)) diff --git a/crates/ui2/src/components/multi_buffer.rs b/crates/ui2/src/components/multi_buffer.rs index 32ac40762af8d550e3bee02dc063c532c7c3e647..0b47f2c3d88009330610c5212506ebf21be76829 100644 --- a/crates/ui2/src/components/multi_buffer.rs +++ b/crates/ui2/src/components/multi_buffer.rs @@ -34,7 +34,7 @@ impl MultiBuffer { .p_4() .bg(color.editor_subheader) .child(Label::new("main.rs")) - .child(IconButton::new(Icon::ArrowUpRight)), + .child(IconButton::new(Icon::ArrowUpRight, "arrow_up_right")), ) .child(buffer) })) diff --git a/crates/ui2/src/components/status_bar.rs b/crates/ui2/src/components/status_bar.rs index 69d5344290c4e81a23964ffd55b3027e45d01bdc..cfcfdf070012b9312bbdc0cbfd020386ac9e3abb 100644 --- a/crates/ui2/src/components/status_bar.rs +++ b/crates/ui2/src/components/status_bar.rs @@ -113,7 +113,7 @@ impl StatusBar { .items_center() .gap_1() .child( - IconButton::::new(Icon::FileTree) + IconButton::::new(Icon::FileTree, "project_panel") .when(workspace.is_project_panel_open(), |this| { this.color(IconColor::Accent) }) @@ -122,7 +122,7 @@ impl StatusBar { }), ) .child( - IconButton::::new(Icon::Hash) + IconButton::::new(Icon::Hash, "collab_panel") .when(workspace.is_collab_panel_open(), |this| { this.color(IconColor::Accent) }) @@ -131,7 +131,7 @@ impl StatusBar { }), ) .child(ToolDivider::new()) - .child(IconButton::new(Icon::XCircle)) + .child(IconButton::new(Icon::XCircle, "diagnostics")) } fn right_tools( @@ -164,11 +164,11 @@ impl StatusBar { .items_center() .gap_1() .child( - IconButton::new(Icon::Copilot) + IconButton::new(Icon::Copilot, "copilot") .on_click(|_, _| println!("Copilot clicked.")), ) .child( - IconButton::new(Icon::Envelope) + IconButton::new(Icon::Envelope, "envelope") .on_click(|_, _| println!("Send Feedback clicked.")), ), ) @@ -179,7 +179,7 @@ impl StatusBar { .items_center() .gap_1() .child( - IconButton::::new(Icon::Terminal) + IconButton::::new(Icon::Terminal, "terminal") .when(workspace.is_terminal_open(), |this| { this.color(IconColor::Accent) }) @@ -188,7 +188,7 @@ impl StatusBar { }), ) .child( - IconButton::::new(Icon::MessageBubbles) + IconButton::::new(Icon::MessageBubbles, "chat_panel") .when(workspace.is_chat_panel_open(), |this| { this.color(IconColor::Accent) }) @@ -197,7 +197,7 @@ impl StatusBar { }), ) .child( - IconButton::::new(Icon::Ai) + IconButton::::new(Icon::Ai, "assistant_panel") .when(workspace.is_assistant_panel_open(), |this| { this.color(IconColor::Accent) }) diff --git a/crates/ui2/src/components/tab_bar.rs b/crates/ui2/src/components/tab_bar.rs index 5c20bbe397b7278c50524a46ee75a9702657a862..445f2d6deb74eb04376288dbf42e3bffb767be15 100644 --- a/crates/ui2/src/components/tab_bar.rs +++ b/crates/ui2/src/components/tab_bar.rs @@ -51,11 +51,11 @@ impl TabBar { .items_center() .gap_px() .child( - IconButton::new(Icon::ArrowLeft) + IconButton::new(Icon::ArrowLeft, "arrow_left") .state(InteractionState::Enabled.if_enabled(can_navigate_back)), ) .child( - IconButton::new(Icon::ArrowRight).state( + IconButton::new(Icon::ArrowRight, "arrow_right").state( InteractionState::Enabled.if_enabled(can_navigate_forward), ), ), @@ -83,8 +83,8 @@ impl TabBar { .flex() .items_center() .gap_px() - .child(IconButton::new(Icon::Plus)) - .child(IconButton::new(Icon::Split)), + .child(IconButton::new(Icon::Plus, "plus")) + .child(IconButton::new(Icon::Split, "split")), ), ) } diff --git a/crates/ui2/src/components/terminal.rs b/crates/ui2/src/components/terminal.rs index 8a68d050673a0ef16c9dbaa5c963c8c3f86aaa1e..aaf4382f516827dccce4dcc7ab54aabfbe4fad0a 100644 --- a/crates/ui2/src/components/terminal.rs +++ b/crates/ui2/src/components/terminal.rs @@ -40,11 +40,11 @@ impl Terminal { .items_center() .gap_px() .child( - IconButton::new(Icon::ArrowLeft).state( + IconButton::new(Icon::ArrowLeft, "arrow_left").state( InteractionState::Enabled.if_enabled(can_navigate_back), ), ) - .child(IconButton::new(Icon::ArrowRight).state( + .child(IconButton::new(Icon::ArrowRight, "arrow_right").state( InteractionState::Enabled.if_enabled(can_navigate_forward), )), ), diff --git a/crates/ui2/src/components/title_bar.rs b/crates/ui2/src/components/title_bar.rs index 4c106c82d60292fc2cd6a7bb4792ee70a60ff811..c33d37238a3aca7c4339715e03f4b4a2fd8900b3 100644 --- a/crates/ui2/src/components/title_bar.rs +++ b/crates/ui2/src/components/title_bar.rs @@ -129,7 +129,7 @@ impl TitleBar { .child(Button::new("nate/gpui2-ui-components")), ) .children(player_list.map(|p| PlayerStack::new(p))) - .child(IconButton::new(Icon::Plus)), + .child(IconButton::new(Icon::Plus, "plus")), ) .child( div() @@ -141,8 +141,8 @@ impl TitleBar { .flex() .items_center() .gap_1() - .child(IconButton::new(Icon::FolderX)) - .child(IconButton::new(Icon::Exit)), + .child(IconButton::new(Icon::FolderX, "folder_x")) + .child(IconButton::new(Icon::Exit, "exit")), ) .child(ToolDivider::new()) .child( @@ -152,17 +152,17 @@ impl TitleBar { .items_center() .gap_1() .child( - IconButton::::new(Icon::Mic) + IconButton::::new(Icon::Mic, "toggle_mic_status") .when(self.is_mic_muted(), |this| this.color(IconColor::Error)) .on_click(|title_bar, cx| title_bar.toggle_mic_status(cx)), ) .child( - IconButton::::new(Icon::AudioOn) + IconButton::::new(Icon::AudioOn, "toggle_deafened") .when(self.is_deafened, |this| this.color(IconColor::Error)) .on_click(|title_bar, cx| title_bar.toggle_deafened(cx)), ) .child( - IconButton::::new(Icon::Screen) + IconButton::::new(Icon::Screen, "toggle_screen_share") .when( self.screen_share_status == ScreenShareStatus::Shared, |this| this.color(IconColor::Accent), diff --git a/crates/ui2/src/components/toolbar.rs b/crates/ui2/src/components/toolbar.rs index aeec01bc7f8fd8d916de98aee87bdb78e3a1ea49..081c3664be2e0a0cf619e4ce81276f2d308bffe8 100644 --- a/crates/ui2/src/components/toolbar.rs +++ b/crates/ui2/src/components/toolbar.rs @@ -130,9 +130,9 @@ mod stories { ], )) .right_items(vec![ - IconButton::new(Icon::InlayHint), - IconButton::new(Icon::MagnifyingGlass), - IconButton::new(Icon::MagicWand), + IconButton::new(Icon::InlayHint, "toggle_inlay_hints"), + IconButton::new(Icon::MagnifyingGlass, "buffer_search"), + IconButton::new(Icon::MagicWand, "inline_assist"), ]), ) }