2021-04-03 10:19:20 +03:00
|
|
|
use gpui::{
|
2021-08-03 22:48:58 +03:00
|
|
|
color::Color,
|
2021-04-03 10:19:20 +03:00
|
|
|
fonts::{Properties, Weight},
|
2021-09-07 01:36:09 +03:00
|
|
|
text_layout::RunStyle,
|
2021-04-07 08:50:13 +03:00
|
|
|
DebugContext, Element as _, Quad,
|
2021-04-03 10:19:20 +03:00
|
|
|
};
|
|
|
|
use log::LevelFilter;
|
2021-04-07 08:50:13 +03:00
|
|
|
use pathfinder_geometry::rect::RectF;
|
2021-04-03 10:19:20 +03:00
|
|
|
use simplelog::SimpleLogger;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
|
|
|
|
|
2021-05-29 01:25:15 +03:00
|
|
|
gpui::App::new(()).unwrap().run(|cx| {
|
|
|
|
cx.platform().activate(true);
|
2021-08-05 20:48:35 +03:00
|
|
|
cx.add_window(Default::default(), |_| TextView);
|
2021-04-10 06:33:17 +03:00
|
|
|
});
|
2021-04-03 10:19:20 +03:00
|
|
|
}
|
|
|
|
|
2021-04-06 15:30:05 +03:00
|
|
|
struct TextView;
|
|
|
|
struct TextElement;
|
2021-04-03 10:19:20 +03:00
|
|
|
|
2021-04-06 15:30:05 +03:00
|
|
|
impl gpui::Entity for TextView {
|
2021-04-03 10:19:20 +03:00
|
|
|
type Event = ();
|
|
|
|
}
|
|
|
|
|
2021-04-06 15:30:05 +03:00
|
|
|
impl gpui::View for TextView {
|
2021-04-03 10:19:20 +03:00
|
|
|
fn ui_name() -> &'static str {
|
2021-04-06 15:30:05 +03:00
|
|
|
"View"
|
2021-04-03 10:19:20 +03:00
|
|
|
}
|
|
|
|
|
2021-08-30 18:51:26 +03:00
|
|
|
fn render(&mut self, _: &mut gpui::RenderContext<Self>) -> gpui::ElementBox {
|
2021-04-06 15:30:05 +03:00
|
|
|
TextElement.boxed()
|
2021-04-03 10:19:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 15:30:05 +03:00
|
|
|
impl gpui::Element for TextElement {
|
2021-04-03 10:19:20 +03:00
|
|
|
type LayoutState = ();
|
|
|
|
|
|
|
|
type PaintState = ();
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
constraint: gpui::SizeConstraint,
|
2021-04-06 15:30:05 +03:00
|
|
|
_: &mut gpui::LayoutContext,
|
2021-04-03 10:19:20 +03:00
|
|
|
) -> (pathfinder_geometry::vector::Vector2F, Self::LayoutState) {
|
|
|
|
(constraint.max, ())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
2021-04-07 08:50:13 +03:00
|
|
|
bounds: RectF,
|
2021-09-02 15:33:52 +03:00
|
|
|
visible_bounds: RectF,
|
2021-04-06 15:30:05 +03:00
|
|
|
_: &mut Self::LayoutState,
|
2021-05-29 01:25:15 +03:00
|
|
|
cx: &mut gpui::PaintContext,
|
2021-04-03 10:19:20 +03:00
|
|
|
) -> Self::PaintState {
|
2021-04-06 15:30:05 +03:00
|
|
|
let font_size = 12.;
|
2021-05-29 01:25:15 +03:00
|
|
|
let family = cx.font_cache.load_family(&["SF Pro Display"]).unwrap();
|
2021-09-07 01:36:09 +03:00
|
|
|
let normal = RunStyle {
|
|
|
|
font_id: cx
|
|
|
|
.font_cache
|
|
|
|
.select_font(family, &Default::default())
|
|
|
|
.unwrap(),
|
|
|
|
color: Color::default(),
|
|
|
|
underline: false,
|
|
|
|
};
|
|
|
|
let bold = RunStyle {
|
|
|
|
font_id: cx
|
|
|
|
.font_cache
|
|
|
|
.select_font(
|
|
|
|
family,
|
|
|
|
&Properties {
|
|
|
|
weight: Weight::BOLD,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
color: Color::default(),
|
|
|
|
underline: false,
|
|
|
|
};
|
2021-04-03 10:19:20 +03:00
|
|
|
|
2021-04-06 15:30:05 +03:00
|
|
|
let text = "Hello world!";
|
2021-05-29 01:25:15 +03:00
|
|
|
let line = cx.text_layout_cache.layout_str(
|
2021-04-06 15:30:05 +03:00
|
|
|
text,
|
2021-04-03 10:19:20 +03:00
|
|
|
font_size,
|
2021-04-06 15:30:05 +03:00
|
|
|
&[
|
2021-09-07 01:36:09 +03:00
|
|
|
(1, normal.clone()),
|
|
|
|
(1, bold.clone()),
|
|
|
|
(1, normal.clone()),
|
|
|
|
(1, bold.clone()),
|
|
|
|
(text.len() - 4, normal.clone()),
|
2021-04-06 15:30:05 +03:00
|
|
|
],
|
2021-04-03 10:19:20 +03:00
|
|
|
);
|
|
|
|
|
2021-05-29 01:25:15 +03:00
|
|
|
cx.scene.push_quad(Quad {
|
2021-09-02 15:33:52 +03:00
|
|
|
bounds,
|
2021-08-03 22:48:58 +03:00
|
|
|
background: Some(Color::white()),
|
2021-04-06 15:30:05 +03:00
|
|
|
..Default::default()
|
|
|
|
});
|
2021-09-02 15:33:52 +03:00
|
|
|
line.paint(bounds.origin(), visible_bounds, bounds.height(), cx);
|
2021-04-03 10:19:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dispatch_event(
|
|
|
|
&mut self,
|
2021-04-06 15:30:05 +03:00
|
|
|
_: &gpui::Event,
|
2021-04-07 08:50:13 +03:00
|
|
|
_: RectF,
|
2021-04-06 15:30:05 +03:00
|
|
|
_: &mut Self::LayoutState,
|
|
|
|
_: &mut Self::PaintState,
|
|
|
|
_: &mut gpui::EventContext,
|
2021-04-03 10:19:20 +03:00
|
|
|
) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2021-04-07 08:50:13 +03:00
|
|
|
|
|
|
|
fn debug(
|
|
|
|
&self,
|
|
|
|
_: RectF,
|
|
|
|
_: &Self::LayoutState,
|
|
|
|
_: &Self::PaintState,
|
|
|
|
_: &DebugContext,
|
|
|
|
) -> gpui::json::Value {
|
|
|
|
todo!()
|
|
|
|
}
|
2021-04-03 10:19:20 +03:00
|
|
|
}
|