text.rs

  1use gpui::{
  2    color::Color,
  3    fonts::{Properties, Weight},
  4    DebugContext, Element as _, Quad,
  5};
  6use log::LevelFilter;
  7use pathfinder_geometry::rect::RectF;
  8use simplelog::SimpleLogger;
  9
 10fn main() {
 11    SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
 12
 13    gpui::App::new(()).unwrap().run(|cx| {
 14        cx.platform().activate(true);
 15        cx.add_window(Default::default(), |_| TextView);
 16    });
 17}
 18
 19struct TextView;
 20struct TextElement;
 21
 22impl gpui::Entity for TextView {
 23    type Event = ();
 24}
 25
 26impl gpui::View for TextView {
 27    fn ui_name() -> &'static str {
 28        "View"
 29    }
 30
 31    fn render(&self, _: &mut gpui::RenderContext<Self>) -> gpui::ElementBox {
 32        TextElement.boxed()
 33    }
 34}
 35
 36impl gpui::Element for TextElement {
 37    type LayoutState = ();
 38
 39    type PaintState = ();
 40
 41    fn layout(
 42        &mut self,
 43        constraint: gpui::SizeConstraint,
 44        _: &mut gpui::LayoutContext,
 45    ) -> (pathfinder_geometry::vector::Vector2F, Self::LayoutState) {
 46        (constraint.max, ())
 47    }
 48
 49    fn paint(
 50        &mut self,
 51        bounds: RectF,
 52        _: &mut Self::LayoutState,
 53        cx: &mut gpui::PaintContext,
 54    ) -> Self::PaintState {
 55        let font_size = 12.;
 56        let family = cx.font_cache.load_family(&["SF Pro Display"]).unwrap();
 57        let normal = cx
 58            .font_cache
 59            .select_font(family, &Default::default())
 60            .unwrap();
 61        let bold = cx
 62            .font_cache
 63            .select_font(
 64                family,
 65                &Properties {
 66                    weight: Weight::BOLD,
 67                    ..Default::default()
 68                },
 69            )
 70            .unwrap();
 71
 72        let text = "Hello world!";
 73        let line = cx.text_layout_cache.layout_str(
 74            text,
 75            font_size,
 76            &[
 77                (1, normal, Color::default()),
 78                (1, bold, Color::default()),
 79                (1, normal, Color::default()),
 80                (1, bold, Color::default()),
 81                (text.len() - 4, normal, Color::default()),
 82            ],
 83        );
 84
 85        cx.scene.push_quad(Quad {
 86            bounds: bounds,
 87            background: Some(Color::white()),
 88            ..Default::default()
 89        });
 90        line.paint(bounds.origin(), bounds, cx);
 91    }
 92
 93    fn dispatch_event(
 94        &mut self,
 95        _: &gpui::Event,
 96        _: RectF,
 97        _: &mut Self::LayoutState,
 98        _: &mut Self::PaintState,
 99        _: &mut gpui::EventContext,
100    ) -> bool {
101        false
102    }
103
104    fn debug(
105        &self,
106        _: RectF,
107        _: &Self::LayoutState,
108        _: &Self::PaintState,
109        _: &DebugContext,
110    ) -> gpui::json::Value {
111        todo!()
112    }
113}