File size: 23,000 Bytes
2409829
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
use crate::messages::debug::utility_types::MessageLoggingVerbosity;
use crate::messages::dialog::DialogMessageData;
use crate::messages::portfolio::document::node_graph::document_node_definitions;
use crate::messages::prelude::*;

#[derive(Debug, Default)]
pub struct Dispatcher {
	buffered_queue: Option<Vec<VecDeque<Message>>>,
	message_queues: Vec<VecDeque<Message>>,
	pub responses: Vec<FrontendMessage>,
	pub message_handlers: DispatcherMessageHandlers,
}

#[derive(Debug, Default)]
pub struct DispatcherMessageHandlers {
	animation_message_handler: AnimationMessageHandler,
	broadcast_message_handler: BroadcastMessageHandler,
	debug_message_handler: DebugMessageHandler,
	dialog_message_handler: DialogMessageHandler,
	globals_message_handler: GlobalsMessageHandler,
	input_preprocessor_message_handler: InputPreprocessorMessageHandler,
	key_mapping_message_handler: KeyMappingMessageHandler,
	layout_message_handler: LayoutMessageHandler,
	pub portfolio_message_handler: PortfolioMessageHandler,
	preferences_message_handler: PreferencesMessageHandler,
	tool_message_handler: ToolMessageHandler,
	workspace_message_handler: WorkspaceMessageHandler,
}

impl DispatcherMessageHandlers {
	pub fn with_executor(executor: crate::node_graph_executor::NodeGraphExecutor) -> Self {
		Self {
			portfolio_message_handler: PortfolioMessageHandler::with_executor(executor),
			..Default::default()
		}
	}
}

/// For optimization, these are messages guaranteed to be redundant when repeated.
/// The last occurrence of the message in the message queue is sufficient to ensure correct behavior.
/// In addition, these messages do not change any state in the backend (aside from caches).
const SIDE_EFFECT_FREE_MESSAGES: &[MessageDiscriminant] = &[
	MessageDiscriminant::Portfolio(PortfolioMessageDiscriminant::Document(DocumentMessageDiscriminant::PropertiesPanel(
		PropertiesPanelMessageDiscriminant::Refresh,
	))),
	MessageDiscriminant::Portfolio(PortfolioMessageDiscriminant::Document(DocumentMessageDiscriminant::DocumentStructureChanged)),
	MessageDiscriminant::Portfolio(PortfolioMessageDiscriminant::Document(DocumentMessageDiscriminant::Overlays(OverlaysMessageDiscriminant::Draw))),
	MessageDiscriminant::Portfolio(PortfolioMessageDiscriminant::Document(DocumentMessageDiscriminant::RenderRulers)),
	MessageDiscriminant::Portfolio(PortfolioMessageDiscriminant::Document(DocumentMessageDiscriminant::RenderScrollbars)),
	MessageDiscriminant::Frontend(FrontendMessageDiscriminant::UpdateDocumentLayerStructure),
	MessageDiscriminant::Frontend(FrontendMessageDiscriminant::TriggerFontLoad),
];
const DEBUG_MESSAGE_BLOCK_LIST: &[MessageDiscriminant] = &[MessageDiscriminant::Broadcast(BroadcastMessageDiscriminant::TriggerEvent(BroadcastEventDiscriminant::AnimationFrame))];
// TODO: Find a way to combine these with the list above. We use strings for now since these are the standard variant names used by multiple messages. But having these also type-checked would be best.
const DEBUG_MESSAGE_ENDING_BLOCK_LIST: &[&str] = &["PointerMove", "PointerOutsideViewport", "Overlays", "Draw", "CurrentTime", "Time"];

impl Dispatcher {
	pub fn new() -> Self {
		Self::default()
	}

	pub fn with_executor(executor: crate::node_graph_executor::NodeGraphExecutor) -> Self {
		Self {
			message_handlers: DispatcherMessageHandlers::with_executor(executor),
			..Default::default()
		}
	}

	// If the deepest queues (higher index in queues list) are now empty (after being popped from) then remove them
	fn cleanup_queues(&mut self, leave_last: bool) {
		while self.message_queues.last().filter(|queue| queue.is_empty()).is_some() {
			if leave_last && self.message_queues.len() == 1 {
				break;
			}
			self.message_queues.pop();
		}
	}

	/// Add a message to a queue so that it can be executed.
	/// If `process_after_all_current` is set, all currently queued messages (including children) will be processed first.
	/// If not set, it (and its children) will be processed as soon as possible.
	pub fn schedule_execution(message_queues: &mut Vec<VecDeque<Message>>, process_after_all_current: bool, messages: impl IntoIterator<Item = Message>) {
		match message_queues.first_mut() {
			// If there are currently messages being processed and we are processing after them, add to the end of the first queue
			Some(queue) if process_after_all_current => queue.extend(messages),
			// In all other cases, make a new inner queue and add our message there
			_ => message_queues.push(VecDeque::from_iter(messages)),
		}
	}

	pub fn handle_message<T: Into<Message>>(&mut self, message: T, process_after_all_current: bool) {
		let message = message.into();
		// Add all additional messages to the buffer if it exists (except from the end buffer message)
		if !matches!(message, Message::EndBuffer(_)) {
			if let Some(buffered_queue) = &mut self.buffered_queue {
				Self::schedule_execution(buffered_queue, true, [message]);

				return;
			}
		}

		// If we are not maintaining the buffer, simply add to the current queue
		Self::schedule_execution(&mut self.message_queues, process_after_all_current, [message]);

		while let Some(message) = self.message_queues.last_mut().and_then(VecDeque::pop_front) {
			// Skip processing of this message if it will be processed later (at the end of the shallowest level queue)
			if SIDE_EFFECT_FREE_MESSAGES.contains(&message.to_discriminant()) {
				let already_in_queue = self.message_queues.first().filter(|queue| queue.contains(&message)).is_some();
				if already_in_queue {
					self.log_deferred_message(&message, &self.message_queues, self.message_handlers.debug_message_handler.message_logging_verbosity);
					self.cleanup_queues(false);
					continue;
				} else if self.message_queues.len() > 1 {
					self.log_deferred_message(&message, &self.message_queues, self.message_handlers.debug_message_handler.message_logging_verbosity);
					self.cleanup_queues(true);
					self.message_queues[0].add(message);
					continue;
				}
			}

			// Print the message at a verbosity level of `info`
			self.log_message(&message, &self.message_queues, self.message_handlers.debug_message_handler.message_logging_verbosity);

			// Create a new queue for the child messages
			let mut queue = VecDeque::new();

			// Process the action by forwarding it to the relevant message handler, or saving the FrontendMessage to be sent to the frontend
			match message {
				Message::StartBuffer => {
					self.buffered_queue = Some(std::mem::take(&mut self.message_queues));
				}
				Message::EndBuffer(render_metadata) => {
					// Assign the message queue to the currently buffered queue
					if let Some(buffered_queue) = self.buffered_queue.take() {
						self.cleanup_queues(false);
						assert!(self.message_queues.is_empty(), "message queues are always empty when ending a buffer");
						self.message_queues = buffered_queue;
					};

					let graphene_std::renderer::RenderMetadata {
						upstream_footprints: footprints,
						local_transforms,
						click_targets,
						clip_targets,
					} = render_metadata;

					// Run these update state messages immediately
					let messages = [
						DocumentMessage::UpdateUpstreamTransforms {
							upstream_footprints: footprints,
							local_transforms,
						},
						DocumentMessage::UpdateClickTargets { click_targets },
						DocumentMessage::UpdateClipTargets { clip_targets },
					];
					Self::schedule_execution(&mut self.message_queues, false, messages.map(Message::from));
				}
				Message::NoOp => {}
				Message::Init => {
					// Load persistent data from the browser database
					queue.add(FrontendMessage::TriggerLoadFirstAutoSaveDocument);
					queue.add(FrontendMessage::TriggerLoadPreferences);

					// Display the menu bar at the top of the window
					queue.add(MenuBarMessage::SendLayout);

					// Send the information for tooltips and categories for each node/input.
					queue.add(FrontendMessage::SendUIMetadata {
						node_descriptions: document_node_definitions::collect_node_descriptions(),
						node_types: document_node_definitions::collect_node_types(),
					});

					// Finish loading persistent data from the browser database
					queue.add(FrontendMessage::TriggerLoadRestAutoSaveDocuments);
				}
				Message::Animation(message) => {
					self.message_handlers.animation_message_handler.process_message(message, &mut queue, ());
				}
				Message::Batched(messages) => {
					messages.iter().for_each(|message| self.handle_message(message.to_owned(), false));
				}
				Message::Broadcast(message) => self.message_handlers.broadcast_message_handler.process_message(message, &mut queue, ()),
				Message::Debug(message) => {
					self.message_handlers.debug_message_handler.process_message(message, &mut queue, ());
				}
				Message::Dialog(message) => {
					let data = DialogMessageData {
						portfolio: &self.message_handlers.portfolio_message_handler,
						preferences: &self.message_handlers.preferences_message_handler,
					};
					self.message_handlers.dialog_message_handler.process_message(message, &mut queue, data);
				}
				Message::Frontend(message) => {
					// Handle these messages immediately by returning early
					if let FrontendMessage::TriggerFontLoad { .. } = message {
						self.responses.push(message);
						self.cleanup_queues(false);

						// Return early to avoid running the code after the match block
						return;
					} else {
						// `FrontendMessage`s are saved and will be sent to the frontend after the message queue is done being processed
						self.responses.push(message);
					}
				}
				Message::Globals(message) => {
					self.message_handlers.globals_message_handler.process_message(message, &mut queue, ());
				}
				Message::InputPreprocessor(message) => {
					let keyboard_platform = GLOBAL_PLATFORM.get().copied().unwrap_or_default().as_keyboard_platform_layout();

					self.message_handlers
						.input_preprocessor_message_handler
						.process_message(message, &mut queue, InputPreprocessorMessageData { keyboard_platform });
				}
				Message::KeyMapping(message) => {
					let input = &self.message_handlers.input_preprocessor_message_handler;
					let actions = self.collect_actions();

					self.message_handlers
						.key_mapping_message_handler
						.process_message(message, &mut queue, KeyMappingMessageData { input, actions });
				}
				Message::Layout(message) => {
					let action_input_mapping = &|action_to_find: &MessageDiscriminant| self.message_handlers.key_mapping_message_handler.action_input_mapping(action_to_find);

					self.message_handlers.layout_message_handler.process_message(message, &mut queue, action_input_mapping);
				}
				Message::Portfolio(message) => {
					let ipp = &self.message_handlers.input_preprocessor_message_handler;
					let preferences = &self.message_handlers.preferences_message_handler;
					let current_tool = &self.message_handlers.tool_message_handler.tool_state.tool_data.active_tool_type;
					let message_logging_verbosity = self.message_handlers.debug_message_handler.message_logging_verbosity;
					let reset_node_definitions_on_open = self.message_handlers.portfolio_message_handler.reset_node_definitions_on_open;
					let timing_information = self.message_handlers.animation_message_handler.timing_information();
					let animation = &self.message_handlers.animation_message_handler;

					self.message_handlers.portfolio_message_handler.process_message(
						message,
						&mut queue,
						PortfolioMessageData {
							ipp,
							preferences,
							current_tool,
							message_logging_verbosity,
							reset_node_definitions_on_open,
							timing_information,
							animation,
						},
					);
				}
				Message::Preferences(message) => {
					self.message_handlers.preferences_message_handler.process_message(message, &mut queue, ());
				}
				Message::Tool(message) => {
					let document_id = self.message_handlers.portfolio_message_handler.active_document_id().unwrap();
					let Some(document) = self.message_handlers.portfolio_message_handler.documents.get_mut(&document_id) else {
						warn!("Called ToolMessage without an active document.\nGot {message:?}");
						return;
					};

					let data = ToolMessageData {
						document_id,
						document,
						input: &self.message_handlers.input_preprocessor_message_handler,
						persistent_data: &self.message_handlers.portfolio_message_handler.persistent_data,
						node_graph: &self.message_handlers.portfolio_message_handler.executor,
						preferences: &self.message_handlers.preferences_message_handler,
					};

					self.message_handlers.tool_message_handler.process_message(message, &mut queue, data);
				}
				Message::Workspace(message) => {
					self.message_handlers.workspace_message_handler.process_message(message, &mut queue, ());
				}
			}

			// If there are child messages, append the queue to the list of queues
			if !queue.is_empty() {
				self.message_queues.push(queue);
			}

			self.cleanup_queues(false);
		}
	}

	pub fn collect_actions(&self) -> ActionList {
		// TODO: Reduce the number of heap allocations
		let mut list = Vec::new();
		list.extend(self.message_handlers.dialog_message_handler.actions());
		list.extend(self.message_handlers.animation_message_handler.actions());
		list.extend(self.message_handlers.input_preprocessor_message_handler.actions());
		list.extend(self.message_handlers.key_mapping_message_handler.actions());
		list.extend(self.message_handlers.debug_message_handler.actions());
		if let Some(document) = self.message_handlers.portfolio_message_handler.active_document() {
			if !document.graph_view_overlay_open {
				list.extend(self.message_handlers.tool_message_handler.actions());
			}
		}
		list.extend(self.message_handlers.portfolio_message_handler.actions());
		list
	}

	pub fn poll_node_graph_evaluation(&mut self, responses: &mut VecDeque<Message>) -> Result<(), String> {
		self.message_handlers.portfolio_message_handler.poll_node_graph_evaluation(responses)
	}

	/// Create the tree structure for logging the messages as a tree
	fn create_indents(queues: &[VecDeque<Message>]) -> String {
		String::from_iter(queues.iter().enumerate().skip(1).map(|(index, queue)| {
			if index == queues.len() - 1 {
				if queue.is_empty() { "└── " } else { "β”œβ”€β”€ " }
			} else if queue.is_empty() {
				"   "
			} else {
				"β”‚    "
			}
		}))
	}

	/// Logs a message that is about to be executed, either as a tree
	/// with a discriminant or the entire payload (depending on settings)
	fn log_message(&self, message: &Message, queues: &[VecDeque<Message>], message_logging_verbosity: MessageLoggingVerbosity) {
		let discriminant = MessageDiscriminant::from(message);
		let is_blocked = DEBUG_MESSAGE_BLOCK_LIST.contains(&discriminant) || DEBUG_MESSAGE_ENDING_BLOCK_LIST.iter().any(|blocked_name| discriminant.local_name().ends_with(blocked_name));

		if !is_blocked {
			match message_logging_verbosity {
				MessageLoggingVerbosity::Off => {}
				MessageLoggingVerbosity::Names => {
					info!("{}{:?}", Self::create_indents(queues), message.to_discriminant());
				}
				MessageLoggingVerbosity::Contents => {
					if !(matches!(message, Message::InputPreprocessor(_))) {
						info!("Message: {}{:?}", Self::create_indents(queues), message);
					}
				}
			}
		}
	}

	/// Logs into the tree that the message is in the side effect free messages and its execution will be deferred
	fn log_deferred_message(&self, message: &Message, queues: &[VecDeque<Message>], message_logging_verbosity: MessageLoggingVerbosity) {
		if let MessageLoggingVerbosity::Names = message_logging_verbosity {
			info!("{}Deferred \"{:?}\" because it's a SIDE_EFFECT_FREE_MESSAGE", Self::create_indents(queues), message.to_discriminant());
		}
	}
}

#[cfg(test)]
mod test {
	pub use crate::test_utils::test_prelude::*;

	/// Create an editor with three layers
	/// 1. A red rectangle
	/// 2. A blue shape
	/// 3. A green ellipse
	async fn create_editor_with_three_layers() -> EditorTestUtils {
		let mut editor = EditorTestUtils::create();

		editor.new_document().await;

		editor.select_primary_color(Color::RED).await;
		editor.draw_rect(100., 200., 300., 400.).await;

		editor.select_primary_color(Color::BLUE).await;
		editor.draw_polygon(10., 1200., 1300., 400.).await;

		editor.select_primary_color(Color::GREEN).await;
		editor.draw_ellipse(104., 1200., 1300., 400.).await;

		editor
	}

	/// - create rect, shape and ellipse
	/// - copy
	/// - paste
	/// - assert that ellipse was copied
	#[tokio::test]
	async fn copy_paste_single_layer() {
		let mut editor = create_editor_with_three_layers().await;

		let layers_before_copy = editor.active_document().metadata().all_layers().collect::<Vec<_>>();
		editor.handle_message(PortfolioMessage::Copy { clipboard: Clipboard::Internal }).await;
		editor
			.handle_message(PortfolioMessage::PasteIntoFolder {
				clipboard: Clipboard::Internal,
				parent: LayerNodeIdentifier::ROOT_PARENT,
				insert_index: 0,
			})
			.await;

		let layers_after_copy = editor.active_document().metadata().all_layers().collect::<Vec<_>>();

		assert_eq!(layers_before_copy.len(), 3);
		assert_eq!(layers_after_copy.len(), 4);

		// Existing layers are unaffected
		for i in 0..=2 {
			assert_eq!(layers_before_copy[i], layers_after_copy[i + 1]);
		}
	}

	#[cfg_attr(miri, ignore)]
	/// - create rect, shape and ellipse
	/// - select shape
	/// - copy
	/// - paste
	/// - assert that shape was copied
	#[tokio::test]
	async fn copy_paste_single_layer_from_middle() {
		let mut editor = create_editor_with_three_layers().await;

		let layers_before_copy = editor.active_document().metadata().all_layers().collect::<Vec<_>>();
		let shape_id = editor.active_document().metadata().all_layers().nth(1).unwrap();

		editor.handle_message(NodeGraphMessage::SelectedNodesSet { nodes: vec![shape_id.to_node()] }).await;
		editor.handle_message(PortfolioMessage::Copy { clipboard: Clipboard::Internal }).await;
		editor
			.handle_message(PortfolioMessage::PasteIntoFolder {
				clipboard: Clipboard::Internal,
				parent: LayerNodeIdentifier::ROOT_PARENT,
				insert_index: 0,
			})
			.await;

		let layers_after_copy = editor.active_document().metadata().all_layers().collect::<Vec<_>>();

		assert_eq!(layers_before_copy.len(), 3);
		assert_eq!(layers_after_copy.len(), 4);

		// Existing layers are unaffected
		for i in 0..=2 {
			assert_eq!(layers_before_copy[i], layers_after_copy[i + 1]);
		}
	}

	#[cfg_attr(miri, ignore)]
	/// - create rect, shape and ellipse
	/// - select ellipse and rect
	/// - copy
	/// - delete
	/// - create another rect
	/// - paste
	/// - paste
	#[tokio::test]
	async fn copy_paste_deleted_layers() {
		let mut editor = create_editor_with_three_layers().await;
		assert_eq!(editor.active_document().metadata().all_layers().count(), 3);

		let layers_before_copy = editor.active_document().metadata().all_layers().collect::<Vec<_>>();
		let rect_id = layers_before_copy[0];
		let shape_id = layers_before_copy[1];
		let ellipse_id = layers_before_copy[2];

		editor
			.handle_message(NodeGraphMessage::SelectedNodesSet {
				nodes: vec![rect_id.to_node(), ellipse_id.to_node()],
			})
			.await;
		editor.handle_message(PortfolioMessage::Copy { clipboard: Clipboard::Internal }).await;
		editor.handle_message(NodeGraphMessage::DeleteSelectedNodes { delete_children: true }).await;
		editor.draw_rect(0., 800., 12., 200.).await;
		editor
			.handle_message(PortfolioMessage::PasteIntoFolder {
				clipboard: Clipboard::Internal,
				parent: LayerNodeIdentifier::ROOT_PARENT,
				insert_index: 0,
			})
			.await;
		editor
			.handle_message(PortfolioMessage::PasteIntoFolder {
				clipboard: Clipboard::Internal,
				parent: LayerNodeIdentifier::ROOT_PARENT,
				insert_index: 0,
			})
			.await;

		let layers_after_copy = editor.active_document().metadata().all_layers().collect::<Vec<_>>();

		assert_eq!(layers_before_copy.len(), 3);
		assert_eq!(layers_after_copy.len(), 6);

		println!("{:?} {:?}", layers_after_copy, layers_before_copy);

		assert_eq!(layers_after_copy[5], shape_id);
	}

	#[tokio::test]
	/// This test will fail when you make changes to the underlying serialization format for a document.
	async fn check_if_demo_art_opens() {
		use crate::messages::layout::utility_types::widget_prelude::*;

		let print_problem_to_terminal_on_failure = |value: &String| {
			println!();
			println!("-------------------------------------------------");
			println!("Failed test due to receiving a DisplayDialogError while loading a Graphite demo file.");
			println!();
			println!("NOTE:");
			println!("Document upgrading isn't performed in tests like when opening in the actual editor.");
			println!("You may need to open and re-save a document in the editor to apply its migrations.");
			println!();
			println!("DisplayDialogError details:");
			println!();
			println!("Description:");
			println!("{value}");
			println!("-------------------------------------------------");
			println!();

			panic!()
		};

		let mut editor = EditorTestUtils::create();

		// UNCOMMENT THIS FOR RUNNING UNDER MIRI
		//
		// let files = [
		// 	include_str!("../../demo-artwork/changing-seasons.graphite"),
		// 	include_str!("../../demo-artwork/isometric-fountain.graphite"),
		// 	include_str!("../../demo-artwork/painted-dreams.graphite"),
		// 	include_str!("../../demo-artwork/procedural-string-lights.graphite"),
		// 	include_str!("../../demo-artwork/parametric-dunescape.graphite"),
		// 	include_str!("../../demo-artwork/red-dress.graphite"),
		// 	include_str!("../../demo-artwork/valley-of-spires.graphite"),
		// ];
		// for (id, document_serialized_content) in files.iter().enumerate() {
		// let document_name = format!("document {id}");

		for (document_name, _, file_name) in crate::messages::dialog::simple_dialogs::ARTWORK {
			let document_serialized_content = std::fs::read_to_string(format!("../demo-artwork/{file_name}")).unwrap();

			assert_eq!(
				document_serialized_content.lines().count(),
				1,
				"Demo artwork '{document_name}' has more than 1 line (remember to open and re-save it in Graphite)",
			);

			let responses = editor.editor.handle_message(PortfolioMessage::OpenDocumentFile {
				document_name: document_name.into(),
				document_serialized_content,
			});

			// Check if the graph renders
			if let Err(e) = editor.eval_graph().await {
				print_problem_to_terminal_on_failure(&format!("Failed to evaluate the graph for document '{document_name}':\n{e}"));
			}

			for response in responses {
				// Check for the existence of the file format incompatibility warning dialog after opening the test file
				if let FrontendMessage::UpdateDialogColumn1 { layout_target: _, diff } = response {
					if let DiffUpdate::SubLayout(sub_layout) = &diff[0].new_value {
						if let LayoutGroup::Row { widgets } = &sub_layout[0] {
							if let Widget::TextLabel(TextLabel { value, .. }) = &widgets[0].widget {
								print_problem_to_terminal_on_failure(value);
							}
						}
					}
				}
			}
		}
	}
}