text.rs

  1use gpui::{
  2    color::ColorU,
  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(|_| 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<'a>(&self, _: &gpui::AppContext) -> 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 after_layout(
 50        &mut self,
 51        _: pathfinder_geometry::vector::Vector2F,
 52        _: &mut Self::LayoutState,
 53        _: &mut gpui::AfterLayoutContext,
 54    ) {
 55    }
 56
 57    fn paint(
 58        &mut self,
 59        bounds: RectF,
 60        _: &mut Self::LayoutState,
 61        cx: &mut gpui::PaintContext,
 62    ) -> Self::PaintState {
 63        let font_size = 12.;
 64        let family = cx.font_cache.load_family(&["SF Pro Display"]).unwrap();
 65        let normal = cx
 66            .font_cache
 67            .select_font(family, &Default::default())
 68            .unwrap();
 69        let bold = cx
 70            .font_cache
 71            .select_font(
 72                family,
 73                &Properties {
 74                    weight: Weight::BOLD,
 75                    ..Default::default()
 76                },
 77            )
 78            .unwrap();
 79
 80        let text = "Hello world!";
 81        let line = cx.text_layout_cache.layout_str(
 82            text,
 83            font_size,
 84            &[
 85                (1, normal, ColorU::default()),
 86                (1, bold, ColorU::default()),
 87                (1, normal, ColorU::default()),
 88                (1, bold, ColorU::default()),
 89                (text.len() - 4, normal, ColorU::default()),
 90            ],
 91        );
 92
 93        cx.scene.push_quad(Quad {
 94            bounds: bounds,
 95            background: Some(ColorU::white()),
 96            ..Default::default()
 97        });
 98        line.paint(bounds.origin(), bounds, cx);
 99    }
100
101    fn dispatch_event(
102        &mut self,
103        _: &gpui::Event,
104        _: RectF,
105        _: &mut Self::LayoutState,
106        _: &mut Self::PaintState,
107        _: &mut gpui::EventContext,
108    ) -> bool {
109        false
110    }
111
112    fn debug(
113        &self,
114        _: RectF,
115        _: &Self::LayoutState,
116        _: &Self::PaintState,
117        _: &DebugContext,
118    ) -> gpui::json::Value {
119        todo!()
120    }
121}