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    Menu, 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;
27    fn on_menu_command<F: 'static + FnMut(&str)>(self, callback: F) -> Self;
28    fn on_become_active<F: 'static + FnMut()>(self, callback: F) -> Self;
29    fn on_resign_active<F: 'static + FnMut()>(self, callback: F) -> Self;
30    fn on_event<F: 'static + FnMut(Event) -> bool>(self, callback: F) -> Self;
31    fn on_open_files<F: 'static + FnMut(Vec<PathBuf>)>(self, callback: F) -> Self;
32    fn set_menus(self, menus: &[Menu]) -> Self;
33    fn run(self);
34}
35
36pub trait App {
37    fn dispatcher(&self) -> Arc<dyn Dispatcher>;
38    fn activate(&self, ignoring_other_apps: bool);
39    fn open_window(
40        &self,
41        options: WindowOptions,
42        executor: Rc<executor::Foreground>,
43    ) -> Result<Box<dyn Window>>;
44    fn prompt_for_paths(&self, options: PathPromptOptions) -> Option<Vec<PathBuf>>;
45    fn fonts(&self) -> Arc<dyn FontSystem>;
46    fn quit(&self);
47}
48
49pub trait Dispatcher: Send + Sync {
50    fn is_main_thread(&self) -> bool;
51    fn run_on_main_thread(&self, task: Runnable);
52}
53
54pub trait Window: WindowContext {
55    fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
56    fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn WindowContext)>);
57}
58
59pub trait WindowContext {
60    fn size(&self) -> Vector2F;
61    fn scale_factor(&self) -> f32;
62    fn present_scene(&mut self, scene: Scene);
63}
64
65pub struct WindowOptions<'a> {
66    pub bounds: RectF,
67    pub title: Option<&'a str>,
68}
69
70pub struct PathPromptOptions {
71    pub files: bool,
72    pub directories: bool,
73    pub multiple: bool,
74}
75
76pub trait FontSystem: Send + Sync {
77    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
78    fn select_font(
79        &self,
80        font_ids: &[FontId],
81        properties: &FontProperties,
82    ) -> anyhow::Result<FontId>;
83    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
84    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
85    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
86    fn rasterize_glyph(
87        &self,
88        font_id: FontId,
89        font_size: f32,
90        glyph_id: GlyphId,
91        subpixel_shift: Vector2F,
92        scale_factor: f32,
93    ) -> Option<(RectI, Vec<u8>)>;
94    fn layout_str(&self, text: &str, font_size: f32, runs: &[(Range<usize>, FontId)]) -> Line;
95}