mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-17 15:28:04 +08:00
Major overhaul of input and communication systems
* Add input manager * WIP lifetime hell * Hell yeah, dark lifetime magic * Replace events with actions in tools * Fix borrow in GlobalEventHandler * Fix typo in response-handler * Distribute dispatch structure * Add translation from events to actions * Port default key actions to input_mapper * Split actions macro * Add handling for Ambiguous Mouse events * Fix warnings and clippy lints * Add actions macro * WIP rework * Add AsMessage macro * Add implementation for derived enums * Add macro implementation for top level message * Add #[child] attribute to indicate derivation * Replace some mentions of Actions and Responses with Message * It compiles !!! * Add functionality to some message handlers * Add document rendering * ICE * Rework the previous code while keeping basic functionality * Reduce parent-top-level macro args to only two * Add workaround for ICE * Fix cyclic reference in document.rs * Make derive_transitive_child a bit more powerful This addresses the todo that was left, enabling arbitrary expressions to be passed as the last parameter of a #[parent] attribute * Adapt frontend message format * Make responses use VecDeque Our responses are a queue so we should use a queue type for them * Move traits to sensible location * Are we rectangle yet? * Simplify, improve & document `derive_discriminant` * Change `child` to `sub_discriminant` This only applies to `ToDiscriminant`. Code using `#[impl_message]` continues to work. * Add docs for `derive_transitive_child` * Finish docs and improve macros The improvements are that impl_message now uses trait resolution to obtain the parent's discriminant and that derive_as_message now allows for non-unit variants (which we don't use but it's nice to have, just in case) * Remove logging call * Move files around and cleanup structure * Fix proc macro doc tests * Improve actions_fn!() macro * Add ellipse tool * Pass populated actions list to the input mapper * Add KeyState bitvector * Merge mouse buttons into "keyboard" * Add macro for initialization of key mapper table * Add syntactic sugar for the macro * Implement mapping function * Translate the remaining tools * Fix shape tool * Add keybindings for line and pen tool * Fix modifiers * Cleanup * Add doc comments for the actions macro * Fix formatting * Rename MouseMove to PointerMove * Add keybinds for tools * Apply review suggestions * Rename KeyMappings -> KeyMappingEntries * Apply review changes Co-authored-by: T0mstone <realt0mstone@gmail.com> Co-authored-by: Paul Kupper <kupper.pa@gmail.com>
This commit is contained in:
committed by
Keavon Chambers
parent
c08b2d4b31
commit
4b19a459b7
@@ -26,55 +26,35 @@ export function registerResponseHandler(responseType: ResponseType, callback: Re
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export function handleResponse(responseIdentifier: string, responseData: any) {
|
||||
const [origin, responesType] = responseIdentifier.split("::", 2);
|
||||
const callback = window.responseMap[responesType];
|
||||
const data = parseResponse(origin, responesType, responseData);
|
||||
export function handleResponse(responseType: string, responseData: any) {
|
||||
const callback = window.responseMap[responseType];
|
||||
const data = parseResponse(responseType, responseData);
|
||||
|
||||
if (callback && data) {
|
||||
callback(data);
|
||||
} else if (data) {
|
||||
console.error(`Received a Response of type "${responseIdentifier}" but no handler was registered for it from the client.`);
|
||||
console.error(`Received a Response of type "${responseType}" but no handler was registered for it from the client.`);
|
||||
} else {
|
||||
console.error(`Received a Response of type "${responseIdentifier}" but but was not able to parse the data.`);
|
||||
console.error(`Received a Response of type "${responseType}" but but was not able to parse the data.`);
|
||||
}
|
||||
}
|
||||
|
||||
enum OriginNames {
|
||||
Document = "Document",
|
||||
Tool = "Tool",
|
||||
}
|
||||
|
||||
function parseResponse(origin: string, responseType: string, data: any): Response {
|
||||
const response = (() => {
|
||||
switch (origin) {
|
||||
case OriginNames.Document:
|
||||
switch (responseType) {
|
||||
case "DocumentChanged":
|
||||
return newDocumentChanged(data.Document.DocumentChanged);
|
||||
case "CollapseFolder":
|
||||
return newCollapseFolder(data.Document.CollapseFolder);
|
||||
case "ExpandFolder":
|
||||
return newExpandFolder(data.Document.ExpandFolder);
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
case OriginNames.Tool:
|
||||
switch (responseType) {
|
||||
case "SetActiveTool":
|
||||
return newSetActiveTool(data.Tool.SetActiveTool);
|
||||
case "UpdateCanvas":
|
||||
return newUpdateCanvas(data.Tool.UpdateCanvas);
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
})();
|
||||
|
||||
if (!response) throw new Error(`Unrecognized origin/responseType pair: ${origin}, ${responseType}`);
|
||||
return response;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
function parseResponse(responseType: string, data: any): Response {
|
||||
switch (responseType) {
|
||||
case "DocumentChanged":
|
||||
return newDocumentChanged(data.DocumentChanged);
|
||||
case "CollapseFolder":
|
||||
return newCollapseFolder(data.CollapseFolder);
|
||||
case "ExpandFolder":
|
||||
return newExpandFolder(data.ExpandFolder);
|
||||
case "SetActiveTool":
|
||||
return newSetActiveTool(data.SetActiveTool);
|
||||
case "UpdateCanvas":
|
||||
return newUpdateCanvas(data.UpdateCanvas);
|
||||
default:
|
||||
throw new Error(`Unrecognized origin/responseType pair: ${origin}, ${responseType}`);
|
||||
}
|
||||
}
|
||||
|
||||
export type Response = SetActiveTool | UpdateCanvas | DocumentChanged | CollapseFolder | ExpandFolder;
|
||||
|
||||
@@ -1,54 +1,22 @@
|
||||
use crate::shims::Error;
|
||||
use crate::wrappers::{translate_key, translate_tool, Color};
|
||||
use crate::EDITOR_STATE;
|
||||
use editor_core::{events, LayerId};
|
||||
use editor_core::message_prelude::*;
|
||||
use editor_core::{
|
||||
input::mouse::{MouseState, ViewportPosition},
|
||||
LayerId,
|
||||
};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
fn convert_error(err: editor_core::EditorError) -> JsValue {
|
||||
Error::new(&err.to_string()).into()
|
||||
}
|
||||
|
||||
mod mouse_state {
|
||||
pub(super) type MouseKeys = u8;
|
||||
use editor_core::events::{self, Event, MouseState, ViewportPosition};
|
||||
static mut MOUSE_STATE: MouseKeys = 0;
|
||||
|
||||
pub(super) fn translate_mouse_down(mod_keys: MouseKeys, position: ViewportPosition) -> Event {
|
||||
translate_mouse_event(mod_keys, position, true)
|
||||
}
|
||||
pub(super) fn translate_mouse_up(mod_keys: MouseKeys, position: ViewportPosition) -> Event {
|
||||
translate_mouse_event(mod_keys, position, false)
|
||||
}
|
||||
|
||||
fn translate_mouse_event(mod_keys: MouseKeys, position: ViewportPosition, down: bool) -> Event {
|
||||
let diff = unsafe { MOUSE_STATE } ^ mod_keys;
|
||||
unsafe { MOUSE_STATE = mod_keys };
|
||||
let mouse_keys = events::MouseKeys::from_bits(mod_keys).expect("invalid modifier keys");
|
||||
let state = MouseState { position, mouse_keys };
|
||||
match (down, diff) {
|
||||
(true, 1) => Event::LmbDown(state),
|
||||
(true, 2) => Event::RmbDown(state),
|
||||
(true, 4) => Event::MmbDown(state),
|
||||
(false, 1) => Event::LmbUp(state),
|
||||
(false, 2) => Event::RmbUp(state),
|
||||
(false, 4) => Event::MmbUp(state),
|
||||
(down, _) => {
|
||||
log::warn!("two buttons where modified at the same time. Modification: {:#010b}", diff);
|
||||
if down {
|
||||
Event::AmbiguousMouseDown(state)
|
||||
} else {
|
||||
Event::AmbiguousMouseUp(state)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Modify the currently selected tool in the document state store
|
||||
#[wasm_bindgen]
|
||||
pub fn select_tool(tool: String) -> Result<(), JsValue> {
|
||||
EDITOR_STATE.with(|editor| match translate_tool(&tool) {
|
||||
Some(tool) => editor.borrow_mut().handle_event(events::Event::SelectTool(tool)).map_err(convert_error),
|
||||
Some(tool) => editor.borrow_mut().handle_message(ToolMessage::SelectTool(tool)).map_err(convert_error),
|
||||
None => Err(Error::new(&format!("Couldn't select {} because it was not recognized as a valid tool", tool)).into()),
|
||||
})
|
||||
}
|
||||
@@ -58,26 +26,24 @@ pub fn select_tool(tool: String) -> Result<(), JsValue> {
|
||||
#[wasm_bindgen]
|
||||
pub fn on_mouse_move(x: u32, y: u32) -> Result<(), JsValue> {
|
||||
// TODO: Convert these screenspace viewport coordinates to canvas coordinates based on the current zoom and pan
|
||||
let ev = events::Event::MouseMove(events::ViewportPosition { x, y });
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
|
||||
let ev = InputPreprocessorMessage::MouseMove(ViewportPosition { x, y });
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
|
||||
}
|
||||
|
||||
/// A mouse button depressed within screenspace the bounds of the viewport
|
||||
#[wasm_bindgen]
|
||||
pub fn on_mouse_down(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
|
||||
// TODO: Convert these screenspace viewport coordinates to canvas coordinates based on the current zoom and pan
|
||||
let pos = events::ViewportPosition { x, y };
|
||||
let ev = mouse_state::translate_mouse_down(mouse_keys, pos);
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
|
||||
let pos = ViewportPosition { x, y };
|
||||
let ev = InputPreprocessorMessage::MouseDown(MouseState::from_u8_pos(mouse_keys, pos));
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
|
||||
}
|
||||
|
||||
/// A mouse button released
|
||||
#[wasm_bindgen]
|
||||
pub fn on_mouse_up(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
|
||||
// TODO: Convert these screenspace viewport coordinates to canvas coordinates based on the current zoom and pan
|
||||
let pos = events::ViewportPosition { x, y };
|
||||
let ev = mouse_state::translate_mouse_up(mouse_keys, pos);
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
|
||||
let pos = ViewportPosition { x, y };
|
||||
let ev = InputPreprocessorMessage::MouseUp(MouseState::from_u8_pos(mouse_keys, pos));
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
|
||||
}
|
||||
|
||||
/// A keyboard button depressed within screenspace the bounds of the viewport
|
||||
@@ -85,8 +51,8 @@ pub fn on_mouse_up(x: u32, y: u32, mouse_keys: u8) -> Result<(), JsValue> {
|
||||
pub fn on_key_down(name: String) -> Result<(), JsValue> {
|
||||
let key = translate_key(&name);
|
||||
log::trace!("key down {:?}, name: {}", key, name);
|
||||
let ev = events::Event::KeyDown(key);
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
|
||||
let ev = InputPreprocessorMessage::KeyDown(key);
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
|
||||
}
|
||||
|
||||
/// A keyboard button released
|
||||
@@ -94,15 +60,15 @@ pub fn on_key_down(name: String) -> Result<(), JsValue> {
|
||||
pub fn on_key_up(name: String) -> Result<(), JsValue> {
|
||||
let key = translate_key(&name);
|
||||
log::trace!("key up {:?}, name: {}", key, name);
|
||||
let ev = events::Event::KeyUp(key);
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(ev)).map_err(convert_error)
|
||||
let ev = InputPreprocessorMessage::KeyUp(key);
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ev)).map_err(convert_error)
|
||||
}
|
||||
|
||||
/// Update primary color
|
||||
#[wasm_bindgen]
|
||||
pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
|
||||
EDITOR_STATE
|
||||
.with(|editor| editor.borrow_mut().handle_event(events::Event::SelectPrimaryColor(primary_color.inner())))
|
||||
.with(|editor| editor.borrow_mut().handle_message(ToolMessage::SelectPrimaryColor(primary_color.inner())))
|
||||
.map_err(convert_error)
|
||||
}
|
||||
|
||||
@@ -110,33 +76,35 @@ pub fn update_primary_color(primary_color: Color) -> Result<(), JsValue> {
|
||||
#[wasm_bindgen]
|
||||
pub fn update_secondary_color(secondary_color: Color) -> Result<(), JsValue> {
|
||||
EDITOR_STATE
|
||||
.with(|editor| editor.borrow_mut().handle_event(events::Event::SelectSecondaryColor(secondary_color.inner())))
|
||||
.with(|editor| editor.borrow_mut().handle_message(ToolMessage::SelectSecondaryColor(secondary_color.inner())))
|
||||
.map_err(convert_error)
|
||||
}
|
||||
|
||||
/// Swap primary and secondary color
|
||||
#[wasm_bindgen]
|
||||
pub fn swap_colors() -> Result<(), JsValue> {
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::SwapColors)).map_err(convert_error)
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ToolMessage::SwapColors)).map_err(convert_error)
|
||||
}
|
||||
|
||||
/// Reset primary and secondary colors to their defaults
|
||||
#[wasm_bindgen]
|
||||
pub fn reset_colors() -> Result<(), JsValue> {
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::ResetColors)).map_err(convert_error)
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(ToolMessage::ResetColors)).map_err(convert_error)
|
||||
}
|
||||
|
||||
/// Select a layer from the layer list
|
||||
#[wasm_bindgen]
|
||||
pub fn select_layer(path: Vec<LayerId>) -> Result<(), JsValue> {
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::SelectLayer(path))).map_err(convert_error)
|
||||
EDITOR_STATE
|
||||
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::SelectLayer(path)))
|
||||
.map_err(convert_error)
|
||||
}
|
||||
|
||||
/// Toggle visibility of a layer from the layer list
|
||||
#[wasm_bindgen]
|
||||
pub fn toggle_layer_visibility(path: Vec<LayerId>) -> Result<(), JsValue> {
|
||||
EDITOR_STATE
|
||||
.with(|editor| editor.borrow_mut().handle_event(events::Event::ToggleLayerVisibility(path)))
|
||||
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::ToggleLayerVisibility(path)))
|
||||
.map_err(convert_error)
|
||||
}
|
||||
|
||||
@@ -144,7 +112,7 @@ pub fn toggle_layer_visibility(path: Vec<LayerId>) -> Result<(), JsValue> {
|
||||
#[wasm_bindgen]
|
||||
pub fn toggle_layer_expansion(path: Vec<LayerId>) -> Result<(), JsValue> {
|
||||
EDITOR_STATE
|
||||
.with(|editor| editor.borrow_mut().handle_event(events::Event::ToggleLayerExpansion(path)))
|
||||
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::ToggleLayerExpansion(path)))
|
||||
.map_err(convert_error)
|
||||
}
|
||||
|
||||
@@ -152,18 +120,20 @@ pub fn toggle_layer_expansion(path: Vec<LayerId>) -> Result<(), JsValue> {
|
||||
#[wasm_bindgen]
|
||||
pub fn rename_layer(path: Vec<LayerId>, new_name: String) -> Result<(), JsValue> {
|
||||
EDITOR_STATE
|
||||
.with(|editor| editor.borrow_mut().handle_event(events::Event::RenameLayer(path, new_name)))
|
||||
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::RenameLayer(path, new_name)))
|
||||
.map_err(convert_error)
|
||||
}
|
||||
|
||||
/// Deletes a layer from the layer list
|
||||
#[wasm_bindgen]
|
||||
pub fn delete_layer(path: Vec<LayerId>) -> Result<(), JsValue> {
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::DeleteLayer(path))).map_err(convert_error)
|
||||
EDITOR_STATE
|
||||
.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::DeleteLayer(path)))
|
||||
.map_err(convert_error)
|
||||
}
|
||||
|
||||
/// Requests the backend to add a layer to the layer list
|
||||
#[wasm_bindgen]
|
||||
pub fn add_layer(path: Vec<LayerId>) -> Result<(), JsValue> {
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_event(events::Event::AddLayer(path))).map_err(convert_error)
|
||||
pub fn add_folder(path: Vec<LayerId>) -> Result<(), JsValue> {
|
||||
EDITOR_STATE.with(|editor| editor.borrow_mut().handle_message(DocumentMessage::AddFolder(path))).map_err(convert_error)
|
||||
}
|
||||
|
||||
@@ -4,11 +4,10 @@ pub mod utils;
|
||||
pub mod window;
|
||||
pub mod wrappers;
|
||||
|
||||
use editor_core::{events::Response, Editor};
|
||||
use editor_core::{message_prelude::*, Editor};
|
||||
use std::cell::RefCell;
|
||||
use utils::WasmLog;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wrappers::WasmResponse;
|
||||
|
||||
// the thread_local macro provides a way to initialize static variables with non-constant functions
|
||||
thread_local! { pub static EDITOR_STATE: RefCell<Editor> = RefCell::new(Editor::new(Box::new(handle_response))) }
|
||||
@@ -21,19 +20,20 @@ pub fn init() {
|
||||
log::set_max_level(log::LevelFilter::Debug);
|
||||
}
|
||||
|
||||
fn handle_response(response: Response) {
|
||||
let response_type = response.to_string();
|
||||
fn handle_response(response: FrontendMessage) {
|
||||
let response_type = response.to_discriminant().local_name();
|
||||
send_response(response_type, response);
|
||||
}
|
||||
|
||||
fn send_response(response_type: String, response_data: Response) {
|
||||
let response_data = JsValue::from_serde(&WasmResponse::new(response_data)).expect("Failed to serialize response");
|
||||
handleResponse(response_type, response_data);
|
||||
fn send_response(response_type: String, response_data: FrontendMessage) {
|
||||
let response_data = JsValue::from_serde(&response_data).expect("Failed to serialize response");
|
||||
let _ = handleResponse(response_type, response_data).map_err(|error| log::error!("javascript threw an error: {:?}", error));
|
||||
}
|
||||
|
||||
#[wasm_bindgen(module = "/../src/response-handler.ts")]
|
||||
extern "C" {
|
||||
fn handleResponse(responseType: String, responseData: JsValue);
|
||||
#[wasm_bindgen(catch)]
|
||||
fn handleResponse(responseType: String, responseData: JsValue) -> Result<(), JsValue>;
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
||||
@@ -20,7 +20,7 @@ pub fn get_active_document() -> DocumentId {
|
||||
todo!("get_active_document")
|
||||
}
|
||||
|
||||
use editor_core::workspace::PanelId;
|
||||
type PanelId = u32;
|
||||
/// Notify the editor that the mouse hovers above a panel
|
||||
#[wasm_bindgen]
|
||||
pub fn panel_hover_enter(panel_id: PanelId) {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
use crate::shims::Error;
|
||||
use editor_core::events;
|
||||
use editor_core::tools::{SelectAppendMode, ToolType};
|
||||
use editor_core::input::keyboard::Key;
|
||||
use editor_core::tool::{SelectAppendMode, ToolType};
|
||||
use editor_core::Color as InnerColor;
|
||||
use events::Response;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
@@ -26,15 +24,6 @@ impl Color {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct WasmResponse(Response);
|
||||
|
||||
impl WasmResponse {
|
||||
pub fn new(response: Response) -> Self {
|
||||
Self(response)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! match_string_to_enum {
|
||||
(match ($e:expr) {$($var:ident),* $(,)?}) => {
|
||||
match $e {
|
||||
@@ -85,8 +74,9 @@ pub fn translate_append_mode(name: &str) -> Option<SelectAppendMode> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn translate_key(name: &str) -> events::Key {
|
||||
use events::Key::*;
|
||||
pub fn translate_key(name: &str) -> Key {
|
||||
log::trace!("pressed key: {}", name);
|
||||
use Key::*;
|
||||
match name {
|
||||
"e" => KeyE,
|
||||
"v" => KeyV,
|
||||
@@ -109,6 +99,7 @@ pub fn translate_key(name: &str) -> events::Key {
|
||||
"9" => Key9,
|
||||
"Enter" => KeyEnter,
|
||||
"Shift" => KeyShift,
|
||||
"CapsLock" => KeyCaps,
|
||||
"Control" => KeyControl,
|
||||
"Alt" => KeyAlt,
|
||||
"Escape" => KeyEscape,
|
||||
|
||||
Reference in New Issue
Block a user