mod.rs

 1mod event;
 2#[cfg(target_os = "macos")]
 3pub mod mac;
 4pub mod test;
 5pub mod current {
 6    #[cfg(target_os = "macos")]
 7    pub use super::mac::*;
 8}
 9
10use crate::{
11    executor,
12    fonts::{FontId, GlyphId, Metrics as FontMetrics, Properties as FontProperties},
13    geometry::{
14        rect::{RectF, RectI},
15        vector::Vector2F,
16    },
17    text_layout::Line,
18    Scene,
19};
20use anyhow::Result;
21use async_task::Runnable;
22pub use event::Event;
23use std::{ops::Range, path::PathBuf, rc::Rc, sync::Arc};
24
25pub trait Runner {
26    fn on_finish_launching<F: 'static + FnOnce()>(self, callback: F) -> Self where;
27    fn on_become_active<F: 'static + FnMut()>(self, callback: F) -> Self;
28    fn on_resign_active<F: 'static + FnMut()>(self, callback: F) -> Self;
29    fn on_event<F: 'static + FnMut(Event) -> bool>(self, callback: F) -> Self;
30    fn on_open_files<F: 'static + FnMut(Vec<PathBuf>)>(self, callback: F) -> Self;
31    fn run(self);
32}
33
34pub trait App {
35    fn dispatcher(&self) -> Arc<dyn Dispatcher>;
36    fn activate(&self, ignoring_other_apps: bool);
37    fn open_window(
38        &self,
39        options: WindowOptions,
40        executor: Rc<executor::Foreground>,
41    ) -> Result<Box<dyn Window>>;
42    fn fonts(&self) -> Arc<dyn FontSystem>;
43}
44
45pub trait Dispatcher: Send + Sync {
46    fn is_main_thread(&self) -> bool;
47    fn run_on_main_thread(&self, task: Runnable);
48}
49
50pub trait Window: WindowContext {
51    fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
52    fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn WindowContext)>);
53}
54
55pub trait WindowContext {
56    fn size(&self) -> Vector2F;
57    fn scale_factor(&self) -> f32;
58    fn present_scene(&mut self, scene: Scene);
59}
60
61pub struct WindowOptions<'a> {
62    pub bounds: RectF,
63    pub title: Option<&'a str>,
64}
65
66pub trait FontSystem: Send + Sync {
67    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
68    fn select_font(
69        &self,
70        font_ids: &[FontId],
71        properties: &FontProperties,
72    ) -> anyhow::Result<FontId>;
73    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
74    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
75    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
76    fn rasterize_glyph(
77        &self,
78        font_id: FontId,
79        font_size: f32,
80        glyph_id: GlyphId,
81        subpixel_shift: Vector2F,
82        scale_factor: f32,
83    ) -> Option<(RectI, Vec<u8>)>;
84    fn layout_str(&self, text: &str, font_size: f32, runs: &[(Range<usize>, FontId)]) -> Line;
85}