diff --git a/crates/story/examples/text.rs b/crates/story/examples/text.rs new file mode 100644 index 00000000..7fcbb715 --- /dev/null +++ b/crates/story/examples/text.rs @@ -0,0 +1,35 @@ +use gpui::*; +use story::{Assets, TextStory}; + +pub struct Example { + root: Entity, +} + +impl Example { + pub fn new(window: &mut Window, cx: &mut Context) -> Self { + let root = TextStory::view(window, cx); + + Self { root } + } + + fn view(window: &mut Window, cx: &mut App) -> Entity { + cx.new(|cx| Self::new(window, cx)) + } +} + +impl Render for Example { + fn render(&mut self, _window: &mut Window, _cx: &mut Context) -> impl IntoElement { + div().p_4().size_full().child(self.root.clone()) + } +} + +fn main() { + let app = Application::new().with_assets(Assets); + + app.run(move |cx| { + story::init(cx); + cx.activate(true); + + story::create_new_window("List Example", Example::view, cx); + }); +} diff --git a/crates/ui/src/label.rs b/crates/ui/src/label.rs index 2ea15ee7..c8ec6c99 100644 --- a/crates/ui/src/label.rs +++ b/crates/ui/src/label.rs @@ -1,58 +1,31 @@ use gpui::{ - div, prelude::FluentBuilder, rems, App, Div, IntoElement, ParentElement, RenderOnce, - SharedString, Styled, Window, + div, rems, App, Div, IntoElement, ParentElement, RenderOnce, SharedString, Styled, Window, }; -use crate::{h_flex, ActiveTheme}; +use crate::ActiveTheme; const MASKED: &'static str = "•"; -#[derive(Default, PartialEq, Eq)] -pub enum TextAlign { - #[default] - Left, - Center, - Right, -} - #[derive(IntoElement)] pub struct Label { base: Div, label: SharedString, - align: TextAlign, + chars_count: usize, marked: bool, } impl Label { pub fn new(label: impl Into) -> Self { + let label: SharedString = label.into(); + let chars_count = label.chars().count(); Self { - base: h_flex().line_height(rems(1.25)), - label: label.into(), - align: TextAlign::default(), + base: div().line_height(rems(1.25)), + label, + chars_count, marked: false, } } - pub fn text_align(mut self, align: TextAlign) -> Self { - self.align = align; - self - } - - pub fn text_left(mut self) -> Self { - self.align = TextAlign::Left; - self - } - - pub fn text_center(mut self) -> Self { - self.align = TextAlign::Center; - self - } - - pub fn text_right(mut self) -> Self { - self.align = TextAlign::Right; - self - } - pub fn masked(mut self, masked: bool) -> Self { self.marked = masked; self @@ -67,28 +40,14 @@ impl Styled for Label { impl RenderOnce for Label { fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { - let text = self.label; - - let text_display = if self.marked { - MASKED.repeat(text.chars().count()) + let text = if self.marked { + SharedString::from(MASKED.repeat(self.chars_count)) } else { - text.to_string() + self.label }; - div().text_color(cx.theme().foreground).child( - self.base - .map(|this| match self.align { - TextAlign::Left => this.justify_start(), - TextAlign::Center => this.justify_center(), - TextAlign::Right => this.justify_end(), - }) - .map(|this| { - if self.align == TextAlign::Left { - this.child(div().size_full().child(text_display)) - } else { - this.child(text_display) - } - }), - ) + div() + .text_color(cx.theme().foreground) + .child(self.base.child(text)) } }