| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | use crate::api::PlatformError; |
| 5 | use crate::platform::{EventLoopProxy, Platform}; |
| 6 | use crate::Property; |
| 7 | use alloc::boxed::Box; |
| 8 | use alloc::rc::Rc; |
| 9 | |
| 10 | crate::thread_local! { |
| 11 | pub(crate) static GLOBAL_CONTEXT : once_cell::unsync::OnceCell<SlintContext> |
| 12 | = const { once_cell::unsync::OnceCell::new() } |
| 13 | } |
| 14 | |
| 15 | pub(crate) struct SlintContextInner { |
| 16 | platform: Box<dyn Platform>, |
| 17 | pub(crate) window_count: core::cell::RefCell<isize>, |
| 18 | /// This property is read by all translations, and marked dirty when the language changes, |
| 19 | /// so that every translated string gets re-translated. The property's value is the current selected |
| 20 | /// language when bundling translations. |
| 21 | 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 | /// This context is meant to hold the state and the backend. |
| 31 | /// Currently it is not possible to have several platform at the same time in one process, but in the future it might be. |
| 32 | /// See issue #4294 |
| 33 | #[derive (Clone)] |
| 34 | pub struct SlintContext(pub(crate) Rc<SlintContextInner>); |
| 35 | |
| 36 | impl SlintContext { |
| 37 | /// Create a new context with a given platform |
| 38 | 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 | /// Return a reference to the platform abstraction |
| 51 | pub fn platform(&self) -> &dyn Platform { |
| 52 | &*self.0.platform |
| 53 | } |
| 54 | |
| 55 | /// Return an event proxy |
| 56 | // FIXME: Make EvenLoopProxy cloneable, and maybe wrap in a struct |
| 57 | 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 | /// Context specific version of `slint::spawn_local` |
| 63 | 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 | |
| 92 | /// Internal function to access the context. |
| 93 | /// The factory function is called if the platform abstraction is not yet |
| 94 | /// initialized, and should be given by the platform_selector |
| 95 | pub 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: &OnceCell| match p.get() { |
| 100 | Some(ctx: &SlintContext) => Ok(f(ctx)), |
| 101 | None => { |
| 102 | crate::platform::set_platform(factory()?).map_err(op:PlatformError::SetPlatformError)?; |
| 103 | Ok(f(p.get().unwrap())) |
| 104 | } |
| 105 | }) |
| 106 | } |
| 107 | |
| 108 | /// Internal function to set a hook that's invoked whenever a slint::Window is shown. This |
| 109 | /// is used by the system testing module. Returns a previously set hook, if any. |
| 110 | pub 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: &OnceCell| match p.get() { |
| 114 | Some(ctx: &SlintContext) => Ok(ctx.0.window_shown_hook.replace(hook)), |
| 115 | None => Err(PlatformError::NoPlatform), |
| 116 | }) |
| 117 | } |
| 118 | |