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?)
This commit is contained in:
Hans 2024-04-10 22:30:13 +08:00 committed by GitHub
parent 26299fb8c9
commit 664efef76b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 29 additions and 5 deletions

View File

@ -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()
});

View File

@ -83,6 +83,7 @@ impl Item for ImageView {
.to_string_lossy()
.to_string();
Label::new(title)
.single_line()
.color(if selected {
Color::Default
} else {

View File

@ -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(),
)

View File

@ -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)
}
}