From e1d9efea7f0c6e748fe2589d869b37b6c54b8f80 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Mon, 9 Dec 2024 15:19:14 +0100 Subject: [PATCH] :tada: Suport for show-content in render wasm --- frontend/src/app/render_wasm/api.cljs | 32 +++++++++++++++---------- frontend/src/app/render_wasm/shape.cljs | 19 ++++++++------- render-wasm/src/main.rs | 10 +++++++- render-wasm/src/render.rs | 7 ++++++ render-wasm/src/shapes.rs | 2 ++ 5 files changed, 47 insertions(+), 23 deletions(-) diff --git a/frontend/src/app/render_wasm/api.cljs b/frontend/src/app/render_wasm/api.cljs index acac75437..f8b3339a5 100644 --- a/frontend/src/app/render_wasm/api.cljs +++ b/frontend/src/app/render_wasm/api.cljs @@ -80,6 +80,10 @@ (aget buffer 2) (aget buffer 3)))) +(defn set-shape-clip-content + [clip-content] + (h/call internal-module "_set_shape_clip_content" clip-content)) + (defn set-shape-selrect [selrect] (h/call internal-module "_set_shape_selrect" @@ -247,21 +251,23 @@ pending (loop [index 0 pending []] (if (< index total-shapes) - (let [shape (nth shapes index) - type (dm/get-prop shape :type) - id (dm/get-prop shape :id) - selrect (dm/get-prop shape :selrect) - rotation (dm/get-prop shape :rotation) - transform (dm/get-prop shape :transform) - fills (if (= type :group) - [] (dm/get-prop shape :fills)) - children (dm/get-prop shape :shapes) - blend-mode (dm/get-prop shape :blend-mode) - opacity (dm/get-prop shape :opacity) - hidden (dm/get-prop shape :hidden) - content (dm/get-prop shape :content)] + (let [shape (nth shapes index) + type (dm/get-prop shape :type) + id (dm/get-prop shape :id) + clip-content (not (dm/get-prop shape :show-content)) + selrect (dm/get-prop shape :selrect) + rotation (dm/get-prop shape :rotation) + transform (dm/get-prop shape :transform) + fills (if (= type :group) + [] (dm/get-prop shape :fills)) + children (dm/get-prop shape :shapes) + blend-mode (dm/get-prop shape :blend-mode) + opacity (dm/get-prop shape :opacity) + hidden (dm/get-prop shape :hidden) + content (dm/get-prop shape :content)] (use-shape id) + (set-shape-clip-content clip-content) (set-shape-selrect selrect) (set-shape-rotation rotation) (set-shape-transform transform) diff --git a/frontend/src/app/render_wasm/shape.cljs b/frontend/src/app/render_wasm/shape.cljs index 2eb61b6db..65c48da3f 100644 --- a/frontend/src/app/render_wasm/shape.cljs +++ b/frontend/src/app/render_wasm/shape.cljs @@ -111,15 +111,16 @@ (when ^boolean shape/*wasm-sync* (api/use-shape (:id self)) (case k - :selrect (api/set-shape-selrect v) - :rotation (api/set-shape-rotation v) - :transform (api/set-shape-transform v) - :fills (api/set-shape-fills v) - :blend-mode (api/set-shape-blend-mode v) - :opacity (api/set-shape-opacity v) - :hidden (api/set-shape-hidden v) - :shapes (api/set-shape-children v) - :content (api/set-shape-path-content v) + :selrect (api/set-shape-selrect v) + :show-content (api/set-shape-clip-content (not v)) + :rotation (api/set-shape-rotation v) + :transform (api/set-shape-transform v) + :fills (api/set-shape-fills v) + :blend-mode (api/set-shape-blend-mode v) + :opacity (api/set-shape-opacity v) + :hidden (api/set-shape-hidden v) + :shapes (api/set-shape-children v) + :content (api/set-shape-path-content v) nil) ;; when something synced with wasm ;; is modified, we need to request diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index c316961a3..53ce2c541 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -111,7 +111,15 @@ pub extern "C" fn set_shape_selrect(left: f32, top: f32, right: f32, bottom: f32 } #[no_mangle] -pub extern "C" fn set_shape_rotation(rotation: f32) { +pub unsafe extern "C" fn set_shape_clip_content(clip_content: bool) { + let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); + if let Some(shape) = state.current_shape() { + shape.clip_content = clip_content; + } +} + +#[no_mangle] +pub unsafe extern "C" fn set_shape_rotation(rotation: f32) { let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer"); if let Some(shape) = state.current_shape() { shape.rotation = rotation; diff --git a/render-wasm/src/render.rs b/render-wasm/src/render.rs index 74639741f..a7668a98a 100644 --- a/render-wasm/src/render.rs +++ b/render-wasm/src/render.rs @@ -413,6 +413,13 @@ impl RenderState { if !id.is_nil() { self.render_single_shape(shape); + if shape.clip_content { + self.drawing_surface.canvas().clip_rect( + shape.selrect, + skia::ClipOp::Intersect, + true, + ); + } } // draw all the children shapes diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index 273908511..5770e4a13 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -51,6 +51,7 @@ pub struct Shape { pub selrect: math::Rect, pub transform: Matrix, pub rotation: f32, + pub clip_content: bool, fills: Vec, pub blend_mode: BlendMode, pub opacity: f32, @@ -66,6 +67,7 @@ impl Shape { selrect: math::Rect::new_empty(), transform: Matrix::identity(), rotation: 0., + clip_content: true, fills: vec![], blend_mode: BlendMode::default(), opacity: 1.,