1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use alloc::boxed::Box;
use alloc::rc::Rc;
use alloc::string::String;
use crate::graphics::{Image, Size};
use crate::window::Window;
#[cfg(feature = "std")]
use once_cell::sync::OnceCell;
#[cfg(all(not(feature = "std"), feature = "unsafe_single_core"))]
use crate::unsafe_single_core::OnceCell;
#[derive(Copy, Clone)]
pub enum EventLoopQuitBehavior {
QuitOnLastWindowClosed,
QuitOnlyExplicitly,
}
pub trait Backend: Send + Sync {
fn create_window(&'static self) -> Rc<Window>;
fn run_event_loop(&'static self, behavior: EventLoopQuitBehavior);
fn quit_event_loop(&'static self);
#[cfg(feature = "std")]
fn register_font_from_memory(
&'static self,
data: &'static [u8],
) -> Result<(), Box<dyn std::error::Error>>;
#[cfg(feature = "std")]
fn register_font_from_path(
&'static self,
path: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>>;
fn set_clipboard_text(&'static self, text: String);
fn clipboard_text(&'static self) -> Option<String>;
fn post_event(&'static self, event: Box<dyn FnOnce() + Send>);
fn image_size(&'static self, image: &Image) -> Size;
fn duration_since_start(&'static self) -> core::time::Duration {
#[cfg(feature = "std")]
{
let the_beginning = *INITIAL_INSTANT.get_or_init(instant::Instant::now);
instant::Instant::now() - the_beginning
}
#[cfg(not(feature = "std"))]
core::time::Duration::ZERO
}
}
#[cfg(feature = "std")]
static INITIAL_INSTANT: once_cell::sync::OnceCell<instant::Instant> =
once_cell::sync::OnceCell::new();
#[cfg(feature = "std")]
impl std::convert::From<crate::animations::Instant> for instant::Instant {
fn from(our_instant: crate::animations::Instant) -> Self {
let the_beginning = *INITIAL_INSTANT.get_or_init(instant::Instant::now);
the_beginning + core::time::Duration::from_millis(our_instant.0)
}
}
static PRIVATE_BACKEND_INSTANCE: OnceCell<Box<dyn Backend + 'static>> = OnceCell::new();
pub fn instance() -> Option<&'static dyn Backend> {
use core::ops::Deref;
PRIVATE_BACKEND_INSTANCE.get().map(|backend_box| backend_box.deref())
}
pub fn instance_or_init(
factory_fn: impl FnOnce() -> Box<dyn Backend + 'static>,
) -> &'static dyn Backend {
use core::ops::Deref;
PRIVATE_BACKEND_INSTANCE.get_or_init(factory_fn).deref()
}