text.rs

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