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