Skip to main content

Module graph

Module graph 

Source
Expand description

A node graph editor over an application’s graph, drawn in egui’s Scene, its pan and zoom driven by the pointer profile.

Experimental: may change in any release (docs/stability.md).

The application keeps the graph. It describes its nodes, their typed ports, its wires and groups through GraphModel; the editor draws them and hands back what the user did as Edits (connect, disconnect, move, delete, add, a parameter changed), which the application applies to its graph or not. Each edit goes through the on_edit hook first: allow, veto or redirect (egui-pop-imaging runs them as the graph.* steps).

  • Pan and zoom, as the pointer profile says (its canvas.graph scope, docs/pointer-gestures.md): by default the wheel pans, Ctrl (Cmd) and the wheel or a pinch zoom around the pointer, a drag with the middle button pans, and a double click on the canvas fits the graph; a drag with Space held always pans. GraphMinimap is the navigator’s pattern: the whole graph, the part in view framed.
  • Levels: the zoom picks what is drawn (Level): the overview (compact nodes, groups folded into stacks), the ports, and the detail, where each node’s parameters are egui-pop sliders edited inline.
  • Nodes: a click selects (Ctrl or Shift adds), a drag moves the selection (one Edit::Move at its end, snapped to the grid with snap; Escape puts it back), the chevron or a double click on the title collapses, a slot in the title bar is the application’s (header), and so is a status drawn there (status: a short word in a tone, a colour or a theme token’s colour, its detail as a tooltip, read aloud as part of the node’s description). A drag on the canvas draws a rubber band.
  • Wires: a drag from a port draws a wire, checked against the ports’ types while it is drawn; dropped on a port that accepts it, it connects (replacing an input’s wire); a drag off a connected input takes its wire along, dropped nowhere it disconnects. A click on a port, then on another, connects too.
  • A wire itself answers the pointer within 6 points (12 on a touch screen), where no node or port is, at the ports and detail levels: hovered, the pointer becomes a hand and the wire is drawn thicker and lighter with its ports ringed and a tooltip naming its ends; a click selects it (and no node), Ctrl (Cmd) or Shift adds it to the nodes and wires selected; selected, it is drawn thicker in the selection’s colour over a soft glow, with a larger handle near each end and its ports ringed (graph.wire-hover.*, graph.wire-selected.*). A drag on it takes its nearer end along (one of an output’s several wires too): dropped on a port that accepts it, one Edit::Connect whose replaces holds the old wire; dropped nowhere, a disconnect. Delete removes the wires selected as one Edit::Disconnect (with nodes, one Edit::Delete); its menu disconnects it or selects the node at either end. A click on the canvas clears the selection, nodes and wires.
  • Groups: a frame around the group’s nodes, whose title folds them into a stack (a click on the stack unfolds it).
  • Keyboard (GraphKeys): Tab goes through the nodes and their ports; on a node the arrows go to the nearest node that way and Shift with the arrows moves the selection; on a port the arrows go to the node’s other ports, Enter or Space picks it as a wire’s end (twice: connected), Delete disconnects it and Escape goes back to the node. After a connected input, Tab reaches each of its wires: Delete disconnects it, Enter picks its input end up and Shift+Enter its output end (then a port, as above), the menu key opens its menu, Escape goes back to the port. Delete removes the selection, Ctrl+D duplicates it, Ctrl+A selects everything, Home fits. A large graph is one Tab stop (TabStops): Tab enters and leaves it, Enter goes into a node’s parts and Escape back.
  • Accessibility: the canvas is a group named by its label, each node a group (role description “node”) with its ports as buttons, their type and wires as the value, and each wire into an input a button after it (role description “wire”, named by its two ports, its keys as the description, selected or not); refusals, wires made or removed and the wires selected are announced.
use egui_pop_viewport::graph::{Edge, Edit, GraphEditor, GraphModel, GraphState, Node};

struct Pipeline {
    nodes: Vec<Node>,
    edges: Vec<Edge>,
}

impl GraphModel for Pipeline {
    fn nodes(&self) -> Vec<Node> {
        self.nodes.clone()
    }
    fn edges(&self) -> Vec<Edge> {
        self.edges.clone()
    }
}

fn ui(ui: &mut egui::Ui, graph: &mut Pipeline, state: &mut GraphState) {
    let out = GraphEditor::new(&*graph, state).snap(Some(10.0)).show(ui);
    for edit in out.edits {
        if let Edit::Connect { from, to, replaces } = edit {
            graph.edges.retain(|e| !replaces.contains(e));
            graph.edges.push(Edge::new(from, to));
        }
    }
}

Modules§

yaml
A reference GraphModel kept in a YAML file, for applications that have no graph model of their own (the graph-yaml feature).

Structs§

Edge
A wire, from an output to an input.
ExportOptions
What an export draws.
ExportTheme
What an export draws with: the theme’s colours and text sizes, resolved, so that an export needs no egui::Context.
GraphEditor
The node graph editor.
GraphExport
A graph ready to export: an owned copy of what the editor draws, kept as long as needed (an agent’s graph.export reads the copy the editor kept at its last frame).
GraphKeys
The node editors’ keys, as data for input profiles (set_keys).
GraphMinimap
The whole graph and the part in view, sharing the editor’s GraphState. A drag of the frame pans (Escape puts the view back), a press elsewhere centres the view there, the wheel zooms, a double click fits; focused, the arrows pan, + and - zoom, 0 fits.
GraphOutput
What the editor did in a frame.
GraphState
A node editor’s state: keep it with the graph’s view.
GraphTokens
The node graph’s colours, from the theme (graph.*).
Group
A group of nodes: drawn as a frame around them, or folded into a stack.
Kind
A kind of node the user may add (the canvas’s menu).
Moved
A node moved: where it was and where it goes.
NewNode
A node to add: its kind and place, or a copy of another node.
Node
A node as the application describes it.
NodeStatus
A node’s status: a short text drawn small at its title bar’s right end (left of the header slot), in its colour, its detail shown as a tooltip; both are part of the node’s accessible description.
Param
A node’s parameter, edited inline with a slider in the detail view.
Port
A port: where a wire ends (an input) or starts (an output).
PortRef
A port of a node: a node’s id and the port’s id within it.
PortType
What flows through a port (image, mask, number), and the token its ports and wires are drawn with.

Enums§

Edit
What the editor asks the application to do to its graph.
ExportError
Why a PNG or PDF export failed.
GraphEvent
What the editor reports besides edits.
Level
How much the editor shows, from the zoom.
Refusal
Why a wire is refused.
Request
What an application (or an agent, through a command) asks of the editor, done at its next frame as if the user had done it.
Side
Which side of a node a port is on.
StatusColor
The colour of a node’s status.
TabStops
How Tab goes through a graph, set with GraphEditor::tab_stops.
WireStyle
How a graph’s wires are drawn (GraphEditor::wires).

Constants§

ONE_STOP_FROM
From how many nodes a graph is one Tab stop with TabStops::Auto.
TYPE_TOKENS
The colour tokens a port type takes by default, one picked from its name (graph.type-1 to graph.type-6).

Traits§

GraphModel
The graph, as the application keeps it. The editor reads it each frame and never changes it: what the user does comes back as Edits for the application to apply.

Functions§

check
The default type check: an output feeds an input of the same type, on another node.
describe
The graph as drawn: nodes (place, ports and their wires, group, collapsed, selected, parameters), wires, groups, the selection (nodes, and wires as selected_edges), the view and the level; the wire style, and each wire’s route when it is orthogonal. What graph.read answers.
keys
The node editors’ keys in a context (the defaults until set).
set_keys
Remaps the node editors’ keys in a context (an input profile).