nativeview_layout/scene/close_request.rs
1//! Close-request routing ([INV:close-request-single-path]): an `Escape` / `GoBack` key down that
2//! no listener consumed closes the topmost open overlay that owns close requests.
3//!
4//! Walking the [`crate::TopLayerIndex::hoisted`] roots front-to-back (reverse paint order, the
5//! published `NvOverlayItem` order) with their resolved flags, the first overlay that is modal
6//! or has `CLOSE_ON_ESCAPE` owns the request. It receives a target-only
7//! `Custom { name: "close" }` only when it has `CLOSE_ON_ESCAPE`; a non-escapable modal
8//! (`closedby="none"`) swallows the request, so nothing beneath it closes — HTML close-watcher
9//! semantics.
10//!
11//! [`CloseRequestAction`] is the built-in, statically ordered `DefaultAction` #0 of every host
12//! that owns a scene; they dispatch through [`LaidOutScene::dispatch_routing_close_request`] (or
13//! its fallible twin): the one funnel that dispatches a batch, then runs #0 on the close key the
14//! dispatch reported unconsumed. The app reports a close request as one bit
15//! (`DispatchResult::CLOSE_REQUEST`, the C ABI), so the funnel cuts the batch after every close
16//! key: each part holds at most one, as its last event, and the bit names that key.
17
18use std::convert::Infallible;
19
20use nativeview_abi::DispatchResult;
21use nativeview_engine::ext::input::{DefaultAction, Disposition};
22use nativeview_store::{EventBatchBuf, EventFlags, NodeId, ValidatedEvent, ValidatedEvents};
23
24use super::{LaidOutScene, close_request_owner};
25use crate::StoreLayoutEngine;
26
27/// Built-in default action #0: an unconsumed close-request key down writes one target-only
28/// `close` to [`LaidOutScene::close_request_target`] into the context batch (cleared first) and
29/// is consumed; anything else passes through. Zero allocations once the batch holds one custom
30/// event.
31pub struct CloseRequestAction<'s, L: StoreLayoutEngine>(pub &'s LaidOutScene<L>);
32
33impl<L: StoreLayoutEngine> DefaultAction<EventBatchBuf> for CloseRequestAction<'_, L> {
34 fn run(&mut self, ev: &ValidatedEvent<'_>, out: &mut EventBatchBuf) -> Disposition {
35 out.clear();
36 if !ev.is_close_request_key_down() {
37 return Disposition::PassThrough;
38 }
39 let Some(target) = self.0.close_request_target() else {
40 return Disposition::PassThrough;
41 };
42 out.push_custom_with_flags(ev.surface(), target, ev.timestamp(), "close", &[], EventFlags::NO_BUBBLE);
43 Disposition::Consumed
44 }
45}
46
47impl<L: StoreLayoutEngine> LaidOutScene<L> {
48 /// The overlay an unconsumed close request closes (see the module docs). O(open overlays).
49 #[inline]
50 pub fn close_request_target(&self) -> Option<NodeId> {
51 close_request_owner(self.open_overlays())
52 }
53
54 /// Dispatches `events` through `dispatch`, cut after every close key (a batch without one is
55 /// one dispatch); when a part reports a close request, its close key is unconsumed. Then
56 /// runs [`CloseRequestAction`] on the last unconsumed close key and dispatches its `close`
57 /// through `dispatch` too (that dispatch's own close request is ignored: a `close` is no
58 /// close key). One `close` per call: the scene is not laid out again in between, so a second
59 /// would hit the same overlay. `batch` is the host's reused close batch. Returns whether any
60 /// dispatch dirtied app state. Zero steady-state allocations.
61 #[inline]
62 pub fn dispatch_routing_close_request(
63 &self,
64 events: ValidatedEvents<'_>,
65 batch: &mut EventBatchBuf,
66 mut dispatch: impl FnMut(ValidatedEvents<'_>) -> DispatchResult,
67 ) -> bool {
68 let Ok(dirty) = self.try_dispatch_routing_close_request(events, batch, |ev| {
69 Ok::<_, Infallible>(dispatch(ev))
70 });
71 dirty
72 }
73
74 /// [`Self::dispatch_routing_close_request`] for a fallible `dispatch` (e.g. a sandboxed
75 /// plugin slot): the first error aborts and is returned.
76 pub fn try_dispatch_routing_close_request<E>(
77 &self,
78 events: ValidatedEvents<'_>,
79 batch: &mut EventBatchBuf,
80 mut dispatch: impl FnMut(ValidatedEvents<'_>) -> Result<DispatchResult, E>,
81 ) -> Result<bool, E> {
82 let (mut dirty, mut trigger, mut rest) = (false, None, events);
83 loop {
84 let end = rest.iter().position(|ev| ev.is_close_request_key_down()).map_or(rest.len(), |i| i + 1);
85 let (part, tail) = rest.split_at(end);
86 let result = dispatch(part)?;
87 dirty |= result.is_dirty();
88 if result.is_close_request()
89 && let Some(key) = end.checked_sub(1).and_then(|last| part.get(last))
90 && key.is_close_request_key_down()
91 {
92 trigger = Some(key);
93 }
94 if tail.is_empty() {
95 break;
96 }
97 rest = tail;
98 }
99 if let Some(trigger) = trigger
100 && CloseRequestAction(self).run(&trigger, batch).is_consumed()
101 {
102 dirty |= dispatch(batch.as_validated())?.is_dirty();
103 }
104 Ok(dirty)
105 }
106}