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    executor,
 12    fonts::{FontId, GlyphId, Metrics as FontMetrics, Properties as FontProperties},
 13    geometry::{
 14        rect::{RectF, RectI},
 15        vector::Vector2F,
 16    },
 17    text_layout::{LineLayout, RunStyle},
 18    Action, ClipboardItem, Menu, Scene,
 19};
 20use anyhow::Result;
 21use async_task::Runnable;
 22pub use event::{Event, NavigationDirection};
 23use postage::oneshot;
 24use std::{
 25    any::Any,
 26    path::{Path, PathBuf},
 27    rc::Rc,
 28    sync::Arc,
 29};
 30use time::UtcOffset;
 31
 32pub trait Platform: Send + Sync {
 33    fn dispatcher(&self) -> Arc<dyn Dispatcher>;
 34    fn fonts(&self) -> Arc<dyn FontSystem>;
 35
 36    fn activate(&self, ignoring_other_apps: bool);
 37    fn open_window(
 38        &self,
 39        id: usize,
 40        options: WindowOptions,
 41        executor: Rc<executor::Foreground>,
 42    ) -> Box<dyn Window>;
 43    fn key_window_id(&self) -> Option<usize>;
 44    fn quit(&self);
 45
 46    fn write_to_clipboard(&self, item: ClipboardItem);
 47    fn read_from_clipboard(&self) -> Option<ClipboardItem>;
 48    fn open_url(&self, url: &str);
 49
 50    fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>;
 51    fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>>;
 52    fn delete_credentials(&self, url: &str) -> Result<()>;
 53
 54    fn set_cursor_style(&self, style: CursorStyle);
 55
 56    fn local_timezone(&self) -> UtcOffset;
 57
 58    fn path_for_resource(&self, name: Option<&str>, extension: Option<&str>) -> Result<PathBuf>;
 59}
 60
 61pub(crate) trait ForegroundPlatform {
 62    fn on_become_active(&self, callback: Box<dyn FnMut()>);
 63    fn on_resign_active(&self, callback: Box<dyn FnMut()>);
 64    fn on_quit(&self, callback: Box<dyn FnMut()>);
 65    fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
 66    fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
 67    fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>);
 68    fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
 69
 70    fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>);
 71    fn set_menus(&self, menus: Vec<Menu>);
 72    fn prompt_for_paths(
 73        &self,
 74        options: PathPromptOptions,
 75    ) -> oneshot::Receiver<Option<Vec<PathBuf>>>;
 76    fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>>;
 77}
 78
 79pub trait Dispatcher: Send + Sync {
 80    fn is_main_thread(&self) -> bool;
 81    fn run_on_main_thread(&self, task: Runnable);
 82}
 83
 84pub trait Window: WindowContext {
 85    fn as_any_mut(&mut self) -> &mut dyn Any;
 86    fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
 87    fn on_resize(&mut self, callback: Box<dyn FnMut()>);
 88    fn on_close(&mut self, callback: Box<dyn FnOnce()>);
 89    fn prompt(&self, level: PromptLevel, msg: &str, answers: &[&str]) -> oneshot::Receiver<usize>;
 90    fn activate(&self);
 91}
 92
 93pub trait WindowContext {
 94    fn size(&self) -> Vector2F;
 95    fn scale_factor(&self) -> f32;
 96    fn titlebar_height(&self) -> f32;
 97    fn present_scene(&mut self, scene: Scene);
 98}
 99
100#[derive(Debug)]
101pub struct WindowOptions<'a> {
102    pub bounds: WindowBounds,
103    pub title: Option<&'a str>,
104    pub titlebar_appears_transparent: bool,
105    pub traffic_light_position: Option<Vector2F>,
106}
107
108#[derive(Debug)]
109pub enum WindowBounds {
110    Maximized,
111    Fixed(RectF),
112}
113
114pub struct PathPromptOptions {
115    pub files: bool,
116    pub directories: bool,
117    pub multiple: bool,
118}
119
120pub enum PromptLevel {
121    Info,
122    Warning,
123    Critical,
124}
125
126#[derive(Copy, Clone, Debug)]
127pub enum CursorStyle {
128    Arrow,
129    ResizeLeftRight,
130    PointingHand,
131}
132
133#[derive(Copy, Clone, Debug)]
134pub enum RasterizationOptions {
135    Alpha,
136    Bgra,
137}
138
139pub trait FontSystem: Send + Sync {
140    fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
141    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
142    fn select_font(
143        &self,
144        font_ids: &[FontId],
145        properties: &FontProperties,
146    ) -> anyhow::Result<FontId>;
147    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
148    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
149    fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F>;
150    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
151    fn rasterize_glyph(
152        &self,
153        font_id: FontId,
154        font_size: f32,
155        glyph_id: GlyphId,
156        subpixel_shift: Vector2F,
157        scale_factor: f32,
158        options: RasterizationOptions,
159    ) -> Option<(RectI, Vec<u8>)>;
160    fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
161    fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
162}
163
164impl<'a> Default for WindowOptions<'a> {
165    fn default() -> Self {
166        Self {
167            bounds: WindowBounds::Maximized,
168            title: Default::default(),
169            titlebar_appears_transparent: Default::default(),
170            traffic_light_position: Default::default(),
171        }
172    }
173}