Skip to content

ProCraft for Android: An Android native painting app written in Rust

Rust

fix(canvas): open fitted when a kept view would leave the canvas off the page

Joshf776323e4005
diff --git a/apps/procraft-android/src/lib.rs b/apps/procraft-android/src/lib.rs
index 9917e7b..30a2c43 100644
--- a/apps/procraft-android/src/lib.rs
+++ b/apps/procraft-android/src/lib.rs
@@ -849,7 +849,13 @@ impl CanvasSession {
let mut st = self.shared.lock();
st.window_size = (width.max(1), height.max(1));
if st.view.is_none() {
- let kept = st.saved_view.take().and_then(|v| v.view(st.window_size));
+ // the view kept with the artwork, unless it would leave the canvas off the page
+ let canvas = (st.doc.width(), st.doc.height());
+ let kept = st
+ .saved_view
+ .take()
+ .and_then(|v| v.view(st.window_size))
+ .filter(|v| project::shows_canvas(v, st.window_size, canvas));
st.view = Some(kept.unwrap_or_else(|| st.fitted_view()));
}
st.stop = false;
diff --git a/apps/procraft-android/src/project.rs b/apps/procraft-android/src/project.rs
index fc1c4cc..30fc02f 100644
--- a/apps/procraft-android/src/project.rs
+++ b/apps/procraft-android/src/project.rs
@@ -67,6 +67,22 @@ impl SavedView {
}
}
+/// Whether `view` shows enough of a `canvas`-sized canvas in a `window`-sized window to work on:
+/// its box on screen covers a tenth of the window, or half of it is in the window. A kept view
+/// that doesn't (the canvas pushed off the page) is not used again.
+#[cfg_attr(not(target_os = "android"), allow(dead_code))]
+pub(crate) fn shows_canvas(view: &View, window: (u32, u32), canvas: (u32, u32)) -> bool {
+ let m = view.to_screen();
+ let (cw, ch) = (canvas.0 as f32, canvas.1 as f32);
+ let corners = [Vec2::new(0.0, 0.0), Vec2::new(cw, 0.0), Vec2::new(0.0, ch), Vec2::new(cw, ch)].map(|p| m.apply(p));
+ let (x0, x1) = corners.iter().fold((f32::MAX, f32::MIN), |(a, b), p| (a.min(p.x), b.max(p.x)));
+ let (y0, y1) = corners.iter().fold((f32::MAX, f32::MIN), |(a, b), p| (a.min(p.y), b.max(p.y)));
+ let (ww, wh) = (window.0 as f32, window.1 as f32);
+ let seen = (x1.min(ww) - x0.max(0.0)).max(0.0) * (y1.min(wh) - y0.max(0.0)).max(0.0);
+ let whole = (x1 - x0) * (y1 - y0);
+ seen.is_finite() && (seen >= 0.1 * ww * wh || seen >= 0.5 * whole)
+}
+
/// An artwork's time-lapse in a package: whether it records, its video's settings (quality 0 to 3,
/// resolution 0 to 2, HEVC or H.264), and its video so far as MP4 files in order, `width` ×
/// `height` at `fps` and `bitrate` bits a second.
diff --git a/apps/procraft-android/src/tests.rs b/apps/procraft-android/src/tests.rs
index 52cf641..353c188 100644
--- a/apps/procraft-android/src/tests.rs
+++ b/apps/procraft-android/src/tests.rs
@@ -3105,3 +3105,21 @@ fn a_krita_preset_becomes_a_round_brush_with_its_settings() {
assert!(b.grain.source.is_some());
assert_eq!((b.grain.depth, b.grain.scale), (0.7, 50.0));
}
+
+#[test]
+fn a_kept_view_that_leaves_the_canvas_off_the_page_is_not_used() {
+ use crate::project::shows_canvas;
+ let (window, canvas) = ((2560, 1600), (2048, 2048));
+ // fitted: shows
+ let fitted = View::fit(canvas, window);
+ assert!(shows_canvas(&fitted, window, canvas));
+ // centred on a point far beyond the canvas: none of it shows
+ let away = View::centred(procraft_engine::geom::Vec2::new(9000.0, -7000.0), 0.78, 0.0, window).unwrap();
+ assert!(!shows_canvas(&away, window, canvas));
+ // half off the right edge: still enough to work on
+ let half = View::centred(procraft_engine::geom::Vec2::new(0.0, 1024.0), 0.78, 0.0, window).unwrap();
+ assert!(shows_canvas(&half, window, canvas));
+ // zoomed far out, small but whole on screen
+ let tiny = View::centred(procraft_engine::geom::Vec2::new(1024.0, 1024.0), 0.01, 0.0, window).unwrap();
+ assert!(shows_canvas(&tiny, window, canvas));
+}