text.rs

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