gpui-component/crates/ui/src/label.rs
Jason Lee 4a26776289
label: Avoid String alloc and remove custom text-align. (#616)
Reverted #615 and recreate again.

## Break changes

- Remove `TextAlign` and `text_left`, `text_right` ... method from
label, we can use same things from GPUI.

<img width="1151" alt="image"
src="https://github.com/user-attachments/assets/95e9066a-e58d-47a8-9acf-b483b0203b1b"
/>
2025-02-11 10:30:45 +08:00

53 lines
1.2 KiB
Rust

use gpui::{
div, rems, App, Div, IntoElement, ParentElement, RenderOnce, SharedString, Styled, Window,
};
use crate::ActiveTheme;
const MASKED: &'static str = "";
#[derive(IntoElement)]
pub struct Label {
base: Div,
label: SharedString,
chars_count: usize,
marked: bool,
}
impl Label {
pub fn new(label: impl Into<SharedString>) -> Self {
let label: SharedString = label.into();
let chars_count = label.chars().count();
Self {
base: div().line_height(rems(1.25)),
label,
chars_count,
marked: false,
}
}
pub fn masked(mut self, masked: bool) -> Self {
self.marked = masked;
self
}
}
impl Styled for Label {
fn style(&mut self) -> &mut gpui::StyleRefinement {
self.base.style()
}
}
impl RenderOnce for Label {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let text = if self.marked {
SharedString::from(MASKED.repeat(self.chars_count))
} else {
self.label
};
div()
.text_color(cx.theme().foreground)
.child(self.base.child(text))
}
}