use uzor::CursorIcon;
use uzor::render::InvalidateBits;
use uzor::input::{KeyboardShortcut, MouseButton};
use crate::types::bus::{
ClipboardResult, DropInput, ImeInput, InputEnvelope, InputEvent, KeyInput, KeyState,
PointerInput, WheelInput,
};
use crate::types::command::LayoutHit;
use crate::types::ids::{Seconds, WindowId};
use crate::types::intent::{DropIntent, Intent, UnhandledInput};
use crate::types::ops::{
Capture, DockPointer, EngineTarget, InputEffect, InputOp, Intercepted, KeyRoute, LayoutOp,
OverlayEffect, OverlayOp, PointerRoute, PointerTarget, WindowOp,
};
use crate::types::spec::Spec;
use crate::types::window::WindowCommand;
use super::Kernel;
impl<S: Spec> Kernel<S> {
pub(super) fn route(&mut self, now: Seconds, inputs: &mut Vec<InputEnvelope>) {
for env in inputs.drain(..) {
self.trace.clear();
let win = env.window;
match env.event {
InputEvent::Pointer(input) => self.route_pointer(now, win, input),
InputEvent::Wheel(input) => self.route_wheel(now, win, input),
InputEvent::Key(input) => self.route_key(now, win, input),
InputEvent::Ime(input) => self.route_ime(now, win, input),
InputEvent::Clipboard(result) => self.route_clipboard(now, win, result),
InputEvent::Paste(text) => {
self.trace_push("route::paste");
let fx = self.input.apply(InputOp::Paste { win, now, text });
self.conduct_input(now, fx);
}
InputEvent::ClipboardImage(image) => {
self.outbox.intents.push(Intent::PasteImage(crate::types::intent::PastedImage { win, image }));
}
InputEvent::Drop(input) => self.route_drop(now, win, input),
InputEvent::Window(_) | InputEvent::Host(_) | InputEvent::Timer(_) => {}
InputEvent::Touch(t) => self.route_touch(now, win, t),
}
self.trace_max = self.trace_max.max(self.trace.len());
}
}
fn route_touch(&mut self, now: Seconds, win: WindowId, t: crate::types::bus::TouchInput) {
use crate::types::bus::{TouchInput as T, WheelDelta, WheelInput};
let mods = uzor::input::ModifierKeys::default();
let mean = |v: &[(u64, crate::types::window::Point)]| {
let n = v.len().max(1) as f64;
crate::types::window::Point::new(v.iter().map(|c| c.1.x).sum::<f64>() / n, v.iter().map(|c| c.1.y).sum::<f64>() / n)
};
match t {
T::Start { id, pos } => {
self.touches.push((id, pos));
if self.touches.len() == 1 {
self.route_pointer(now, win, PointerInput::Moved { pos, mods });
self.route_pointer(now, win, PointerInput::Down { pos, button: MouseButton::Left, mods });
} else if self.touches.len() == 2 {
self.route_pointer(now, win, PointerInput::Cancelled);
}
}
T::Move { id, pos } => {
let before = mean(&self.touches);
let primary = self.touches.first().map(|c| c.0) == Some(id);
if let Some(c) = self.touches.iter_mut().find(|c| c.0 == id) {
c.1 = pos;
}
if self.touches.len() >= 2 {
let after = mean(&self.touches);
let (dx, dy) = (after.x - before.x, after.y - before.y);
if dx != 0.0 || dy != 0.0 {
self.route_wheel(now, win, WheelInput { pos: after, delta: WheelDelta::Pixels { x: dx, y: dy }, mods });
}
} else if primary {
self.route_pointer(now, win, PointerInput::Moved { pos, mods });
}
}
T::End { id, pos } => {
let n = self.touches.len();
let primary = self.touches.first().map(|c| c.0) == Some(id);
self.touches.retain(|c| c.0 != id);
if n == 1 && primary {
self.route_pointer(now, win, PointerInput::Up { pos, button: MouseButton::Left, mods });
}
}
T::Cancel { id } => {
let n = self.touches.len();
self.touches.retain(|c| c.0 != id);
if n == 1 {
self.route_pointer(now, win, PointerInput::Cancelled);
}
}
}
}
fn route_pointer(&mut self, now: Seconds, win: WindowId, input: PointerInput) {
self.trace_push("route::pointer");
#[cfg(feature = "inspector")]
if self.inspector.enabled {
self.invalidate(win, None, InvalidateBits::ALL);
}
let fx = self.input.apply(InputOp::Pointer { win, now, input });
let Some(cooked) = fx.iter().find_map(|e| match e {
InputEffect::Pointer(c) => Some(c.clone()),
_ => None,
}) else {
self.conduct_input(now, fx);
return;
};
self.conduct_input(
now,
fx.into_iter()
.filter(|e| !matches!(e, InputEffect::Pointer(_)))
.collect(),
);
match cooked.target.clone() {
PointerTarget::Captured(Capture::Widget(_)) => {
self.trace_push("capture:widget");
self.content_cursor_at(now, win, input);
let fx = self.input.apply(InputOp::Deliver { win, input });
self.conduct_routed_content(now, win, input, fx);
return;
}
PointerTarget::Captured(Capture::Engine(EngineTarget::Layout)) => {
self.trace_push("capture:layout");
if let Some(event) = dock_pointer(input) {
let fx = self.layout.apply(LayoutOp::Pointer { win, now, event });
self.conduct_layout(now, fx);
}
return;
}
PointerTarget::Captured(Capture::Engine(EngineTarget::Overlay(slot))) => {
self.trace_push("capture:overlay");
let fx = self
.overlays
.apply(OverlayOp::Captured { win, slot, input });
self.conduct_overlay(now, fx);
return;
}
PointerTarget::HitTest => {}
}
let fx = self.overlays.apply(OverlayOp::Intercept {
win,
event: Intercepted::Pointer(input),
});
let route = fx.iter().find_map(|e| match e {
OverlayEffect::Pointer { route, .. } => Some(*route),
_ => None,
});
self.conduct_overlay(
now,
fx.into_iter()
.filter(|e| !matches!(e, OverlayEffect::Pointer { .. } | OverlayEffect::Key { .. }))
.collect(),
);
match route {
Some(PointerRoute::Consumed) => {
self.trace_push("overlay:consumed");
self.content_cursor_at(now, win, input);
return;
}
Some(PointerRoute::Overlay { .. }) => {
self.trace_push("overlay:body");
self.content_cursor_at(now, win, input);
let fx = self.input.apply(InputOp::Deliver { win, input });
self.conduct_routed_content(now, win, input, fx);
return;
}
Some(PointerRoute::Pass) | None => {}
}
if let Some(pos) = pointer_pos(input) {
let mut hit = self.layout.hit(win, pos);
let floating = self
.layout
.view()
.window(win)
.and_then(|wv| wv.chrome_model().floating.clone());
if let Some(fc) = floating {
if hit == LayoutHit::Chrome(uzor::widgets::composite::chrome::ChromeHit::Drag)
&& self
.input
.view()
.coordinator(win)
.and_then(|c| c.hit_test_now(pos.x, pos.y))
.is_some()
{
hit = LayoutHit::None;
}
let vp = self.windows.view().window(win).map(|w| w.geometry().viewport);
if vp.is_some_and(|vp| pos.y < vp.y + fc.style.button_h.max(fc.style.dot_inset * 2.0 + fc.style.dot) + 4.0) {
self.invalidate(win, None, InvalidateBits::ALL);
}
}
self.update_cursor(now, win, &hit, pos);
let primary_down = matches!(
input,
PointerInput::Down {
button: MouseButton::Left,
..
}
);
if primary_down && hit.claims_press() {
self.trace_push("layout:zone");
let fx = self.layout.apply(LayoutOp::Pointer {
win,
now,
event: DockPointer::Down {
pos,
button: MouseButton::Left,
},
});
self.conduct_layout(now, fx);
return;
}
}
self.trace_push("content");
let fx = self.input.apply(InputOp::Deliver { win, input });
self.conduct_routed_content(now, win, input, fx);
}
fn conduct_routed_content(
&mut self,
now: Seconds,
win: WindowId,
input: PointerInput,
fx: crate::engine::input::InputEffects,
) {
let target = fx.iter().find_map(|e| match e {
InputEffect::Content { target, .. } => Some(target.clone()),
_ => None,
});
let _ = &input;
self.invalidate(win, None, InvalidateBits::MATERIAL);
self.conduct_input(
now,
fx.into_iter()
.filter(|e| !matches!(e, InputEffect::Content { .. }))
.collect(),
);
if matches!(target, Some(None)) && matches!(input, PointerInput::Down { .. }) {
self.outbox
.intents
.push(Intent::Unhandled(UnhandledInput::Pointer {
win,
pointer: input,
}));
}
}
fn route_wheel(&mut self, now: Seconds, win: WindowId, input: WheelInput) {
self.trace_push("route::wheel");
match self.input.view().capture(win).cloned() {
Some(Capture::Widget(_)) => {
self.trace_push("capture:widget");
let fx = self.input.apply(InputOp::Wheel { win, input });
self.conduct_wheel_content(now, win, input, fx);
}
Some(Capture::Engine(EngineTarget::Layout)) => {
self.trace_push("capture:layout");
}
Some(Capture::Engine(EngineTarget::Overlay(_))) | None => {
let fx = self.overlays.apply(OverlayOp::Intercept {
win,
event: Intercepted::Wheel(input),
});
let route = fx.iter().find_map(|e| match e {
OverlayEffect::Pointer { route, .. } => Some(*route),
_ => None,
});
self.conduct_overlay(
now,
fx.into_iter()
.filter(|e| {
!matches!(e, OverlayEffect::Pointer { .. } | OverlayEffect::Key { .. })
})
.collect(),
);
match route {
Some(PointerRoute::Consumed) => self.trace_push("overlay:consumed"),
Some(PointerRoute::Overlay { .. }) | Some(PointerRoute::Pass) | None => {
self.trace_push("content");
let fx = self.input.apply(InputOp::Wheel { win, input });
self.conduct_wheel_content(now, win, input, fx);
}
}
}
}
}
fn conduct_wheel_content(
&mut self,
now: Seconds,
win: WindowId,
input: WheelInput,
fx: crate::engine::input::InputEffects,
) {
let target = fx.iter().find_map(|e| match e {
InputEffect::Content { target, .. } => Some(target.clone()),
_ => None,
});
let _ = &input;
self.invalidate(win, None, InvalidateBits::MATERIAL);
self.conduct_input(
now,
fx.into_iter()
.filter(|e| !matches!(e, InputEffect::Content { .. }))
.collect(),
);
if matches!(target, Some(None)) {
self.outbox
.intents
.push(Intent::Unhandled(UnhandledInput::Wheel {
win,
wheel: input,
}));
}
}
fn route_key(&mut self, now: Seconds, win: WindowId, input: KeyInput) {
self.trace_push("route::key");
#[cfg(feature = "inspector")]
if input.code == uzor::input::KeyCode::F12 {
if input.state == KeyState::Down {
if input.mods.shift {
self.inspector.pin_pointer();
} else {
self.inspector.toggle();
}
self.invalidate(win, None, InvalidateBits::ALL);
}
return;
}
let focus_before = self.input.view().window(win).map(|v| (v.focused.clone(), v.focus_visible));
let fx = self.input.apply(InputOp::Key {
win,
now,
input: input.clone(),
});
if self.input.view().window(win).map(|v| (v.focused.clone(), v.focus_visible)) != focus_before {
self.invalidate(win, None, InvalidateBits::ALL);
}
let passed = fx.iter().find_map(|e| match e {
InputEffect::KeyPassed { key, .. } => Some(key.clone()),
_ => None,
});
self.conduct_input(
now,
fx.into_iter()
.filter(|e| !matches!(e, InputEffect::KeyPassed { .. }))
.collect(),
);
let Some(key) = passed else {
self.trace_push("text");
return;
};
let fx = self.overlays.apply(OverlayOp::Intercept {
win,
event: Intercepted::Key(key.clone()),
});
let route = fx.iter().find_map(|e| match e {
OverlayEffect::Key { route, .. } => Some(*route),
_ => None,
});
self.conduct_overlay(
now,
fx.into_iter()
.filter(|e| !matches!(e, OverlayEffect::Pointer { .. } | OverlayEffect::Key { .. }))
.collect(),
);
let (top_overlay, modal_open) = match route {
Some(KeyRoute::Consumed) => {
self.trace_push("overlay:escape");
return;
}
Some(KeyRoute::Pass {
top_overlay,
modal_open,
}) => (top_overlay, modal_open),
None => (None, false),
};
if key.state != KeyState::Up {
let chord = KeyboardShortcut::new(key.mods, key.code);
let focused = self.input.view().focused(win).cloned();
if let Some(action) =
self.keymap
.resolve(&chord, focused.as_ref(), top_overlay.as_ref(), modal_open)
{
self.trace_push("keymap");
self.outbox.intents.push(Intent::Action(action));
return;
}
self.outbox
.intents
.push(Intent::Unhandled(UnhandledInput::Key { win, key }));
}
}
fn route_ime(&mut self, now: Seconds, win: WindowId, input: ImeInput) {
self.trace_push("route::ime");
let fx = self.input.apply(InputOp::Ime { win, now, input });
self.conduct_input(now, fx);
}
fn route_clipboard(&mut self, now: Seconds, win: WindowId, result: ClipboardResult) {
self.trace_push("route::clipboard");
let fx = self.input.apply(InputOp::Clipboard { win, now, result });
self.conduct_input(now, fx);
}
fn route_drop(&mut self, now: Seconds, win: WindowId, input: DropInput) {
let _ = now;
self.trace_push("route::drop");
let file = match input {
DropInput::Dropped(f) => f,
DropInput::Hovered(file) => {
self.outbox.intents.push(Intent::Drop(DropIntent::Hovering { win, file }));
return;
}
DropInput::Cancelled => {
self.outbox.intents.push(Intent::Drop(DropIntent::Left { win }));
return;
}
};
let pos = self.input.view().coordinator(win).and_then(|c| c.pointer_pos());
let target = pos
.and_then(|(x, y)| {
self.input
.view()
.coordinator(win)
.and_then(|c| c.hit_test_now(x, y))
});
match target {
Some(target) => self.outbox.intents.push(Intent::Drop(DropIntent::Files { win, target, files: vec![file], pos })),
None => self.outbox.intents.push(Intent::Drop(DropIntent::Window { win, files: vec![file], pos })),
}
}
fn content_cursor_at(&mut self, now: Seconds, win: WindowId, input: PointerInput) {
if let Some(pos) = pointer_pos(input) {
self.update_cursor(now, win, &LayoutHit::None, pos);
}
}
fn update_cursor(&mut self, now: Seconds, win: WindowId, hit: &LayoutHit, pos: crate::types::window::Point) {
let icon = match hit.cursor() {
Some(i) => i,
None => self.input.view().coordinator(win).map_or(CursorIcon::Default, |c| content_cursor(c, pos)),
};
let current = self.windows.view().window(win).map(|w| w.cursor());
if current != Some(icon) {
let fx = self.windows.apply(WindowOp::Enqueue {
win,
cmd: WindowCommand::SetCursor(icon),
});
self.conduct_window_all(now, fx);
}
}
fn trace_push(&mut self, hop: &'static str) {
self.trace.push(hop);
}
}
pub(crate) fn content_cursor(c: &uzor::input::InputCoordinator, pos: crate::types::window::Point) -> CursorIcon {
c.cursor_at(pos.x, pos.y)
}
fn pointer_pos(input: PointerInput) -> Option<crate::types::window::Point> {
match input {
PointerInput::Moved { pos, .. }
| PointerInput::Down { pos, .. }
| PointerInput::Up { pos, .. } => Some(pos),
PointerInput::Entered
| PointerInput::Left
| PointerInput::Cancelled
| PointerInput::RawDelta { .. } => None,
}
}
fn dock_pointer(input: PointerInput) -> Option<DockPointer> {
match input {
PointerInput::Down { pos, button, .. } => Some(DockPointer::Down { pos, button }),
PointerInput::Moved { pos, .. } => Some(DockPointer::Move(pos)),
PointerInput::Up { pos, .. } => Some(DockPointer::Up(pos)),
PointerInput::Cancelled => Some(DockPointer::Cancel),
PointerInput::Entered | PointerInput::Left | PointerInput::RawDelta { .. } => None,
}
}