Skip to content

Instantly share code, notes, and snippets.

@aDotInTheVoid
Created July 28, 2020 18:14
Show Gist options
  • Save aDotInTheVoid/8dbcb41bca30cdb0e91bd71acc7f9b06 to your computer and use it in GitHub Desktop.
Save aDotInTheVoid/8dbcb41bca30cdb0e91bd71acc7f9b06 to your computer and use it in GitHub Desktop.
use druid::piet::{FontBuilder, Text, TextLayout, TextLayoutBuilder};
use druid::theme;
use druid::{widget::prelude::*, WindowDesc, AppLauncher};
struct TextTest;
impl Widget<()> for TextTest {
fn event(&mut self, _: &mut EventCtx, _: &Event, _: &mut (), _: &Env) {}
fn lifecycle(&mut self, _: &mut LifeCycleCtx, _: &LifeCycle, _: &(), _: &Env) {}
fn update(&mut self, _: &mut UpdateCtx, _: &(), _: &(), _: &Env) {}
fn layout(&mut self, _: &mut LayoutCtx, bc: &BoxConstraints, _: &(), _: &Env) -> Size {
bc.max()
}
fn paint(&mut self, ctx: &mut PaintCtx, _: &(), env: &Env) {
let font_name = env.get(theme::FONT_NAME);
let font_size = env.get(theme::TEXT_SIZE_NORMAL);
let font = ctx
.text()
.new_font_by_name(font_name, font_size)
.build()
.unwrap();
let text = ctx
.text()
.new_text_layout(&font, "Test", None)
.build()
.unwrap();
let pos = text.hit_test_text_position(1).unwrap().point;
panic!("{:#?}", pos);
}
}
fn main() {
let main_window = WindowDesc::new(||TextTest)
.title("hello")
.window_size((400.0, 400.0));
AppLauncher::with_window(main_window)
.launch(())
.unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment