Minor code readability improvements

This commit is contained in:
Keavon Chambers 2021-08-27 03:39:47 -07:00
parent d2395b4dcf
commit 4174f339be
2 changed files with 57 additions and 31 deletions

View File

@ -383,19 +383,19 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
self.backup(); self.backup();
responses.push_front(SelectMessage::UpdateSelectionBoundingBox.into()); responses.push_front(SelectMessage::UpdateSelectionBoundingBox.into());
for path in self.selected_layers().cloned() { for path in self.selected_layers().cloned() {
responses.push_front(DocumentOperation::DeleteLayer { path }.into()) responses.push_front(DocumentOperation::DeleteLayer { path }.into());
} }
} }
ClearOverlays => { ClearOverlays => {
responses.push_front(SelectMessage::UpdateSelectionBoundingBox.into()); responses.push_front(SelectMessage::UpdateSelectionBoundingBox.into());
for path in self.layer_data.keys().filter(|path| self.document.layer(path).unwrap().overlay).cloned() { for path in self.layer_data.keys().filter(|path| self.document.layer(path).unwrap().overlay).cloned() {
responses.push_front(DocumentOperation::DeleteLayer { path }.into()) responses.push_front(DocumentOperation::DeleteLayer { path }.into());
} }
} }
DuplicateSelectedLayers => { DuplicateSelectedLayers => {
self.backup(); self.backup();
for path in self.selected_layers_sorted() { for path in self.selected_layers_sorted() {
responses.push_back(DocumentOperation::DuplicateLayer { path }.into()) responses.push_back(DocumentOperation::DuplicateLayer { path }.into());
} }
} }
SetSelectedLayers(paths) => { SetSelectedLayers(paths) => {
@ -465,7 +465,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
.flatten(), .flatten(),
); );
if canvas_dirty { if canvas_dirty {
responses.push_back(RenderDocument.into()) responses.push_back(RenderDocument.into());
} }
} }
Err(e) => log::error!("DocumentError: {:?}", e), Err(e) => log::error!("DocumentError: {:?}", e),
@ -539,7 +539,7 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
let offset = if relative_position < 0 || non_selected.is_empty() { 0 } else { 1 }; let offset = if relative_position < 0 || non_selected.is_empty() { 0 } else { 1 };
let fallback = offset * (non_selected.len()); let fallback = offset * (non_selected.len());
let insert_index = non_selected.iter().position(|x| *x == id).map(|x| x + offset).unwrap_or(fallback) as isize; let insert_index = non_selected.iter().position(|x| *x == id).map(|x| x + offset).unwrap_or(fallback) as isize;
responses.push_back(DocumentMessage::MoveSelectedLayersTo { path: path.to_vec(), insert_index }.into()) responses.push_back(DocumentMessage::MoveSelectedLayersTo { path: path.to_vec(), insert_index }.into());
} }
} }
} }

View File

@ -51,16 +51,24 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
let (document, input) = data; let (document, input) = data;
use ToolMessage::*; use ToolMessage::*;
match message { match message {
SelectPrimaryColor(c) => { SelectPrimaryColor(color) => {
self.tool_state.document_tool_data.primary_color = c; let document_data = &mut self.tool_state.document_tool_data;
document_data.primary_color = color;
update_working_colors(&self.tool_state.document_tool_data, responses); update_working_colors(&self.tool_state.document_tool_data, responses);
} }
SelectSecondaryColor(c) => { SelectSecondaryColor(color) => {
self.tool_state.document_tool_data.secondary_color = c; let document_data = &mut self.tool_state.document_tool_data;
update_working_colors(&self.tool_state.document_tool_data, responses); document_data.secondary_color = color;
update_working_colors(document_data, responses);
} }
SelectTool(tool) => { SelectTool(tool) => {
let old_tool = self.tool_state.tool_data.active_tool_type; let tool_data = &mut self.tool_state.tool_data;
let document_data = &self.tool_state.document_tool_data;
let old_tool = tool_data.active_tool_type;
// Prepare to reset the old and new tools by obtaining their FSM Abort state, which will be sent to the tool
let reset = |tool| match tool { let reset = |tool| match tool {
ToolType::Ellipse => EllipseMessage::Abort.into(), ToolType::Ellipse => EllipseMessage::Abort.into(),
ToolType::Rectangle => RectangleMessage::Abort.into(), ToolType::Rectangle => RectangleMessage::Abort.into(),
@ -70,40 +78,55 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
ToolType::Select => SelectMessage::Abort.into(), ToolType::Select => SelectMessage::Abort.into(),
_ => ToolMessage::NoOp, _ => ToolMessage::NoOp,
}; };
let (new, old) = (reset(tool), reset(old_tool)); let new = reset(tool);
let old = reset(old_tool);
// Send the old and new tools a transition to the FSM Abort state
let mut send_to_tool = |tool_type, message: ToolMessage| { let mut send_to_tool = |tool_type, message: ToolMessage| {
if let Some(tool) = self.tool_state.tool_data.tools.get_mut(&tool_type) { if let Some(tool) = tool_data.tools.get_mut(&tool_type) {
tool.process_action(message, (document, &self.tool_state.document_tool_data, input), responses); tool.process_action(message, (document, document_data, input), responses);
} }
}; };
send_to_tool(tool, new); send_to_tool(tool, new);
send_to_tool(old_tool, old); send_to_tool(old_tool, old);
// Special cases for specific tools
if tool == ToolType::Select { if tool == ToolType::Select {
responses.push_back(SelectMessage::UpdateSelectionBoundingBox.into()); responses.push_back(SelectMessage::UpdateSelectionBoundingBox.into());
} }
self.tool_state.tool_data.active_tool_type = tool; self.tool_state.tool_data.active_tool_type = tool;
responses.push_back(FrontendMessage::SetActiveTool { tool_name: tool.to_string() }.into()) // Notify the frontend about the new active tool to be displayed
responses.push_back(FrontendMessage::SetActiveTool { tool_name: tool.to_string() }.into());
} }
SwapColors => { SwapColors => {
let doc_data = &mut self.tool_state.document_tool_data; let document_data = &mut self.tool_state.document_tool_data;
std::mem::swap(&mut doc_data.primary_color, &mut doc_data.secondary_color);
update_working_colors(doc_data, responses); std::mem::swap(&mut document_data.primary_color, &mut document_data.secondary_color);
update_working_colors(document_data, responses);
} }
ResetColors => { ResetColors => {
let doc_data = &mut self.tool_state.document_tool_data; let document_data = &mut self.tool_state.document_tool_data;
doc_data.primary_color = Color::BLACK;
doc_data.secondary_color = Color::WHITE; document_data.primary_color = Color::BLACK;
update_working_colors(doc_data, responses); document_data.secondary_color = Color::WHITE;
update_working_colors(document_data, responses);
} }
SetToolOptions(tool_type, tool_options) => { SetToolOptions(tool_type, tool_options) => {
self.tool_state.document_tool_data.tool_options.insert(tool_type, tool_options); let document_data = &mut self.tool_state.document_tool_data;
document_data.tool_options.insert(tool_type, tool_options);
} }
message => { message => {
let tool_type = message_to_tool_type(&message); let tool_type = message_to_tool_type(&message);
if let Some(tool) = self.tool_state.tool_data.tools.get_mut(&tool_type) { let document_data = &self.tool_state.document_tool_data;
if tool_type == self.tool_state.tool_data.active_tool_type { let tool_data = &mut self.tool_state.tool_data;
tool.process_action(message, (document, &self.tool_state.document_tool_data, input), responses);
if let Some(tool) = tool_data.tools.get_mut(&tool_type) {
if tool_type == tool_data.active_tool_type {
tool.process_action(message, (document, document_data, input), responses);
} }
} }
} }
@ -112,13 +135,14 @@ impl MessageHandler<ToolMessage, (&DocumentMessageHandler, &InputPreprocessor)>
fn actions(&self) -> ActionList { fn actions(&self) -> ActionList {
let mut list = actions!(ToolMessageDiscriminant; ResetColors, SwapColors, SelectTool, SetToolOptions); let mut list = actions!(ToolMessageDiscriminant; ResetColors, SwapColors, SelectTool, SetToolOptions);
list.extend(self.tool_state.tool_data.active_tool().actions()); list.extend(self.tool_state.tool_data.active_tool().actions());
list list
} }
} }
fn message_to_tool_type(message: &ToolMessage) -> ToolType { fn message_to_tool_type(message: &ToolMessage) -> ToolType {
use ToolMessage::*; use ToolMessage::*;
match message { let tool_type = match message {
Fill(_) => ToolType::Fill, Fill(_) => ToolType::Fill,
Rectangle(_) => ToolType::Rectangle, Rectangle(_) => ToolType::Rectangle,
Ellipse(_) => ToolType::Ellipse, Ellipse(_) => ToolType::Ellipse,
@ -131,14 +155,16 @@ fn message_to_tool_type(message: &ToolMessage) -> ToolType {
Navigate(_) => ToolType::Navigate, Navigate(_) => ToolType::Navigate,
Path(_) => ToolType::Path, Path(_) => ToolType::Path,
_ => unreachable!(), _ => unreachable!(),
} };
tool_type
} }
fn update_working_colors(doc_data: &DocumentToolData, responses: &mut VecDeque<Message>) { fn update_working_colors(document_data: &DocumentToolData, responses: &mut VecDeque<Message>) {
responses.push_back( responses.push_back(
FrontendMessage::UpdateWorkingColors { FrontendMessage::UpdateWorkingColors {
primary: doc_data.primary_color, primary: document_data.primary_color,
secondary: doc_data.secondary_color, secondary: document_data.secondary_color,
} }
.into(), .into(),
); );