Repository: penpot/penpot
File: render-wasm/src/state.rs (Rust)

Code before:
use std::collections::HashMap;

mod shapes_pool;
mod text_editor;
pub use shapes_pool::{ShapesPool, ShapesPoolMutRef, ShapesPoolRef};
pub use text_editor::*;

use crate::render::RenderState;
use crate::shapes::Shape;
use crate::tiles;
use crate::uuid::Uuid;

use crate::shapes::modifiers::grid_layout::grid_cell_data;

/// This struct holds the state of the Rust application between JS calls.
///
/// It is created by [init] and passed to the other exported functions.
/// Note that rust-skia data structures are not thread safe, so a state
/// must not be shared between different Web Workers.
pub(crate) struct State<'a> {
    pub render_state: RenderState,
    pub text_editor_state: TextEditorState,
    pub current_id: Option<Uuid>,
    pub current_browser: u8,
    pub shapes: ShapesPool<'a>,
    pub saved_shapes: Option<ShapesPool<'a>>,
}

impl<'a> State<'a> {
    pub fn new(width: i32, height: i32) -> Self {
        State {
            render_state: RenderState::new(width, height),
            text_editor_state: TextEditorState::new(),
            current_id: None,
            current_browser: 0,
            shapes: ShapesPool::new(),
            saved_shapes: None,
        }
    }

    // Creates a new temporary shapes pool.
    // Will panic if a previous temporary pool exists.
    pub fn start_temp_objects(mut self) -> Self {
        if self.saved_shapes.is_some() {
            panic!("Tried to start a temp objects while the previous have not been restored");
        }
        self.saved_shapes = Some(self.shapes);
        self.shapes = ShapesPool::new();
        self
    }


Review comment (suggestion):
Could we instead create a separate, new state type to store this? And then having a flag in `calculate_bool` to retrieve one set of shapes or other.

Code after:
use std::collections::HashMap;

mod shapes_pool;
mod text_editor;
pub use shapes_pool::{ShapesPool, ShapesPoolMutRef, ShapesPoolRef};
pub use text_editor::*;

use crate::render::RenderState;
use crate::shapes::Shape;
use crate::tiles;
use crate::uuid::Uuid;

use crate::shapes::modifiers::grid_layout::grid_cell_data;

/// This struct holds the state of the Rust application between JS calls.
///
/// It is created by [init] and passed to the other exported functions.
/// Note that rust-skia data structures are not thread safe, so a state
/// must not be shared between different Web Workers.
pub(crate) struct State<'a> {
    pub render_state: RenderState,
    pub text_editor_state: TextEditorState,
    pub current_id: Option<Uuid>,
    pub current_browser: u8,
    pub shapes: ShapesPool<'a>,
    pub saved_shapes: Option<ShapesPool<'a>>,
}

impl<'a> State<'a> {
    pub fn new(width: i32, height: i32) -> Self {
        State {
            render_state: RenderState::new(width, height),
            text_editor_state: TextEditorState::new(),
            current_id: None,
            current_browser: 0,
            shapes: ShapesPool::new(),
            // TODO: Maybe this can be moved to a different object
            saved_shapes: None,
        }
    }

    // Creates a new temporary shapes pool.
    // Will panic if a previous temporary pool exists.
    pub fn start_temp_objects(mut self) -> Self {
        if self.saved_shapes.is_some() {
            panic!("Tried to start a temp objects while the previous have not been restored");
        }
        self.saved_shapes = Some(self.shapes);
        self.shapes = ShapesPool::new();
        self
    }