1use crate::api::PlatformError;
5use crate::platform::{EventLoopProxy, Platform};
6use crate::Property;
7use alloc::boxed::Box;
8use alloc::rc::Rc;
9
10crate::thread_local! {
11 pub(crate) static GLOBAL_CONTEXT : once_cell::unsync::OnceCell<SlintContext>
12 = const { once_cell::unsync::OnceCell::new() }
13}
14
15pub(crate) struct SlintContextInner {
16 platform: Box<dyn Platform>,
17 pub(crate) window_count: core::cell::RefCell<isize>,
18 pub(crate) translations_dirty: core::pin::Pin<Box<Property<usize>>>,
22 pub(crate) translations_bundle_languages:
23 core::cell::RefCell<Option<alloc::vec::Vec<&'static str>>>,
24 pub(crate) window_shown_hook:
25 core::cell::RefCell<Option<Box<dyn FnMut(&Rc<dyn crate::platform::WindowAdapter>)>>>,
26 #[cfg(all(unix, not(target_os = "macos")))]
27 xdg_app_id: core::cell::RefCell<Option<crate::SharedString>>,
28}
29
30#[derive(Clone)]
34pub struct SlintContext(pub(crate) Rc<SlintContextInner>);
35
36impl SlintContext {
37 pub fn new(platform: Box<dyn Platform + 'static>) -> Self {
39 Self(Rc::new(SlintContextInner {
40 platform,
41 window_count: 0.into(),
42 translations_dirty: Box::pin(Property::new_named(0, "SlintContext::translations")),
43 translations_bundle_languages: Default::default(),
44 window_shown_hook: Default::default(),
45 #[cfg(all(unix, not(target_os = "macos")))]
46 xdg_app_id: Default::default(),
47 }))
48 }
49
50 pub fn platform(&self) -> &dyn Platform {
52 &*self.0.platform
53 }
54
55 pub fn event_loop_proxy(&self) -> Option<Box<dyn EventLoopProxy>> {
58 self.0.platform.new_event_loop_proxy()
59 }
60
61 #[cfg(target_has_atomic = "ptr")]
62 pub fn spawn_local<F: core::future::Future + 'static>(
64 &self,
65 fut: F,
66 ) -> Result<crate::future::JoinHandle<F::Output>, crate::api::EventLoopError> {
67 crate::future::spawn_local_with_ctx(self, fut)
68 }
69
70 pub fn run_event_loop(&self) -> Result<(), PlatformError> {
71 self.0.platform.run_event_loop()
72 }
73
74 pub fn set_xdg_app_id(&self, _app_id: crate::SharedString) {
75 #[cfg(all(unix, not(target_os = "macos")))]
76 {
77 self.0.xdg_app_id.replace(Some(_app_id));
78 }
79 }
80
81 #[cfg(all(unix, not(target_os = "macos")))]
82 pub fn xdg_app_id(&self) -> Option<crate::SharedString> {
83 self.0.xdg_app_id.borrow().clone()
84 }
85
86 #[cfg(not(all(unix, not(target_os = "macos"))))]
87 pub fn xdg_app_id(&self) -> Option<crate::SharedString> {
88 None
89 }
90}
91
92pub fn with_global_context<R>(
96 factory: impl FnOnce() -> Result<Box<dyn Platform + 'static>, PlatformError>,
97 f: impl FnOnce(&SlintContext) -> R,
98) -> Result<R, PlatformError> {
99 GLOBAL_CONTEXT.with(|p| match p.get() {
100 Some(ctx) => Ok(f(ctx)),
101 None => {
102 crate::platform::set_platform(factory()?).map_err(PlatformError::SetPlatformError)?;
103 Ok(f(p.get().unwrap()))
104 }
105 })
106}
107
108pub fn set_window_shown_hook(
111 hook: Option<Box<dyn FnMut(&Rc<dyn crate::platform::WindowAdapter>)>>,
112) -> Result<Option<Box<dyn FnMut(&Rc<dyn crate::platform::WindowAdapter>)>>, PlatformError> {
113 GLOBAL_CONTEXT.with(|p| match p.get() {
114 Some(ctx) => Ok(ctx.0.window_shown_hook.replace(hook)),
115 None => Err(PlatformError::NoPlatform),
116 })
117}