2021-11-23 18:13:08 +00:00
|
|
|
use super::{CursorStyle, WindowBounds};
|
|
|
|
use crate::{
|
|
|
|
geometry::vector::{vec2f, Vector2F},
|
|
|
|
AnyAction, ClipboardItem,
|
|
|
|
};
|
2021-10-25 09:02:35 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2021-06-07 23:35:27 +00:00
|
|
|
use parking_lot::Mutex;
|
2021-05-05 18:04:39 +00:00
|
|
|
use std::{
|
|
|
|
any::Any,
|
|
|
|
cell::RefCell,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
rc::Rc,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2021-09-02 17:15:05 +00:00
|
|
|
use time::UtcOffset;
|
2021-05-05 18:04:39 +00:00
|
|
|
|
2021-07-29 14:53:10 +00:00
|
|
|
pub struct Platform {
|
2021-03-31 23:59:03 +00:00
|
|
|
dispatcher: Arc<dyn super::Dispatcher>,
|
2021-04-02 21:35:44 +00:00
|
|
|
fonts: Arc<dyn super::FontSystem>,
|
2021-06-07 23:35:27 +00:00
|
|
|
current_clipboard_item: Mutex<Option<ClipboardItem>>,
|
2021-08-27 17:01:49 +00:00
|
|
|
cursor: Mutex<CursorStyle>,
|
2021-03-31 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 23:32:03 +00:00
|
|
|
#[derive(Default)]
|
2021-07-29 14:53:10 +00:00
|
|
|
pub struct ForegroundPlatform {
|
2021-06-07 23:32:03 +00:00
|
|
|
last_prompt_for_new_path_args: RefCell<Option<(PathBuf, Box<dyn FnOnce(Option<PathBuf>)>)>>,
|
|
|
|
}
|
2021-06-07 23:21:34 +00:00
|
|
|
|
2021-03-31 23:59:03 +00:00
|
|
|
struct Dispatcher;
|
|
|
|
|
|
|
|
pub struct Window {
|
|
|
|
size: Vector2F,
|
|
|
|
scale_factor: f32,
|
|
|
|
current_scene: Option<crate::Scene>,
|
|
|
|
event_handlers: Vec<Box<dyn FnMut(super::Event)>>,
|
2021-08-27 22:03:37 +00:00
|
|
|
resize_handlers: Vec<Box<dyn FnMut()>>,
|
2021-05-05 17:34:49 +00:00
|
|
|
close_handlers: Vec<Box<dyn FnOnce()>>,
|
2021-05-12 13:19:46 +00:00
|
|
|
pub(crate) last_prompt: RefCell<Option<Box<dyn FnOnce(usize)>>>,
|
2021-03-31 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 23:42:49 +00:00
|
|
|
impl ForegroundPlatform {
|
2021-06-07 23:32:03 +00:00
|
|
|
pub(crate) fn simulate_new_path_selection(
|
|
|
|
&self,
|
|
|
|
result: impl FnOnce(PathBuf) -> Option<PathBuf>,
|
|
|
|
) {
|
|
|
|
let (dir_path, callback) = self
|
|
|
|
.last_prompt_for_new_path_args
|
|
|
|
.take()
|
|
|
|
.expect("prompt_for_new_path was not called");
|
|
|
|
callback(result(dir_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn did_prompt_for_new_path(&self) -> bool {
|
|
|
|
self.last_prompt_for_new_path_args.borrow().is_some()
|
|
|
|
}
|
|
|
|
}
|
2021-06-07 23:02:24 +00:00
|
|
|
|
2021-06-07 23:42:49 +00:00
|
|
|
impl super::ForegroundPlatform for ForegroundPlatform {
|
2021-06-07 23:02:24 +00:00
|
|
|
fn on_become_active(&self, _: Box<dyn FnMut()>) {}
|
|
|
|
|
|
|
|
fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
|
2021-11-01 18:56:49 +00:00
|
|
|
|
|
|
|
fn on_quit(&self, _: Box<dyn FnMut()>) {}
|
2021-06-07 23:02:24 +00:00
|
|
|
|
|
|
|
fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
|
|
|
|
|
|
|
|
fn on_open_files(&self, _: Box<dyn FnMut(Vec<std::path::PathBuf>)>) {}
|
|
|
|
|
|
|
|
fn run(&self, _on_finish_launching: Box<dyn FnOnce() -> ()>) {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2021-06-07 23:32:03 +00:00
|
|
|
|
2021-08-22 17:58:19 +00:00
|
|
|
fn on_menu_command(&self, _: Box<dyn FnMut(&dyn AnyAction)>) {}
|
2021-06-07 23:32:03 +00:00
|
|
|
|
|
|
|
fn set_menus(&self, _: Vec<crate::Menu>) {}
|
|
|
|
|
|
|
|
fn prompt_for_paths(
|
|
|
|
&self,
|
|
|
|
_: super::PathPromptOptions,
|
|
|
|
_: Box<dyn FnOnce(Option<Vec<std::path::PathBuf>>)>,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prompt_for_new_path(&self, path: &Path, f: Box<dyn FnOnce(Option<std::path::PathBuf>)>) {
|
|
|
|
*self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), f));
|
|
|
|
}
|
2021-06-07 23:02:24 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 19:03:26 +00:00
|
|
|
impl Platform {
|
2021-03-31 23:59:03 +00:00
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
dispatcher: Arc::new(Dispatcher),
|
2021-04-02 21:35:44 +00:00
|
|
|
fonts: Arc::new(super::current::FontSystem::new()),
|
2021-05-05 18:04:39 +00:00
|
|
|
current_clipboard_item: Default::default(),
|
2021-08-27 17:01:49 +00:00
|
|
|
cursor: Mutex::new(CursorStyle::Arrow),
|
2021-03-31 23:59:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 19:03:26 +00:00
|
|
|
impl super::Platform for Platform {
|
2021-03-31 23:59:03 +00:00
|
|
|
fn dispatcher(&self) -> Arc<dyn super::Dispatcher> {
|
|
|
|
self.dispatcher.clone()
|
|
|
|
}
|
|
|
|
|
2021-04-09 19:03:26 +00:00
|
|
|
fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
|
|
|
|
self.fonts.clone()
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:35:44 +00:00
|
|
|
fn activate(&self, _ignoring_other_apps: bool) {}
|
2021-03-31 23:59:03 +00:00
|
|
|
|
|
|
|
fn open_window(
|
|
|
|
&self,
|
2021-04-12 21:09:49 +00:00
|
|
|
_: usize,
|
2021-03-31 23:59:03 +00:00
|
|
|
options: super::WindowOptions,
|
2021-04-02 21:35:44 +00:00
|
|
|
_executor: Rc<super::executor::Foreground>,
|
2021-04-12 22:14:25 +00:00
|
|
|
) -> Box<dyn super::Window> {
|
2021-11-23 18:13:08 +00:00
|
|
|
Box::new(Window::new(match options.bounds {
|
|
|
|
WindowBounds::Maximized => vec2f(1024., 768.),
|
|
|
|
WindowBounds::Fixed(rect) => rect.size(),
|
|
|
|
}))
|
2021-03-31 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 21:09:49 +00:00
|
|
|
fn key_window_id(&self) -> Option<usize> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-04-08 22:56:21 +00:00
|
|
|
fn quit(&self) {}
|
2021-04-09 05:25:54 +00:00
|
|
|
|
2021-04-13 23:51:28 +00:00
|
|
|
fn write_to_clipboard(&self, item: ClipboardItem) {
|
2021-06-07 23:35:27 +00:00
|
|
|
*self.current_clipboard_item.lock() = Some(item);
|
2021-04-13 23:51:28 +00:00
|
|
|
}
|
2021-04-13 12:58:10 +00:00
|
|
|
|
2021-04-13 17:03:56 +00:00
|
|
|
fn read_from_clipboard(&self) -> Option<ClipboardItem> {
|
2021-06-07 23:35:27 +00:00
|
|
|
self.current_clipboard_item.lock().clone()
|
2021-04-13 12:58:10 +00:00
|
|
|
}
|
2021-06-08 01:15:11 +00:00
|
|
|
|
|
|
|
fn open_url(&self, _: &str) {}
|
2021-06-09 00:44:45 +00:00
|
|
|
|
2021-09-06 08:40:19 +00:00
|
|
|
fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-06-09 00:44:45 +00:00
|
|
|
|
2021-09-06 08:40:19 +00:00
|
|
|
fn read_credentials(&self, _: &str) -> Result<Option<(String, Vec<u8>)>> {
|
|
|
|
Ok(None)
|
2021-06-09 00:44:45 +00:00
|
|
|
}
|
2021-08-27 17:01:49 +00:00
|
|
|
|
2021-09-15 02:36:03 +00:00
|
|
|
fn delete_credentials(&self, _: &str) -> Result<()> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-08-27 17:01:49 +00:00
|
|
|
fn set_cursor_style(&self, style: CursorStyle) {
|
|
|
|
*self.cursor.lock() = style;
|
|
|
|
}
|
2021-09-02 17:15:05 +00:00
|
|
|
|
|
|
|
fn local_timezone(&self) -> UtcOffset {
|
|
|
|
UtcOffset::UTC
|
|
|
|
}
|
2021-10-25 09:02:35 +00:00
|
|
|
|
|
|
|
fn path_for_resource(&self, _name: Option<&str>, _extension: Option<&str>) -> Result<PathBuf> {
|
|
|
|
Err(anyhow!("app not running inside a bundle"))
|
|
|
|
}
|
2021-03-31 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Window {
|
|
|
|
fn new(size: Vector2F) -> Self {
|
|
|
|
Self {
|
|
|
|
size,
|
|
|
|
event_handlers: Vec::new(),
|
|
|
|
resize_handlers: Vec::new(),
|
2021-05-05 17:34:49 +00:00
|
|
|
close_handlers: Vec::new(),
|
2021-03-31 23:59:03 +00:00
|
|
|
scale_factor: 1.0,
|
|
|
|
current_scene: None,
|
2021-05-12 13:19:46 +00:00
|
|
|
last_prompt: RefCell::new(None),
|
2021-03-31 23:59:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::Dispatcher for Dispatcher {
|
|
|
|
fn is_main_thread(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_on_main_thread(&self, task: async_task::Runnable) {
|
|
|
|
task.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::WindowContext for Window {
|
|
|
|
fn size(&self) -> Vector2F {
|
|
|
|
self.size
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scale_factor(&self) -> f32 {
|
|
|
|
self.scale_factor
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:29 +00:00
|
|
|
fn titlebar_height(&self) -> f32 {
|
|
|
|
24.
|
|
|
|
}
|
|
|
|
|
2021-03-31 23:59:03 +00:00
|
|
|
fn present_scene(&mut self, scene: crate::Scene) {
|
|
|
|
self.current_scene = Some(scene);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::Window for Window {
|
2021-05-12 13:19:46 +00:00
|
|
|
fn as_any_mut(&mut self) -> &mut dyn Any {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-03-31 23:59:03 +00:00
|
|
|
fn on_event(&mut self, callback: Box<dyn FnMut(crate::Event)>) {
|
|
|
|
self.event_handlers.push(callback);
|
|
|
|
}
|
|
|
|
|
2021-08-27 22:03:37 +00:00
|
|
|
fn on_resize(&mut self, callback: Box<dyn FnMut()>) {
|
2021-03-31 23:59:03 +00:00
|
|
|
self.resize_handlers.push(callback);
|
|
|
|
}
|
2021-05-05 17:34:49 +00:00
|
|
|
|
|
|
|
fn on_close(&mut self, callback: Box<dyn FnOnce()>) {
|
|
|
|
self.close_handlers.push(callback);
|
|
|
|
}
|
2021-05-12 09:54:48 +00:00
|
|
|
|
2021-05-12 13:19:46 +00:00
|
|
|
fn prompt(&self, _: crate::PromptLevel, _: &str, _: &[&str], f: Box<dyn FnOnce(usize)>) {
|
|
|
|
self.last_prompt.replace(Some(f));
|
|
|
|
}
|
2021-03-31 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 14:53:10 +00:00
|
|
|
pub fn platform() -> Platform {
|
2021-04-09 19:03:26 +00:00
|
|
|
Platform::new()
|
2021-03-31 23:59:03 +00:00
|
|
|
}
|
2021-06-07 23:42:49 +00:00
|
|
|
|
2021-07-29 14:53:10 +00:00
|
|
|
pub fn foreground_platform() -> ForegroundPlatform {
|
2021-06-07 23:42:49 +00:00
|
|
|
ForegroundPlatform::default()
|
|
|
|
}
|