mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-24 18:32:23 +00:00
Canvas (web): edits once the view is still don't put off its render
still_wait started its STILL_SECS wait again whenever the cache key asked for changed, and the key holds the document's revision. After a heavy document's view moved and stopped while a drag went on editing it, each edit was drawn into the frame shown and restarted the wait, so the canvas never rendered whole for the view while the edits went on. The wait now starts again only when the document or the view (zoom, centre, size, pixel density, rotation, Pixel Preview's pixels) changes. The checks that render at once when more than the view differs from the frame shown are unchanged. Refs #8688
This commit is contained in:
@@ -25,8 +25,8 @@ pub struct CanvasCache {
|
||||
/// last render took more than 8 ms, the canvas draws the frame shown reprojected and renders
|
||||
/// once the view has been still for [`canvas::STILL_SECS`].
|
||||
pub waits_for_still: bool,
|
||||
/// The cache key the canvas last asked for and when it first asked for it (seconds, egui's
|
||||
/// input time), kept while [`Self::waits_for_still`] is set.
|
||||
/// The cache key the canvas last asked for and when it first asked for its view (seconds,
|
||||
/// egui's input time), kept while [`Self::waits_for_still`] is set.
|
||||
pub still: Option<(CacheKey, f64)>,
|
||||
/// The low-resolution frame that covers the parts of the canvas the frame shown leaves
|
||||
/// uncovered while it is drawn for a view it was not rendered for ([`canvas::Fill`]).
|
||||
|
||||
@@ -51,11 +51,13 @@ pub(super) fn hover_wait(moved: &mut Option<(ViewKey, f64)>, key: ViewKey, now:
|
||||
|
||||
/// Note that the canvas asks for `key` at time `now` (seconds, egui's input time) → how many more
|
||||
/// seconds it draws the frame `shown` reprojected instead of rendering `key`: while the last
|
||||
/// render was `slow`, only the view differs between the two, and the canvas has asked for `key`
|
||||
/// for less than [`STILL_SECS`]. `still` holds the key last asked for and when the canvas first
|
||||
/// asked for it. None: the canvas renders `key` now, unless it is the frame shown.
|
||||
/// render was `slow`, only the view differs between the two, and the canvas has asked for the view
|
||||
/// of `key` for less than [`STILL_SECS`]. `still` holds the key last asked for and when the canvas first
|
||||
/// asked for its view. An edit leaves that time as it is: with the view still and edits drawn into
|
||||
/// the frame shown at each step of a drag, the canvas still renders whole once the view has been
|
||||
/// still for [`STILL_SECS`]. None: the canvas renders `key` now, unless it is the frame shown.
|
||||
pub(super) fn still_wait(still: &mut Option<(CacheKey, f64)>, shown: Option<&CacheKey>, key: &CacheKey, slow: bool, now: f64) -> Option<f64> {
|
||||
if still.as_ref().is_none_or(|(k, _)| k != key) {
|
||||
if still.as_ref().is_none_or(|(k, _)| !same_view(k, key)) {
|
||||
*still = Some((key.clone(), now));
|
||||
}
|
||||
let waited = still.as_ref().map_or(0.0, |(_, at)| now - at);
|
||||
@@ -64,6 +66,12 @@ pub(super) fn still_wait(still: &mut Option<(CacheKey, f64)>, shown: Option<&Cac
|
||||
(slow && waiting && shown.is_some_and(|s| s != key && only_view_differs(s, key))).then_some(STILL_SECS - waited)
|
||||
}
|
||||
|
||||
/// Whether two cache keys are of the same document at the same view (zoom, center, rotation,
|
||||
/// canvas size, pixel density and Pixel Preview's document pixels), whatever else differs.
|
||||
fn same_view(a: &CacheKey, b: &CacheKey) -> bool {
|
||||
(a.doc, a.zoom, a.cx, a.cy, a.w, a.h, a.ppp, a.rot, a.pixel) == (b.doc, b.zoom, b.cx, b.cy, b.w, b.h, b.ppp, b.rot, b.pixel)
|
||||
}
|
||||
|
||||
/// Whether two cache keys differ only in the view (zoom, center, rotation, canvas size, pixel
|
||||
/// density and Pixel Preview's document pixels), with Pixel Preview on in both or off in both.
|
||||
pub(super) fn only_view_differs(a: &CacheKey, b: &CacheKey) -> bool {
|
||||
|
||||
@@ -4464,6 +4464,48 @@ fn without_a_worker_a_heavy_canvas() {
|
||||
assert_eq!(app.canvas.key.as_ref().map(|k| k.cx), Some(app.view().unwrap().center.x), "rendered at once");
|
||||
}
|
||||
|
||||
/// With the web build's setting (`waits_for_still`, no render worker), a heavy document's view
|
||||
/// that stops moving while a drag goes on editing the document renders whole once the view has
|
||||
/// been still for STILL_SECS: each edit is drawn into the frame shown and does not start the wait
|
||||
/// again.
|
||||
#[test]
|
||||
fn without_a_worker_edits_while_the_view_is_still_do_not_hold_off_the_render() {
|
||||
keeping_color_settings(edits_while_the_view_is_still);
|
||||
}
|
||||
|
||||
fn edits_while_the_view_is_still() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
app.session.execute("shape.rectangle", &json!({"x": 100, "y": 50, "width": 200, "height": 200})).unwrap();
|
||||
app.canvas.worker_started = true; // no worker, as in the web build
|
||||
app.canvas.waits_for_still = true;
|
||||
let ctx = egui::Context::default();
|
||||
let frame = |app: &mut VectorcraftApp| {
|
||||
let raw = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0))), ..Default::default() };
|
||||
let mut out = ctx.run_ui(raw, |ui| show(app, ui));
|
||||
out.textures_delta.clear();
|
||||
};
|
||||
frame(&mut app); // the first frame fits the view to the artboard
|
||||
frame(&mut app);
|
||||
app.canvas.last_ms = 50.0;
|
||||
// The view moves once (a drag at an edge of the canvas scrolls it) and then stays still.
|
||||
app.view_mut().unwrap().center.y += 10.0;
|
||||
frame(&mut app);
|
||||
let shown = app.canvas.key.clone().unwrap();
|
||||
assert_ne!(shown.cy, app.view().unwrap().center.y, "waiting");
|
||||
// The drag edits the document on every frame, at 60 fps (egui's input time without a clock).
|
||||
let mut frames = 0;
|
||||
while app.canvas.key.as_ref().is_none_or(|k| k.cy != app.view().unwrap().center.y) && frames < 60 {
|
||||
frames += 1;
|
||||
let x = 110 + (frames % 10) * 5;
|
||||
app.session.execute("shape.rectangle", &json!({"x": x, "y": 60, "width": 4, "height": 4})).unwrap();
|
||||
frame(&mut app);
|
||||
let k = app.canvas.key.as_ref().unwrap();
|
||||
assert_eq!(k.revision, app.session.active().unwrap().revision, "each edit is drawn");
|
||||
}
|
||||
assert!((11..=13).contains(&frames), "rendered whole about STILL_SECS after the view stopped: {frames} frames");
|
||||
}
|
||||
|
||||
/// [`still_wait`] waits while only the view changes on a slow canvas: each change starts
|
||||
/// STILL_SECS again. It does not wait for a light document, a still view, a clock that went
|
||||
/// back, an edit, a switch into Pixel Preview or a canvas with no frame shown.
|
||||
|
||||
+2
-2
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user