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