Render example text in example via Line::paint

This commit is contained in:
Nathan Sobo 2021-04-06 06:30:05 -06:00
parent 765c3f9c18
commit fc4135d55b

View file

@ -1,11 +1,10 @@
use gpui::{ use gpui::{
color::ColorU,
fonts::{Properties, Weight}, fonts::{Properties, Weight},
platform::{current as platform, Runner}, platform::{current as platform, Runner},
scene::Glyph, Border, Element as _, Quad,
Element as _,
}; };
use log::LevelFilter; use log::LevelFilter;
use pathfinder_color::ColorU;
use simplelog::SimpleLogger; use simplelog::SimpleLogger;
fn main() { fn main() {
@ -15,29 +14,29 @@ fn main() {
platform::runner() platform::runner()
.on_finish_launching(move || { .on_finish_launching(move || {
app.platform().activate(true); app.platform().activate(true);
app.add_window(|_| View); app.add_window(|_| TextView);
}) })
.run(); .run();
} }
struct View; struct TextView;
struct Element; struct TextElement;
impl gpui::Entity for View { impl gpui::Entity for TextView {
type Event = (); type Event = ();
} }
impl gpui::View for View { impl gpui::View for TextView {
fn ui_name() -> &'static str { fn ui_name() -> &'static str {
"view" "View"
} }
fn render<'a>(&self, app: &gpui::AppContext) -> gpui::ElementBox { fn render<'a>(&self, app: &gpui::AppContext) -> gpui::ElementBox {
Element.boxed() TextElement.boxed()
} }
} }
impl gpui::Element for Element { impl gpui::Element for TextElement {
type LayoutState = (); type LayoutState = ();
type PaintState = (); type PaintState = ();
@ -45,27 +44,27 @@ impl gpui::Element for Element {
fn layout( fn layout(
&mut self, &mut self,
constraint: gpui::SizeConstraint, constraint: gpui::SizeConstraint,
ctx: &mut gpui::LayoutContext, _: &mut gpui::LayoutContext,
) -> (pathfinder_geometry::vector::Vector2F, Self::LayoutState) { ) -> (pathfinder_geometry::vector::Vector2F, Self::LayoutState) {
(constraint.max, ()) (constraint.max, ())
} }
fn after_layout( fn after_layout(
&mut self, &mut self,
size: pathfinder_geometry::vector::Vector2F, _: pathfinder_geometry::vector::Vector2F,
layout: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut gpui::AfterLayoutContext, _: &mut gpui::AfterLayoutContext,
) { ) {
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: pathfinder_geometry::rect::RectF, bounds: pathfinder_geometry::rect::RectF,
layout: &mut Self::LayoutState, _: &mut Self::LayoutState,
ctx: &mut gpui::PaintContext, ctx: &mut gpui::PaintContext,
) -> Self::PaintState { ) -> Self::PaintState {
let font_size = 18.; let font_size = 12.;
let family = ctx.font_cache.load_family(&["Fira Code"]).unwrap(); let family = ctx.font_cache.load_family(&["SF Pro Display"]).unwrap();
let normal = ctx let normal = ctx
.font_cache .font_cache
.select_font(family, &Default::default()) .select_font(family, &Default::default())
@ -81,32 +80,39 @@ impl gpui::Element for Element {
) )
.unwrap(); .unwrap();
let text = "Hello world!";
let line = ctx.text_layout_cache.layout_str( let line = ctx.text_layout_cache.layout_str(
"xxXX", text,
font_size, font_size,
&[(0..1, normal), (1..2, bold), (2..3, normal), (3..4, bold)], &[
(0..1, normal),
(1..2, bold),
(2..3, normal),
(3..4, bold),
(4..text.len(), normal),
],
); );
for run in line.runs { ctx.scene.push_quad(Quad {
for glyph in run.glyphs { bounds: bounds,
ctx.scene.push_glyph(Glyph { background: Some(ColorU::white()),
font_id: run.font_id, ..Default::default()
font_size, });
id: glyph.id, line.paint(
origin: glyph.position, bounds.origin(),
color: ColorU::black(), bounds,
}); &[(0..text.len(), ColorU::black())],
} ctx,
} );
} }
fn dispatch_event( fn dispatch_event(
&mut self, &mut self,
event: &gpui::Event, _: &gpui::Event,
bounds: pathfinder_geometry::rect::RectF, _: pathfinder_geometry::rect::RectF,
layout: &mut Self::LayoutState, _: &mut Self::LayoutState,
paint: &mut Self::PaintState, _: &mut Self::PaintState,
ctx: &mut gpui::EventContext, _: &mut gpui::EventContext,
) -> bool { ) -> bool {
false false
} }