use crate::configure::LayerConf;
use slint::platform::{EventLoopProxy, Platform, WindowAdapter};
use std::{
cell::RefCell,
rc::Rc,
sync::{Arc, Mutex},
};
use tracing::{Level, info, span, warn};
thread_local! {
pub(crate) static ADAPTERS: RefCell<Vec<Rc<SpellSkiaWinAdapter>>> = const { RefCell::new(Vec::new()) };
}
#[cfg(not(docsrs))]
#[cfg(feature = "i-slint-renderer-skia")]
use crate::skia_non_docs::SpellSkiaWinAdapterReal;
#[cfg(not(docsrs))]
#[cfg(feature = "i-slint-renderer-skia")]
pub type SpellSkiaWinAdapter = SpellSkiaWinAdapterReal;
#[cfg(docsrs)]
use crate::dummy_skia_docs::SpellSkiaWinAdapterDummy;
#[cfg(docsrs)]
pub type SpellSkiaWinAdapter = SpellSkiaWinAdapterDummy;
pub struct SpellLayerShell {
pub span: span::Span,
}
impl Default for SpellLayerShell {
fn default() -> Self {
SpellLayerShell {
span: span!(Level::INFO, "slint-log",),
}
}
}
impl Platform for SpellLayerShell {
fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter>, slint::PlatformError> {
let adapter = ADAPTERS.with(|v| v.borrow().last().unwrap().clone());
Ok(adapter)
}
fn debug_log(&self, arguments: core::fmt::Arguments) {
self.span.in_scope(|| {
if let Some(val) = arguments.as_str() {
info!(val);
} else {
info!("{}", arguments.to_string());
}
})
}
fn new_event_loop_proxy(&self) -> Option<Box<dyn EventLoopProxy>> {
Some(Box::new(SlintEventProxy(ADAPTERS.with(|v| {
v.borrow().last().unwrap().slint_event_proxy.clone()
}))))
}
}
pub struct SpellMultiWinHandler {
pub(crate) windows: Vec<(String, LayerConf)>,
pub(crate) adapter: Vec<Rc<SpellSkiaWinAdapter>>,
pub(crate) value_given: u32,
}
impl SpellMultiWinHandler {
pub(crate) fn new_lock(lock_outputs: Vec<(String, (u32, u32))>) -> Rc<RefCell<Self>> {
let new_locks: Vec<(String, LayerConf)> = lock_outputs
.iter()
.map(|(output_name, conf)| (output_name.clone(), LayerConf::Lock(conf.0, conf.1)))
.collect();
Rc::new(RefCell::new(SpellMultiWinHandler {
windows: new_locks,
adapter: Vec::new(),
value_given: 0,
}))
}
fn request_new_lock(&mut self) -> Rc<dyn WindowAdapter> {
self.value_given += 1;
let index = self.value_given - 1;
self.adapter[index as usize].clone()
}
}
pub struct SpellLockShell {
pub window_manager: Rc<RefCell<SpellMultiWinHandler>>,
pub span: span::Span,
}
impl SpellLockShell {
pub fn new(window_manager: Rc<RefCell<SpellMultiWinHandler>>) -> Self {
SpellLockShell {
window_manager,
span: span!(Level::INFO, "slint-lock-log",),
}
}
}
impl Platform for SpellLockShell {
fn create_window_adapter(&self) -> Result<Rc<dyn WindowAdapter>, slint::PlatformError> {
let value = self.window_manager.borrow_mut().request_new_lock();
Ok(value)
}
fn debug_log(&self, arguments: core::fmt::Arguments) {
self.span.in_scope(|| {
if let Some(val) = arguments.as_str() {
info!(val);
} else {
info!("{}", arguments.to_string());
}
})
}
}
#[allow(clippy::type_complexity)]
struct SlintEventProxy(Arc<Mutex<Vec<Box<dyn FnOnce() + Send>>>>);
impl EventLoopProxy for SlintEventProxy {
fn quit_event_loop(&self) -> Result<(), i_slint_core::api::EventLoopError> {
Ok(())
}
fn invoke_from_event_loop(
&self,
event: Box<dyn FnOnce() + Send>,
) -> Result<(), i_slint_core::api::EventLoopError> {
if let Ok(mut list_of_event) = self.0.try_lock() {
(*list_of_event).push(event);
} else {
warn!("Slint proxy event could not be processed");
}
Ok(())
}
}