mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-16 23:08:05 +08:00
Restructure project directories (#333)
`/client/web` -> `/frontend` `/client/cli` -> *delete for now* `/client/native` -> *delete for now* `/core/editor` -> `/editor` `/core/document` -> `/graphene` `/core/renderer` -> `/charcoal` `/core/proc-macro` -> `/proc-macros` *(now plural)*
This commit is contained in:
18
editor/src/misc/derivable_custom_traits.rs
Normal file
18
editor/src/misc/derivable_custom_traits.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
//! Traits that can be derived using macros from `graphite-proc-macros`
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub trait Hint {
|
||||
fn hints(&self) -> HashMap<String, String>;
|
||||
}
|
||||
|
||||
pub trait ToDiscriminant {
|
||||
type Discriminant;
|
||||
|
||||
fn to_discriminant(&self) -> Self::Discriminant;
|
||||
}
|
||||
|
||||
pub trait TransitiveChild: Into<Self::Parent> + Into<Self::TopParent> {
|
||||
type TopParent;
|
||||
type Parent;
|
||||
}
|
||||
35
editor/src/misc/error.rs
Normal file
35
editor/src/misc/error.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use crate::Color;
|
||||
use graphene::DocumentError;
|
||||
use thiserror::Error;
|
||||
|
||||
/// The error type used by the Graphite editor.
|
||||
#[derive(Clone, Debug, Error)]
|
||||
pub enum EditorError {
|
||||
#[error("Failed to execute operation: {0}")]
|
||||
InvalidOperation(String),
|
||||
#[error("{0}")]
|
||||
Misc(String),
|
||||
#[error("Tried to construct an invalid color {0:?}")]
|
||||
Color(String),
|
||||
#[error("The requested tool does not exist")]
|
||||
UnknownTool,
|
||||
#[error("The operation caused a document error {0:?}")]
|
||||
Document(String),
|
||||
#[error("A Rollback was initated but no transaction was in progress")]
|
||||
NoTransactionInProgress,
|
||||
}
|
||||
|
||||
macro_rules! derive_from {
|
||||
($type:ty, $kind:ident) => {
|
||||
impl From<$type> for EditorError {
|
||||
fn from(error: $type) -> Self {
|
||||
EditorError::$kind(format!("{:?}", error))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
derive_from!(&str, Misc);
|
||||
derive_from!(String, Misc);
|
||||
derive_from!(Color, Color);
|
||||
derive_from!(DocumentError, Document);
|
||||
138
editor/src/misc/macros.rs
Normal file
138
editor/src/misc/macros.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
/// Counts args in the macro invocation by adding `+ 1` for every arg.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// let x = count_args!(("example1"), (10), (25));
|
||||
/// assert_eq!(x, 3);
|
||||
/// ```
|
||||
/// expands to
|
||||
/// ```ignore
|
||||
/// let x = 0 + 1 + 1 + 1;
|
||||
/// assert_eq!(x, 3);
|
||||
/// ```
|
||||
macro_rules! count_args {
|
||||
(@one $($t:tt)*) => { 1 };
|
||||
($(($($x:tt)*)),*$(,)?) => {
|
||||
0 $(+ count_args!(@one $($x)*))*
|
||||
};
|
||||
}
|
||||
|
||||
/// Generates a [`std::collections::HashMap`] for `ToolState`'s `tools` variable.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// let tools = gen_tools_hash_map! {
|
||||
/// Select => select::Select,
|
||||
/// Crop => crop::Crop,
|
||||
/// };
|
||||
/// ```
|
||||
/// expands to
|
||||
/// ```ignore
|
||||
/// let tools = {
|
||||
/// let mut hash_map: std::collections::HashMap<crate::tool::ToolType, Box<dyn crate::tool::Tool>> = std::collections::HashMap::with_capacity(count_args!(/* Macro args */));
|
||||
///
|
||||
/// hash_map.insert(crate::tool::ToolType::Select, Box::new(select::Select::default()));
|
||||
/// hash_map.insert(crate::tool::ToolType::Crop, Box::new(crop::Crop::default()));
|
||||
///
|
||||
/// hash_map
|
||||
/// };
|
||||
/// ```
|
||||
macro_rules! gen_tools_hash_map {
|
||||
($($enum_variant:ident => $struct_path:ty),* $(,)?) => {{
|
||||
let mut hash_map: ::std::collections::HashMap<$crate::tool::ToolType, ::std::boxed::Box<dyn for<'a> $crate::message_prelude::MessageHandler<$crate::tool::tool_messages::ToolMessage,$crate::tool::ToolActionHandlerData<'a>>>> = ::std::collections::HashMap::with_capacity(count_args!($(($enum_variant)),*));
|
||||
$(hash_map.insert($crate::tool::ToolType::$enum_variant, ::std::boxed::Box::new(<$struct_path>::default()));)*
|
||||
|
||||
hash_map
|
||||
}};
|
||||
}
|
||||
|
||||
/// Creates a string representation of an enum value that exactly matches the given name of each enum variant
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// enum E {
|
||||
/// A(u8),
|
||||
/// B
|
||||
/// }
|
||||
///
|
||||
/// // this line is important
|
||||
/// use E::*;
|
||||
///
|
||||
/// let a = E::A(7);
|
||||
/// let s = match_variant_name!(match (a) { A, B });
|
||||
/// ```
|
||||
///
|
||||
/// expands to
|
||||
///
|
||||
/// ```ignore
|
||||
/// // ...
|
||||
///
|
||||
/// let s = match a {
|
||||
/// A { .. } => "A",
|
||||
/// B { .. } => "B"
|
||||
/// };
|
||||
/// ```
|
||||
macro_rules! match_variant_name {
|
||||
(match ($e:expr) { $($v:ident),* $(,)? }) => {
|
||||
match $e {
|
||||
$(
|
||||
$v { .. } => stringify!($v)
|
||||
),*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Syntax sugar for initializing an `ActionList`
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// actions!(DocumentMessage::Undo, DocumentMessage::Redo);
|
||||
/// ```
|
||||
///
|
||||
/// expands to:
|
||||
/// ```ignore
|
||||
/// vec![vec![DocumentMessage::Undo, DocumentMessage::Redo]];
|
||||
/// ```
|
||||
///
|
||||
/// and
|
||||
/// ```ignore
|
||||
/// actions!(DocumentMessage; Undo, Redo);
|
||||
/// ```
|
||||
///
|
||||
/// expands to:
|
||||
/// ```ignore
|
||||
/// vec![vec![DocumentMessage::Undo, DocumentMessage::Redo]];
|
||||
/// ```
|
||||
///
|
||||
macro_rules! actions {
|
||||
($($v:expr),* $(,)?) => {{
|
||||
vec![$(vec![$v.into()]),*]
|
||||
}};
|
||||
($name:ident; $($v:ident),* $(,)?) => {{
|
||||
vec![vec![$(($name::$v).into()),*]]
|
||||
}};
|
||||
}
|
||||
|
||||
/// Does the same thing as the `actions!` macro but wraps everything in:
|
||||
///
|
||||
/// ```ignore
|
||||
/// fn actions(&self) -> ActionList {
|
||||
/// actions!(…)
|
||||
/// }
|
||||
/// ```
|
||||
macro_rules! advertise_actions {
|
||||
($($v:expr),* $(,)?) => {
|
||||
fn actions(&self) -> $crate::communication::ActionList {
|
||||
actions!($($v),*)
|
||||
}
|
||||
};
|
||||
($name:ident; $($v:ident),* $(,)?) => {
|
||||
fn actions(&self) -> $crate::communication::ActionList {
|
||||
actions!($name; $($v),*)
|
||||
}
|
||||
}
|
||||
}
|
||||
8
editor/src/misc/mod.rs
Normal file
8
editor/src/misc/mod.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
pub mod derivable_custom_traits;
|
||||
mod error;
|
||||
pub mod test_utils;
|
||||
|
||||
pub use error::EditorError;
|
||||
pub use macros::*;
|
||||
85
editor/src/misc/test_utils.rs
Normal file
85
editor/src/misc/test_utils.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use crate::{
|
||||
input::{
|
||||
mouse::{MouseKeys, MouseState, ScrollDelta},
|
||||
InputPreprocessorMessage, ModifierKeys,
|
||||
},
|
||||
message_prelude::{Message, ToolMessage},
|
||||
tool::ToolType,
|
||||
Editor,
|
||||
};
|
||||
use graphene::color::Color;
|
||||
|
||||
/// A set of utility functions to make the writing of editor test more declarative
|
||||
pub trait EditorTestUtils {
|
||||
fn draw_rect(&mut self, x1: u32, y1: u32, x2: u32, y2: u32);
|
||||
fn draw_shape(&mut self, x1: u32, y1: u32, x2: u32, y2: u32);
|
||||
fn draw_ellipse(&mut self, x1: u32, y1: u32, x2: u32, y2: u32);
|
||||
|
||||
/// Select given tool and drag it from (x1, y1) to (x2, y2)
|
||||
fn drag_tool(&mut self, typ: ToolType, x1: u32, y1: u32, x2: u32, y2: u32);
|
||||
fn move_mouse(&mut self, x: u32, y: u32);
|
||||
fn mousedown(&mut self, state: MouseState);
|
||||
fn mouseup(&mut self, state: MouseState);
|
||||
fn lmb_mousedown(&mut self, x: u32, y: u32);
|
||||
fn input(&mut self, message: InputPreprocessorMessage);
|
||||
fn select_tool(&mut self, typ: ToolType);
|
||||
fn select_primary_color(&mut self, color: Color);
|
||||
}
|
||||
|
||||
impl EditorTestUtils for Editor {
|
||||
fn draw_rect(&mut self, x1: u32, y1: u32, x2: u32, y2: u32) {
|
||||
self.drag_tool(ToolType::Rectangle, x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
fn draw_shape(&mut self, x1: u32, y1: u32, x2: u32, y2: u32) {
|
||||
self.drag_tool(ToolType::Shape, x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
fn draw_ellipse(&mut self, x1: u32, y1: u32, x2: u32, y2: u32) {
|
||||
self.drag_tool(ToolType::Ellipse, x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
fn drag_tool(&mut self, typ: ToolType, x1: u32, y1: u32, x2: u32, y2: u32) {
|
||||
self.select_tool(typ);
|
||||
self.move_mouse(x1, y1);
|
||||
self.lmb_mousedown(x1, y1);
|
||||
self.move_mouse(x2, y2);
|
||||
self.mouseup(MouseState {
|
||||
position: (x2, y2).into(),
|
||||
mouse_keys: MouseKeys::empty(),
|
||||
scroll_delta: ScrollDelta::default(),
|
||||
});
|
||||
}
|
||||
|
||||
fn move_mouse(&mut self, x: u32, y: u32) {
|
||||
self.input(InputPreprocessorMessage::MouseMove((x, y).into(), ModifierKeys::default()));
|
||||
}
|
||||
|
||||
fn mousedown(&mut self, state: MouseState) {
|
||||
self.input(InputPreprocessorMessage::MouseDown(state, ModifierKeys::default()));
|
||||
}
|
||||
|
||||
fn mouseup(&mut self, state: MouseState) {
|
||||
self.handle_message(InputPreprocessorMessage::MouseUp(state, ModifierKeys::default())).unwrap()
|
||||
}
|
||||
|
||||
fn lmb_mousedown(&mut self, x: u32, y: u32) {
|
||||
self.mousedown(MouseState {
|
||||
position: (x, y).into(),
|
||||
mouse_keys: MouseKeys::LEFT,
|
||||
scroll_delta: ScrollDelta::default(),
|
||||
})
|
||||
}
|
||||
|
||||
fn input(&mut self, message: InputPreprocessorMessage) {
|
||||
self.handle_message(Message::InputPreprocessor(message)).unwrap();
|
||||
}
|
||||
|
||||
fn select_tool(&mut self, typ: ToolType) {
|
||||
self.handle_message(Message::Tool(ToolMessage::SelectTool(typ))).unwrap();
|
||||
}
|
||||
|
||||
fn select_primary_color(&mut self, color: Color) {
|
||||
self.handle_message(Message::Tool(ToolMessage::SelectPrimaryColor(color))).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user