canvas-widgets-w13: net change of 12 commits

- canvas-widgets-w13: partly written (run interrupted)
- canvas-widgets-w13: partly written (run interrupted)
- canvas-widgets-w13: partly written (run interrupted)
- canvas-widgets-w13: review fixes (review-1)
- canvas-widgets-w13: input enters a frame's first pass only (per-frame input rule)
- fixup: count the rerun trial per consumer
- Integration: access trials keep framework-01-w5's face_record module
- canvas-widgets-w13: the listed-without-size trial clears its first frame's texture delta (fwcw round 3)
- canvas-widgets-w13: the first-pass gate holds back only the next step, so an unrun step is still withdrawn when its target changes (fwcw round 3)
- canvas-widgets-w13: wheel-turn trial carries Alt, which no input stage reinterprets (fwcw round 3)
- canvas-widgets-w13: bounds reads the last finished pass (egui widget_rect_last_pass, repin) (fwcw round 3)
- Integration: Cargo.lock for the egui repin
This commit is contained in:
aiongg
2026-11-15 04:46:06 +00:00
parent d2d7d870c9
commit c0023f9e92
17 changed files with 573 additions and 148 deletions
Generated
+14 -14
View File
@@ -4038,7 +4038,7 @@ dependencies = [
[[package]]
name = "ecolor"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
dependencies = [
"bytemuck",
"emath",
@@ -4094,7 +4094,7 @@ dependencies = [
[[package]]
name = "eframe"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
dependencies = [
"ahash",
"bytemuck",
@@ -4129,7 +4129,7 @@ dependencies = [
[[package]]
name = "egui"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
dependencies = [
"accesskit",
"ahash",
@@ -4148,7 +4148,7 @@ dependencies = [
[[package]]
name = "egui-wgpu"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
dependencies = [
"ahash",
"bytemuck",
@@ -4167,7 +4167,7 @@ dependencies = [
[[package]]
name = "egui-winit"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
dependencies = [
"block2 0.6.2",
"bytemuck",
@@ -4187,7 +4187,7 @@ dependencies = [
[[package]]
name = "egui_glow"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
dependencies = [
"bytemuck",
"egui",
@@ -4200,7 +4200,7 @@ dependencies = [
[[package]]
name = "egui_kittest"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
dependencies = [
"eframe",
"egui",
@@ -4268,7 +4268,7 @@ dependencies = [
[[package]]
name = "emath"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
dependencies = [
"bytemuck",
]
@@ -4329,7 +4329,7 @@ dependencies = [
[[package]]
name = "epaint"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
dependencies = [
"ahash",
"bytemuck",
@@ -4355,7 +4355,7 @@ dependencies = [
[[package]]
name = "epaint_default_fonts"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
[[package]]
name = "equivalent"
@@ -12498,19 +12498,19 @@ dependencies = [
[[patch.unused]]
name = "egui_demo_lib"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
[[patch.unused]]
name = "egui_extras"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
[[patch.unused]]
name = "egui_inspection"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
[[patch.unused]]
name = "egui_system_font_provider"
version = "0.37.0"
source = "git+https://github.com/storytold/egui.git?rev=dcd4de1fbc7502c3a2d69476d9665610947440df#dcd4de1fbc7502c3a2d69476d9665610947440df"
source = "git+https://github.com/storytold/egui.git?rev=5d6f991d5159b51f464c210b43d650a279cd844a#5d6f991d5159b51f464c210b43d650a279cd844a"
+14 -14
View File
@@ -220,20 +220,20 @@ hayro-jbig2 = { path = "vendor/hayro-jbig2" }
hayro-cmap = { path = "vendor/hayro-cmap" }
hayro-postscript = { path = "vendor/hayro-postscript" }
linesweeper = { path = "vendor/linesweeper" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
emath = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
egui = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "dcd4de1fbc7502c3a2d69476d9665610947440df" }
ecolor = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
emath = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
epaint = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
epaint_default_fonts = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
egui = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
egui-winit = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
egui-wgpu = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
egui_glow = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
egui_extras = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
egui_kittest = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
egui_system_font_provider = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
eframe = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
egui_demo_lib = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
egui_inspection = { git = "https://github.com/storytold/egui.git", rev = "5d6f991d5159b51f464c210b43d650a279cd844a" }
winit = { git = "https://github.com/storytold/winit.git", rev = "a517b3846ee8457d84ee4975110bfda6797c44dc" }
# symphonia's FLAC decoder restores LPC samples as `residual + (prediction as i32)`. In a valid
+109 -67
View File
@@ -1,19 +1,21 @@
//! `ui.screenshot`: bounded, cancellable capture requests over [`WindowHost::screenshot`].
//!
//! The slot correlates one capture with one image. This queue owns what the slot leaves to the
//! app: request identity (token, output path, reply), deadlines and settling, the admission budget
//! ([`MAX_PENDING`] requests, [`MAX_IMAGE_BYTES`] per image, [`MAX_TOTAL_BYTES`] in all), the
//! document identity fixed at admission, the geometry epoch recorded at issue so an image is saved
//! only for the layout it was rendered from (a size, scale, zoom or minimised change between issue
//! and collection is a retry, at most [`MAX_RETRIES`]), and a monotonic clock. A request that
//! arrives before the first UI pass recorded the geometry waits instead of failing.
//! app: request identity (token, output path, reply, the viewport it shoots), deadlines and
//! settling, the admission budget ([`MAX_PENDING`] requests, [`MAX_IMAGE_BYTES`] per image,
//! [`MAX_TOTAL_BYTES`] in all), the document identity fixed at admission, each viewport's geometry
//! epoch recorded at issue so an image is saved only for the layout it was rendered from (a size,
//! scale, zoom or minimised change between issue and collection is a retry, at most
//! [`MAX_RETRIES`]), and a monotonic clock. A request for a viewport whose geometry is not recorded
//! yet waits instead of failing; one whose window is not open fails with [`WINDOW_CLOSED`].
//!
//! Phases: [`Captures::issue`] runs in the UI pass (settling counts real layout passes; a
//! discarded pass is skipped); [`Captures::note_geometry`] and [`Captures::drain`] run in the
//! logic phase. The image is encoded and written on the UI thread by
//! discarded pass is skipped); [`Captures::note_windows`] (or [`Captures::note_geometry`] for a
//! synthetic geometry) and [`Captures::drain`] run in the logic phase. The image is encoded and written on the UI thread by
//! [`ControlChannel::finish_captures`](crate::control::ControlChannel::finish_captures), never
//! inside the callback.
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::mpsc::Sender;
use std::time::{Duration, Instant};
@@ -64,7 +66,7 @@ impl DocumentId {
}
}
/// The layout of the root viewport a capture is rendered from: size in points, pixels per point,
/// The layout of the viewport a capture is rendered from: size in points, pixels per point,
/// zoom, the texture side limit and whether the window is minimised. Window position is not part
/// of it.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -189,7 +191,7 @@ impl Finished {
}
}
/// A request admitted before any geometry was recorded.
/// A request not yet queued: it waits while its viewport's geometry is not recorded.
struct WaitingAdmit {
token: u64,
viewport: ViewportId,
@@ -200,13 +202,21 @@ struct WaitingAdmit {
document: Option<DocumentId>,
}
/// What one viewport's window measured: its geometry this pass, `None` while the window is not
/// open, and the epoch that advances on each layout change.
#[derive(Clone, Copy, Default)]
struct Window {
geometry: Option<CaptureGeometry>,
epoch: u64,
}
/// The screenshot request queue and its admission budget.
#[derive(Default)]
pub struct Captures {
records: Vec<PendingCapture>,
waiting: Vec<WaitingAdmit>,
geometry: Option<CaptureGeometry>,
epoch: u64,
/// One entry per recorded viewport; a viewport with no entry was never measured.
windows: HashMap<ViewportId, Window>,
}
impl Captures {
@@ -232,38 +242,54 @@ impl Captures {
/// The geometry recorded by the last [`Self::note_geometry`] for `viewport`, if any.
pub fn last_geometry(&self, viewport: ViewportId) -> Option<CaptureGeometry> {
let _ = viewport; // P3(canvas-widgets-w13): cw.C69 geometry per viewport.
self.geometry
self.windows.get(&viewport).and_then(|window| window.geometry)
}
/// Record this pass's geometry of `viewport`; advances and returns that viewport's epoch when
/// it changed.
pub fn note_geometry(&mut self, viewport: ViewportId, geometry: CaptureGeometry) -> u64 {
let _ = viewport; // P3(canvas-widgets-w13): cw.C69 geometry per viewport.
let changed = self.geometry.is_none_or(|old| !old.same_layout(&geometry));
let window = self.windows.entry(viewport).or_default();
let changed = window.geometry.is_none_or(|old| !old.same_layout(&geometry));
if changed {
self.epoch = self.epoch.wrapping_add(1);
window.epoch = window.epoch.wrapping_add(1);
}
self.geometry = Some(geometry);
self.epoch
window.geometry = Some(geometry);
window.epoch
}
/// The app's one call per pass: for the root and each viewport a pending or waiting request
/// names, records `CaptureGeometry::from_facts` of `host.screenshot_facts(viewport)`, or marks
/// the viewport closed when that is `None`.
#[allow(dead_code)] // P3(canvas-widgets-w13): cw.C69
/// the viewport closed when that is `None`. Viewports no request names are forgotten, so a
/// window measured for an earlier request is measured afresh for the next.
pub fn note_windows(&mut self, host: &WindowHost, document: Option<DocumentId>) {
let _ = (host, document);
todo!("cw.C69")
let mut named = vec![ViewportId::ROOT];
for viewport in self.records.iter().map(|r| r.viewport).chain(self.waiting.iter().map(|w| w.viewport)) {
if !named.contains(&viewport) {
named.push(viewport);
}
}
self.windows.retain(|viewport, _| named.contains(viewport));
for viewport in named {
match host.screenshot_facts(viewport) {
Some(facts) => {
self.note_geometry(viewport, CaptureGeometry::from_facts(facts, document));
}
None if host.is_open(viewport) => {}
None => self.windows.entry(viewport).or_default().geometry = None,
}
}
}
/// Admit a request against the budget. `Err` returns the reply sender with the error text:
/// Admit a request for `viewport` against the budget. `Err` returns the reply sender with the
/// error text:
/// `screenshot token {token} is already pending`,
/// `too many screenshots pending (at most {MAX_PENDING})`,
/// `screenshot {token} failed: {WINDOW_CLOSED}`,
/// `the viewport is not capturable ({w}×{h} points at {ppp} px/pt, texture side {side})`,
/// `a {N} MiB screenshot is over the {MAX_IMAGE_BYTES / 1 MiB} MiB per-image limit` (`N` the
/// expected bytes / 1 MiB, integer division), or
/// `screenshot budget exhausted: {total} bytes reserved of a {MAX_TOTAL_BYTES}-byte aggregate limit`. With no geometry yet the request waits.
/// `screenshot budget exhausted: {total} bytes reserved of a {MAX_TOTAL_BYTES}-byte aggregate limit`.
/// While the viewport's geometry is not recorded the request waits.
pub fn admit(
&mut self,
token: u64,
@@ -280,32 +306,33 @@ impl Captures {
if self.pending() >= MAX_PENDING {
return Err((reply, format!("too many screenshots pending (at most {MAX_PENDING})")));
}
if self.geometry.is_none() {
self.waiting.push(WaitingAdmit { token, viewport, path, reply, deadline: now + DEADLINE, settle_ms, document });
return Ok(());
let request = WaitingAdmit { token, viewport, path, reply, deadline: now + DEADLINE, settle_ms, document };
match self.windows.get(&viewport).copied() {
None => {
self.waiting.push(request);
Ok(())
}
Some(Window { geometry: None, .. }) => Err((request.reply, window_closed_error(token))),
Some(Window { geometry: Some(geometry), epoch }) => self.admit_with_geometry(request, geometry, epoch),
}
self.admit_with_geometry(token, path, viewport, reply, document, settle_ms, now)
}
/// Queues `request` at `geometry` and `epoch`, what its viewport's window recorded.
fn admit_with_geometry(
&mut self,
token: u64,
path: Option<String>,
viewport: ViewportId,
reply: Sender<ControlResponse>,
document: Option<DocumentId>,
settle_ms: f64,
now: Instant,
request: WaitingAdmit,
geometry: CaptureGeometry,
epoch: u64,
) -> Result<(), (Sender<ControlResponse>, String)> {
let WaitingAdmit { token, viewport, path, reply, deadline, settle_ms, document } = request;
// The request's clock started when it arrived.
let arrived = deadline - DEADLINE;
if self.records.iter().any(|r| r.token == token) {
return Err((reply, format!("screenshot token {token} is already pending")));
}
if self.records.len() >= MAX_PENDING {
return Err((reply, format!("too many screenshots pending (at most {MAX_PENDING})")));
}
let Some(geometry) = self.geometry else {
return Err((reply, "the window geometry is not known yet; try again after a frame has been drawn".into()));
};
let expected = match image_budget(&geometry) {
Ok(bytes) => bytes,
Err(e) => return Err((reply, e)),
@@ -320,11 +347,11 @@ impl Captures {
viewport,
path,
reply: Some(reply),
deadline: now + DEADLINE,
ready_at: now + Duration::from_secs_f64(settle / 1000.0),
deadline,
ready_at: arrived + Duration::from_secs_f64(settle / 1000.0),
settle_passes: 0,
geometry,
epoch: self.epoch,
epoch,
issued_epoch: None,
reserved_bytes: expected,
document,
@@ -334,7 +361,8 @@ impl Captures {
Ok(())
}
/// Move waiting requests into the queue once geometry is known; expire the rest.
/// Move waiting requests into the queue once their viewport's geometry is known, fail those
/// whose window is closed, and expire the rest at their deadline.
pub fn admit_waiting(&mut self, now: Instant) {
if self.waiting.is_empty() {
return;
@@ -346,13 +374,15 @@ impl Captures {
let _ = w.reply.send(serde_json::json!({"ok": false, "error": timeout_error(w.token)}));
continue;
}
if self.geometry.is_none() {
still_waiting.push(w);
continue;
}
// The request's clock started when it arrived.
let arrived = w.deadline - DEADLINE;
if let Err((reply, e)) = self.admit_with_geometry(w.token, w.path, w.viewport, w.reply, w.document, w.settle_ms, arrived) {
let admitted = match self.windows.get(&w.viewport).copied() {
None => {
still_waiting.push(w);
continue;
}
Some(Window { geometry: None, .. }) => Err((w.reply, window_closed_error(w.token))),
Some(Window { geometry: Some(geometry), epoch }) => self.admit_with_geometry(w, geometry, epoch),
};
if let Err((reply, e)) = admitted {
let _ = reply.send(serde_json::json!({"ok": false, "error": e}));
}
}
@@ -366,14 +396,10 @@ impl Captures {
}
}
/// UI phase: issue due requests through `host` (a fresh slot each), return the ones that
/// expired meanwhile.
/// UI phase: issue due requests through `host` (a fresh slot each), each in its own viewport,
/// and return the ones that expired or whose window closed meanwhile.
pub fn issue(&mut self, host: &WindowHost, now: Instant, document: Option<DocumentId>) -> Vec<Finished> {
let mut finished = Vec::new();
let Some(current) = self.geometry else { return finished };
// A document switch inside the pass means neither the drawn frame nor the pass's
// document identity is trustworthy.
let pass_changed = !same_activation(current.document, document);
for i in 0..self.records.len() {
if self.records[i].issued() {
continue;
@@ -383,11 +409,18 @@ impl Captures {
finished.push(finish(&mut self.records[i], Err(timeout_error(token))));
continue;
}
let Some(window) = self.windows.get(&self.records[i].viewport).copied() else { continue };
let Some(current) = window.geometry else {
finished.push(finish(&mut self.records[i], Err(window_closed_error(token))));
continue;
};
if !same_activation(document, self.records[i].document) || !same_activation(current.document, self.records[i].document) {
finished.push(finish(&mut self.records[i], Err(document_changed_error(token))));
continue;
}
if pass_changed {
// A document switch inside the pass means neither the drawn frame nor the pass's
// document identity is trustworthy.
if !same_activation(current.document, document) {
continue;
}
// A minimised window has no surface to read back.
@@ -400,10 +433,9 @@ impl Captures {
}
// No pixels exist yet: finalise the geometry at issue, re-checking the budget, and
// let the new layout settle for another pass.
if self.records[i].epoch != self.epoch || !current.same_layout(&self.records[i].geometry) {
if self.records[i].epoch != window.epoch || !current.same_layout(&self.records[i].geometry) {
let others: usize = self.records.iter().enumerate().filter(|(j, _)| *j != i).map(|(_, r)| r.reserved_bytes).sum();
let epoch = self.epoch;
if let Err(e) = revalidate_reservation(&mut self.records[i], others, current, epoch) {
if let Err(e) = revalidate_reservation(&mut self.records[i], others, current, window.epoch) {
finished.push(finish(&mut self.records[i], Err(e)));
}
continue;
@@ -411,16 +443,17 @@ impl Captures {
let slot = host.screenshot(self.records[i].viewport, MAX_IMAGE_BYTES);
let r = &mut self.records[i];
r.slot = Some(slot);
r.issued_epoch = Some(self.epoch);
r.issued_epoch = Some(window.epoch);
}
self.records.retain(|r| !finished.iter().any(|f| f.token == r.token));
finished
}
/// Logic phase: collect completed images (retrying after an epoch advance), expire the late.
/// Logic phase: collect completed images (retrying after an epoch advance), expire the late
/// and fail those whose window closed. Each request is read against the geometry its own
/// viewport recorded this pass.
pub fn drain(&mut self, now: Instant, document: Option<DocumentId>) -> Vec<Finished> {
let current = self.geometry; // P3(canvas-widgets-w13): cw.C69 per-viewport geometry.
// Requests that arrived before the first UI pass start here, once its geometry is known.
// Requests that arrived before their viewport was measured start here, once it is.
self.admit_waiting(now);
let mut finished = Vec::new();
for i in 0..self.records.len() {
@@ -430,6 +463,12 @@ impl Captures {
finished.push(finish(&mut self.records[i], Err(timeout_error(token))));
continue;
}
let window = self.windows.get(&self.records[i].viewport).copied();
if window.is_some_and(|window| window.geometry.is_none()) {
finished.push(finish(&mut self.records[i], Err(window_closed_error(token))));
continue;
}
let current = window.and_then(|window| window.geometry);
if !same_activation(document, self.records[i].document) || !same_activation(current.and_then(|c| c.document), self.records[i].document) {
finished.push(finish(&mut self.records[i], Err(document_changed_error(token))));
continue;
@@ -447,13 +486,13 @@ impl Captures {
continue;
}
};
let Some(current) = current else {
let Some((current, epoch)) = window.and_then(|window| window.geometry.map(|geometry| (geometry, window.epoch))) else {
finished.push(finish(&mut self.records[i], Err(format!("screenshot {token} failed: the window geometry is unknown"))));
continue;
};
// An epoch advance since issue means the image may belong to an intermediate layout,
// even if the values changed back.
let epoch_advanced = self.records[i].issued_epoch.is_none_or(|issued| issued != self.epoch);
let epoch_advanced = self.records[i].issued_epoch.is_none_or(|issued| issued != epoch);
let pass_mixed = !same_activation(current.document, document);
if epoch_advanced || !current.same_layout(&self.records[i].geometry) || pass_mixed {
slot.cancel();
@@ -463,7 +502,6 @@ impl Captures {
}
self.records[i].retries = self.records[i].retries.saturating_add(1);
let others: usize = self.records.iter().enumerate().filter(|(j, _)| *j != i).map(|(_, r)| r.reserved_bytes).sum();
let epoch = self.epoch;
match revalidate_reservation(&mut self.records[i], others, current, epoch) {
Ok(()) => self.records[i].ready_at = now,
Err(e) => finished.push(finish(&mut self.records[i], Err(e))),
@@ -597,6 +635,10 @@ fn timeout_error(token: u64) -> String {
format!("screenshot {token} timed out: no frame was presented within {} s", DEADLINE.as_secs())
}
fn window_closed_error(token: u64) -> String {
format!("screenshot {token} failed: {WINDOW_CLOSED}")
}
fn document_changed_error(token: u64) -> String {
format!("screenshot {token} failed: the document changed while the capture was queued")
}
+15 -7
View File
@@ -262,7 +262,7 @@ pub fn client_token(
}
/// Counts live connections against [`MAX_CONNECTIONS`]; a [`ConnectionPermit`] releases one when
/// dropped.
/// released or dropped.
#[derive(Debug, Default)]
pub struct ConnectionLimiter {
live: std::sync::Arc<std::sync::atomic::AtomicUsize>,
@@ -283,30 +283,38 @@ impl ConnectionLimiter {
return None;
}
match self.live.compare_exchange_weak(current, current + 1, Ordering::AcqRel, Ordering::Acquire) {
Ok(_) => return Some(ConnectionPermit { live: std::sync::Arc::clone(&self.live) }),
Ok(_) => return Some(ConnectionPermit { live: std::sync::Arc::clone(&self.live), released: false }),
Err(actual) => current = actual,
}
}
}
}
/// One live connection's share of [`ConnectionLimiter`]; dropping it frees the slot.
/// One live connection's share of [`ConnectionLimiter`]; [`Self::release`] or dropping it frees
/// the slot.
#[derive(Debug)]
pub struct ConnectionPermit {
live: std::sync::Arc<std::sync::atomic::AtomicUsize>,
/// The slot was freed by [`Self::release`], so `Drop` must not free it again.
released: bool,
}
impl ConnectionPermit {
/// Frees the slot once, atomically, without `Drop` freeing it again; answers whether this was
/// the last live connection.
pub fn release(self) -> bool {
todo!("fx.F21")
/// the last live connection. One atomic decrement decides it, so of two connections ending
/// together exactly one sees itself as the last. A connection admitted between the decrement and
/// `end_injection` may lose input it sends before authenticating; it sends none.
pub fn release(mut self) -> bool {
self.released = true;
self.live.fetch_sub(1, Ordering::AcqRel) == 1
}
}
impl Drop for ConnectionPermit {
fn drop(&mut self) {
self.live.fetch_sub(1, Ordering::AcqRel);
if !self.released {
self.live.fetch_sub(1, Ordering::AcqRel);
}
}
}
+5 -1
View File
@@ -51,8 +51,12 @@ pub fn start_with_deadline(
let host = host.clone();
let token = Arc::clone(&token);
std::thread::spawn(move || {
let _permit = permit;
serve(stream, &token, &tx, &host, auth_deadline);
// What the last client held down would otherwise stay held with no one left to
// release it; while another connection is live, its input is left alone.
if permit.release() {
host.end_injection();
}
});
}
});
+18
View File
@@ -46,6 +46,24 @@ fn deadlines_distinguish_expired_from_timed_out() {
assert_eq!(abandoned_reply.try_recv().expect("a reply")["error"], json!(expired_error()));
}
/// A request sent without a deadline runs even when nobody waits for its reply any more: a fire-and-forget step
/// (a key press, a release) still happens.
#[trial]
fn a_request_without_a_deadline_is_dispatched_after_its_reply_receiver_is_dropped() {
let (tx, rx) = channel();
let mut channel = ControlChannel::new(rx);
let (request, reply) = ControlRequest::new("ui.key", json!({"key": "Escape"}));
assert!(request.deadline.is_none(), "a new request has no deadline");
drop(reply);
tx.send(request).expect("sent");
let next = channel.next_request().expect("the deadline-free request is dispatched");
assert_eq!(next.method, "ui.key");
assert_eq!(next.params, json!({"key": "Escape"}));
assert!(channel.next_request().is_none(), "the request runs once");
}
/// A job's reply goes to the requests waiting on that job only, as the app built it (an error with partial results
/// keeps both); the other job's waiter keeps waiting.
#[trial]
+148 -24
View File
@@ -1,6 +1,7 @@
//! `Captures`: admission budgets, settling, deadlines, cancellation, document identity, the
//! geometry epoch and the image checks.
//! geometry epoch per viewport, the window a capture names, and the image checks.
use std::cell::RefCell;
use std::sync::Arc;
use std::sync::mpsc::{Receiver, channel};
use std::time::{Duration, Instant};
@@ -8,11 +9,14 @@ use std::time::{Duration, Instant};
use craft_automation::control::ControlResponse;
use craft_automation::screenshot::{
CaptureGeometry, Captures, DEADLINE, DocumentId, Finished, MAX_IMAGE_BYTES, MAX_PENDING, MAX_RETRIES, MAX_TOTAL_BYTES, SETTLE_PASSES,
WINDOW_CLOSED,
};
use craft_ui::runtime::motion;
use craft_ui::runtime::windows::WindowHost;
use craft_ui::testing::trial;
use egui::{Color32, ColorImage, Context, ScreenshotCallback, ViewportCommand, ViewportId};
use egui::{
Color32, ColorImage, Context, FullOutput, Pos2, RawInput, Rect, ScreenshotCallback, Vec2, ViewportBuilder, ViewportCommand, ViewportId, ViewportIdMap, ViewportInfo,
};
const DOC: Option<DocumentId> = Some(DocumentId::new(1, 1));
@@ -26,13 +30,19 @@ fn geometry_of(width: f32, height: f32, document: Option<DocumentId>) -> Capture
g
}
fn admit_for(captures: &mut Captures, token: u64, document: Option<DocumentId>, now: Instant) -> Result<Receiver<ControlResponse>, String> {
fn admit_for(
captures: &mut Captures,
token: u64,
viewport: ViewportId,
document: Option<DocumentId>,
now: Instant,
) -> Result<Receiver<ControlResponse>, String> {
let (tx, rx) = channel();
captures.admit(token, None, egui::ViewportId::ROOT, tx, document, 0.0, now).map(|()| rx).map_err(|(_, e)| e)
captures.admit(token, None, viewport, tx, document, 0.0, now).map(|()| rx).map_err(|(_, e)| e)
}
fn admit(captures: &mut Captures, token: u64, now: Instant) -> Result<Receiver<ControlResponse>, String> {
admit_for(captures, token, None, now)
admit_for(captures, token, ViewportId::ROOT, None, now)
}
fn filled(size: [usize; 2], color: Color32) -> Arc<ColorImage> {
@@ -43,13 +53,11 @@ fn image(geometry: &CaptureGeometry) -> Arc<ColorImage> {
filled(geometry.expected_pixels().expect("a capturable geometry"), Color32::from_rgb(240, 240, 240))
}
/// The callbacks of the screenshot commands the context's window host sent since the last pass.
fn sent(ctx: &Context) -> Vec<ScreenshotCallback> {
let mut output = ctx.run_ui(Default::default(), |_| {});
output.textures_delta.clear();
/// The callbacks of the screenshot commands `output` carries for `viewport`.
fn screenshots(output: &FullOutput, viewport: ViewportId) -> Vec<ScreenshotCallback> {
output
.viewport_output
.get(&ViewportId::ROOT)
.get(&viewport)
.map(|out| {
out.commands
.iter()
@@ -59,14 +67,22 @@ fn sent(ctx: &Context) -> Vec<ScreenshotCallback> {
.unwrap_or_default()
}
/// The callbacks of the screenshot commands the context's window host sent to `viewport` since
/// the last pass.
fn sent(ctx: &Context, viewport: ViewportId) -> Vec<ScreenshotCallback> {
let mut output = ctx.run_ui(Default::default(), |_| {});
output.textures_delta.clear();
screenshots(&output, viewport)
}
/// Settles and issues through the context's window host: the requests that finished, and the
/// callbacks of the captures sent.
/// callbacks of the root captures sent.
fn settle_and_issue(captures: &mut Captures, ctx: &Context, now: Instant, document: Option<DocumentId>) -> (Vec<Finished>, Vec<ScreenshotCallback>) {
for _ in 0..SETTLE_PASSES {
captures.note_pass();
}
let finished = captures.issue(&WindowHost::of(ctx), now, document);
(finished, sent(ctx))
(finished, sent(ctx, ViewportId::ROOT))
}
fn issue_one(captures: &mut Captures, ctx: &Context, now: Instant) -> ScreenshotCallback {
@@ -180,7 +196,7 @@ fn settling_counts_passes_and_issues_once() {
for pass in 1..=SETTLE_PASSES + 2 {
captures.note_pass();
assert!(captures.issue(&host, now, None).is_empty());
issued += sent(&ctx).len();
issued += sent(&ctx, ViewportId::ROOT).len();
assert_eq!(issued, usize::from(pass >= SETTLE_PASSES), "after pass {pass}");
}
assert!(!captures.any_unissued());
@@ -278,7 +294,7 @@ fn a_capture_follows_the_document_generation() {
for (after, captured) in [(other, false), (edited, true)] {
let mut captures = Captures::default();
captures.note_geometry(egui::ViewportId::ROOT, g);
admit_for(&mut captures, 21, DOC, now).expect("admitted");
admit_for(&mut captures, 21, ViewportId::ROOT, DOC, now).expect("admitted");
let (_, mut callbacks) = settle_and_issue(&mut captures, &ctx, now, DOC);
callbacks.pop().expect("one capture").complete(image(&g));
let current = geometry_of(800.0, 600.0, after);
@@ -294,7 +310,7 @@ fn a_capture_follows_the_document_generation() {
// The pass began in generation 1 and ended in 2: no capture of a mixed frame.
let mut captures = Captures::default();
captures.note_geometry(egui::ViewportId::ROOT, g);
admit_for(&mut captures, 23, DOC, now).expect("admitted");
admit_for(&mut captures, 23, ViewportId::ROOT, DOC, now).expect("admitted");
let (finished, callbacks) = settle_and_issue(&mut captures, &ctx, now, other);
assert!(callbacks.is_empty());
assert_eq!(finished.len(), 1);
@@ -377,18 +393,126 @@ fn the_image_must_match_the_issued_viewport() {
}
}
/// A minimised window has no surface: the request fails before anything is issued.
const ROOT_SIZE: Vec2 = Vec2::new(800.0, 600.0);
const CHILD_SIZE: Vec2 = Vec2::new(400.0, 300.0);
thread_local! {
/// The screenshot callbacks the child's own passes sent out.
static CHILD_SHOTS: RefCell<Vec<ScreenshotCallback>> = const { RefCell::new(Vec::new()) };
}
fn child() -> ViewportId {
ViewportId::from_hash_of("craft_automation.screenshot.child")
}
/// A window of `size` at one pixel per point, as the integration reports it.
fn window_info(size: Vec2) -> ViewportInfo {
ViewportInfo { inner_rect: Some(Rect::from_min_size(Pos2::ZERO, size)), native_pixels_per_point: Some(1.0), ..ViewportInfo::default() }
}
/// A renderer like eframe's: the child's pass runs nested inside the root's.
fn render_child(ctx: &Context, viewport: egui::ImmediateViewport<'_>) {
let mut show = viewport.viewport_ui_cb;
let mut viewports = ViewportIdMap::default();
viewports.insert(viewport.ids.this, ViewportInfo { parent: Some(viewport.ids.parent), ..window_info(CHILD_SIZE) });
let input = RawInput {
viewport_id: viewport.ids.this,
screen_rect: Some(Rect::from_min_size(Pos2::ZERO, CHILD_SIZE)),
viewports,
..RawInput::default()
};
let mut output = ctx.run_ui(input, |ui| show(ui));
output.textures_delta.clear();
CHILD_SHOTS.with(|shots| shots.borrow_mut().extend(screenshots(&output, viewport.ids.this)));
}
/// One frame of a root that shows the child window while `open`, and lists it in the windows the
/// integration reports. Returns the screenshot callbacks sent to the root and to the child.
fn frame(ctx: &Context, open: bool) -> (Vec<ScreenshotCallback>, Vec<ScreenshotCallback>) {
let mut viewports = ViewportIdMap::default();
viewports.insert(ViewportId::ROOT, window_info(ROOT_SIZE));
if open {
viewports.insert(child(), window_info(CHILD_SIZE));
}
let input = RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, ROOT_SIZE)), viewports, ..RawInput::default() };
let mut output = ctx.run_ui(input, |ui| {
if open {
ui.ctx().show_viewport_immediate(child(), ViewportBuilder::default(), |_, _| {});
}
});
output.textures_delta.clear();
let mut to_child = screenshots(&output, child());
to_child.extend(CHILD_SHOTS.with(|shots| std::mem::take(&mut *shots.borrow_mut())));
(screenshots(&output, ViewportId::ROOT), to_child)
}
/// A capture names its window: it is sized from that window's geometry and shot in that viewport,
/// and once the window is gone it fails at once instead of waiting out the deadline.
#[trial]
fn a_minimized_window_is_refused_before_issue() {
fn a_screenshot_names_its_window_and_fails_once_it_closes() {
let ctx = Context::default();
ctx.set_embed_viewports(false);
Context::set_immediate_viewport_renderer(render_child);
let host = WindowHost::of(&ctx);
let now = motion::now();
let mut captures = Captures::default();
let mut g = geometry(800.0, 600.0);
g.minimized = true;
captures.note_geometry(egui::ViewportId::ROOT, g);
admit(&mut captures, 52, now).expect("admitted");
let (finished, callbacks) = settle_and_issue(&mut captures, &ctx, now, None);
assert!(callbacks.is_empty());
frame(&ctx, true);
let rx = admit_for(&mut captures, 81, child(), None, now).expect("waits for the child's geometry");
captures.note_windows(&host, None);
assert_eq!(captures.last_geometry(child()).map(|g| g.size_points), Some([CHILD_SIZE.x, CHILD_SIZE.y]));
assert!(captures.drain(now, None).is_empty());
assert_eq!(captures.reserved_bytes(), 400 * 300 * 4, "reserved at the child's size, not the root's");
for _ in 0..SETTLE_PASSES {
captures.note_pass();
}
assert!(captures.issue(&host, now, None).is_empty());
let (to_root, mut to_child) = frame(&ctx, true);
assert!(to_root.is_empty(), "the root is not shot");
assert_eq!(to_child.len(), 1, "the child is shot");
to_child.remove(0).complete(filled([400, 300], Color32::WHITE));
captures.note_windows(&host, None);
let finished = captures.drain(now, None);
assert_eq!(finished.len(), 1);
assert!(error(&finished[0]).contains("minimized"));
assert_eq!(finished[0].result.as_ref().map(|image| image.size), Ok([400, 300]));
assert!(rx.try_recv().is_err(), "the caller replies from the Finished");
admit_for(&mut captures, 82, child(), None, now).expect("admitted while the child is open");
frame(&ctx, false);
captures.note_windows(&host, None);
let finished = captures.drain(now, None);
assert_eq!(finished.len(), 1, "no wait for the deadline");
assert_eq!(error(&finished[0]), format!("screenshot 82 failed: {WINDOW_CLOSED}"));
assert_eq!(captures.pending(), 0);
let e = admit_for(&mut captures, 83, child(), None, now).expect_err("the window is closed");
assert_eq!(e, format!("screenshot 83 failed: {WINDOW_CLOSED}"));
}
/// A child window the integration lists before it reports a size is open, not closed: its request keeps waiting, and
/// is shot once the size arrives.
#[trial]
fn a_window_listed_without_a_size_keeps_its_request_waiting() {
let ctx = Context::default();
ctx.set_embed_viewports(false);
Context::set_immediate_viewport_renderer(render_child);
let host = WindowHost::of(&ctx);
let now = motion::now();
let mut captures = Captures::default();
let mut viewports = ViewportIdMap::default();
viewports.insert(ViewportId::ROOT, window_info(ROOT_SIZE));
viewports.insert(child(), ViewportInfo::default());
let input = RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, ROOT_SIZE)), viewports, ..RawInput::default() };
ctx.run_ui(input, |_| {}).textures_delta.clear();
assert!(host.is_open(child()) && host.screenshot_facts(child()).is_none());
admit_for(&mut captures, 91, child(), None, now).expect("waits for the child's geometry");
captures.note_windows(&host, None);
assert!(captures.drain(now, None).is_empty(), "not failed as closed");
assert_eq!(captures.pending(), 1);
frame(&ctx, true);
captures.note_windows(&host, None);
assert_eq!(captures.last_geometry(child()).map(|g| g.size_points), Some([CHILD_SIZE.x, CHILD_SIZE.y]));
assert!(captures.drain(now, None).is_empty());
}
+14 -2
View File
@@ -139,7 +139,7 @@ fn a_held_key_stays_down_until_its_release() {
#[trial]
fn a_wheel_turn_carries_its_modifiers() {
let mut rig = Rig::new();
rig.send("ui.wheel", json!({"x": 30, "y": 20, "dy": 40, "command": true}));
rig.send("ui.wheel", json!({"x": 30, "y": 20, "dy": 40, "alt": true}));
let passes = rig.take();
let turn = passes.iter().find_map(|pass| {
pass.events.iter().find_map(|event| match event {
@@ -149,7 +149,7 @@ fn a_wheel_turn_carries_its_modifiers() {
});
let (pos, delta, modifiers) = turn.expect("a pass saw the turn");
assert_eq!(pos, Some(pos2(30.0, 20.0)));
assert!(delta.y > 0.0 && delta.x == 0.0 && modifiers.command);
assert!(delta.y > 0.0 && delta.x == 0.0 && modifiers.alt);
}
/// The injected Caps Lock state wins over the platform's until it is cleared.
@@ -247,3 +247,15 @@ fn queued_input_is_withdrawn_when_its_target_changes() {
assert!(answer["error"].as_str().is_some_and(|error| error.contains(TARGET_CHANGED)), "{answer}");
assert!(host.injected_idle());
}
/// A queued pass of the root keeps the host busy until one pass of the root ran.
#[trial]
fn a_queued_pass_holds_idle_until_it_ran() {
let mut rig = Rig::new();
let host = rig.host();
host.inject_pass(ViewportId::ROOT);
assert!(!host.injected_idle(), "the pass has not run");
rig.fx.frame(Vec::new());
rig.fx.frame(Vec::new());
assert!(host.injected_idle(), "the pass ran");
}
+5 -4
View File
@@ -28,7 +28,7 @@ use craft_ui::bars::StatusMessage;
use craft_ui::contract::{Pattern, Props as _, Show as _, ShowContainer as _};
use craft_ui::runtime::env::{Env, Label};
use craft_ui::runtime::windows::{ScreenshotSlot, WindowHost};
use egui::{Color32, Context, Pos2, Rect, Ui, ViewportId, pos2, vec2};
use egui::{Color32, Context, Pos2, Rect, Ui, pos2, vec2};
use super::ScreenPickGesture;
@@ -440,8 +440,9 @@ impl ScreenPick {
}
};
let windows = WindowHost::of(ctx);
// P3(canvas-widgets-w13): cw.C66 — `screenshot_facts` takes the viewport and gives `None` when it is not running.
let Some(size) = windows.screenshot_facts(windows.current()) else {
// The frame is measured and shot in the same viewport, so a pick inside a child window samples that window.
let viewport = windows.current();
let Some(size) = windows.screenshot_facts(viewport) else {
self.hold = None;
return None;
};
@@ -458,7 +459,7 @@ impl ScreenPick {
let sampled = hold.frame.as_ref().filter(|_| inside).and_then(|frame| sample_frame(frame, size.pixels_per_point, at, back.as_deref()));
if !released {
if hold.slot.is_none() {
hold.slot = Some(windows.screenshot(ViewportId::ROOT, FRAME_BYTES));
hold.slot = Some(windows.screenshot(viewport, FRAME_BYTES));
}
ctx.request_repaint();
return sampled;
+16 -3
View File
@@ -27,9 +27,21 @@ pub use tree::{AccessTree, Declared, NodeKey};
/// The rect `id`'s response had in the last finished pass, converted as the node's bounds are; `None` when absent,
/// without area, or behind the open modal. The ids it answers are component and item widget ids; parts have none.
/// It answers for the context's current viewport (the root between frames); a child window's rects are read inside
/// that window's pass.
pub fn bounds(ctx: &Context, id: Id) -> Option<egui::Rect> {
let _ = (ctx, id);
todo!("cw.C67")
let widget = ctx.widget_rect_last_pass(id)?;
if behind_modal(ctx, ctx.viewport_id(), widget.layer_id) {
return None;
}
let rect = widget.rect;
(rect.is_finite() && rect.width() > 0.0 && rect.height() > 0.0).then_some(rect)
}
/// Whether a widget on `layer` sits behind the open modal of `viewport`: a modal layer is open there and `layer` is
/// not above it. The access tree and [`bounds`] both ask this, so automation never gets a rect the tree hides.
fn behind_modal(ctx: &Context, viewport: ViewportId, layer: egui::LayerId) -> bool {
crate::runtime::commands::modal_root(ctx, viewport).is_some() && !ctx.memory(|memory| memory.is_above_modal_layer(layer))
}
/// The AccessKit role of a node.
@@ -161,11 +173,12 @@ pub(crate) fn order_children(ctx: &Context) {
/// While a modal layer is open in `viewport`, hides the nodes the runtime wrote on layers below it: what sits behind
/// a modal is inert for assistive technology, as it is for the pointer and the keys.
fn hide_behind_modal(ctx: &Context, viewport: ViewportId, written: &HashSet<Id>) {
// Without a modal nothing is behind one; this spares reading every written node's response.
if crate::runtime::commands::modal_root(ctx, viewport).is_none() {
return;
}
for id in written {
let behind = ctx.read_response(*id).is_some_and(|response| !ctx.memory(|memory| memory.is_above_modal_layer(response.layer_id)));
let behind = ctx.read_response(*id).is_some_and(|response| behind_modal(ctx, viewport, response.layer_id));
if behind {
hide(ctx, *id);
}
@@ -230,6 +230,8 @@ struct Flight {
#[derive(Default)]
struct Lane {
/// The frame whose first pass took the latest step: later passes of that frame take none.
entered: Option<u64>,
queue: VecDeque<Step>,
flight: Option<Flight>,
/// The application withdrew the lane's input ([`withdraw`]); the next input hook carries it out.
@@ -303,6 +305,7 @@ fn remove_event_run(events: &mut Vec<Event>, step: &[Event]) {
pub(crate) fn input_hook(ctx: &Context, input: &mut egui::RawInput) -> bool {
let viewport = input.viewport_id;
let (pass_nr, focused) = (pass::pass_nr(ctx, viewport), crate::runtime::focus::record(ctx, viewport).focused);
let frame = pass::frame_nr(ctx, viewport);
let mut wake = None;
let follow = Service::<Injected>::run(ctx, |injected| {
let Some(lane) = injected.lanes.get_mut(&viewport) else { return false };
@@ -337,6 +340,11 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut egui::RawInput) -> bool {
return !lane.queue.is_empty();
}
}
if lane.entered == Some(frame) {
// A later pass of the frame whose first pass took a step takes none: input enters a frame's first pass
// only. The step in flight still ends, or is withdrawn, as above.
return !lane.queue.is_empty();
}
if let Some(step) = lane.queue.pop_front() {
input.events.extend(step.events.iter().cloned());
let until = (step.rest > 0.0).then_some(input.time).flatten().map(|now| now + step.rest);
@@ -344,6 +352,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut egui::RawInput) -> bool {
let before = held.clone();
held.apply(&step.events);
lane.flight = Some(Flight { step, pass: pass_nr, focus: focused, until, before });
lane.entered = Some(frame);
return !lane.queue.is_empty();
}
false
@@ -461,6 +470,14 @@ pub(crate) fn inject_text(ctx: &Context, viewport: ViewportId, text: &str) {
});
}
/// Queues one pass of `viewport` behind its queue, with no events: `idle` stays false until that pass ran.
pub(crate) fn inject_pass(ctx: &Context, viewport: ViewportId) {
queue(ctx, viewport, |request| {
let mods = request.held.mods;
request.step(mods, []);
});
}
/// Sets the Caps Lock state injected input holds in place of the platform's; `None` returns to the platform's.
pub(crate) fn set_caps_lock(ctx: &Context, on: Option<bool>) {
Service::<Injected>::run(ctx, |injected| injected.caps_lock = on);
@@ -103,7 +103,7 @@ pub struct ScreenshotSlot {
shared: Arc<Shared>,
}
/// What a screenshot of the running viewport measures this pass.
/// What a screenshot of one viewport measures this pass.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ScreenshotFacts {
@@ -129,16 +129,20 @@ pub(crate) fn issue(ctx: &Context, viewport: ViewportId, max_image_bytes: usize)
slot
}
/// What a screenshot of `viewport` measures this pass; `None` when the viewport is not running.
/// What a screenshot of `viewport` measures this pass, from the window that viewport reported; `None` when the
/// viewport is not open. The size is the window's inner rect; only the running viewport falls back to its input
/// rect, since another viewport's input is not this pass's.
pub(crate) fn facts(ctx: &Context, viewport: ViewportId) -> Option<ScreenshotFacts> {
let _ = viewport; // P3(canvas-widgets-w13): cw.C66
let window = viewports::current(ctx);
let size_points = ctx.input(|input| window.inner.map_or_else(|| input.viewport_rect().size(), |inner| inner.size()));
let max_texture_side = ctx.input(|input| input.max_texture_side);
let window = viewports::reported(ctx).into_iter().find_map(|(id, facts)| (id == viewport).then_some(facts))?;
let running = ctx.viewport_id() == viewport;
let (size_points, max_texture_side) = ctx.input(|input| {
let size = window.inner.map(|inner| inner.size()).or_else(|| running.then(|| input.viewport_rect().size()));
(size, input.max_texture_side)
});
let zoom = ctx.zoom_factor();
let pixels_per_point =
window.native_scale.filter(|scale| scale.is_finite() && *scale > 0.0).map_or_else(|| ctx.pixels_per_point(), |scale| scale * zoom);
Some(ScreenshotFacts { size_points, pixels_per_point, zoom, max_texture_side, minimized: window.minimized })
Some(ScreenshotFacts { size_points: size_points?, pixels_per_point, zoom, max_texture_side, minimized: window.minimized })
}
impl ScreenshotSlot {
+2 -1
View File
@@ -415,7 +415,8 @@ pub fn discardable(ctx: &Context) -> bool {
}
/// Discards the running pass, so nothing of it paints, and runs it again, when [`discardable`]: for a host that learns
/// only after a part ran that the part showed what it must not.
/// only after a part ran that the part showed what it must not. A pass after a discard carries no input (events,
/// injected steps and press edges enter a frame's first pass only); held state persists.
pub fn discard_pass(ctx: &Context, reason: &'static str) {
crate::runtime::egui::sizing::request_discard(ctx, crate::runtime::egui::sizing::Rerun::App(reason));
}
+6 -2
View File
@@ -23,7 +23,10 @@ pub(crate) const WHEEL_LINE_POINTS: f32 = 40.0;
/// Points one wheel page adds to `PointerFrame.scroll`.
pub(crate) const WHEEL_PAGE_POINTS: f32 = 400.0;
/// The pointer input of one pass of one viewport, egui-converted.
/// The pointer input of one pass of one viewport, egui-converted. Input enters a frame's first pass only (egui hands
/// the frame's events to it, and injected steps and held-back presses follow the frame): a rerun pass of the
/// frame carries no events, so its `delta`, `press`, `release` and `scroll` are empty while the held facts (`pos`,
/// `buttons`) persist. A consumer applies what its pass hands it and never turns a held fact into an event.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct PointerFrame {
@@ -67,7 +70,8 @@ pub struct PressTargets {
pub drag: Option<Id>,
}
/// What the begin hook read of the pass, for the sensing core and the services.
/// What the begin hook read of the pass, for the sensing core and the services. Built from the pass's own events,
/// so a rerun pass of a frame (see [`PointerFrame`]) reads none.
#[derive(Clone, Debug, Default)]
#[doc(hidden)]
pub struct PassPointer {
+23 -2
View File
@@ -100,6 +100,11 @@ impl WindowHost {
screenshot::issue(&self.ctx, viewport, max_image_bytes)
}
/// Whether `viewport`'s window is open this pass, measured or not.
pub fn is_open(&self, viewport: ViewportId) -> bool {
viewports::reported(&self.ctx).iter().any(|(id, _)| *id == viewport)
}
/// What a screenshot of `viewport` measures this pass; `None` when the viewport is not running.
pub fn screenshot_facts(&self, viewport: ViewportId) -> Option<ScreenshotFacts> {
screenshot::facts(&self.ctx, viewport)
@@ -171,6 +176,12 @@ impl WindowHost {
injected::inject_text(&self.ctx, viewport, text);
}
/// Queues `event` as the platform's input method would deliver it (a preedit or a commit), behind earlier
/// injected input.
pub fn inject_ime(&self, viewport: ViewportId, event: egui::ImeEvent) {
injected::inject_ime(&self.ctx, viewport, event);
}
/// Withdraws the injected input of `viewport` (the document changed) at its next input hook: a step whose pass
/// has not run and the queue behind it; what is dropped counts as withdrawn
/// ([`take_injected_withdrawn`](Self::take_injected_withdrawn)). Called from the raw input hook, so every
@@ -184,12 +195,22 @@ impl WindowHost {
injected::cancel(&self.ctx)
}
/// Ends the injection session: the automation connection that fed it is gone.
/// Ends the injection session: the automation connection that fed it is gone. Drops every queue and step in
/// flight, queues the release of every button, key and modifier injected input holds (they run on each
/// viewport's next pass), and returns Caps Lock to the platform's state.
pub fn end_injection(&self) {
injected::end(&self.ctx);
}
/// Whether no injected step is queued or awaits its pass: the application may answer its waiters.
/// Queues a pass of `viewport` behind its injected input: `injected_idle` stays false until that pass ran. A
/// component that keeps injected input of its own for its next pass (the tool host's `ToolHost::inject`) calls
/// it, so automation's idle answer waits for the pass that takes the input.
pub fn inject_pass(&self, viewport: ViewportId) {
injected::inject_pass(&self.ctx, viewport);
}
/// Whether no injected step is queued or awaits its pass (see [`inject_pass`](Self::inject_pass)): the
/// application may answer its waiters.
pub fn injected_idle(&self) -> bool {
injected::idle(&self.ctx)
}
@@ -0,0 +1,94 @@
//! `runtime::access::bounds`: the rect automation clicks hides what the access tree hides.
use craft_ui::contract::{
Common, Component, Container, ContainerProps, Cx, Mount, Overlay, Props as _, Role, Show as _, ShowContainer as _,
};
use craft_ui::runtime::access::bounds;
use craft_ui::style::StyleCx;
use craft_ui::testing::{Frames, trial};
use craft_ui_test_kit::contract::PlainStyle;
use egui::{Rect, Response, Ui, Vec2};
use std::cell::Cell;
/// A button-like leaf with a fixed size.
#[derive(craft_ui::contract::Component)]
struct Apply {
#[common]
common: Common,
}
impl Component for Apply {
const ROLE: Role = Role::BUTTON;
type Style = PlainStyle;
type Output = Response;
fn style(_: &StyleCx<'_>, _: ApplyStyleKey) -> PlainStyle {
PlainStyle::min(Vec2::new(80.0, 24.0))
}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
/// A modal layer with an empty body.
#[derive(craft_ui::contract::Component)]
#[component(container)]
struct Modal {
#[common]
common: Common,
#[container]
props: ContainerProps,
}
impl Component for Modal {
const ROLE: Role = Role::GROUP;
type Style = PlainStyle;
type Output = Response;
fn style(_: &StyleCx<'_>, _: ModalStyleKey) -> PlainStyle {
PlainStyle::min(Vec2::new(120.0, 60.0))
}
fn finish(self, cx: Cx<'_, Self>) -> Response {
cx.response.clone()
}
}
impl Container for Modal {}
/// A shown button answers its last pass's rect; a button not shown this pass answers `None`; the same button behind
/// an open modal answers `None`, as the access tree hides it.
#[trial]
fn bounds_of_a_part_behind_a_modal_is_none() {
let mut frames = Frames::new();
let shown: Cell<Option<(egui::Id, Rect)>> = Cell::new(None);
let button = |ui: &mut Ui| {
let response = Apply { common: Common::new() }.label("Apply").id_salt("apply").show(ui);
shown.set(Some((response.id, response.rect)));
};
for _ in 0..2 {
frames.frame(vec![], button);
}
let Some((id, rect)) = shown.get() else { panic!("the button was shown") };
assert!(rect.width() > 0.0 && rect.height() > 0.0, "the button has area: {rect:?}");
assert_eq!(bounds(&frames.ctx, id), Some(rect), "a shown button answers its rect");
frames.frame(vec![], |_| {});
assert_eq!(bounds(&frames.ctx, id), None, "a button not shown this pass has no rect");
// The modal registers at the end of a pass and egui takes its layer at the next pass's start, so a few frames
// settle it before the check.
for _ in 0..4 {
frames.frame(vec![], |ui| {
button(ui);
let props = ContainerProps::new().mount(Mount::Layer(Overlay::modal()));
Modal { common: Common::new(), props }.id_salt("modal").show(ui, |_, _| {});
});
}
let Some((behind, _)) = shown.get() else { panic!("the button was shown") };
assert_eq!(behind, id, "the button keeps its id under the modal");
assert!(frames.ctx.read_response(id).is_some(), "the button was shown this pass");
assert_eq!(bounds(&frames.ctx, id), None, "a button behind the open modal has no rect");
}
@@ -239,3 +239,65 @@ fn a_held_press_reads_down_once_through_a_discard_pass_and_later_frames() {
assert!(phases.iter().all(|phase| phase.button_down() || matches!(phase, PressPhase::Release { .. })), "{phases:?}");
assert!(matches!(phases.last(), Some(PressPhase::Release { .. })), "{phases:?}");
}
/// Input enters the first pass of a frame only: a frame with a pointer move, a wheel notch, a key press and an
/// injected step, forced to rerun, hands each to its consumer once. The rerun pass is not stirred, its wheel taker
/// has no share, its focused component has no intent and it carries no injected step.
#[trial]
fn a_rerun_pass_delivers_none_of_the_frames_input_again() {
use craft_behavior::{Input, WheelTaker};
use craft_ui::runtime::focus::{FocusTarget, KeyUse};
use craft_ui::testing::focus::{Stop, intents_of, named_id, register_stop, widget};
use craft_ui::testing::input::key_press;
use craft_ui::testing::wheel::{register, take};
#[derive(Default)]
struct Pass {
stirred: bool,
wheel: bool,
moves: usize,
injected: bool,
}
let field = named_id("rerun_field");
let log: Rc<RefCell<Vec<Pass>>> = Rc::default();
let sink = log.clone();
let mut show = move |ui: &mut Ui| {
let ctx = ui.ctx().clone();
widget(ui, field);
register_stop(&ctx, Stop::new(field, KeyUse::of(Input::Text)));
register(&ctx, WheelTaker::new(field, Rect::from_min_size(Pos2::ZERO, Vec2::splat(400.0))).vertical(true));
let mut intents = intents_of(&ctx, Some(field), false);
let mut moves = 0;
while intents.take_move().is_some() {
moves += 1;
}
sink.borrow_mut().push(Pass {
stirred: craft_ui::runtime::pointer::with_pass_pointer(&ctx, |pass| pass.stirred),
wheel: take(&ctx, field).is_some(),
moves,
injected: craft_ui::runtime::windows::WindowHost::of(&ctx).injected_pass(),
});
};
let mut frames = Frames::new();
frames.idle(2, &mut show);
craft_ui::runtime::focus::request(&frames.ctx, FocusTarget::Id(field));
frames.idle(2, &mut show);
craft_ui::runtime::windows::WindowHost::of(&frames.ctx).inject_text(egui::ViewportId::ROOT, "x");
log.borrow_mut().clear();
let mut first = true;
let events = vec![pointer::moved(Pos2::new(20.0, 20.0)), pointer::wheel(-30.0), key_press(egui::Key::ArrowRight, egui::Modifiers::NONE)];
let passes = frames.frame(events, |ui| {
if std::mem::take(&mut first) {
craft_ui::runtime::frame::discard_pass(ui.ctx(), "test");
}
show(ui);
});
assert_eq!(passes, 2, "the frame reruns");
let log = log.borrow();
assert_eq!(log.len(), 2);
assert_eq!(log.iter().filter(|pass| pass.stirred).count(), 1, "one pass saw the pointer move");
assert_eq!(log.iter().filter(|pass| pass.wheel).count(), 1, "one pass took the wheel notch");
assert_eq!(log.iter().map(|pass| pass.moves).sum::<usize>(), 1, "the key's intent arrived once");
assert_eq!(log.iter().filter(|pass| pass.injected).count(), 1, "the injected step entered once");
assert!(log[0].stirred && log[0].injected && !log[1].stirred && !log[1].injected, "the first pass carries the input");
}