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" />
This commit is contained in:
parent
44e678b9bc
commit
4a26776289
2 changed files with 49 additions and 55 deletions
35
crates/story/examples/text.rs
Normal file
35
crates/story/examples/text.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
use gpui::*;
|
||||||
|
use story::{Assets, TextStory};
|
||||||
|
|
||||||
|
pub struct Example {
|
||||||
|
root: Entity<TextStory>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Example {
|
||||||
|
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||||
|
let root = TextStory::view(window, cx);
|
||||||
|
|
||||||
|
Self { root }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(window: &mut Window, cx: &mut App) -> Entity<Self> {
|
||||||
|
cx.new(|cx| Self::new(window, cx))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Render for Example {
|
||||||
|
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -1,58 +1,31 @@
|
||||||
use gpui::{
|
use gpui::{
|
||||||
div, prelude::FluentBuilder, rems, App, Div, IntoElement, ParentElement, RenderOnce,
|
div, rems, App, Div, IntoElement, ParentElement, RenderOnce, SharedString, Styled, Window,
|
||||||
SharedString, Styled, Window,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{h_flex, ActiveTheme};
|
use crate::ActiveTheme;
|
||||||
|
|
||||||
const MASKED: &'static str = "•";
|
const MASKED: &'static str = "•";
|
||||||
|
|
||||||
#[derive(Default, PartialEq, Eq)]
|
|
||||||
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,
|
||||||
align: TextAlign,
|
chars_count: usize,
|
||||||
marked: bool,
|
marked: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Label {
|
impl Label {
|
||||||
pub fn new(label: impl Into<SharedString>) -> Self {
|
pub fn new(label: impl Into<SharedString>) -> Self {
|
||||||
|
let label: SharedString = label.into();
|
||||||
|
let chars_count = label.chars().count();
|
||||||
Self {
|
Self {
|
||||||
base: h_flex().line_height(rems(1.25)),
|
base: div().line_height(rems(1.25)),
|
||||||
label: label.into(),
|
label,
|
||||||
align: TextAlign::default(),
|
chars_count,
|
||||||
marked: false,
|
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 {
|
pub fn masked(mut self, masked: bool) -> Self {
|
||||||
self.marked = masked;
|
self.marked = masked;
|
||||||
self
|
self
|
||||||
|
|
@ -67,28 +40,14 @@ impl Styled for Label {
|
||||||
|
|
||||||
impl RenderOnce for Label {
|
impl RenderOnce for Label {
|
||||||
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
|
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||||
let text = self.label;
|
let text = if self.marked {
|
||||||
|
SharedString::from(MASKED.repeat(self.chars_count))
|
||||||
let text_display = if self.marked {
|
|
||||||
MASKED.repeat(text.chars().count())
|
|
||||||
} else {
|
} else {
|
||||||
text.to_string()
|
self.label
|
||||||
};
|
};
|
||||||
|
|
||||||
div().text_color(cx.theme().foreground).child(
|
div()
|
||||||
self.base
|
.text_color(cx.theme().foreground)
|
||||||
.map(|this| match self.align {
|
.child(self.base.child(text))
|
||||||
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)
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue