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