mod compose;
mod furniture;
mod phases;
mod publish;
mod route;
use crate::types::vecmap::VecMap;
use uzor::render::InvalidateBits;
use crate::engine::anim::AnimationEngine;
use crate::engine::cadence::CadenceEngine;
use crate::engine::input::InputEngine;
use crate::engine::keymap::KeymapEngine;
use crate::engine::layout::LayoutEngine;
use crate::engine::overlays::OverlayEngine;
use crate::engine::windows::WindowEngine;
use crate::types::anim::{AnimKey, AnimPolicy};
use crate::types::bus::InputEnvelope;
use crate::types::command::{AppCommand, LayoutPolicy};
use crate::types::ids::{Revision, Seconds, WindowId};
use crate::types::intent::{Intent, OverlayIntent, WindowIntent};
use crate::types::ops::{
AnimEffect, AnimOp, CadenceEffect, CadenceOp, Capture, EngineTarget, FocusOp, InputEffect,
InputOp, LayoutEffect, LayoutOp, OverlayEffect, OverlayOp, WindowEffect, WindowOp,
};
use crate::types::spec::Spec;
use crate::types::window::{ClosePolicy, WindowCommand};
use uzor::render::TickRate;
pub(crate) struct Outbox<S: Spec> {
pub intents: Vec<Intent<S>>,
}
impl<S: Spec> Default for Outbox<S> {
fn default() -> Self {
Self {
intents: Vec::new(),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct EngineRevisions {
windows: Revision,
layout: Revision,
overlays: Revision,
input: Revision,
keymap: Revision,
cadence: Revision,
anim: Revision,
}
pub(crate) struct KernelConfig {
pub anim: AnimPolicy,
pub layout: LayoutPolicy,
pub tokens: std::sync::Arc<uzor::tokens::Tokens>,
pub dark: bool,
pub close: ClosePolicy,
pub tick: TickRate,
}
pub(crate) struct Kernel<S: Spec> {
windows: WindowEngine,
layout: LayoutEngine<S::Panel>,
overlays: OverlayEngine<S::Overlay>,
input: InputEngine,
keymap: KeymapEngine<S::Overlay, S::Action>,
cadence: CadenceEngine,
anim: AnimationEngine,
revision: Revision,
last_seen: EngineRevisions,
outbox: Outbox<S>,
default_tick: TickRate,
last_frame: VecMap<WindowId, Seconds>,
hover_paths: VecMap<WindowId, Vec<uzor::types::WidgetId>>,
focus_paths: VecMap<WindowId, Vec<uzor::types::WidgetId>>,
trace: Vec<&'static str>,
trace_max: usize,
touches: Vec<(u64, crate::types::window::Point)>,
gesture: crate::types::gesture::GestureRecognizer,
touch_mode: TouchMode,
touch_win: Option<WindowId>,
fling: Option<(WindowId, crate::types::window::Point, crate::types::gesture::Fling)>,
touch_wheel: bool,
touch_clock: Option<f64>,
coarse_pointer: bool,
gestures: VecMap<WindowId, Vec<crate::types::gesture::Gesture>>,
clock: PhaseClock,
#[cfg(feature = "inspector")]
inspector: uzor::ui::inspector::Inspector,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TouchMode {
Idle,
Press,
Scroll,
Drag,
Held,
Multi,
}
impl<S: Spec> Kernel<S> {
pub(crate) fn new(cfg: &KernelConfig) -> Self {
Self {
windows: WindowEngine::new(cfg.tokens.clone(), cfg.dark, cfg.close),
layout: LayoutEngine::with_policy(S::decode_panel, cfg.layout),
overlays: OverlayEngine::new(),
input: InputEngine::new(),
keymap: KeymapEngine::new(),
cadence: CadenceEngine::new(),
anim: AnimationEngine::new(cfg.anim),
revision: Revision::ZERO,
last_seen: EngineRevisions::default(),
outbox: Outbox::default(),
default_tick: cfg.tick,
last_frame: VecMap::new(),
hover_paths: VecMap::new(),
focus_paths: VecMap::new(),
trace: Vec::new(),
trace_max: 0,
touches: Vec::new(),
gesture: Default::default(),
touch_mode: TouchMode::Idle,
touch_win: None,
fling: None,
touch_wheel: false,
touch_clock: None,
coarse_pointer: cfg!(any(target_os = "ios", target_os = "android")),
gestures: VecMap::new(),
clock: PhaseClock::new(),
#[cfg(feature = "inspector")]
inspector: Default::default(),
}
}
pub(crate) fn step(
&mut self,
now: Seconds,
inputs: &mut Vec<InputEnvelope>,
cmds: &mut Vec<AppCommand<S>>,
) {
self.clock.begin(Phase::Drain);
self.drain(now, cmds);
self.clock.idle();
self.clock.begin(Phase::Lifecycle);
self.lifecycle(now, inputs);
self.clock.idle();
self.clock.begin(Phase::Route);
self.route(now, inputs);
self.clock.idle();
self.clock.begin(Phase::Engines);
self.engines(now);
self.clock.idle();
}
pub(crate) fn take_intents(&mut self) -> Vec<Intent<S>> {
std::mem::take(&mut self.outbox.intents)
}
pub(crate) fn exit_requested(&self) -> bool {
self.windows.view().exit_requested()
}
#[allow(dead_code)]
pub(crate) fn last_trace(&self) -> &[&'static str] {
&self.trace
}
#[allow(dead_code)]
pub(crate) fn trace_max(&self) -> usize {
self.trace_max
}
pub(crate) fn engine_revisions(&self) -> EngineRevisions {
EngineRevisions {
windows: self.windows.revision(),
layout: self.layout.revision(),
overlays: self.overlays.revision(),
input: self.input.revision(),
keymap: self.keymap.revision(),
cadence: self.cadence.revision(),
anim: self.anim.revision(),
}
}
fn conduct_window(&mut self, now: Seconds, fx: WindowEffect) {
match fx {
WindowEffect::Spawned(win) => {
let effects = self.layout.apply(LayoutOp::AdoptPanel { win });
self.conduct_layout(now, effects);
}
WindowEffect::Created(win) => {
let (caps, viewport, tick) = {
let view = self.windows.view();
let w = view.window(win);
let caps = w.map_or_else(Default::default, |r| r.caps());
let viewport = w.map_or_else(Default::default, |r| r.geometry().viewport);
let tick = w.and_then(|r| r.spec().tick);
(caps, viewport, tick)
};
let fx = self.cadence.apply(CadenceOp::Open {
win,
tick: tick.unwrap_or(self.default_tick),
});
self.conduct_cadence(now, fx);
let fx = self.layout.apply(LayoutOp::Open {
win,
caps,
viewport,
});
self.conduct_layout(now, fx);
let fx = self.input.apply(InputOp::Open(win));
self.conduct_input(now, fx);
let fx = self.overlays.apply(OverlayOp::Reclamp { win, viewport });
self.conduct_overlay(now, fx);
if let Some(key) = self.windows.view().window(win).map(|w| w.key().clone()) {
self.outbox
.intents
.push(Intent::Window(WindowIntent::Opened { win, key }));
}
}
WindowEffect::Closed(win) => {
let fx = self.layout.apply(LayoutOp::Close(win));
self.conduct_layout(now, fx);
let fx = self.overlays.apply(OverlayOp::CloseWindow(win));
self.conduct_overlay(now, fx);
let fx = self.input.apply(InputOp::Close(win));
self.conduct_input(now, fx);
let fx = self.cadence.apply(CadenceOp::Close(win));
self.conduct_cadence(now, fx);
let fx = self.anim.apply(AnimOp::DropWindow(win));
self.conduct_anim(now, fx);
self.last_frame.remove(&win);
self.outbox
.intents
.push(Intent::Window(WindowIntent::Closed { win }));
}
WindowEffect::CloseRequested(win) => {
self.outbox
.intents
.push(Intent::Window(WindowIntent::CloseRequested { win }));
}
WindowEffect::GeometryChanged(win) => {
let viewport = self
.windows
.view()
.window(win)
.map_or_else(Default::default, |r| r.geometry().viewport);
let fx = self.layout.apply(LayoutOp::Solve { win, viewport });
self.conduct_layout(now, fx);
let fx = self.overlays.apply(OverlayOp::Reclamp { win, viewport });
self.conduct_overlay(now, fx);
self.invalidate(win, None, InvalidateBits::ALL);
}
WindowEffect::FocusChanged { win, focused } => {
let fx = self.input.apply(InputOp::WindowFocus { win, focused, now });
self.conduct_input(now, fx);
self.outbox
.intents
.push(Intent::Window(WindowIntent::FocusChanged { win, focused }));
}
WindowEffect::ReducedMotionChanged { reduced } => {
self.outbox
.intents
.push(Intent::Window(WindowIntent::ReducedMotionChanged { reduced }));
}
WindowEffect::ThemeChanged => {
let wins: Vec<WindowId> = self.windows.view().ids().collect();
for win in wins {
self.invalidate(win, None, InvalidateBits::ALL);
}
}
}
}
fn conduct_layout(&mut self, now: Seconds, effects: crate::engine::layout::LayoutEffects) {
for fx in effects {
match fx {
LayoutEffect::Window { win, cmd } => {
let effects = self.windows.apply(WindowOp::Enqueue { win, cmd });
self.conduct_window_all(now, effects);
}
LayoutEffect::Chrome { win, action } => self.chrome_action(now, win, action),
LayoutEffect::Capture { win, hold } => {
let capture = hold.then_some(Capture::Engine(EngineTarget::Layout));
let effects = self.input.apply(InputOp::SetCapture { win, capture });
self.conduct_input(now, effects);
}
LayoutEffect::Intent(intent) => {
self.outbox.intents.push(Intent::Dock(intent));
}
LayoutEffect::Invalidate { win, bits } => self.invalidate(win, None, bits),
LayoutEffect::ExpandTarget { win, kind, side } => {
let effects = self.anim.apply(AnimOp::SetTarget {
key: AnimKey::Expand { win, kind },
target: if side.is_some() { 1.0 } else { 0.0 },
now,
});
self.conduct_anim(now, effects);
}
LayoutEffect::ExpandDone { win, kind } => {
let effects = self
.anim
.apply(AnimOp::Remove(AnimKey::Expand { win, kind }));
self.conduct_anim(now, effects);
}
LayoutEffect::SpawnWindow { spec, .. } => {
let effects = self.windows.apply(WindowOp::Create(spec));
self.conduct_window_all(now, effects);
}
}
}
}
fn conduct_overlay(
&mut self,
now: Seconds,
effects: crate::engine::overlays::OverlayEffects<S::Overlay>,
) {
for fx in effects {
match fx {
OverlayEffect::Opened {
win,
id,
slot,
scope,
auto_close_at,
..
} => {
if let Some(owner) = scope {
let effects = self.input.apply(InputOp::Focus {
win,
now,
op: FocusOp::PushScope {
owner,
members: Vec::new(),
},
});
self.conduct_input(now, effects);
}
if let Some(at) = auto_close_at {
let effects = self.cadence.apply(CadenceOp::Arm {
owner: crate::types::frame::TimerOwner::OverlayAutoClose { win, slot },
at,
});
self.conduct_cadence(now, effects);
}
let effects = self.anim.apply(AnimOp::SetTarget {
key: AnimKey::OverlayFade { win, slot },
target: 1.0,
now,
});
self.conduct_anim(now, effects);
self.invalidate(win, None, InvalidateBits::STRUCTURE);
self.outbox
.intents
.push(Intent::Overlay(OverlayIntent::Opened { win, id }));
}
OverlayEffect::Closed {
win,
id,
slot,
scope,
cause,
restore_focus,
auto_close,
} => {
if let Some(owner) = scope {
let effects = self.input.apply(InputOp::Focus {
win,
now,
op: FocusOp::PopScope {
owner,
restore: restore_focus,
},
});
self.conduct_input(now, effects);
}
if auto_close {
let effects = self.cadence.apply(CadenceOp::Disarm(
crate::types::frame::TimerOwner::OverlayAutoClose { win, slot },
));
self.conduct_cadence(now, effects);
}
let effects = self
.anim
.apply(AnimOp::Remove(AnimKey::OverlayFade { win, slot }));
self.conduct_anim(now, effects);
self.invalidate(win, None, InvalidateBits::STRUCTURE);
self.outbox
.intents
.push(Intent::Overlay(OverlayIntent::Closed { win, id, cause }));
}
OverlayEffect::Pointer { .. } | OverlayEffect::Key { .. } => {
}
OverlayEffect::Intent(intent) => {
self.outbox.intents.push(Intent::Overlay(intent));
}
OverlayEffect::Capture { win, slot } => {
let capture = slot.map(|s| Capture::Engine(EngineTarget::Overlay(s)));
let effects = self.input.apply(InputOp::SetCapture { win, capture });
self.conduct_input(now, effects);
}
OverlayEffect::Invalidate { win, bits } => self.invalidate(win, None, bits),
}
}
}
fn conduct_input(&mut self, now: Seconds, effects: crate::engine::input::InputEffects) {
for fx in effects {
match fx {
InputEffect::Pointer(_)
| InputEffect::Content { .. }
| InputEffect::KeyPassed { .. } => {
debug_assert!(false, "routing answers are consumed by the route phase");
}
InputEffect::Text(text) => {
self.outbox.intents.push(Intent::Text(text));
}
InputEffect::CopyText { win, text } => {
let effects = self.windows.apply(WindowOp::Enqueue {
win,
cmd: WindowCommand::ClipboardWrite(text),
});
self.conduct_window_all(now, effects);
}
InputEffect::Pasted { win, text } => {
self.outbox
.intents
.push(Intent::Unhandled(crate::types::intent::UnhandledInput::Paste { win, text }));
}
InputEffect::NeedPaste { win, ticket } => {
let effects = self.windows.apply(WindowOp::Enqueue {
win,
cmd: WindowCommand::ClipboardRead(ticket),
});
self.conduct_window_all(now, effects);
}
InputEffect::ImeArea { win, rect } => {
let effects = self.windows.apply(WindowOp::Enqueue {
win,
cmd: WindowCommand::SetImeCursorArea(rect),
});
self.conduct_window_all(now, effects);
}
InputEffect::ImeAllowed { win, allowed } => {
let effects = self.windows.apply(WindowOp::Enqueue {
win,
cmd: WindowCommand::SetImeAllowed(allowed),
});
self.conduct_window_all(now, effects);
}
InputEffect::CaretDeadline { win, at } => {
let effects = match at {
Some(at) => self.cadence.apply(CadenceOp::Arm {
owner: crate::types::frame::TimerOwner::CaretBlink { win },
at,
}),
None => self.cadence.apply(CadenceOp::Disarm(
crate::types::frame::TimerOwner::CaretBlink { win },
)),
};
self.conduct_cadence(now, effects);
}
InputEffect::InvalidateField { win, rect } => {
let region = rect.and_then(|r| self.cadence.view().region_containing(win, r));
self.invalidate(win, region, InvalidateBits::MATERIAL);
}
InputEffect::ScopePushed { .. } => {}
}
}
}
fn conduct_cadence(&mut self, now: Seconds, effects: crate::engine::cadence::CadenceEffects) {
for fx in effects {
match fx {
CadenceEffect::Armed { .. } => {}
CadenceEffect::Fired { owner, .. } => match owner {
crate::types::frame::TimerOwner::App(n) => {
self.outbox.intents.push(Intent::Timer(n));
}
crate::types::frame::TimerOwner::OverlayAutoClose { win, slot } => {
let effects = self.overlays.apply(OverlayOp::Fire { win, slot });
self.conduct_overlay(now, effects);
}
crate::types::frame::TimerOwner::Tooltip { .. } => {
}
crate::types::frame::TimerOwner::CaretBlink { .. } => {
let effects = self.input.tick(now);
self.conduct_input(now, effects);
}
},
}
}
}
fn conduct_anim(&mut self, now: Seconds, effects: crate::engine::anim::AnimEffects) {
for fx in effects {
let (key, t) = match fx {
AnimEffect::Value { key, t } | AnimEffect::Finished { key, t } => (key, t),
};
match key {
AnimKey::Expand { win, kind } => {
let effects = self.layout.apply(LayoutOp::ExpandValue { win, kind, t });
self.conduct_layout(now, effects);
}
AnimKey::OverlayFade { win, .. } => {
self.invalidate(win, None, InvalidateBits::MATERIAL);
}
}
}
}
fn conduct_window_all(&mut self, now: Seconds, effects: crate::engine::windows::WindowEffects) {
for fx in effects {
self.conduct_window(now, fx);
}
}
fn chrome_action(
&mut self,
now: Seconds,
win: WindowId,
action: uzor::widgets::composite::chrome::ChromeAction,
) {
use uzor::widgets::composite::chrome::ChromeAction as A;
match action {
A::Minimize => {
let fx =
self.windows
.apply(WindowOp::Cmd(crate::types::command::WindowCmd::Minimize {
win,
minimized: true,
}));
self.conduct_window_all(now, fx);
}
A::MaximizeRestore => {
let maximized = self
.windows
.view()
.window(win)
.is_some_and(|w| w.geometry().maximized);
let fx =
self.windows
.apply(WindowOp::Cmd(crate::types::command::WindowCmd::Maximize {
win,
maximized: !maximized,
}));
self.conduct_window_all(now, fx);
}
A::CloseWindow => {
let fx = self
.windows
.apply(WindowOp::Cmd(crate::types::command::WindowCmd::Close(win)));
self.conduct_window_all(now, fx);
}
A::CloseApp => {
let fx = self.windows.apply(WindowOp::Shutdown);
self.conduct_window_all(now, fx);
}
A::NewWindow => {
self.outbox
.intents
.push(Intent::Window(WindowIntent::NewWindowRequested {
from: win,
}));
}
A::SelectTab(index) => {
self.outbox
.intents
.push(Intent::Overlay(OverlayIntent::ChromeTab { win, index }));
}
A::OpenMenu => {
self.outbox
.intents
.push(Intent::Overlay(OverlayIntent::ChromeControl {
win,
control: uzor::layout::ChromeWindowControl::Menu,
}));
}
A::NewTab | A::CloseTab(_) | A::WindowDragStart | A::BeginResize(_) | A::None => {
}
}
}
fn invalidate(
&mut self,
win: WindowId,
region: Option<crate::types::ids::RegionId>,
bits: InvalidateBits,
) {
let fx = self
.cadence
.apply(CadenceOp::Invalidate { win, region, bits });
let _ = fx;
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Phase {
Drain,
Lifecycle,
Route,
Engines,
Compose,
Publish,
}
#[derive(Debug, Default)]
pub(crate) struct PhaseClock {
active: Option<Phase>,
}
impl PhaseClock {
fn new() -> Self {
Self::default()
}
fn begin(&mut self, phase: Phase) {
debug_assert!(
self.active.is_none(),
"phase {phase:?} began inside {:?}",
self.active
);
#[cfg(debug_assertions)]
{
self.active = Some(phase);
}
#[cfg(not(debug_assertions))]
{
let _ = phase;
}
}
fn idle(&mut self) {
#[cfg(debug_assertions)]
{
self.active = None;
}
}
}