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(|ctx| {
 14        ctx.platform().activate(true);
 15        ctx.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        ctx: &mut gpui::PaintContext,
 62    ) -> Self::PaintState {
 63        let font_size = 12.;
 64        let family = ctx.font_cache.load_family(&["SF Pro Display"]).unwrap();
 65        let normal = ctx
 66            .font_cache
 67            .select_font(family, &Default::default())
 68            .unwrap();
 69        let bold = ctx
 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 = ctx.text_layout_cache.layout_str(
 82            text,
 83            font_size,
 84            &[
 85                (0..1, normal),
 86                (1..2, bold),
 87                (2..3, normal),
 88                (3..4, bold),
 89                (4..text.len(), normal),
 90            ],
 91        );
 92
 93        ctx.scene.push_quad(Quad {
 94            bounds: bounds,
 95            background: Some(ColorU::white()),
 96            ..Default::default()
 97        });
 98        line.paint(
 99            bounds.origin(),
100            bounds,
101            &[(0..text.len(), ColorU::black())],
102            ctx,
103        );
104    }
105
106    fn dispatch_event(
107        &mut self,
108        _: &gpui::Event,
109        _: RectF,
110        _: &mut Self::LayoutState,
111        _: &mut Self::PaintState,
112        _: &mut gpui::EventContext,
113    ) -> bool {
114        false
115    }
116
117    fn debug(
118        &self,
119        _: RectF,
120        _: &Self::LayoutState,
121        _: &Self::PaintState,
122        _: &DebugContext,
123    ) -> gpui::json::Value {
124        todo!()
125    }
126}