Refactor text style helpers

This commit is contained in:
2026-05-09 19:31:12 +08:00
parent 6eabc6a0ec
commit 9f1e051073
7 changed files with 44 additions and 58 deletions

View File

@@ -4,10 +4,6 @@ use gpui::{ParentElement, Refineable as _, Styled, div};
#[derive(gpui::IntoElement)]
pub(crate) struct Text {
content: gpui::AnyElement,
font_weight: gpui::FontWeight,
opacity: f32,
text_align: gpui::TextAlign,
line_height: gpui::DefiniteLength,
style: gpui::StyleRefinement,
}
@@ -20,11 +16,11 @@ impl TextContent for gpui::SharedString {}
pub(crate) fn text(content: impl TextContent) -> Text {
Text {
content: content.into_any_element(),
font_weight: gpui::FontWeight::NORMAL,
opacity: 1.,
text_align: gpui::TextAlign::Left,
line_height: gpui::relative(1.5),
style: gpui::StyleRefinement::default(),
style: gpui::StyleRefinement::default()
.font_weight(gpui::FontWeight::NORMAL)
.opacity(1.)
.text_align(gpui::TextAlign::Left)
.line_height(gpui::relative(1.5)),
}
}
@@ -35,31 +31,6 @@ impl Styled for Text {
}
impl Text {
pub(crate) fn light(mut self) -> Self {
self.font_weight = gpui::FontWeight::LIGHT;
self
}
pub(crate) fn medium(mut self) -> Self {
self.font_weight = gpui::FontWeight::MEDIUM;
self
}
pub(crate) fn bold(mut self) -> Self {
self.font_weight = gpui::FontWeight::BOLD;
self
}
pub(crate) fn opacity(mut self, opacity: f32) -> Self {
self.opacity = opacity;
self
}
pub(crate) fn line_height(mut self, line_height: impl Into<gpui::DefiniteLength>) -> Self {
self.line_height = line_height.into();
self
}
pub(crate) fn leading_none(self) -> Self {
self.line_height(gpui::relative(1.0))
}
@@ -115,23 +86,13 @@ impl Text {
pub(crate) fn leading_10(self) -> Self {
self.line_height(gpui::rems(2.5))
}
pub(crate) fn centered(mut self) -> Self {
self.text_align = gpui::TextAlign::Center;
self
}
}
impl gpui::RenderOnce for Text {
fn render(self, _window: &mut gpui::Window, cx: &mut gpui::App) -> impl gpui::IntoElement {
let theme = app::current_theme(cx);
let mut div = div()
.font_weight(self.font_weight)
.opacity(self.opacity)
.text_align(self.text_align)
.line_height(self.line_height)
.child(self.content);
let mut div = div().child(self.content);
div.style().refine(&self.style);
if div.style().text.as_ref().and_then(|it| it.color).is_none() {