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) focus_visible: bool,
pub(crate) clicked: Vec<(WidgetId, u8)>,
pub(crate) scroll: (f64, f64),
pub(crate) scroll_precise: bool,
pub(crate) pointer: Option<(f64, f64)>,
pub(crate) button_down: bool,
pub(crate) press_origin: Option<(f64, f64)>,
pub(crate) context: Option<(f64, f64)>,
pub(crate) captured: bool,
pub(crate) open_overlays: Vec<S::Overlay>,
pub(crate) selections: Vec<(WidgetId, uzor::input::text::TextSelection)>,
pub(crate) hover_path: Vec<WidgetId>,
pub(crate) prev_hover_path: Vec<WidgetId>,
pub(crate) click_paths: Vec<Vec<WidgetId>>,
pub(crate) focus_path: Vec<WidgetId>,
pub(crate) prev_focus_path: Vec<WidgetId>,
}
impl<S: Spec> Default for VisualView<S> {
fn default() -> Self {
Self {
hovered: None,
pressed: None,
focused: None,
focus_visible: false,
clicked: Vec::new(),
scroll: (0.0, 0.0),
scroll_precise: false,
pointer: None,
button_down: false,
press_origin: None,
context: None,
captured: false,
open_overlays: Vec::new(),
selections: Vec::new(),
hover_path: Vec::new(),
prev_hover_path: Vec::new(),
click_paths: Vec::new(),
focus_path: Vec::new(),
prev_focus_path: 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 hover_path(&self) -> &[WidgetId] {
&self.hover_path
}
pub fn is_hover_within(&self, id: &WidgetId) -> bool {
self.hover_path.contains(id)
}
pub fn hover_changes(&self) -> (Vec<WidgetId>, Vec<WidgetId>) {
uzor::input::core::events::enter_leave(&self.prev_hover_path, &self.hover_path)
}
pub fn focus_path(&self) -> &[WidgetId] {
&self.focus_path
}
pub fn is_focus_within(&self, id: &WidgetId) -> bool {
self.focus_path.contains(id)
}
pub fn dispatch_focus(&self, handler: impl FnMut(uzor::input::core::events::FocusEvent, &mut uzor::input::core::events::EventCx<'_>)) {
uzor::input::core::events::dispatch_focus_change(&self.prev_focus_path, &self.focus_path, handler);
}
pub fn dispatch_clicks(&self, mut handler: impl FnMut(&mut uzor::input::core::events::EventCx<'_>, u8)) {
for (i, (w, n)) in self.clicked.iter().enumerate() {
let one = [w.clone()];
let path = self.click_paths.get(i).map_or(&one[..], Vec::as_slice);
uzor::input::core::events::dispatch(path, true, |cx| handler(cx, *n));
}
}
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 focus_visible(&self) -> bool {
self.focus_visible
}
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 scroll_precise(&self) -> bool {
self.scroll_precise
}
#[deprecated(note = "ignores z-order; use pointer_over(id), or raw_pointer() when the raw position is really meant")]
pub fn pointer(&self) -> Option<(f64, f64)> {
self.pointer
}
pub fn raw_pointer(&self) -> Option<(f64, f64)> {
self.pointer
}
pub fn pointer_over(&self, id: &WidgetId) -> Option<(f64, f64)> {
let owns = self.pressed.as_ref() == Some(id) || self.hovered.as_ref() == Some(id) || self.hover_path.contains(id);
if owns {
self.pointer
} else {
None
}
}
pub fn scroll_over(&self, id: &WidgetId) -> (f64, f64) {
if self.pointer_over(id).is_some() {
self.scroll
} else {
(0.0, 0.0)
}
}
pub fn context_click(&self) -> Option<(f64, f64)> {
self.context
}
pub fn button_down(&self) -> bool {
self.button_down
}
pub fn press_origin(&self) -> Option<(f64, f64)> {
if self.button_down && self.pressed.is_some() {
self.press_origin
} else {
None
}
}
pub fn pressed_at(&self, id: &WidgetId) -> Option<(f64, f64)> {
if self.is_pressed(id) {
self.press_origin()
} else {
None
}
}
pub fn captured(&self) -> bool {
self.captured
}
pub fn selection(&self, id: &WidgetId) -> Option<uzor::input::text::TextSelection> {
self.selections.iter().find_map(|(w, s)| (w == id).then_some(*s))
}
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 selectables: &'a mut crate::Selectables,
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>,
}