zed/gpui/examples/text.rs
Max Brunsfeld ef0ffbe819 Use custom color wrapper type everywhere in gpui & zed
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
2021-08-03 12:48:58 -07:00

121 lines
2.8 KiB
Rust

use gpui::{
color::Color,
fonts::{Properties, Weight},
DebugContext, Element as _, Quad,
};
use log::LevelFilter;
use pathfinder_geometry::rect::RectF;
use simplelog::SimpleLogger;
fn main() {
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
gpui::App::new(()).unwrap().run(|cx| {
cx.platform().activate(true);
cx.add_window(|_| TextView);
});
}
struct TextView;
struct TextElement;
impl gpui::Entity for TextView {
type Event = ();
}
impl gpui::View for TextView {
fn ui_name() -> &'static str {
"View"
}
fn render(&self, _: &gpui::RenderContext<Self>) -> gpui::ElementBox {
TextElement.boxed()
}
}
impl gpui::Element for TextElement {
type LayoutState = ();
type PaintState = ();
fn layout(
&mut self,
constraint: gpui::SizeConstraint,
_: &mut gpui::LayoutContext,
) -> (pathfinder_geometry::vector::Vector2F, Self::LayoutState) {
(constraint.max, ())
}
fn after_layout(
&mut self,
_: pathfinder_geometry::vector::Vector2F,
_: &mut Self::LayoutState,
_: &mut gpui::AfterLayoutContext,
) {
}
fn paint(
&mut self,
bounds: RectF,
_: &mut Self::LayoutState,
cx: &mut gpui::PaintContext,
) -> Self::PaintState {
let font_size = 12.;
let family = cx.font_cache.load_family(&["SF Pro Display"]).unwrap();
let normal = cx
.font_cache
.select_font(family, &Default::default())
.unwrap();
let bold = cx
.font_cache
.select_font(
family,
&Properties {
weight: Weight::BOLD,
..Default::default()
},
)
.unwrap();
let text = "Hello world!";
let line = cx.text_layout_cache.layout_str(
text,
font_size,
&[
(1, normal, Color::default()),
(1, bold, Color::default()),
(1, normal, Color::default()),
(1, bold, Color::default()),
(text.len() - 4, normal, Color::default()),
],
);
cx.scene.push_quad(Quad {
bounds: bounds,
background: Some(Color::white()),
..Default::default()
});
line.paint(bounds.origin(), bounds, cx);
}
fn dispatch_event(
&mut self,
_: &gpui::Event,
_: RectF,
_: &mut Self::LayoutState,
_: &mut Self::PaintState,
_: &mut gpui::EventContext,
) -> bool {
false
}
fn debug(
&self,
_: RectF,
_: &Self::LayoutState,
_: &Self::PaintState,
_: &DebugContext,
) -> gpui::json::Value {
todo!()
}
}