use crate::core::{EntityId, EntityMap, Handle, Size};
use crate::view::any_element::AnyElement;
use crate::view::div::div;
use crate::view::element::IntoElement;
use crate::view::render::{Component, ViewContext};
use crate::view::state::StateStore;
use crate::view::theme::Theme;
use crate::view::widget_store::WidgetStore;
use std::cell::RefCell;
use std::rc::Rc;
pub struct Ui<'a> {
pub(crate) theme: &'a Theme,
pub(crate) viewport: Size,
pub(crate) entities: &'a mut EntityMap,
pub(crate) state: &'a mut StateStore,
pub(crate) widgets: &'a mut WidgetStore,
pub(crate) root_id: EntityId,
pub(crate) theme_request: &'a Rc<RefCell<Option<Theme>>>,
}
impl<'a> Ui<'a> {
pub(crate) fn new(
theme: &'a Theme,
viewport: Size,
entities: &'a mut EntityMap,
state: &'a mut StateStore,
widgets: &'a mut WidgetStore,
root_id: EntityId,
theme_request: &'a Rc<RefCell<Option<Theme>>>,
) -> Self {
Self { theme, viewport, entities, state, widgets, root_id, theme_request }
}
pub fn theme(&self) -> &Theme {
self.theme
}
pub fn set_theme(&mut self, theme: Theme) {
*self.theme_request.borrow_mut() = Some(theme);
}
pub fn viewport(&self) -> Size {
self.viewport
}
pub fn entities(&mut self) -> &mut EntityMap {
self.entities
}
pub fn child<V: Component>(&mut self, handle: Handle<V>) -> AnyElement {
let (theme, viewport) = (self.theme, self.viewport);
self.entities
.update(handle, |view, entity| view.view(&mut ViewContext::new(theme, viewport, entity)).into_element())
.unwrap_or_else(|| div().into_element())
}
}