Add text align property for Label (#19)

This commit is contained in:
Floyd Wang 2024-07-09 15:56:41 +08:00 committed by GitHub
parent 310e5e3cde
commit 75e4451764
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,12 +1,24 @@
use gpui::{div, Div, IntoElement, ParentElement, RenderOnce, SharedString, Styled, WindowContext}; use gpui::{
div, prelude::FluentBuilder, Div, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
WindowContext,
};
use crate::theme::ActiveTheme; use crate::{h_flex, theme::ActiveTheme};
#[derive(Default)]
pub enum TextAlign {
#[default]
Left,
Center,
Right,
}
#[derive(IntoElement)] #[derive(IntoElement)]
pub struct Label { pub struct Label {
base: Div, base: Div,
label: SharedString, label: SharedString,
multiple_lines: bool, multiple_lines: bool,
align: TextAlign,
} }
impl Label { impl Label {
@ -15,6 +27,7 @@ impl Label {
base: div(), base: div(),
label: label.into(), label: label.into(),
multiple_lines: true, multiple_lines: true,
align: TextAlign::default(),
} }
} }
@ -22,6 +35,11 @@ impl Label {
self.multiple_lines = true; self.multiple_lines = true;
self self
} }
pub fn text_align(mut self, align: TextAlign) -> Self {
self.align = align;
self
}
} }
impl Styled for Label { impl Styled for Label {
@ -38,7 +56,12 @@ impl RenderOnce for Label {
self.label self.label
}; };
div() h_flex()
.map(|this| match self.align {
TextAlign::Left => this.justify_start(),
TextAlign::Center => this.justify_center(),
TextAlign::Right => this.justify_end(),
})
.text_color(cx.theme().foreground) .text_color(cx.theme().foreground)
.child(self.base.child(text)) .child(self.base.child(text))
} }