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::Color,
 12    executor,
 13    fonts::{FontId, GlyphId, Metrics as FontMetrics, Properties as FontProperties},
 14    geometry::{
 15        rect::{RectF, RectI},
 16        vector::{vec2f, Vector2F},
 17    },
 18    text_layout::LineLayout,
 19    AnyAction, 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(&dyn AnyAction)>);
 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 titlebar_height(&self) -> f32;
 96    fn present_scene(&mut self, scene: Scene);
 97}
 98
 99pub struct WindowOptions<'a> {
100    pub bounds: RectF,
101    pub title: Option<&'a str>,
102    pub titlebar_appears_transparent: bool,
103}
104
105pub struct PathPromptOptions {
106    pub files: bool,
107    pub directories: bool,
108    pub multiple: bool,
109}
110
111pub enum PromptLevel {
112    Info,
113    Warning,
114    Critical,
115}
116
117pub trait FontSystem: Send + Sync {
118    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
119    fn select_font(
120        &self,
121        font_ids: &[FontId],
122        properties: &FontProperties,
123    ) -> anyhow::Result<FontId>;
124    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
125    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
126    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
127    fn rasterize_glyph(
128        &self,
129        font_id: FontId,
130        font_size: f32,
131        glyph_id: GlyphId,
132        subpixel_shift: Vector2F,
133        scale_factor: f32,
134    ) -> Option<(RectI, Vec<u8>)>;
135    fn layout_line(
136        &self,
137        text: &str,
138        font_size: f32,
139        runs: &[(usize, FontId, Color)],
140    ) -> LineLayout;
141    fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
142}
143
144impl<'a> Default for WindowOptions<'a> {
145    fn default() -> Self {
146        Self {
147            bounds: RectF::new(Default::default(), vec2f(1024.0, 768.0)),
148            title: Default::default(),
149            titlebar_appears_transparent: Default::default(),
150        }
151    }
152}