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