Skip to content

Commit

Permalink
Add Label::halign (emilk#4975)
Browse files Browse the repository at this point in the history
Function to specify `Align` to `Label`

There are cases where you want to display `Label` on the left or right
regardless of other `Layout` specifications.

Before : Note the part showing the rust source code.

![20240818-1](https://github.com/user-attachments/assets/a08f7594-1ec1-4c6a-b96d-1a5f735d02c1)

After : Note the part showing the rust source code.

![20240818-2](https://github.com/user-attachments/assets/807ff9cf-f8cd-4415-9c78-b62869d1696d)

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
rustbasic and emilk authored Aug 26, 2024
1 parent 5a1ab9b commit 47c0aeb
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion crates/egui/src/widgets/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Label {
wrap_mode: Option<TextWrapMode>,
sense: Option<Sense>,
selectable: Option<bool>,
halign: Option<Align>,
}

impl Label {
Expand All @@ -35,6 +36,7 @@ impl Label {
wrap_mode: None,
sense: None,
selectable: None,
halign: None,
}
}

Expand Down Expand Up @@ -76,6 +78,13 @@ impl Label {
self
}

/// Sets the horizontal alignment of the Label to the given `Align` value.
#[inline]
pub fn halign(mut self, align: Align) -> Self {
self.halign = Some(align);
self
}

/// Can the user select the text with the mouse?
///
/// Overrides [`crate::style::Interaction::selectable_labels`].
Expand Down Expand Up @@ -211,7 +220,7 @@ impl Label {
layout_job.halign = Align::LEFT;
layout_job.justify = false;
} else {
layout_job.halign = ui.layout().horizontal_placement();
layout_job.halign = self.halign.unwrap_or(ui.layout().horizontal_placement());
layout_job.justify = ui.layout().horizontal_justify();
};

Expand Down

0 comments on commit 47c0aeb

Please sign in to comment.