use crate::config::WindowConfig;
use crate::{App, EventIterator};
use downcast_rs::{impl_downcast, Downcast};
use futures::prelude::*;
use futures::Future;
use notan_graphics::DeviceBackend;
#[cfg(feature = "audio")]
use notan_audio::AudioBackend;
#[cfg(feature = "audio")]
use std::cell::RefCell;
#[cfg(feature = "audio")]
use std::rc::Rc;
pub type InitializeFn<S, R> = dyn FnOnce(App, S, R) -> Result<(), String>;
pub type LoadFileFn = Box<dyn Fn(String) -> Box<dyn Future<Output = Result<Vec<u8>, String>>>>;
pub trait Backend: Downcast {
fn window(&mut self) -> &mut dyn WindowBackend;
fn events_iter(&mut self) -> EventIterator;
fn exit(&mut self);
fn system_timestamp(&self) -> u64;
fn open_link(&self, url: &str, new_tab: bool);
}
impl_downcast!(Backend);
pub enum FrameState {
End,
Skip,
}
pub trait BackendSystem: Backend {
fn initialize<S, R>(&mut self, window: WindowConfig) -> Result<Box<InitializeFn<S, R>>, String>
where
Self: Backend,
S: 'static,
R: FnMut(&mut App, &mut S) -> Result<FrameState, String> + 'static;
fn get_file_loader(&self) -> LoadFileFn {
Box::new(|path| Box::new(platter2::load_file(path).map_err(|e| e.to_string())))
}
fn get_graphics_backend(&self) -> Box<dyn DeviceBackend>;
#[cfg(feature = "audio")]
fn get_audio_backend(&self) -> Rc<RefCell<dyn AudioBackend>>;
}
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq)]
pub enum CursorIcon {
Default,
None,
ContextMenu,
Help,
PointingHand,
Progress,
Wait,
Cell,
Crosshair,
Text,
VerticalText,
Alias,
Copy,
Move,
NoDrop,
NotAllowed,
Grab,
Grabbing,
AllScroll,
ResizeHorizontal,
ResizeNeSw,
ResizeNwSe,
ResizeVertical,
ZoomIn,
ZoomOut,
ResizeEast,
ResizeSouthEast,
ResizeSouth,
ResizeSouthWest,
ResizeWest,
ResizeNorthWest,
ResizeNorth,
ResizeNorthEast,
ResizeColumn,
ResizeRow,
}
pub trait WindowBackend {
fn set_size(&mut self, width: i32, height: i32);
fn size(&self) -> (i32, i32);
fn set_fullscreen(&mut self, enabled: bool);
fn is_fullscreen(&self) -> bool;
fn width(&self) -> i32 {
self.size().0
}
fn height(&self) -> i32 {
self.size().1
}
fn dpi(&self) -> f64;
fn set_lazy_loop(&mut self, lazy: bool);
fn lazy_loop(&self) -> bool;
fn request_frame(&mut self);
fn set_cursor(&mut self, cursor: CursorIcon);
fn cursor(&self) -> CursorIcon;
fn set_capture_cursor(&mut self, capture: bool);
fn capture_cursor(&self) -> bool;
fn set_visible(&mut self, visible: bool);
fn visible(&self) -> bool;
}