Input Mapping & Preprocessing
Relevant source files
The following files were used as context for generating this wiki page:
The Input Mapping and Preprocessing system in Graphite serves as the bridge between raw user interactions at the frontend and semantic actions within the editor backend. This system transforms hardware-level events—such as raw mouse movements, key presses, and stylus pressure—into a unified stream of messages that the editor's business logic can consume.
By separating the raw input handling (Preprocessing) from the logical command binding (Mapping), Graphite ensures that the editor's internal state remains decoupled from specific hardware platforms or frontend technologies.
Architecture & Data Flow
Graphite utilizes a tiered message-passing architecture to handle user input. The flow begins at the Frontend, passes through a Wasm wrapper, and is then handled by the Input Preprocessor (IPP) before reaching the Input Mapper.
The diagram shows the sequential transformation of input from hardware to editor logic.
Sources: website/content/volunteer/guide/codebase-overview/_index.md, frontend/README.md
Input Preprocessor (IPP)
The Input Preprocessor is the first stage in the backend that receives input data. Its primary responsibility is to maintain the current "hardware state" of the editor, such as the position of the mouse, the status of modifier keys (Shift, Ctrl, Alt), and the current pressure of a stylus.
Key Responsibilities
- Coordinate Transformation: Converting viewport coordinates into document or canvas space.
- State Tracking: Storing the current state of all keys and mouse buttons to allow for "chord" detection (e.g., Shift + Click).
- Message Normalization: Translating frontend-specific event data into a standardized
InputPreprocessorMessage.
Sources: editor/src/messages/input_preprocessor/input_preprocessor_message_handler.rs, website/content/volunteer/guide/codebase-overview/_index.md
Input Mapper
The Input Mapper sits between the Preprocessor and the rest of the editor. It listens for InputMapperMessage events and uses a mapping table to determine which high-level Message should be dispatched to the editor's message queue.
Mapping Logic
The mapper evaluates the incoming input against a set of KeyMapping entries. A mapping is triggered when the physical input matches the requirements, which often includes specific modifier combinations.
| Component | Description |
|---|---|
| Mapping Table | A collection of rules linking physical inputs to backend messages. |
| Modifier State | The current status of Alt, Control, and Shift, used to filter mappings. |
| Action Dispatcher | The mechanism that sends the mapped message to the relevant message handler. |
Sources: editor/src/messages/input_mapper/input_mapper_message_handler.rs, editor/src/messages/input_mapper/input_mappings.rs
Sequence of an Input Event
The following sequence diagram illustrates how a key press is converted into a logical action like "Undo".
The diagram shows a synchronous flow where a raw key event is mapped to a specific document command.
Sources: editor/src/messages/input_mapper/input_mapper_message_handler.rs, website/content/volunteer/guide/codebase-overview/_index.md
Configuration & Customization
Input mappings are defined in input_mappings.rs. This file serves as the central registry for the editor's hotkeys and mouse shortcuts.
Input Mapping Structure
Mappings typically consist of:
- Input Event: The trigger (e.g.,
MouseWheel,KeyDown). - Modifiers: Optional requirements for Shift, Control, or Alt.
- Target Message: The message to be sent when the trigger occurs.
// Conceptual example based on input_mappings.rs
KeyMapping {
input: InputEvent::KeyDown(Key::KeyZ),
modifiers: Modifiers::CONTROL,
action: Message::Document(DocumentMessage::Undo),
}
Sources: editor/src/messages/input_mapper/input_mappings.rs:1-50
Implementation Details
The system is implemented using the MessageHandler trait, which allows the Preprocessor and Mapper to react to messages asynchronously within the editor's main loop.
- InputPreprocessorMessageHandler: Manages the
InputPreprocessorStateand processesInputPreprocessorMessage. - InputMapperMessageHandler: Manages the input map and translates preprocessed input into logical actions.
Sources: editor/src/messages/input_preprocessor/input_preprocessor_message_handler.rs, editor/src/messages/input_mapper/input_mapper_message_handler.rs
Summary
The Input Mapping & Preprocessing system provides a robust abstraction layer for the Graphite editor. By handling hardware state in the Preprocessor and logical command association in the Mapper, the architecture allows for easy extension of hotkeys and support for diverse input devices without modifying the core functional logic of the graphics engine.