Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ui: Ensure Label with single_line set does not wrap #21444

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/ui/src/components/label/highlighted_label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ impl LabelCommon for HighlightedLabel {
self.base = self.base.underline(underline);
self
}

fn single_line(mut self) -> Self {
self.base = self.base.single_line();
self
}
}

pub fn highlight_ranges(
Expand Down
20 changes: 6 additions & 14 deletions crates/ui/src/components/label/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,6 @@ impl Label {
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();
/// ```
pub fn single_line(mut self) -> Self {
self.single_line = true;
self
}
}

// Style methods.
Expand Down Expand Up @@ -177,6 +163,12 @@ impl LabelCommon for Label {
self.base = self.base.underline(underline);
self
}

fn single_line(mut self) -> Self {
self.single_line = true;
self.base = self.base.single_line();
self
}
}

impl RenderOnce for Label {
Expand Down
11 changes: 11 additions & 0 deletions crates/ui/src/components/label/label_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub trait LabelCommon {

/// Sets the alpha property of the label, overwriting the alpha value of the color.
fn alpha(self, alpha: f32) -> Self;

/// Sets the label to render as a single line.
fn single_line(self) -> Self;
}

#[derive(IntoElement)]
Expand All @@ -63,6 +66,7 @@ pub struct LabelLike {
children: SmallVec<[AnyElement; 2]>,
alpha: Option<f32>,
underline: bool,
single_line: bool,
}

impl Default for LabelLike {
Expand All @@ -84,6 +88,7 @@ impl LabelLike {
children: SmallVec::new(),
alpha: None,
underline: false,
single_line: false,
}
}
}
Expand Down Expand Up @@ -139,6 +144,11 @@ impl LabelCommon for LabelLike {
self.alpha = Some(alpha);
self
}

fn single_line(mut self) -> Self {
self.single_line = true;
self
}
}

impl ParentElement for LabelLike {
Expand Down Expand Up @@ -178,6 +188,7 @@ impl RenderOnce for LabelLike {
this
})
.when(self.strikethrough, |this| this.line_through())
.when(self.single_line, |this| this.whitespace_nowrap())
.text_color(color)
.font_weight(self.weight.unwrap_or(settings.ui_font.weight))
.children(self.children)
Expand Down
Loading