Fix layer deletion bugs when tools are in use (#684)

* Added test case of layer delete bug

* Fixed crash in `PenTool`

Updated the document `MessageHandler` to cancel all active tools if the
layer is deleted.  This prevents the tools from crashing due to the
layer being pulled from under them.

* Moved Abort into pre-graphene DeleteLayer message

* Renamed test case for clarity

* Moved tool crash tests to the `tools` module

* Added `test-case` to the dev dependencies

* Added crash test case for all tools

* Ran cargo fmt

Co-authored-by: otdavies <oliver@psyfer.io>
Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Alexi
2022-06-24 09:35:07 -05:00
committed by GitHub
parent a4a174db04
commit 8f4cb882a9
5 changed files with 89 additions and 1 deletions

View File

@@ -991,6 +991,7 @@ impl MessageHandler<DocumentMessage, (&InputPreprocessorMessageHandler, &FontCac
}
DeleteLayer { layer_path } => {
responses.push_front(DocumentOperation::DeleteLayer { path: layer_path.clone() }.into());
responses.push_front(ToolMessage::AbortCurrentTool.into());
responses.push_back(PropertiesPanelMessage::CheckSelectedWasDeleted { path: layer_path }.into());
}
DeleteSelectedLayers => {

View File

@@ -199,7 +199,11 @@ impl<'a> Selected<'a> {
pub fn new(original_transforms: &'a mut OriginalTransforms, pivot: &'a mut DVec2, selected: &'a [&'a Vec<LayerId>], responses: &'a mut VecDeque<Message>, document: &'a Document) -> Self {
for path in selected {
if !original_transforms.contains_key(*path) {
original_transforms.insert(path.to_vec(), document.layer(path).unwrap().transform);
if let Ok(layer) = document.layer(path) {
original_transforms.insert(path.to_vec(), layer.transform);
} else {
log::warn!("Didn't find a layer for {:?}", path);
}
}
}
Self {

View File

@@ -4,3 +4,32 @@ pub mod tool_message;
pub mod tool_message_handler;
pub mod tools;
pub mod vector_editor;
#[cfg(test)]
mod tool_crash_on_layer_delete_tests {
use crate::communication::set_uuid_seed;
use crate::misc::test_utils::EditorTestUtils;
use crate::viewport_tools::tool::ToolType;
use crate::{DocumentMessage, Editor};
use test_case::test_case;
#[test_case(ToolType::Pen ; "while using pen tool")]
#[test_case(ToolType::Freehand ; "while using freehand tool")]
#[test_case(ToolType::Spline ; "while using spline tool")]
#[test_case(ToolType::Line ; "while using line tool")]
#[test_case(ToolType::Rectangle ; "while using rectangle tool")]
#[test_case(ToolType::Ellipse ; "while using ellipse tool")]
#[test_case(ToolType::Shape ; "while using shape tool")]
#[test_case(ToolType::Path ; "while using path tool")]
fn should_not_crash_when_layer_is_deleted(tool: ToolType) {
set_uuid_seed(0);
let mut test_editor = Editor::new();
test_editor.select_tool(tool);
test_editor.lmb_mousedown(0.0, 0.0);
test_editor.move_mouse(100.0, 100.0);
test_editor.handle_message(DocumentMessage::DeleteSelectedLayers);
}
}