Reusable Widgets & Inputs
Relevant source files
The following files were used as context for generating this wiki page:
- frontend/src/components/widgets/inputs/NumberInput.svelte
- frontend/src/components/widgets/inputs/ColorInput.svelte
- editor/src/messages/layout/utility_types/widgets/input_widgets.rs
- frontend/src/components/README.md
- frontend/src/README.md
- frontend/README.md
- website/content/volunteer/guide/codebase-overview/_index.md
Graphite utilizes a modular system of reusable widgets and inputs to construct its graphical user interface. These components serve as the interactive layer between the user and the editor's business logic, which resides in the Rust-based backend. The frontend, built with Svelte, focuses on presentation and reactivity, while the backend maintains the source of truth for the state of these widgets.
The widget system is designed to be lightweight and minimal, following a philosophy where the frontend handles user interactions and quickly hands off data to the WebAssembly (Wasm) backend for processing. This is achieved through a message-based communication system where widgets emit updates that are routed to the backend's message system.
Sources: frontend/README.md, website/content/volunteer/guide/codebase-overview/_index.md, frontend/src/README.md
Architecture and Data Flow
The architecture of Graphite's widgets relies on a bi-directional communication bridge between Svelte components and the Rust backend. Widgets in the frontend are responsible for displaying state provided by the backend and capturing user input to send back as updates.
Communication Pattern
Communication is facilitated by the EditorWrapper and a subscriptions router. When a user interacts with a widget (e.g., changing a number or selecting a color), the widget dispatches an event or calls a function that eventually triggers a message to the backend. The backend processes this, updates the internal state, and sends a FrontendMessage back to the frontend to update the UI.
Sources: website/content/volunteer/guide/codebase-overview/_index.md, frontend/src/README.md
The following diagram illustrates the lifecycle of a widget interaction:
The diagram shows how a user interaction flows from the frontend components through the router and wrapper to the backend business logic, and back again to update the UI state.
Sources: website/content/volunteer/guide/codebase-overview/_index.md, frontend/src/README.md
Bi-directional Props
In Svelte components, bi-directional data flow is often handled using custom event dispatchers. A component declares a property and a corresponding event dispatcher to notify the parent of changes.
const dispatch = createEventDispatcher<{ theProperty: type }>();
export let theProperty: type;
function onUpdate(newValue) {
dispatch("theProperty", newValue);
}
Sources: frontend/src/components/README.md
Frontend Input Components
Graphite provides specific input widgets tailored for different data types required by a graphics editor.
Input Widget Types
| Component | Description | Context/Source |
|---|---|---|
NumberInput | Handles numeric values, often supporting dragging, units, and ranges. | NumberInput.svelte |
ColorInput | Allows users to select colors, likely supporting various color spaces. | ColorInput.svelte |
DropdownInput | Provides a list of options for selection. | README.md |
TextInput | Standard text entry for names, labels, or values. | input_widgets.rs |
Sources: frontend/src/components/README.md, frontend/src/components/widgets/inputs/NumberInput.svelte, frontend/src/components/widgets/inputs/ColorInput.svelte
Component Structure
Each Svelte widget component typically consists of three sections:
- Script Section (TypeScript): Defines props, internal logic, and event dispatching.
- Template Section (HTML): Defines the structure and layout of the widget.
- Style Section (SCSS): Contains local styles for the component.
Sources: frontend/src/README.md
Backend Representation
The editor backend defines the metadata and structure for these widgets in Rust. This ensures that the layout and available controls are synchronized with the tools and nodes currently active in the editor.
Data Structures
The file editor/src/messages/layout/utility_types/widgets/input_widgets.rs contains the Rust definitions for these interactive elements. These structures define properties such as:
- Default values and ranges.
- Tooltips and labels.
- Visibility states (e.g., hidden or exposed).
- Associated units (e.g., pixels, percentages).
These definitions are converted into JSON-compatible formats and sent to the frontend to dynamically render the Properties panel or other UI areas.
Sources: editor/src/messages/layout/utility_types/widgets/input_widgets.rs, tools/node-docs/src/page_node.rs
This diagram represents the logical grouping of widgets as defined in the backend to be sent to the frontend for layout rendering.
Sources: editor/src/messages/layout/utility_types/widgets/input_widgets.rs
Implementation Guidelines
The project maintains specific standards for developing reusable widgets to ensure consistency and performance.
- Logic Location: Avoid implementing complex business logic in the Svelte components. Logic should be handled in the Rust backend.
- Reusability: Widgets should be designed to be placed in various contexts, such as panels (Properties, Layers) or floating menus (dialogs, popovers).
- State Management: Stores in the frontend (
/frontend/src/stores/) provide reactive state to components via Svelte's context API (setContext/getContext).
Sources: frontend/src/README.md, frontend/src/components/README.md
Reusable widgets and inputs form the backbone of the Graphite interface, providing a performant and consistent way for users to interact with the underlying procedural engine while maintaining a strict separation between UI presentation and core graphics logic.