mirror of
https://github.com/penpot/penpot.git
synced 2025-10-04 22:17:24 +00:00
52 lines
1.5 KiB
Rust
52 lines
1.5 KiB
Rust
use std::collections::HashMap;
|
|
use uuid::Uuid;
|
|
|
|
use crate::render::RenderState;
|
|
use crate::shapes::Shape;
|
|
|
|
/// 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 current_id: Option<Uuid>,
|
|
pub current_shape: Option<&'a mut Shape>,
|
|
pub shapes: HashMap<Uuid, Shape>,
|
|
}
|
|
|
|
impl<'a> State<'a> {
|
|
pub fn with_capacity(width: i32, height: i32, capacity: usize) -> Self {
|
|
State {
|
|
render_state: RenderState::new(width, height),
|
|
current_id: None,
|
|
current_shape: None,
|
|
shapes: HashMap::with_capacity(capacity),
|
|
}
|
|
}
|
|
|
|
pub fn render_state(&'a mut self) -> &'a mut RenderState {
|
|
&mut self.render_state
|
|
}
|
|
|
|
pub fn draw_all_shapes(&mut self, zoom: f32, pan_x: f32, pan_y: f32) {
|
|
self.render_state
|
|
.draw_all_shapes(zoom, pan_x, pan_y, &self.shapes);
|
|
}
|
|
|
|
pub fn use_shape(&'a mut self, id: Uuid) {
|
|
if !self.shapes.contains_key(&id) {
|
|
let new_shape = Shape::new(id);
|
|
self.shapes.insert(id, new_shape);
|
|
}
|
|
|
|
self.current_id = Some(id);
|
|
self.current_shape = self.shapes.get_mut(&id);
|
|
}
|
|
|
|
pub fn current_shape(&'a mut self) -> Option<&'a mut Shape> {
|
|
self.current_shape.as_deref_mut()
|
|
}
|
|
}
|