test.rs

  1use crate::fonts::FontId;
  2use pathfinder_geometry::{
  3    rect,
  4    vector::{vec2f, vec2i, Vector2F},
  5};
  6use std::rc::Rc;
  7use std::sync::Arc;
  8
  9struct App {
 10    dispatcher: Arc<dyn super::Dispatcher>,
 11}
 12
 13struct Dispatcher;
 14struct FontSystem;
 15
 16pub struct Window {
 17    size: Vector2F,
 18    scale_factor: f32,
 19    current_scene: Option<crate::Scene>,
 20    event_handlers: Vec<Box<dyn FnMut(super::Event)>>,
 21    resize_handlers: Vec<Box<dyn FnMut(&mut dyn super::WindowContext)>>,
 22}
 23
 24pub struct WindowContext {}
 25
 26impl App {
 27    fn new() -> Self {
 28        Self {
 29            dispatcher: Arc::new(Dispatcher),
 30        }
 31    }
 32}
 33
 34impl super::App for App {
 35    fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
 36        self.dispatcher.clone()
 37    }
 38
 39    fn activate(&self, ignoring_other_apps: bool) {}
 40
 41    fn open_window(
 42        &self,
 43        options: super::WindowOptions,
 44        executor: Rc<super::executor::Foreground>,
 45    ) -> anyhow::Result<Box<dyn super::Window>> {
 46        Ok(Box::new(Window::new(options.bounds.size())))
 47    }
 48
 49    fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
 50        Arc::new(FontSystem)
 51    }
 52}
 53
 54impl Window {
 55    fn new(size: Vector2F) -> Self {
 56        Self {
 57            size,
 58            event_handlers: Vec::new(),
 59            resize_handlers: Vec::new(),
 60            scale_factor: 1.0,
 61            current_scene: None,
 62        }
 63    }
 64}
 65
 66impl super::Dispatcher for Dispatcher {
 67    fn is_main_thread(&self) -> bool {
 68        true
 69    }
 70
 71    fn run_on_main_thread(&self, task: async_task::Runnable) {
 72        task.run();
 73    }
 74}
 75
 76impl super::WindowContext for Window {
 77    fn size(&self) -> Vector2F {
 78        self.size
 79    }
 80
 81    fn scale_factor(&self) -> f32 {
 82        self.scale_factor
 83    }
 84
 85    fn present_scene(&mut self, scene: crate::Scene) {
 86        self.current_scene = Some(scene);
 87    }
 88}
 89
 90impl super::Window for Window {
 91    fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
 92        self.event_handlers.push(callback);
 93    }
 94
 95    fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn super::WindowContext)>) {
 96        self.resize_handlers.push(callback);
 97    }
 98}
 99
100impl super::FontSystem for FontSystem {
101    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>> {
102        Ok(vec![FontId(0)])
103    }
104
105    fn select_font(
106        &self,
107        font_ids: &[FontId],
108        properties: &font_kit::properties::Properties,
109    ) -> anyhow::Result<FontId> {
110        Ok(font_ids[0])
111    }
112
113    fn font_metrics(&self, font_id: FontId) -> font_kit::metrics::Metrics {
114        font_kit::metrics::Metrics {
115            units_per_em: 1,
116            ascent: 0.,
117            descent: 0.,
118            line_gap: 0.,
119            underline_position: 1.,
120            underline_thickness: 1.,
121            cap_height: 12.,
122            x_height: 12.,
123            bounding_box: rect::RectF::new(vec2f(0., 0.), vec2f(10., 10.)),
124        }
125    }
126
127    fn typographic_bounds(
128        &self,
129        font_id: FontId,
130        glyph_id: crate::fonts::GlyphId,
131    ) -> anyhow::Result<rect::RectF> {
132        Ok(rect::RectF::new(vec2f(0., 0.), vec2f(0., 0.)))
133    }
134
135    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<crate::fonts::GlyphId> {
136        Some(0)
137    }
138
139    fn rasterize_glyph(
140        &self,
141        font_id: FontId,
142        font_size: f32,
143        glyph_id: crate::fonts::GlyphId,
144        subpixel_shift: Vector2F,
145        scale_factor: f32,
146    ) -> Option<(rect::RectI, Vec<u8>)> {
147        Some((rect::RectI::new(vec2i(0, 0), vec2i(0, 0)), vec![]))
148    }
149
150    fn layout_str(
151        &self,
152        text: &str,
153        font_size: f32,
154        runs: &[(std::ops::Range<usize>, FontId)],
155    ) -> crate::text_layout::Line {
156        crate::text_layout::Line {
157            width: 0.,
158            runs: vec![],
159            len: 0,
160            font_size: 12.,
161        }
162    }
163}
164
165pub fn app() -> impl super::App {
166    App::new()
167}