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