mod.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::Line,
 18    ClipboardItem, Menu, Scene,
 19};
 20use async_task::Runnable;
 21pub use event::Event;
 22use std::{
 23    any::Any,
 24    ops::Range,
 25    path::{Path, PathBuf},
 26    rc::Rc,
 27    sync::Arc,
 28};
 29
 30pub trait Platform {
 31    fn on_menu_command(&self, callback: Box<dyn FnMut(&str, Option<&dyn Any>)>);
 32    fn on_become_active(&self, callback: Box<dyn FnMut()>);
 33    fn on_resign_active(&self, callback: Box<dyn FnMut()>);
 34    fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
 35    fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
 36    fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
 37
 38    fn dispatcher(&self) -> Arc<dyn Dispatcher>;
 39    fn fonts(&self) -> Arc<dyn FontSystem>;
 40
 41    fn activate(&self, ignoring_other_apps: bool);
 42    fn open_window(
 43        &self,
 44        id: usize,
 45        options: WindowOptions,
 46        executor: Rc<executor::Foreground>,
 47    ) -> Box<dyn Window>;
 48    fn key_window_id(&self) -> Option<usize>;
 49    fn prompt_for_paths(
 50        &self,
 51        options: PathPromptOptions,
 52        done_fn: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
 53    );
 54    fn prompt_for_new_path(
 55        &self,
 56        directory: &Path,
 57        done_fn: Box<dyn FnOnce(Option<std::path::PathBuf>)>,
 58    );
 59    fn quit(&self);
 60    fn write_to_clipboard(&self, item: ClipboardItem);
 61    fn read_from_clipboard(&self) -> Option<ClipboardItem>;
 62    fn set_menus(&self, menus: Vec<Menu>);
 63}
 64
 65pub trait Dispatcher: Send + Sync {
 66    fn is_main_thread(&self) -> bool;
 67    fn run_on_main_thread(&self, task: Runnable);
 68}
 69
 70pub trait Window: WindowContext {
 71    fn on_event(&mut self, callback: Box<dyn FnMut(Event)>);
 72    fn on_resize(&mut self, callback: Box<dyn FnMut(&mut dyn WindowContext)>);
 73    fn on_close(&mut self, callback: Box<dyn FnOnce()>);
 74}
 75
 76pub trait WindowContext {
 77    fn size(&self) -> Vector2F;
 78    fn scale_factor(&self) -> f32;
 79    fn present_scene(&mut self, scene: Scene);
 80}
 81
 82pub struct WindowOptions<'a> {
 83    pub bounds: RectF,
 84    pub title: Option<&'a str>,
 85}
 86
 87pub struct PathPromptOptions {
 88    pub files: bool,
 89    pub directories: bool,
 90    pub multiple: bool,
 91}
 92
 93pub trait FontSystem: Send + Sync {
 94    fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
 95    fn select_font(
 96        &self,
 97        font_ids: &[FontId],
 98        properties: &FontProperties,
 99    ) -> anyhow::Result<FontId>;
100    fn font_metrics(&self, font_id: FontId) -> FontMetrics;
101    fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
102    fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
103    fn rasterize_glyph(
104        &self,
105        font_id: FontId,
106        font_size: f32,
107        glyph_id: GlyphId,
108        subpixel_shift: Vector2F,
109        scale_factor: f32,
110    ) -> Option<(RectI, Vec<u8>)>;
111    fn layout_str(&self, text: &str, font_size: f32, runs: &[(Range<usize>, FontId)]) -> Line;
112}