From 664efef76ba6a5d1531b4b4479dead565a1c5588 Mon Sep 17 00:00:00 2001 From: Hans Date: Wed, 10 Apr 2024 22:30:13 +0800 Subject: [PATCH] Remove line breaks when displaying file names in the project panel (#10231) - Fixed #8603 For the label title of the project panel, I find that there is no place to use to get the title of the label to do some operations, it should be safe to modify it, but I'm not sure how we need to modify the problem, I can think of two scenarios: 1. Modify every place where you don't want multiple lines to appear 2. Make the label only display a single line (e.g. provide a new parameter, or a new label type?) --- crates/breadcrumbs/src/breadcrumbs.rs | 2 +- crates/image_viewer/src/image_viewer.rs | 1 + crates/project_panel/src/project_panel.rs | 8 +++++--- crates/ui/src/components/label/label.rs | 23 ++++++++++++++++++++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/crates/breadcrumbs/src/breadcrumbs.rs b/crates/breadcrumbs/src/breadcrumbs.rs index 1c3c28192f..4c8a7cb33f 100644 --- a/crates/breadcrumbs/src/breadcrumbs.rs +++ b/crates/breadcrumbs/src/breadcrumbs.rs @@ -60,7 +60,7 @@ impl Render for Breadcrumbs { let mut text_style = cx.text_style(); text_style.color = Color::Muted.color(cx); - StyledText::new(segment.text) + StyledText::new(segment.text.replace('\n', "␤")) .with_highlights(&text_style, segment.highlights.unwrap_or_default()) .into_any() }); diff --git a/crates/image_viewer/src/image_viewer.rs b/crates/image_viewer/src/image_viewer.rs index 1b30c128cb..86383163a7 100644 --- a/crates/image_viewer/src/image_viewer.rs +++ b/crates/image_viewer/src/image_viewer.rs @@ -83,6 +83,7 @@ impl Item for ImageView { .to_string_lossy() .to_string(); Label::new(title) + .single_line() .color(if selected { Color::Default } else { diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs index a3786b578f..4e8df2d93e 100644 --- a/crates/project_panel/src/project_panel.rs +++ b/crates/project_panel/src/project_panel.rs @@ -1442,9 +1442,11 @@ impl ProjectPanel { if let (Some(editor), true) = (Some(&self.filename_editor), show_editor) { h_flex().h_6().w_full().child(editor.clone()) } else { - h_flex() - .h_6() - .child(Label::new(file_name).color(filename_text_color)) + h_flex().h_6().child( + Label::new(file_name) + .single_line() + .color(filename_text_color), + ) } .ml_1(), ) diff --git a/crates/ui/src/components/label/label.rs b/crates/ui/src/components/label/label.rs index 13ee416145..09ded5db63 100644 --- a/crates/ui/src/components/label/label.rs +++ b/crates/ui/src/components/label/label.rs @@ -34,6 +34,7 @@ use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle}; pub struct Label { base: LabelLike, label: SharedString, + single_line: bool, } impl Label { @@ -50,8 +51,23 @@ impl Label { Self { base: LabelLike::new(), label: label.into(), + single_line: false, } } + + /// Make the label display in a single line mode + /// + /// # Examples + /// + /// ``` + /// use ui::prelude::*; + /// + /// let my_label = Label::new("Hello, World!").single_line(true); + /// ``` + pub fn single_line(mut self) -> Self { + self.single_line = true; + self + } } impl LabelCommon for Label { @@ -114,6 +130,11 @@ impl LabelCommon for Label { impl RenderOnce for Label { fn render(self, _cx: &mut WindowContext) -> impl IntoElement { - self.base.child(self.label) + let target_label = if self.single_line { + SharedString::from(self.label.replace('\n', "␤")) + } else { + self.label + }; + self.base.child(target_label) } }