text.rs

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