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