2021-02-20 17:02:34 +00:00
|
|
|
mod event;
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
pub mod mac;
|
2021-03-31 23:59:03 +00:00
|
|
|
pub mod test;
|
2021-02-20 17:02:34 +00:00
|
|
|
pub mod current {
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
pub use super::mac::*;
|
|
|
|
}
|
|
|
|
|
2021-03-19 03:33:16 +00:00
|
|
|
use crate::{
|
|
|
|
executor,
|
2021-03-25 09:58:33 +00:00
|
|
|
fonts::{FontId, GlyphId, Metrics as FontMetrics, Properties as FontProperties},
|
2021-03-24 15:51:28 +00:00
|
|
|
geometry::{
|
|
|
|
rect::{RectF, RectI},
|
2021-11-23 18:13:08 +00:00
|
|
|
vector::Vector2F,
|
2021-03-24 15:51:28 +00:00
|
|
|
},
|
2022-05-19 17:04:01 +00:00
|
|
|
keymap,
|
2021-09-06 22:36:09 +00:00
|
|
|
text_layout::{LineLayout, RunStyle},
|
2022-04-07 23:20:49 +00:00
|
|
|
Action, ClipboardItem, Menu, Scene,
|
2021-03-19 03:33:16 +00:00
|
|
|
};
|
2022-04-04 16:59:57 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2021-02-20 23:05:36 +00:00
|
|
|
use async_task::Runnable;
|
2022-07-06 22:36:42 +00:00
|
|
|
pub use event::*;
|
2022-01-20 08:51:29 +00:00
|
|
|
use postage::oneshot;
|
2022-04-22 16:54:09 +00:00
|
|
|
use serde::Deserialize;
|
2021-05-05 02:04:11 +00:00
|
|
|
use std::{
|
|
|
|
any::Any,
|
2022-05-02 13:17:43 +00:00
|
|
|
fmt::{self, Display},
|
2022-07-20 13:07:09 +00:00
|
|
|
ops::Range,
|
2021-05-05 02:04:11 +00:00
|
|
|
path::{Path, PathBuf},
|
|
|
|
rc::Rc,
|
2022-04-04 16:59:57 +00:00
|
|
|
str::FromStr,
|
2021-05-05 02:04:11 +00:00
|
|
|
sync::Arc,
|
|
|
|
};
|
2021-09-02 17:15:05 +00:00
|
|
|
use time::UtcOffset;
|
2021-02-20 17:02:34 +00:00
|
|
|
|
2021-06-07 23:42:49 +00:00
|
|
|
pub trait Platform: Send + Sync {
|
|
|
|
fn dispatcher(&self) -> Arc<dyn Dispatcher>;
|
|
|
|
fn fonts(&self) -> Arc<dyn FontSystem>;
|
|
|
|
|
|
|
|
fn activate(&self, ignoring_other_apps: bool);
|
2022-09-08 17:07:11 +00:00
|
|
|
fn hide(&self);
|
|
|
|
fn hide_other_apps(&self);
|
|
|
|
fn unhide_other_apps(&self);
|
|
|
|
fn quit(&self);
|
|
|
|
|
2021-06-07 23:42:49 +00:00
|
|
|
fn open_window(
|
|
|
|
&self,
|
|
|
|
id: usize,
|
|
|
|
options: WindowOptions,
|
|
|
|
executor: Rc<executor::Foreground>,
|
|
|
|
) -> Box<dyn Window>;
|
|
|
|
fn key_window_id(&self) -> Option<usize>;
|
2022-09-08 17:07:11 +00:00
|
|
|
|
2022-09-12 10:03:03 +00:00
|
|
|
fn add_status_item(&self) -> Box<dyn Window>;
|
2021-06-09 00:44:45 +00:00
|
|
|
|
2021-06-07 23:42:49 +00:00
|
|
|
fn write_to_clipboard(&self, item: ClipboardItem);
|
|
|
|
fn read_from_clipboard(&self) -> Option<ClipboardItem>;
|
2021-06-08 01:15:11 +00:00
|
|
|
fn open_url(&self, url: &str);
|
2021-06-09 00:44:45 +00:00
|
|
|
|
2021-09-06 08:40:19 +00:00
|
|
|
fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>;
|
|
|
|
fn read_credentials(&self, url: &str) -> Result<Option<(String, Vec<u8>)>>;
|
2021-09-15 02:36:03 +00:00
|
|
|
fn delete_credentials(&self, url: &str) -> Result<()>;
|
2021-08-27 17:01:49 +00:00
|
|
|
|
|
|
|
fn set_cursor_style(&self, style: CursorStyle);
|
2021-09-02 17:15:05 +00:00
|
|
|
|
|
|
|
fn local_timezone(&self) -> UtcOffset;
|
2021-10-25 09:02:35 +00:00
|
|
|
|
2022-04-19 23:15:00 +00:00
|
|
|
fn path_for_auxiliary_executable(&self, name: &str) -> Result<PathBuf>;
|
2022-04-05 03:53:40 +00:00
|
|
|
fn app_path(&self) -> Result<PathBuf>;
|
2022-04-04 16:59:57 +00:00
|
|
|
fn app_version(&self) -> Result<AppVersion>;
|
2021-06-07 23:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait ForegroundPlatform {
|
2021-04-09 19:03:26 +00:00
|
|
|
fn on_become_active(&self, callback: Box<dyn FnMut()>);
|
|
|
|
fn on_resign_active(&self, callback: Box<dyn FnMut()>);
|
2021-11-01 18:56:49 +00:00
|
|
|
fn on_quit(&self, callback: Box<dyn FnMut()>);
|
2021-04-09 19:03:26 +00:00
|
|
|
fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
|
2022-04-15 23:33:56 +00:00
|
|
|
fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>);
|
2022-08-10 21:39:24 +00:00
|
|
|
fn run(&self, on_finish_launching: Box<dyn FnOnce()>);
|
2021-06-07 23:32:03 +00:00
|
|
|
|
2022-04-07 23:20:49 +00:00
|
|
|
fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn Action)>);
|
2022-05-19 23:24:02 +00:00
|
|
|
fn on_validate_menu_command(&self, callback: Box<dyn FnMut(&dyn Action) -> bool>);
|
2022-05-20 17:04:43 +00:00
|
|
|
fn on_will_open_menu(&self, callback: Box<dyn FnMut()>);
|
2022-05-19 17:04:01 +00:00
|
|
|
fn set_menus(&self, menus: Vec<Menu>, matcher: &keymap::Matcher);
|
2021-06-07 23:32:03 +00:00
|
|
|
fn prompt_for_paths(
|
|
|
|
&self,
|
|
|
|
options: PathPromptOptions,
|
2022-01-20 08:51:29 +00:00
|
|
|
) -> oneshot::Receiver<Option<Vec<PathBuf>>>;
|
|
|
|
fn prompt_for_new_path(&self, directory: &Path) -> oneshot::Receiver<Option<PathBuf>>;
|
2021-06-07 23:02:24 +00:00
|
|
|
}
|
2021-04-09 19:03:26 +00:00
|
|
|
|
2021-02-20 23:05:36 +00:00
|
|
|
pub trait Dispatcher: Send + Sync {
|
|
|
|
fn is_main_thread(&self) -> bool;
|
|
|
|
fn run_on_main_thread(&self, task: Runnable);
|
|
|
|
}
|
|
|
|
|
2022-07-20 13:07:09 +00:00
|
|
|
pub trait InputHandler {
|
2022-07-20 23:44:26 +00:00
|
|
|
fn selected_text_range(&self) -> Option<Range<usize>>;
|
2022-07-21 19:22:12 +00:00
|
|
|
fn marked_text_range(&self) -> Option<Range<usize>>;
|
2022-07-21 15:35:40 +00:00
|
|
|
fn text_for_range(&self, range_utf16: Range<usize>) -> Option<String>;
|
2022-07-20 23:44:26 +00:00
|
|
|
fn replace_text_in_range(&mut self, replacement_range: Option<Range<usize>>, text: &str);
|
|
|
|
fn replace_and_mark_text_in_range(
|
2022-07-20 13:07:09 +00:00
|
|
|
&mut self,
|
2022-07-21 15:35:40 +00:00
|
|
|
range_utf16: Option<Range<usize>>,
|
2022-07-20 23:44:26 +00:00
|
|
|
new_text: &str,
|
2022-07-20 13:07:09 +00:00
|
|
|
new_selected_range: Option<Range<usize>>,
|
|
|
|
);
|
2022-07-20 23:44:26 +00:00
|
|
|
fn unmark_text(&mut self);
|
2022-07-21 15:35:40 +00:00
|
|
|
fn rect_for_range(&self, range_utf16: Range<usize>) -> Option<RectF>;
|
2022-07-20 13:07:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 13:24:28 +00:00
|
|
|
pub trait Window {
|
2021-05-12 13:19:46 +00:00
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any;
|
2022-06-17 15:09:16 +00:00
|
|
|
fn on_event(&mut self, callback: Box<dyn FnMut(Event) -> bool>);
|
2022-04-22 21:18:50 +00:00
|
|
|
fn on_active_status_change(&mut self, callback: Box<dyn FnMut(bool)>);
|
2021-08-27 22:03:37 +00:00
|
|
|
fn on_resize(&mut self, callback: Box<dyn FnMut()>);
|
2022-08-09 19:34:57 +00:00
|
|
|
fn on_fullscreen(&mut self, callback: Box<dyn FnMut(bool)>);
|
2022-06-23 09:43:19 +00:00
|
|
|
fn on_should_close(&mut self, callback: Box<dyn FnMut() -> bool>);
|
2021-05-05 17:34:49 +00:00
|
|
|
fn on_close(&mut self, callback: Box<dyn FnOnce()>);
|
2022-07-20 13:07:09 +00:00
|
|
|
fn set_input_handler(&mut self, input_handler: Box<dyn InputHandler>);
|
2022-01-20 08:51:29 +00:00
|
|
|
fn prompt(&self, level: PromptLevel, msg: &str, answers: &[&str]) -> oneshot::Receiver<usize>;
|
2022-01-21 03:45:30 +00:00
|
|
|
fn activate(&self);
|
2022-05-27 17:45:55 +00:00
|
|
|
fn set_title(&mut self, title: &str);
|
2022-06-23 08:59:50 +00:00
|
|
|
fn set_edited(&mut self, edited: bool);
|
2022-07-19 12:51:22 +00:00
|
|
|
fn show_character_palette(&self);
|
2022-08-03 13:13:30 +00:00
|
|
|
fn minimize(&self);
|
|
|
|
fn zoom(&self);
|
2022-08-15 09:53:37 +00:00
|
|
|
fn toggle_full_screen(&self);
|
2021-03-21 04:15:04 +00:00
|
|
|
|
2022-09-14 13:43:51 +00:00
|
|
|
fn bounds(&self) -> RectF;
|
|
|
|
fn content_size(&self) -> Vector2F;
|
2021-03-19 03:33:16 +00:00
|
|
|
fn scale_factor(&self) -> f32;
|
2021-08-06 15:08:29 +00:00
|
|
|
fn titlebar_height(&self) -> f32;
|
2021-03-21 04:15:04 +00:00
|
|
|
fn present_scene(&mut self, scene: Scene);
|
2022-09-14 09:47:43 +00:00
|
|
|
fn appearance(&self) -> Appearance;
|
|
|
|
fn on_appearance_changed(&mut self, callback: Box<dyn FnMut()>);
|
2021-03-19 03:33:16 +00:00
|
|
|
}
|
2021-02-20 23:05:36 +00:00
|
|
|
|
2021-11-23 18:13:08 +00:00
|
|
|
#[derive(Debug)]
|
2021-02-20 23:05:36 +00:00
|
|
|
pub struct WindowOptions<'a> {
|
2021-11-23 18:13:08 +00:00
|
|
|
pub bounds: WindowBounds,
|
2022-08-22 16:17:14 +00:00
|
|
|
pub titlebar: Option<TitlebarOptions<'a>>,
|
2022-09-14 13:43:51 +00:00
|
|
|
pub center: bool,
|
2022-09-15 09:44:51 +00:00
|
|
|
pub kind: WindowKind,
|
|
|
|
pub is_movable: bool,
|
2022-08-22 16:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TitlebarOptions<'a> {
|
2021-02-20 23:05:36 +00:00
|
|
|
pub title: Option<&'a str>,
|
2022-08-22 16:17:14 +00:00
|
|
|
pub appears_transparent: bool,
|
2021-09-07 16:37:45 +00:00
|
|
|
pub traffic_light_position: Option<Vector2F>,
|
2021-02-20 23:05:36 +00:00
|
|
|
}
|
2021-03-24 15:51:28 +00:00
|
|
|
|
2022-09-14 09:47:43 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum Appearance {
|
|
|
|
Light,
|
|
|
|
VibrantLight,
|
|
|
|
Dark,
|
|
|
|
VibrantDark,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Appearance {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Light
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 15:09:07 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2022-09-15 09:44:51 +00:00
|
|
|
pub enum WindowKind {
|
2022-09-14 15:09:07 +00:00
|
|
|
Normal,
|
|
|
|
PopUp,
|
|
|
|
}
|
|
|
|
|
2021-11-23 18:13:08 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum WindowBounds {
|
|
|
|
Maximized,
|
|
|
|
Fixed(RectF),
|
|
|
|
}
|
|
|
|
|
2021-04-09 05:25:54 +00:00
|
|
|
pub struct PathPromptOptions {
|
|
|
|
pub files: bool,
|
|
|
|
pub directories: bool,
|
|
|
|
pub multiple: bool,
|
|
|
|
}
|
|
|
|
|
2021-05-12 09:54:48 +00:00
|
|
|
pub enum PromptLevel {
|
|
|
|
Info,
|
|
|
|
Warning,
|
|
|
|
Critical,
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:54:09 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Deserialize)]
|
2021-08-27 17:01:49 +00:00
|
|
|
pub enum CursorStyle {
|
|
|
|
Arrow,
|
|
|
|
ResizeLeftRight,
|
|
|
|
PointingHand,
|
2022-04-22 16:54:09 +00:00
|
|
|
IBeam,
|
2021-08-27 17:01:49 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 09:47:43 +00:00
|
|
|
impl Default for CursorStyle {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Arrow
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 16:59:57 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub struct AppVersion {
|
|
|
|
major: usize,
|
|
|
|
minor: usize,
|
|
|
|
patch: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for AppVersion {
|
|
|
|
type Err = anyhow::Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self> {
|
|
|
|
let mut components = s.trim().split('.');
|
|
|
|
let major = components
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| anyhow!("missing major version number"))?
|
|
|
|
.parse()?;
|
|
|
|
let minor = components
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| anyhow!("missing minor version number"))?
|
|
|
|
.parse()?;
|
|
|
|
let patch = components
|
|
|
|
.next()
|
|
|
|
.ok_or_else(|| anyhow!("missing patch version number"))?
|
|
|
|
.parse()?;
|
|
|
|
Ok(Self {
|
|
|
|
major,
|
|
|
|
minor,
|
|
|
|
patch,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-02 13:17:43 +00:00
|
|
|
impl Display for AppVersion {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 15:31:10 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum RasterizationOptions {
|
|
|
|
Alpha,
|
|
|
|
Bgra,
|
|
|
|
}
|
|
|
|
|
2021-03-24 15:51:28 +00:00
|
|
|
pub trait FontSystem: Send + Sync {
|
2021-09-06 12:08:42 +00:00
|
|
|
fn add_fonts(&self, fonts: &[Arc<Vec<u8>>]) -> anyhow::Result<()>;
|
2021-03-24 15:51:28 +00:00
|
|
|
fn load_family(&self, name: &str) -> anyhow::Result<Vec<FontId>>;
|
|
|
|
fn select_font(
|
|
|
|
&self,
|
|
|
|
font_ids: &[FontId],
|
|
|
|
properties: &FontProperties,
|
|
|
|
) -> anyhow::Result<FontId>;
|
|
|
|
fn font_metrics(&self, font_id: FontId) -> FontMetrics;
|
|
|
|
fn typographic_bounds(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<RectF>;
|
2021-11-24 09:28:32 +00:00
|
|
|
fn advance(&self, font_id: FontId, glyph_id: GlyphId) -> anyhow::Result<Vector2F>;
|
2021-03-24 15:51:28 +00:00
|
|
|
fn glyph_for_char(&self, font_id: FontId, ch: char) -> Option<GlyphId>;
|
|
|
|
fn rasterize_glyph(
|
|
|
|
&self,
|
|
|
|
font_id: FontId,
|
|
|
|
font_size: f32,
|
|
|
|
glyph_id: GlyphId,
|
2021-03-24 17:35:48 +00:00
|
|
|
subpixel_shift: Vector2F,
|
2021-03-24 15:51:28 +00:00
|
|
|
scale_factor: f32,
|
2022-04-13 15:31:10 +00:00
|
|
|
options: RasterizationOptions,
|
2021-03-24 15:51:28 +00:00
|
|
|
) -> Option<(RectI, Vec<u8>)>;
|
2021-09-06 22:36:09 +00:00
|
|
|
fn layout_line(&self, text: &str, font_size: f32, runs: &[(usize, RunStyle)]) -> LineLayout;
|
2021-07-17 00:14:43 +00:00
|
|
|
fn wrap_line(&self, text: &str, font_id: FontId, font_size: f32, width: f32) -> Vec<usize>;
|
2021-03-24 15:51:28 +00:00
|
|
|
}
|
2021-08-20 17:01:24 +00:00
|
|
|
|
|
|
|
impl<'a> Default for WindowOptions<'a> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2021-11-23 18:13:08 +00:00
|
|
|
bounds: WindowBounds::Maximized,
|
2022-08-22 16:17:14 +00:00
|
|
|
titlebar: Some(TitlebarOptions {
|
|
|
|
title: Default::default(),
|
|
|
|
appears_transparent: Default::default(),
|
|
|
|
traffic_light_position: Default::default(),
|
|
|
|
}),
|
2022-09-14 13:43:51 +00:00
|
|
|
center: false,
|
2022-09-15 09:44:51 +00:00
|
|
|
kind: WindowKind::Normal,
|
|
|
|
is_movable: true,
|
2021-08-20 17:01:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|