platform.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    color::ColorU,
 12    executor,
 13    fonts::{FontId, GlyphId, Metrics as FontMetrics, Properties as FontProperties},
 14    geometry::{
 15        rect::{RectF, RectI},
 16        vector::Vector2F,
 17    },
 18    text_layout::LineLayout,
 19    ClipboardItem, Menu, Scene,
 20};
 21use async_task::Runnable;
 22pub use event::Event;
 23use std::{
 24    any::Any,
 25    path::{Path, PathBuf},
 26    rc::Rc,
 27    sync::Arc,
 28};
 29
 30pub trait Platform {
 31    fn on_menu_command(&self, callback: Box<dyn FnMut(&str, Option<&dyn Any>)>);
 32    fn on_become_active(&self, callback: Box<dyn FnMut()>);
 33    fn on_resign_active(&self, callback: Box<dyn FnMut()>);
 34    fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
 35    fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
 36    fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
 37
 38    fn dispatcher(&self) -> Arc<dyn Dispatcher>;
 39    fn fonts(&self) -> Arc<dyn FontSystem>;
 40
 41    fn activate(&self, ignoring_other_apps: bool);
 42    fn open_window(
 43        &self,
 44        id: usize,
 45        options: WindowOptions,
 46        executor: Rc<executor::Foreground>,
 47    ) -> Box<dyn Window>;
 48    fn key_window_id(&self) -> Option<usize>;
 49    fn prompt_for_paths(
 50        &self,
 51        options: PathPromptOptions,
 52        done_fn: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
 53    );
 54    fn prompt_for_new_path(
 55        &self,
 56        directory: &Path,
 57        done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
 58    );
 59    fn quit(&self);
 60    fn write_to_clipboard(&self, item: ClipboardItem);
 61    fn read_from_clipboard(&self) -> Option<ClipboardItem>;
 62    fn set_menus(&self, menus: Vec<Menu>);
 63}
 64
 65pub trait Dispatcher: Send + Sync {
 66    fn is_main_thread(&self) -> bool;
 67    fn run_on_main_thread(&self, task: Runnable);
 68}
 69
 70pub trait Window: WindowContext {
 71    fn as_any_mut(&mut self) -> &mut dyn Any;
 72    fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
 73    fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn WindowContext)>);
 74    fn on_close(&mut self, callback: Box<dyn FnOnce()>);
 75    fn prompt(
 76        &self,
 77        level: PromptLevel,
 78        msg: &str,
 79        answers: &[&str],
 80        done_fn: Box<dyn FnOnce(usize)>,
 81    );
 82}
 83
 84pub trait WindowContext {
 85    fn size(&self) -> Vector2F;
 86    fn scale_factor(&self) -> f32;
 87    fn present_scene(&mut self, scene: Scene);
 88}
 89
 90pub struct WindowOptions<'a> {
 91    pub bounds: RectF,
 92    pub title: Option<&'a str>,
 93}
 94
 95pub struct PathPromptOptions {
 96    pub files: bool,
 97    pub directories: bool,
 98    pub multiple: bool,
 99}
100
101pub enum PromptLevel {
102    Info,
103    Warning,
104    Critical,
105}
106
107pub trait FontSystem: Send + Sync {
108    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
109    fn select_font(
110        &self,
111        font_ids: &[FontId],
112        properties: &FontProperties,
113    ) -> anyhow::Result<FontId>;
114    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
115    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
116    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
117    fn rasterize_glyph(
118        &self,
119        font_id: FontId,
120        font_size: f32,
121        glyph_id: GlyphId,
122        subpixel_shift: Vector2F,
123        scale_factor: f32,
124    ) -> Option<(RectI, Vec<u8>)>;
125    fn layout_str(
126        &self,
127        text: &str,
128        font_size: f32,
129        runs: &[(usize, FontId, ColorU)],
130    ) -> LineLayout;
131}