use crate::core::app_context::AppContext;
use crate::core::element::ElementArena;
use crate::core::id::ElementId;
use crate::event::EventRegistry;
use crate::platform::clipboard::Clipboard;
use crate::platform::WindowHandle;
use crate::style::StyleRefinement;
use crate::theme::m3::roles::{ComponentRole, ResolvedComponentStyle};
use crate::theme::M3Theme;
use std::rc::Weak;
pub struct MountContext<'a> {
pub arena: &'a mut ElementArena,
pub parent_id: Option<ElementId>,
pub event_registry: Option<&'a mut EventRegistry>,
pub theme: &'a M3Theme,
pub window_handle: Option<&'a WindowHandle>,
pub clipboard: Clipboard,
pub app: Weak<AppContext>,
#[cfg(feature = "i18n")]
pub i18n: Option<&'a crate::i18n::I18n>,
}
impl<'a> MountContext<'a> {
pub fn new(
arena: &'a mut ElementArena,
parent_id: Option<ElementId>,
registry: Option<&'a mut EventRegistry>,
theme: &'a M3Theme,
window_handle: Option<&'a WindowHandle>,
app: Weak<AppContext>,
) -> Self {
Self {
arena,
parent_id,
event_registry: registry,
theme,
window_handle,
clipboard: Clipboard::new(),
app,
#[cfg(feature = "i18n")]
i18n: None,
}
}
pub fn child_with_events<'b>(&'b mut self, parent_id: ElementId) -> MountContext<'b>
where
'a: 'b,
{
MountContext {
arena: self.arena,
parent_id: Some(parent_id),
event_registry: self.event_registry.as_deref_mut(),
theme: self.theme,
window_handle: self.window_handle,
clipboard: Clipboard::new(),
app: self.app.clone(),
#[cfg(feature = "i18n")]
i18n: self.i18n,
}
}
pub fn parent_id(&self) -> Option<ElementId> {
self.parent_id
}
pub fn preallocate(&mut self, id: ElementId, mask: u64) {
self.arena
.component_tables
.borrow_mut()
.preallocate(id, mask);
}
pub fn register_theme_component(
&mut self,
id: ElementId,
resolved: &ResolvedComponentStyle,
role: &ComponentRole,
style: &StyleRefinement,
) {
if let Some(el) = self.arena.get_mut(id) {
crate::theme::apply::apply_style_to_element(
el,
resolved,
style,
self.theme.is_dark,
self.theme.scheme.design_interaction,
);
}
if let Some(lc) = self.arena.component_tables.borrow_mut().lc.get_mut(&id) {
lc.component_role = Some(role.clone());
lc.style_refinement = Some(style.clone());
}
crate::ecs::register_theme_element(id);
}
}
pub struct LayoutCtx<'a> {
pub(crate) _phantom: std::marker::PhantomData<&'a ()>,
}
pub struct PaintCtx<'a> {
pub viewport: crate::style::Size,
pub scale_factor: f64,
pub window_handle: Option<&'a WindowHandle>,
_phantom: std::marker::PhantomData<&'a ()>,
}
impl<'a> PaintCtx<'a> {
pub fn new(
viewport: crate::style::Size,
scale_factor: f64,
window_handle: Option<&'a WindowHandle>,
) -> Self {
Self {
viewport,
scale_factor,
window_handle,
_phantom: std::marker::PhantomData,
}
}
}
pub struct EventCtx<'a> {
pub(crate) propagation_stopped: &'a mut bool,
pub(crate) default_prevented: &'a mut bool,
pub window_handle: Option<&'a WindowHandle>,
pub clipboard: Clipboard,
}
impl<'a> EventCtx<'a> {
pub fn new(
propagation_stopped: &'a mut bool,
default_prevented: &'a mut bool,
window_handle: Option<&'a WindowHandle>,
) -> Self {
Self {
propagation_stopped,
default_prevented,
window_handle,
clipboard: Clipboard::new(),
}
}
pub fn stop_propagation(&mut self) {
*self.propagation_stopped = true;
}
pub fn prevent_default(&mut self) {
*self.default_prevented = true;
}
pub fn is_propagation_stopped(&self) -> bool {
*self.propagation_stopped
}
pub fn is_default_prevented(&self) -> bool {
*self.default_prevented
}
}