guise-ui 1.5.2

A component library for gpui, Zed's GPU-accelerated UI framework: a themed palette, sizing tokens, 130+ composable components, a reactive state layer, and an in-app Safari-style inspector.
Documentation
//! `Text` — themed inline text with size, weight, and color controls.

use gpui::prelude::*;
use gpui::{div, px, App, FontWeight, IntoElement, SharedString, Window};

use crate::devtools::Probed;
use crate::theme::{theme, Color, Size};

/// Themed body text.
#[derive(IntoElement)]
pub struct Text {
  content: SharedString,
  size: Size,
  weight: FontWeight,
  color: Option<Color>,
  dimmed: bool,
}

impl Text {
  pub fn new(content: impl Into<SharedString>) -> Self {
    Text {
      content: content.into(),
      size: Size::Md,
      weight: FontWeight::NORMAL,
      color: None,
      dimmed: false,
    }
  }

  pub fn size(mut self, size: Size) -> Self {
    self.size = size;
    self
  }

  pub fn weight(mut self, weight: FontWeight) -> Self {
    self.weight = weight;
    self
  }

  /// Render at the medium font weight (500).
  pub fn medium(self) -> Self {
    self.weight(FontWeight::MEDIUM)
  }

  /// Render at the bold font weight (700).
  pub fn bold(self) -> Self {
    self.weight(FontWeight::BOLD)
  }

  /// Override the text color.
  pub fn color(mut self, color: Color) -> Self {
    self.color = Some(color);
    self
  }

  /// Use the muted/secondary text color.
  pub fn dimmed(mut self) -> Self {
    self.dimmed = true;
    self
  }
}

impl RenderOnce for Text {
  fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
    let t = theme(cx);
    let color = match (self.color, self.dimmed) {
      (Some(c), _) => c,
      (None, true) => t.dimmed(),
      (None, false) => t.text(),
    };
    div()
      .min_w(px(0.0))
      .text_size(px(t.font_size(self.size)))
      .font_weight(self.weight)
      .text_color(color.hsla())
      .child(self.content)
      .probe("Text")
      .attr("size", self.size.label())
      .attr_if("dimmed", self.dimmed)
  }
}