2bc41aeb8602
control.rs
use super::{
MAX_PENDING_SCREENSHOTS, MAX_SCREENSHOT_BYTES, MAX_SCREENSHOT_TOTAL_BYTES, PendingScreenshot, QueuedScreenshot, SCREENSHOT_TIMEOUT_MS,
VectorcraftApp, canvas, file_access, json, now_ms,
};
use crate::control;
impl VectorcraftApp {
pub(crate) fn drain_control(&mut self, ctx: &egui::Context) {
let Some(rx) = self.control_rx.take() else { return };
let mut handled = 0;
while handled < control::MAX_CONTROL_REQUESTS_PER_FRAME {
let Some(req) = self.deferred_control.take().or_else(|| rx.try_recv().ok()) else { break };
// Synthetic input sent before this tool action was dropped (see
// `discard_stale_injected_batch`): it would reach the tool without that input.
let tool_action = matches!(req.method.as_str(), "ui.pointer" | "ui.tool.select");
if tool_action && req.made_before(self.synthetic_dropped_before) {
// One its caller gave up is skipped without counting toward this frame's limit.
if req.claim() {
handled += 1;
let _ = req.reply.send(json!({
"ok": false,
"error": "input sent before this tool action was dropped because the document, dialog or focus changed"
}));
}
continue;
}
// `ui.pointer` and `ui.tool.select` act at once, while `ui.key` and other synthetic
// input reach the tool in a later UI pass. Wait for that earlier input, so a key
// pressed during a drag reaches its tool before a pointer event or tool switch.
// One its caller gave up meanwhile is dropped below instead of holding back the rest.
if tool_action && !req.given_up() && (!self.synthetic.is_empty() || self.synthetic_injected) {
self.deferred_control = Some(req);
ctx.request_repaint();
break;
}
// Its caller stopped waiting (the app was busy or its window not drawn for too long)
// and was told it won't run: an edit applied now would come from nowhere (#1725).
if !req.claim() {
continue;
}
handled += 1;
let reply = req.reply.clone();
// Guarded per request: a panic must not drop the channel (taken out of `self` above).
let roots = self.automation_roots.clone();
self.ui.answering_control = true;
let answer = file_access::confine(roots.as_ref(), || vectorcraft_engine::guard::catch_panic(|| control::handle(self, ctx, &req)));
self.ui.answering_control = false;
let outcome = match answer {
Ok(outcome) => outcome,
Err(msg) => {
self.picks.recover();
self.session.set_replaying(false);
control::err(format!("internal error: {msg} (please report this bug)"))
}
};
match outcome {
control::Outcome::Done(v) => {
let _ = reply.send(v);
}
control::Outcome::Screenshot { path, data } => {
// Admit with the shared ceilings (4 requests, 128 MiB each, 256 MiB together)
// before issue, reserving the checked bytes this viewport would need.
match self.screenshot_pixels(ctx) {
Ok((width, height, bytes)) if bytes > MAX_SCREENSHOT_BYTES => {
let _ = reply.send(json!({"ok": false, "error": format!(
"screenshot would use {bytes} bytes ({width}×{height}), over the {MAX_SCREENSHOT_BYTES}-byte limit"
)}));
}
Ok((width, height, bytes)) if data && path.is_none() && bytes > control::MAX_SCREENSHOT_PNG_REPLY_BYTES => {
let _ = reply.send(json!({"ok": false, "error": format!(
"a PNG reply of this {width}×{height} screenshot ({bytes} bytes of pixels) would exceed the {}-byte reply-encoding limit; write it to a path instead",
control::MAX_SCREENSHOT_PNG_REPLY_BYTES
)}));
}
Ok((_, _, _)) if self.pending_screenshots.len() >= MAX_PENDING_SCREENSHOTS => {
let _ = reply.send(json!({"ok": false, "error": format!(
"already {MAX_PENDING_SCREENSHOTS} screenshots are waiting; wait for one to finish"
)}));
}
Ok((width, height, bytes)) if self.screenshot_reserved.saturating_add(bytes) > MAX_SCREENSHOT_TOTAL_BYTES => {
let _ = reply.send(json!({
"ok": false,
"error": format!(
"screenshots already reserved {} bytes; {bytes} more ({width}×{height}) would pass the {MAX_SCREENSHOT_TOTAL_BYTES}-byte limit",
self.screenshot_reserved
)
}));
}
Ok((_, _, bytes)) => {
self.screenshot_token += 1;
let id = self.screenshot_token;
let settle = ctx.global_style().animation_time as f64 * 2000.0 + 80.0;
let due_ms = now_ms() + settle;
self.screenshot_reserved = self.screenshot_reserved.saturating_add(bytes);
self.queued_screenshots.push(QueuedScreenshot { id, due_ms, frames: 0 });
let document = self.document_identity();
self.pending_screenshots.push(PendingScreenshot {
id,
path,
data,
reply,
deadline_ms: due_ms + SCREENSHOT_TIMEOUT_MS,
reserved_bytes: bytes,
document,
slot: None,
issued: None,
attempts: 0,
});
}
Err(e) => {
let _ = reply.send(json!({"ok": false, "error": e}));
}
}
}
control::Outcome::AfterToolEdit { from, to, document } => {
self.pointer_replies.push(control::PendingPointerReply { from, to, document, reply });
}
}
}
if handled == control::MAX_CONTROL_REQUESTS_PER_FRAME {
// Leave later requests on the channel and schedule another frame to serve them.
ctx.request_repaint();
}
self.control_rx = Some(rx);
}
/// Sends the `ui.pointer` replies that wait for the tool's edit once their events have been
/// made and the edit is applied. The canvas checks on the tool each frame it is drawn; this
/// checks too, for frames without it.
pub(crate) fn answer_pointer_replies(&mut self, ctx: &egui::Context) {
// Those whose events the canvas's check made go first, before this check makes the input
// held after them.
self.send_pointer_replies();
if self.pointer_replies.is_empty() {
return;
}
if self.session.tool_editing() {
let r = self.session.tool_poll(self.view_info(), false);
canvas::apply_requests(self, r);
}
self.send_pointer_replies();
if !self.pointer_replies.is_empty() {
ctx.request_repaint_after(std::time::Duration::from_millis(16));
}
}
/// Sends the waiting `ui.pointer` replies whose events have been made.
fn send_pointer_replies(&mut self) {
for pending in std::mem::take(&mut self.pointer_replies) {
let control::PendingPointerReply { from, to, document, reply } = pending;
if self.session.input_applied(from, to) {
let _ = reply.send(control::pointer_reply_for(self, document));
} else {
self.pointer_replies.push(control::PendingPointerReply { from, to, document, reply });
}
}
}
/// The active document's identity: its process-unique uid and revision. A capture records it
/// at admission and issuance and refuses to answer with a different document or undo state.
pub(super) fn document_identity(&self) -> Option<(u64, u64)> {
self.session.active().map(|document| (document.uid, document.revision))
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.