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