Skip to content

Diagram surface

Nodes and connectors, built entirely on top of the document model. Bit.Bink knows nothing about diagrams: a node is a rectangle, a connector is a line and a label is text, and what joins them is element metadata plus one listener. Move a node and its connectors and label follow, in the same undo step as the move.

Drawing with 8 elements on 2 layers. Connector. Connector. Diagram node: Capture. Diagram node: Process. Diagram node: Store. Label: Capture. Label: Process. Label: Store.
Drag with Node to draw a box (a click makes a default-sized one), then drag with Connector from one box to another. Switch to Select and move a node: the connectors re-route while the move is still being reported, so the document never shows a connector detached from its node, and one undo puts everything back. Delete a node, and its connectors and label go with it. Select a single node to rename it, or pick a colour to recolour the selection in one undo step. The wheel pans and Ctrl+wheel zooms.

Derived geometry, in the same undo step

The diagram is one listener on BinkDocument.Changed (Diagramming/DiagramGraph.cs). When a node moves it reroutes every connector touching it, applying those edits while it is still handling the change that caused them. The document reports them with a greater Depth, and history folds them into the same undo step - so connector endpoints and label positions are recomputed, never stored as offsets.

// Inside the Changed handler: a consequence of the change being handled, not a new edit.
using (_document.BeginBatch("Update diagram"))
{
    if (updates.Count > 0) _document.ReplaceElements(updates, "Route connectors");
    foreach (BinkElement label in additions) _document.AddElement(label, LabelLayerId);
    if (deletions.Count > 0) _document.RemoveElements(deletions);
}

Custom tools

Node and Connector are the demo's own tools (Diagramming/DiagramTools.cs). A custom tool needs nothing from the library beyond BinkToolBase: it reads pointer samples, draws its own preview on the overlay, and adds an ordinary element when the gesture ends. Undo, selection, hit testing, serialization and export follow because the result is a normal document element.

public sealed class NodeTool : BinkToolBase
{
    public override string Id => "diagram-node";
    public override string DisplayName => "Node";
    public override string Cursor => "crosshair";

    public override bool OnPointerDown(IBinkToolContext context, in BinkInputSample sample) { … }
}

The connector tool shows its own dashed preview while it is dragged, and announces "Start a connector on a node" when a drag starts on empty space, so a screen reader hears why nothing happened.

Snapping, in a tool of your own

With Snap to grid on, the canvas shows graph paper (BinkGridStyle.Lines, every fifth line stronger) and snaps to it. Moving a node with the select tool snaps by itself; the node tool is the demo's own, and snaps the way the built-in shape tool does: it builds a BinkSnapper from the canvas's options when the drag starts, which collects every edge and centre to snap to once, so each frame is a lookup. It lines a corner up with other nodes before the grid, and draws a guide while it does.

<BinkCanvas Grid="new BinkGrid { Style = BinkGridStyle.Lines, Spacing = 20f, MajorEvery = 5 }"
            Snapping="new BinkSnapOptions { GridSize = 20f }" />

// In the tool, when the drag starts:
_snapper = BinkSnapper.Create(context.Document, context.Snapping, null, context.Viewport.VisibleRect(context.ViewSize));
// and on every move:
point = _snapper.SnapPoint(point, context.Snapping.Threshold / context.Viewport.Zoom, guides: _guides);

Labels that never get in the way

A node's label is stored in the node's metadata; what is drawn is an ordinary text element derived from it and kept centred on the node. The labels live on a locked layer above the diagram, so they are drawn over the boxes, while a click on the text still reaches the node underneath, because locked layers are not hit tested. Every element carries a description ("Diagram node: Process", "Connector"), so the canvas's accessible summary and the exported SVG both say what the diagram contains.

An unhandled error has occurred. Reload 🗙