diff --git a/render-wasm/src/main.rs b/render-wasm/src/main.rs index d224d1acfb..2d6111339c 100644 --- a/render-wasm/src/main.rs +++ b/render-wasm/src/main.rs @@ -29,7 +29,7 @@ use uuid::Uuid; pub(crate) static mut STATE: Option> = None; #[macro_export] -macro_rules! with_state { +macro_rules! with_state_mut { ($state:ident, $block:block) => {{ let $state = unsafe { #[allow(static_mut_refs)] @@ -40,14 +40,39 @@ macro_rules! with_state { }}; } +macro_rules! with_state { + ($state:ident, $block:block) => {{ + let $state = unsafe { + #[allow(static_mut_refs)] + STATE.as_ref() + } + .expect("Got an invalid state pointer"); + $block + }}; +} + #[macro_export] -macro_rules! with_current_shape { +macro_rules! with_current_shape_mut { ($state:ident, |$shape:ident: &mut Shape| $block:block) => { let $state = unsafe { #[allow(static_mut_refs)] STATE.as_mut() } .expect("Got an invalid state pointer"); + if let Some($shape) = $state.current_shape_mut() { + $block + } + }; +} + +#[macro_export] +macro_rules! with_current_shape { + ($state:ident, |$shape:ident: &Shape| $block:block) => { + let $state = unsafe { + #[allow(static_mut_refs)] + STATE.as_ref() + } + .expect("Got an invalid state pointer"); if let Some($shape) = $state.current_shape() { $block } @@ -71,15 +96,15 @@ pub extern "C" fn clean_up() { #[no_mangle] pub extern "C" fn clear_drawing_cache() { - with_state!(state, { + with_state_mut!(state, { state.rebuild_tiles(); }); } #[no_mangle] pub extern "C" fn set_render_options(debug: u32, dpr: f32) { - with_state!(state, { - let render_state = state.render_state(); + with_state_mut!(state, { + let render_state = state.render_state_mut(); render_state.set_debug_flags(debug); render_state.set_dpr(dpr); }); @@ -87,7 +112,7 @@ pub extern "C" fn set_render_options(debug: u32, dpr: f32) { #[no_mangle] pub extern "C" fn set_canvas_background(raw_color: u32) { - with_state!(state, { + with_state_mut!(state, { let color = skia::Color::new(raw_color); state.set_background_color(color); }); @@ -95,7 +120,7 @@ pub extern "C" fn set_canvas_background(raw_color: u32) { #[no_mangle] pub extern "C" fn render(_: i32) { - with_state!(state, { + with_state_mut!(state, { state .start_render_loop(performance::get_time()) .expect("Error rendering"); @@ -104,7 +129,7 @@ pub extern "C" fn render(_: i32) { #[no_mangle] pub extern "C" fn render_from_cache(_: i32) { - with_state!(state, { + with_state_mut!(state, { state.render_from_cache(); }); } @@ -112,7 +137,7 @@ pub extern "C" fn render_from_cache(_: i32) { #[no_mangle] pub extern "C" fn process_animation_frame(timestamp: i32) { let result = std::panic::catch_unwind(|| { - with_state!(state, { + with_state_mut!(state, { state .process_animation_frame(timestamp) .expect("Error processing animation frame"); @@ -133,24 +158,24 @@ pub extern "C" fn process_animation_frame(timestamp: i32) { #[no_mangle] pub extern "C" fn reset_canvas() { - with_state!(state, { - state.render_state().reset_canvas(); + with_state_mut!(state, { + state.render_state_mut().reset_canvas(); }); } #[no_mangle] pub extern "C" fn resize_viewbox(width: i32, height: i32) { - with_state!(state, { + with_state_mut!(state, { state.resize(width, height); }); } #[no_mangle] pub extern "C" fn set_view(zoom: f32, x: f32, y: f32) { - with_state!(state, { - let render_state = state.render_state(); + with_state_mut!(state, { + let render_state = state.render_state_mut(); render_state.viewbox.set_all(zoom, x, y); - with_state!(state, { + with_state_mut!(state, { // We can have renders in progress state.render_state.cancel_animation_frame(); if state.render_state.options.is_profile_rebuild_tiles() { @@ -164,7 +189,7 @@ pub extern "C" fn set_view(zoom: f32, x: f32, y: f32) { #[no_mangle] pub extern "C" fn clear_focus_mode() { - with_state!(state, { + with_state_mut!(state, { state.clear_focus_mode(); }); } @@ -178,21 +203,21 @@ pub extern "C" fn set_focus_mode() { .map(|data| Uuid::from_bytes(data.try_into().unwrap())) .collect(); - with_state!(state, { + with_state_mut!(state, { state.set_focus_mode(entries); }); } #[no_mangle] pub extern "C" fn init_shapes_pool(capacity: usize) { - with_state!(state, { + with_state_mut!(state, { state.init_shapes_pool(capacity); }); } #[no_mangle] pub extern "C" fn use_shape(a: u32, b: u32, c: u32, d: u32) { - with_state!(state, { + with_state_mut!(state, { let id = uuid_from_u32_quartet(a, b, c, d); state.use_shape(id); }); @@ -200,7 +225,7 @@ pub extern "C" fn use_shape(a: u32, b: u32, c: u32, d: u32) { #[no_mangle] pub extern "C" fn set_parent(a: u32, b: u32, c: u32, d: u32) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { let id = uuid_from_u32_quartet(a, b, c, d); shape.set_parent(id); }); @@ -208,56 +233,56 @@ pub extern "C" fn set_parent(a: u32, b: u32, c: u32, d: u32) { #[no_mangle] pub extern "C" fn set_shape_masked_group(masked: bool) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_masked(masked); }); } #[no_mangle] pub extern "C" fn set_shape_bool_type(raw_bool_type: u8) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_bool_type(BoolType::from(raw_bool_type)); }); } #[no_mangle] pub extern "C" fn set_shape_type(shape_type: u8) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_shape_type(Type::from(shape_type)); }); } #[no_mangle] pub extern "C" fn set_shape_selrect(left: f32, top: f32, right: f32, bottom: f32) { - with_state!(state, { + with_state_mut!(state, { state.set_selrect_for_current_shape(left, top, right, bottom); }); } #[no_mangle] pub extern "C" fn set_shape_clip_content(clip_content: bool) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_clip(clip_content); }); } #[no_mangle] pub extern "C" fn set_shape_rotation(rotation: f32) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_rotation(rotation); }); } #[no_mangle] pub extern "C" fn set_shape_transform(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_transform(a, b, c, d, e, f); }); } #[no_mangle] pub extern "C" fn add_shape_child(a: u32, b: u32, c: u32, d: u32) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { let id = uuid_from_u32_quartet(a, b, c, d); shape.add_child(id); }); @@ -274,12 +299,12 @@ pub extern "C" fn set_children() { let mut deleted = IndexSet::new(); - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { (_, deleted) = shape.compute_children_differences(&entries); shape.children = entries.clone(); }); - with_state!(state, { + with_state_mut!(state, { for id in deleted { state.delete_shape(id); } @@ -301,18 +326,18 @@ pub extern "C" fn store_image( c2: u32, d2: u32, ) { - with_state!(state, { + with_state_mut!(state, { let image_id = uuid_from_u32_quartet(a2, b2, c2, d2); let image_bytes = mem::bytes(); - if let Err(msg) = state.render_state().add_image(image_id, &image_bytes) { + if let Err(msg) = state.render_state_mut().add_image(image_id, &image_bytes) { eprintln!("{}", msg); } mem::free_bytes(); }); - with_state!(state, { + with_state_mut!(state, { let shape_id = uuid_from_u32_quartet(a1, b1, c1, d1); state.update_tile_for_shape(shape_id); }); @@ -320,7 +345,7 @@ pub extern "C" fn store_image( #[no_mangle] pub extern "C" fn is_image_cached(a: u32, b: u32, c: u32, d: u32) -> bool { - with_state!(state, { + with_state_mut!(state, { let id = uuid_from_u32_quartet(a, b, c, d); state.render_state().has_image(&id) }) @@ -328,7 +353,7 @@ pub extern "C" fn is_image_cached(a: u32, b: u32, c: u32, d: u32) -> bool { #[no_mangle] pub extern "C" fn set_shape_svg_raw_content() { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { let bytes = mem::bytes(); let svg_raw_content = String::from_utf8(bytes) .unwrap() @@ -342,56 +367,56 @@ pub extern "C" fn set_shape_svg_raw_content() { #[no_mangle] pub extern "C" fn set_shape_blend_mode(mode: i32) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_blend_mode(render::BlendMode::from(mode)); }); } #[no_mangle] pub extern "C" fn set_shape_vertical_align(align: u8) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_vertical_align(VerticalAlign::from(align)); }); } #[no_mangle] pub extern "C" fn set_shape_opacity(opacity: f32) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_opacity(opacity); }); } #[no_mangle] pub extern "C" fn set_shape_constraint_h(constraint: u8) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_constraint_h(ConstraintH::from(constraint)); }); } #[no_mangle] pub extern "C" fn set_shape_constraint_v(constraint: u8) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_constraint_v(ConstraintV::from(constraint)); }); } #[no_mangle] pub extern "C" fn set_shape_hidden(hidden: bool) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_hidden(hidden); }); } #[no_mangle] pub extern "C" fn set_shape_blur(blur_type: u8, hidden: bool, value: f32) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_blur(blur_type, hidden, value); }); } #[no_mangle] pub extern "C" fn set_shape_corners(r1: f32, r2: f32, r3: f32, r4: f32) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_corners((r1, r2, r3, r4)); }); } @@ -427,7 +452,7 @@ pub extern "C" fn get_selection_rect() -> *mut u8 { }) .collect(); - with_state!(state, { + with_state_mut!(state, { let bbs: Vec<_> = entries .iter() .flat_map(|id| { @@ -472,7 +497,7 @@ pub extern "C" fn set_structure_modifiers() { .map(|data| StructureEntry::from_bytes(data.try_into().unwrap())) .collect(); - with_state!(state, { + with_state_mut!(state, { for entry in entries { match entry.entry_type { StructureEntryType::ScaleContent => { @@ -500,7 +525,7 @@ pub extern "C" fn set_structure_modifiers() { #[no_mangle] pub extern "C" fn clean_modifiers() { - with_state!(state, { + with_state_mut!(state, { state.structure.clear(); state.scale_content.clear(); state.modifiers.clear(); @@ -516,7 +541,7 @@ pub extern "C" fn set_modifiers() { .map(|data| TransformEntry::from_bytes(data.try_into().unwrap())) .collect(); - with_state!(state, { + with_state_mut!(state, { for entry in entries { state.modifiers.insert(entry.id, entry.transform); } @@ -534,7 +559,7 @@ pub extern "C" fn add_shape_shadow( raw_style: u8, hidden: bool, ) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { let color = skia::Color::new(raw_color); let style = shapes::ShadowStyle::from(raw_style); let shadow = shapes::Shadow::new(color, blur, spread, (x, y), style, hidden); @@ -544,14 +569,14 @@ pub extern "C" fn add_shape_shadow( #[no_mangle] pub extern "C" fn clear_shape_shadows() { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.clear_shadows(); }); } #[no_mangle] pub extern "C" fn update_shape_tiles() { - with_state!(state, { + with_state_mut!(state, { state.update_tile_for_current_shape(); }); } @@ -578,7 +603,7 @@ pub extern "C" fn set_flex_layout_data( let justify_content = shapes::JustifyContent::from_u8(justify_content); let wrap_type = shapes::WrapType::from_u8(wrap_type); - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_flex_layout_data( dir, row_gap, @@ -629,7 +654,7 @@ pub extern "C" fn set_layout_child_data( None }; - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_flex_layout_child_data( margin_top, margin_right, @@ -668,7 +693,7 @@ pub extern "C" fn set_grid_layout_data( let justify_items = shapes::JustifyItems::from_u8(justify_items); let justify_content = shapes::JustifyContent::from_u8(justify_content); - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_grid_layout_data( dir, row_gap, @@ -694,7 +719,7 @@ pub extern "C" fn set_grid_columns() { .map(|data| shapes::RawGridTrack::from_bytes(data.try_into().unwrap())) .collect(); - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_grid_columns(entries); }); @@ -710,7 +735,7 @@ pub extern "C" fn set_grid_rows() { .map(|data| shapes::RawGridTrack::from_bytes(data.try_into().unwrap())) .collect(); - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_grid_rows(entries); }); @@ -726,7 +751,7 @@ pub extern "C" fn set_grid_cells() { .map(|data| shapes::RawGridCell::from_bytes(data.try_into().unwrap())) .collect(); - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.set_grid_cells(entries); }); @@ -735,7 +760,7 @@ pub extern "C" fn set_grid_cells() { #[no_mangle] pub extern "C" fn show_grid(a: u32, b: u32, c: u32, d: u32) { - with_state!(state, { + with_state_mut!(state, { let id = uuid_from_u32_quartet(a, b, c, d); state.render_state.show_grid = Some(id); }); @@ -743,7 +768,7 @@ pub extern "C" fn show_grid(a: u32, b: u32, c: u32, d: u32) { #[no_mangle] pub extern "C" fn hide_grid() { - with_state!(state, { + with_state_mut!(state, { state.render_state.show_grid = None; }); } diff --git a/render-wasm/src/render.rs b/render-wasm/src/render.rs index d730243780..fa544e60b8 100644 --- a/render-wasm/src/render.rs +++ b/render-wasm/src/render.rs @@ -260,7 +260,7 @@ impl RenderState { self.images.add(id, image_data) } - pub fn has_image(&mut self, id: &Uuid) -> bool { + pub fn has_image(&self, id: &Uuid) -> bool { self.images.contains(id) } @@ -635,7 +635,7 @@ impl RenderState { pub fn start_render_loop( &mut self, - tree: &mut HashMap, + tree: &HashMap, modifiers: &HashMap, structure: &HashMap>, scale_content: &HashMap, @@ -690,7 +690,7 @@ impl RenderState { pub fn process_animation_frame( &mut self, - tree: &mut HashMap, + tree: &HashMap, modifiers: &HashMap, structure: &HashMap>, scale_content: &HashMap, @@ -719,7 +719,7 @@ impl RenderState { } #[inline] - pub fn render_shape_enter(&mut self, element: &mut Shape, mask: bool) { + pub fn render_shape_enter(&mut self, element: &Shape, mask: bool) { // Masked groups needs two rendering passes, the first one rendering // the content and the second one rendering the mask so we need to do // an extra save_layer to keep all the masked group separate from @@ -765,7 +765,7 @@ impl RenderState { } #[inline] - pub fn render_shape_exit(&mut self, element: &mut Shape, visited_mask: bool) { + pub fn render_shape_exit(&mut self, element: &Shape, visited_mask: bool) { if visited_mask { // Because masked groups needs two rendering passes (first drawing // the content and then drawing the mask), we need to do an @@ -849,7 +849,7 @@ impl RenderState { pub fn render_shape_tree_partial_uncached( &mut self, - tree: &mut HashMap, + tree: &HashMap, modifiers: &HashMap, structure: &HashMap>, scale_content: &HashMap, @@ -867,7 +867,7 @@ impl RenderState { } = node_render_state; is_empty = false; - let element = tree.get_mut(&node_id).ok_or( + let element = tree.get(&node_id).ok_or( "Error: Element with root_id {node_render_state.id} not found in the tree." .to_string(), )?; @@ -962,7 +962,7 @@ impl RenderState { pub fn render_shape_tree_partial( &mut self, - tree: &mut HashMap, + tree: &HashMap, modifiers: &HashMap, structure: &HashMap>, scale_content: &HashMap, diff --git a/render-wasm/src/render/grid_layout.rs b/render-wasm/src/render/grid_layout.rs index c1c6fae987..0ec9f5ffe4 100644 --- a/render-wasm/src/render/grid_layout.rs +++ b/render-wasm/src/render/grid_layout.rs @@ -14,7 +14,7 @@ pub fn render_overlay( modifiers: &HashMap, structure: &HashMap>, ) { - let cells = grid_cell_data(shape.clone(), shapes, modifiers, structure, true); + let cells = grid_cell_data(shape, shapes, modifiers, structure, true); let mut paint = skia::Paint::default(); paint.set_style(skia::PaintStyle::Stroke); diff --git a/render-wasm/src/shapes/modifiers/grid_layout.rs b/render-wasm/src/shapes/modifiers/grid_layout.rs index 3dcd18f6b6..a59fefdeed 100644 --- a/render-wasm/src/shapes/modifiers/grid_layout.rs +++ b/render-wasm/src/shapes/modifiers/grid_layout.rs @@ -601,7 +601,7 @@ pub fn create_cell_data<'a>( } pub fn grid_cell_data<'a>( - shape: Shape, + shape: &Shape, shapes: &'a HashMap, modifiers: &HashMap, structure: &HashMap>, diff --git a/render-wasm/src/shapes/text_paths.rs b/render-wasm/src/shapes/text_paths.rs index ca02a3e8d0..0f08921070 100644 --- a/render-wasm/src/shapes/text_paths.rs +++ b/render-wasm/src/shapes/text_paths.rs @@ -171,7 +171,7 @@ impl TextPaths { blob_offset_x: f32, blob_offset_y: f32, ) -> Option<(skia::Path, skia::Rect)> { - with_state!(state, { + with_state_mut!(state, { let utf16_text = leaf_text.encode_utf16().collect::>(); let text = unsafe { skia_safe::as_utf16_unchecked(&utf16_text) }; let emoji_font = state.render_state.fonts().get_emoji_font(font.size()); @@ -195,4 +195,4 @@ impl Deref for TextPaths { fn deref(&self) -> &Self::Target { &self.0 } -} \ No newline at end of file +} diff --git a/render-wasm/src/state.rs b/render-wasm/src/state.rs index 9514950de8..f3c3956e67 100644 --- a/render-wasm/src/state.rs +++ b/render-wasm/src/state.rs @@ -106,10 +106,14 @@ impl<'a> State<'a> { self.render_state.resize(width, height); } - pub fn render_state(&'a mut self) -> &'a mut RenderState { + pub fn render_state_mut(&'a mut self) -> &'a mut RenderState { &mut self.render_state } + pub fn render_state(&'a self) -> &'a RenderState { + &self.render_state + } + pub fn render_from_cache(&mut self) { self.render_state .render_from_cache(&self.shapes, &self.modifiers, &self.structure); @@ -117,7 +121,7 @@ impl<'a> State<'a> { pub fn start_render_loop(&mut self, timestamp: i32) -> Result<(), String> { self.render_state.start_render_loop( - &mut self.shapes, + &self.shapes, &self.modifiers, &self.structure, &self.scale_content, @@ -128,7 +132,7 @@ impl<'a> State<'a> { pub fn process_animation_frame(&mut self, timestamp: i32) -> Result<(), String> { self.render_state.process_animation_frame( - &mut self.shapes, + &self.shapes, &self.modifiers, &self.structure, &self.scale_content, @@ -172,43 +176,42 @@ impl<'a> State<'a> { } } - pub fn current_shape(&mut self) -> Option<&mut Shape> { + pub fn current_shape_mut(&mut self) -> Option<&mut Shape> { self.current_shape.as_deref_mut() } + pub fn current_shape(&self) -> Option<&Shape> { + self.current_shape.as_deref() + } + pub fn set_background_color(&mut self, color: skia::Color) { self.render_state.set_background_color(color); } pub fn set_selrect_for_current_shape(&mut self, left: f32, top: f32, right: f32, bottom: f32) { - match self.current_shape.as_mut() { - Some(shape) => { - shape.set_selrect(left, top, right, bottom); - // We don't need to update the tile for the root shape. - if !shape.id.is_nil() { - self.render_state.update_tile_for(shape); - } - } - None => panic!("Invalid current shape"), + let Some(shape) = self.current_shape.as_deref_mut() else { + panic!("Invalid current shape") + }; + + shape.set_selrect(left, top, right, bottom); + // We don't need to update the tile for the root shape. + if !shape.id.is_nil() { + self.render_state.update_tile_for(shape); } } pub fn update_tile_for_shape(&mut self, shape_id: Uuid) { - if let Some(shape) = self.shapes.get_mut(&shape_id) { + if let Some(shape) = self.shapes.get(&shape_id) { self.render_state.update_tile_for(shape); } } pub fn update_tile_for_current_shape(&mut self) { - match self.current_shape.as_mut() { - Some(shape) => { - // We don't need to update the tile for the root shape. - // We can also have deleted the selected shape - if !shape.id.is_nil() && self.shapes.contains_key(&shape.id) { - self.render_state.update_tile_for(shape); - } - } - None => panic!("Invalid current shape"), + let Some(shape) = self.current_shape.as_deref() else { + panic!("Invalid current shape") + }; + if !shape.id.is_nil() && self.shapes.contains_key(&shape.id) { + self.render_state.update_tile_for(shape); } } @@ -227,7 +230,7 @@ impl<'a> State<'a> { .rebuild_modifier_tiles(&mut self.shapes, &self.modifiers); } - pub fn get_grid_coords(&mut self, pos_x: f32, pos_y: f32) -> (i32, i32) { + pub fn get_grid_coords(&self, pos_x: f32, pos_y: f32) -> (i32, i32) { let Some(shape) = self.current_shape() else { return (-1, -1); }; @@ -235,13 +238,7 @@ impl<'a> State<'a> { let bounds = shape.bounds(); let position = Point::new(pos_x, pos_y); - let cells = grid_cell_data( - shape.clone(), - &self.shapes, - &self.modifiers, - &self.structure, - true, - ); + let cells = grid_cell_data(shape, &self.shapes, &self.modifiers, &self.structure, true); for cell in cells { let points = &[ diff --git a/render-wasm/src/utils.rs b/render-wasm/src/utils.rs index 62d3a504f4..c0d0fc4a54 100644 --- a/render-wasm/src/utils.rs +++ b/render-wasm/src/utils.rs @@ -1,6 +1,6 @@ use crate::skia::Image; use crate::uuid::Uuid; -use crate::with_state; +use crate::with_state_mut; use crate::STATE; use std::collections::HashSet; @@ -24,10 +24,10 @@ pub fn uuid_from_u32(id: [u32; 4]) -> Uuid { } pub fn get_image(image_id: &Uuid) -> Option<&Image> { - with_state!(state, { state.render_state().images.get(image_id) }) + with_state_mut!(state, { state.render_state_mut().images.get(image_id) }) } // FIXME: move to a different place ? pub fn get_fallback_fonts() -> &'static HashSet { - with_state!(state, { state.render_state().fonts().get_fallback() }) + with_state_mut!(state, { state.render_state().fonts().get_fallback() }) } diff --git a/render-wasm/src/wasm/fills.rs b/render-wasm/src/wasm/fills.rs index 9b8d2588d1..29c0d573d6 100644 --- a/render-wasm/src/wasm/fills.rs +++ b/render-wasm/src/wasm/fills.rs @@ -4,7 +4,7 @@ mod solid; use crate::mem; use crate::shapes; -use crate::with_current_shape; +use crate::with_current_shape_mut; use crate::STATE; const RAW_FILL_DATA_SIZE: usize = std::mem::size_of::(); @@ -65,7 +65,7 @@ pub fn parse_fills_from_bytes(buffer: &[u8], num_fills: usize) -> Vec bool { - with_state!(state, { + with_state_mut!(state, { let id = uuid_from_u32_quartet(a, b, c, d); let family = FontFamily::new(id, weight, style.into()); let res = state.render_state().fonts().has_family(&family, is_emoji); diff --git a/render-wasm/src/wasm/paths.rs b/render-wasm/src/wasm/paths.rs index 6f8b4c724c..1aaedcf149 100644 --- a/render-wasm/src/wasm/paths.rs +++ b/render-wasm/src/wasm/paths.rs @@ -1,5 +1,5 @@ use crate::shapes::{Path, Segment}; -use crate::{mem, with_current_shape, STATE}; +use crate::{mem, with_current_shape_mut, STATE}; const RAW_SEGMENT_DATA_SIZE: usize = size_of::(); @@ -79,7 +79,7 @@ impl From> for Path { #[no_mangle] pub extern "C" fn set_shape_path_content() { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { let bytes = mem::bytes(); let segments = bytes @@ -112,7 +112,7 @@ fn extract_string(start: &mut usize, bytes: &[u8]) -> String { #[no_mangle] pub extern "C" fn set_shape_path_attrs(num_attrs: u32) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { let bytes = mem::bytes(); let mut start = 0; for _ in 0..num_attrs { diff --git a/render-wasm/src/wasm/strokes.rs b/render-wasm/src/wasm/strokes.rs index 4f9905daac..4e64fedad6 100644 --- a/render-wasm/src/wasm/strokes.rs +++ b/render-wasm/src/wasm/strokes.rs @@ -1,11 +1,11 @@ use crate::mem; use crate::shapes; -use crate::with_current_shape; +use crate::with_current_shape_mut; use crate::STATE; #[no_mangle] pub extern "C" fn add_shape_center_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.add_stroke(shapes::Stroke::new_center_stroke( width, style, cap_start, cap_end, )); @@ -14,7 +14,7 @@ pub extern "C" fn add_shape_center_stroke(width: f32, style: u8, cap_start: u8, #[no_mangle] pub extern "C" fn add_shape_inner_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.add_stroke(shapes::Stroke::new_inner_stroke( width, style, cap_start, cap_end, )); @@ -23,7 +23,7 @@ pub extern "C" fn add_shape_inner_stroke(width: f32, style: u8, cap_start: u8, c #[no_mangle] pub extern "C" fn add_shape_outer_stroke(width: f32, style: u8, cap_start: u8, cap_end: u8) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.add_stroke(shapes::Stroke::new_outer_stroke( width, style, cap_start, cap_end, )); @@ -32,7 +32,7 @@ pub extern "C" fn add_shape_outer_stroke(width: f32, style: u8, cap_start: u8, c #[no_mangle] pub extern "C" fn add_shape_stroke_fill() { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { let bytes = mem::bytes(); let raw_fill = super::fills::RawFillData::try_from(&bytes[..]).expect("Invalid fill data"); shape @@ -43,7 +43,7 @@ pub extern "C" fn add_shape_stroke_fill() { #[no_mangle] pub extern "C" fn clear_shape_strokes() { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.clear_strokes(); }); } diff --git a/render-wasm/src/wasm/text.rs b/render-wasm/src/wasm/text.rs index a7b0b2c078..74f9dd7795 100644 --- a/render-wasm/src/wasm/text.rs +++ b/render-wasm/src/wasm/text.rs @@ -2,11 +2,11 @@ use crate::mem; use crate::shapes::{auto_height, auto_width, max_width, GrowType, RawTextData, Type}; use crate::STATE; -use crate::{with_current_shape, with_state}; +use crate::{with_current_shape, with_current_shape_mut, with_state_mut}; #[no_mangle] pub extern "C" fn clear_shape_text() { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { shape.clear_text(); }); } @@ -14,7 +14,7 @@ pub extern "C" fn clear_shape_text() { #[no_mangle] pub extern "C" fn set_shape_text_content() { let bytes = mem::bytes(); - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { let raw_text_data = RawTextData::from(&bytes); shape .add_paragraph(raw_text_data.paragraph) @@ -26,7 +26,7 @@ pub extern "C" fn set_shape_text_content() { #[no_mangle] pub extern "C" fn set_shape_grow_type(grow_type: u8) { - with_current_shape!(state, |shape: &mut Shape| { + with_current_shape_mut!(state, |shape: &mut Shape| { if let Type::Text(text_content) = &mut shape.shape_type { text_content.set_grow_type(GrowType::from(grow_type)); } @@ -36,14 +36,15 @@ pub extern "C" fn set_shape_grow_type(grow_type: u8) { #[no_mangle] pub extern "C" fn get_text_dimensions() -> *mut u8 { let font_col; - with_state!(state, { + with_state_mut!(state, { font_col = state.render_state.fonts.font_collection(); }); let mut width = 0.01; let mut height = 0.01; let mut m_width = 0.01; - with_current_shape!(state, |shape: &mut Shape| { + + with_current_shape!(state, |shape: &Shape| { width = shape.selrect.width(); height = shape.selrect.height();