use uzor::app_context::StateRegistry;
use uzor::input::{InputCoordinator, LayerId};
use uzor::layout::docking::LeafId;
use uzor::render::RenderContext;
use uzor::tokens::Tokens;
use uzor::{Rect, WidgetId};
use crate::types::command::{AppCommand, ChromeModel};
use crate::types::ids::{Seconds, WindowId};
use crate::types::intent::Intent;
use crate::types::overlay_model::OverlayModel;
use crate::types::snapshot::VisualSnapshot;
use crate::types::spec::Spec;
pub trait App: 'static {
type Spec: Spec;
fn init(&mut self, cx: &mut InitCx<'_, Self::Spec>) {
let _ = cx;
}
fn chrome_model(&self, win: WindowId) -> ChromeModel {
let _ = win;
ChromeModel::default()
}
fn overlay_model(&self, win: WindowId, id: <Self::Spec as Spec>::Overlay) -> OverlayModel;
fn panel(&mut self, cx: &mut PanelCx<'_, Self::Spec>);
fn overlay_body(&mut self, cx: &mut OverlayCx<'_, Self::Spec>);
fn intent(&mut self, intent: Intent<Self::Spec>, cx: &mut IntentCx<'_, Self::Spec>);
fn shutdown(&mut self) {}
}
pub struct InitCx<'a, S: Spec> {
pub(crate) out: &'a mut Vec<AppCommand<S>>,
pub(crate) handle: &'a super::Handle<S>,
}
impl<'a, S: Spec> InitCx<'a, S> {
pub fn command(&mut self, cmd: AppCommand<S>) {
self.out.push(cmd);
}
pub fn handle(&self) -> super::Handle<S> {
self.handle.clone()
}
}
pub struct IntentCx<'a, S: Spec> {
pub view: &'a VisualSnapshot<S>,
pub(crate) out: &'a mut Vec<AppCommand<S>>,
}
impl<'a, S: Spec> IntentCx<'a, S> {
pub fn command(&mut self, cmd: AppCommand<S>) {
self.out.push(cmd);
}
}
pub enum HookOp<S: Spec> {
RequestFrame,
ImeArea(Rect),
Command(AppCommand<S>),
}
pub struct HookOps<S: Spec> {
pub(crate) ops: Vec<HookOp<S>>,
}
impl<S: Spec> Default for HookOps<S> {
fn default() -> Self {
Self { ops: Vec::new() }
}
}
impl<S: Spec> HookOps<S> {
pub fn request_frame(&mut self) {
self.ops.push(HookOp::RequestFrame);
}
pub fn ime_area(&mut self, rect: Rect) {
self.ops.push(HookOp::ImeArea(rect));
}
pub fn command(&mut self, cmd: AppCommand<S>) {
self.ops.push(HookOp::Command(cmd));
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct FrameTime {
pub now: Seconds,
pub dt: f64,
}
pub struct VisualView<S: Spec> {
pub(crate) hovered: Option<WidgetId>,
pub(crate) pressed: Option<WidgetId>,
pub(crate) focused: Option<WidgetId>,
pub(crate) clicked: Vec<(WidgetId, u8)>,
pub(crate) scroll: (f64, f64),
pub(crate) captured: bool,
pub(crate) open_overlays: Vec<S::Overlay>,
}
impl<S: Spec> Default for VisualView<S> {
fn default() -> Self {
Self {
hovered: None,
pressed: None,
focused: None,
clicked: Vec::new(),
scroll: (0.0, 0.0),
captured: false,
open_overlays: Vec::new(),
}
}
}
impl<S: Spec> VisualView<S> {
pub fn hovered(&self) -> Option<&WidgetId> {
self.hovered.as_ref()
}
pub fn is_hovered(&self, id: &WidgetId) -> bool {
self.hovered.as_ref() == Some(id)
}
pub fn pressed(&self) -> Option<&WidgetId> {
self.pressed.as_ref()
}
pub fn is_pressed(&self, id: &WidgetId) -> bool {
self.pressed.as_ref() == Some(id)
}
pub fn focused(&self) -> Option<&WidgetId> {
self.focused.as_ref()
}
pub fn is_focused(&self, id: &WidgetId) -> bool {
self.focused.as_ref() == Some(id)
}
pub fn clicked(&self, id: &WidgetId) -> bool {
self.click_count(id) > 0
}
pub fn click_count(&self, id: &WidgetId) -> u8 {
self.clicked
.iter()
.find_map(|(w, n)| (w == id).then_some(*n))
.unwrap_or(0)
}
pub fn clicks(&self) -> &[(WidgetId, u8)] {
&self.clicked
}
pub fn scroll(&self) -> (f64, f64) {
self.scroll
}
pub fn captured(&self) -> bool {
self.captured
}
pub fn overlay_open(&self, id: S::Overlay) -> bool {
self.open_overlays.contains(&id)
}
pub fn open_overlays(&self) -> &[S::Overlay] {
&self.open_overlays
}
}
pub struct Widgets<'a> {
pub coord: &'a mut InputCoordinator,
pub states: &'a mut StateRegistry,
pub layer: LayerId,
}
pub struct PanelCx<'a, S: Spec> {
pub window: WindowId,
pub leaf: LeafId,
pub panel: &'a S::Panel,
pub index: usize,
pub rect: Rect,
pub dpr: f64,
pub tokens: &'a Tokens,
pub render: &'a mut dyn RenderContext,
pub widgets: Widgets<'a>,
pub view: &'a VisualView<S>,
pub time: FrameTime,
pub out: &'a mut HookOps<S>,
}
pub struct OverlayCx<'a, S: Spec> {
pub window: WindowId,
pub id: S::Overlay,
pub body: Rect,
pub rect: Rect,
pub dpr: f64,
pub tokens: &'a Tokens,
pub render: &'a mut dyn RenderContext,
pub widgets: Widgets<'a>,
pub view: &'a VisualView<S>,
pub time: FrameTime,
pub out: &'a mut HookOps<S>,
}