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    fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>;
 58}
 59
 60pub(crate) trait ForegroundPlatform {
 61    fn on_become_active(&self, callback: Box<dyn FnMut()>);
 62    fn on_resign_active(&self, callback: Box<dyn FnMut()>);
 63    fn on_quit(&self, callback: Box<dyn FnMut()>);
 64    fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
 65    fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>);
 66    fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
 67
 68    fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>);
 69    fn set_menus(&self, menus: Vec<Menu>);
 70    fn prompt_for_paths(
 71        &self,
 72        options: PathPromptOptions,
 73    ) -> oneshot::Receiver<Option<Vec<PathBuf>>>;
 74    fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>>;
 75}
 76
 77pub trait Dispatcher: Send + Sync {
 78    fn is_main_thread(&self) -> bool;
 79    fn run_on_main_thread(&self, task: Runnable);
 80}
 81
 82pub trait Window: WindowContext {
 83    fn as_any_mut(&mut self) -> &mut dyn Any;
 84    fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
 85    fn on_resize(&mut self, callback: Box<dyn FnMut()>);
 86    fn on_close(&mut self, callback: Box<dyn FnOnce()>);
 87    fn prompt(&self, level: PromptLevel, msg: &str, answers: &[&str]) -> oneshot::Receiver<usize>;
 88    fn activate(&self);
 89}
 90
 91pub trait WindowContext {
 92    fn size(&self) -> Vector2F;
 93    fn scale_factor(&self) -> f32;
 94    fn titlebar_height(&self) -> f32;
 95    fn present_scene(&mut self, scene: Scene);
 96}
 97
 98#[derive(Debug)]
 99pub struct WindowOptions<'a> {
100    pub bounds: WindowBounds,
101    pub title: Option<&'a str>,
102    pub titlebar_appears_transparent: bool,
103    pub traffic_light_position: Option<Vector2F>,
104}
105
106#[derive(Debug)]
107pub enum WindowBounds {
108    Maximized,
109    Fixed(RectF),
110}
111
112pub struct PathPromptOptions {
113    pub files: bool,
114    pub directories: bool,
115    pub multiple: bool,
116}
117
118pub enum PromptLevel {
119    Info,
120    Warning,
121    Critical,
122}
123
124#[derive(Copy, Clone, Debug)]
125pub enum CursorStyle {
126    Arrow,
127    ResizeLeftRight,
128    PointingHand,
129}
130
131#[derive(Copy, Clone, Debug)]
132pub enum RasterizationOptions {
133    Alpha,
134    Bgra,
135}
136
137pub trait FontSystem: Send + Sync {
138    fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
139    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
140    fn select_font(
141        &self,
142        font_ids: &[FontId],
143        properties: &FontProperties,
144    ) -> anyhow::Result<FontId>;
145    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
146    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
147    fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F>;
148    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
149    fn rasterize_glyph(
150        &self,
151        font_id: FontId,
152        font_size: f32,
153        glyph_id: GlyphId,
154        subpixel_shift: Vector2F,
155        scale_factor: f32,
156        options: RasterizationOptions,
157    ) -> Option<(RectI, Vec<u8>)>;
158    fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
159    fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
160}
161
162impl<'a> Default for WindowOptions<'a> {
163    fn default() -> Self {
164        Self {
165            bounds: WindowBounds::Maximized,
166            title: Default::default(),
167            titlebar_appears_transparent: Default::default(),
168            traffic_light_position: Default::default(),
169        }
170    }
171}