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