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