text.rs

  1use gpui::{
  2    color::Color,
  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(|cx| {
 14        cx.platform().activate(true);
 15        cx.add_window(Default::default(), |_| 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(&mut self, _: &mut gpui::RenderContext<Self>) -> 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 paint(
 50        &mut self,
 51        bounds: RectF,
 52        visible_bounds: RectF,
 53        _: &mut Self::LayoutState,
 54        cx: &mut gpui::PaintContext,
 55    ) -> Self::PaintState {
 56        let font_size = 12.;
 57        let family = cx.font_cache.load_family(&["SF Pro Display"]).unwrap();
 58        let normal = cx
 59            .font_cache
 60            .select_font(family, &Default::default())
 61            .unwrap();
 62        let bold = cx
 63            .font_cache
 64            .select_font(
 65                family,
 66                &Properties {
 67                    weight: Weight::BOLD,
 68                    ..Default::default()
 69                },
 70            )
 71            .unwrap();
 72
 73        let text = "Hello world!";
 74        let line = cx.text_layout_cache.layout_str(
 75            text,
 76            font_size,
 77            &[
 78                (1, normal, Color::default()),
 79                (1, bold, Color::default()),
 80                (1, normal, Color::default()),
 81                (1, bold, Color::default()),
 82                (text.len() - 4, normal, Color::default()),
 83            ],
 84        );
 85
 86        cx.scene.push_quad(Quad {
 87            bounds,
 88            background: Some(Color::white()),
 89            ..Default::default()
 90        });
 91        line.paint(bounds.origin(), visible_bounds, bounds.height(), cx);
 92    }
 93
 94    fn dispatch_event(
 95        &mut self,
 96        _: &gpui::Event,
 97        _: RectF,
 98        _: &mut Self::LayoutState,
 99        _: &mut Self::PaintState,
100        _: &mut gpui::EventContext,
101    ) -> bool {
102        false
103    }
104
105    fn debug(
106        &self,
107        _: RectF,
108        _: &Self::LayoutState,
109        _: &Self::PaintState,
110        _: &DebugContext,
111    ) -> gpui::json::Value {
112        todo!()
113    }
114}