-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathartboard_message_handler.rs
More file actions
83 lines (76 loc) · 2.64 KB
/
artboard_message_handler.rs
File metadata and controls
83 lines (76 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
pub use crate::document::layer_panel::*;
use crate::document::{DocumentMessage, LayerMetadata};
use crate::input::InputPreprocessor;
use crate::message_prelude::*;
use glam::{DAffine2, DVec2};
use graphene::color::Color;
use graphene::document::Document as GrapheneDocument;
use graphene::layers::style::{self, Fill, ViewMode};
use graphene::Operation as DocumentOperation;
use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
#[impl_message(Message, DocumentMessage, Artboard)]
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub enum ArtboardMessage {
DispatchOperation(Box<DocumentOperation>),
AddArtboard { top: f64, left: f64, height: f64, width: f64 },
RenderArtboards,
}
impl From<DocumentOperation> for ArtboardMessage {
fn from(operation: DocumentOperation) -> Self {
Self::DispatchOperation(Box::new(operation))
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ArtboardMessageHandler {
pub artboards_graphene_document: GrapheneDocument,
pub artboard_ids: Vec<LayerId>,
}
impl MessageHandler<ArtboardMessage, (&mut LayerMetadata, &GrapheneDocument, &InputPreprocessor)> for ArtboardMessageHandler {
fn process_action(&mut self, message: ArtboardMessage, _data: (&mut LayerMetadata, &GrapheneDocument, &InputPreprocessor), responses: &mut VecDeque<Message>) {
// let (layer_metadata, document, ipp) = data;
use ArtboardMessage::*;
match message {
DispatchOperation(operation) => match self.artboards_graphene_document.handle_operation(&operation) {
Ok(_) => (),
Err(e) => log::error!("Artboard Error: {:?}", e),
},
AddArtboard { top, left, height, width } => {
let artboard_id = generate_uuid();
self.artboard_ids.push(artboard_id);
responses.push_back(
ArtboardMessage::DispatchOperation(
DocumentOperation::AddRect {
path: vec![artboard_id],
insert_index: -1,
transform: DAffine2::from_scale_angle_translation(DVec2::new(height, width), 0., DVec2::new(top, left)).to_cols_array(),
style: style::PathStyle::new(None, Some(Fill::new(Color::WHITE))),
}
.into(),
)
.into(),
);
}
RenderArtboards => {}
}
// Render an infinite canvas if there are no artboards
if self.artboard_ids.is_empty() {
responses.push_back(
FrontendMessage::UpdateArtboards {
svg: r##"<rect width="100%" height="100%" fill="#ffffff" />"##.to_string(),
}
.into(),
)
} else {
responses.push_back(
FrontendMessage::UpdateArtboards {
svg: self.artboards_graphene_document.render_root(ViewMode::Normal),
}
.into(),
);
}
}
fn actions(&self) -> ActionList {
actions!(ArtBoardMessageDiscriminant;)
}
}