Skip to main content

NodeGraph

Struct NodeGraph 

Source
pub struct NodeGraph<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> NodeGraph<'a>

Source

pub fn new( nodes: Vec<NodeLayout<'a>>, connections: Vec<Connection<'a>>, width: usize, height: usize, ) -> Self

Source

pub fn with_direction(self, direction: FlowDirection) -> Self

Set the graph’s FlowDirection (which edge the root anchors to and which way children flow). The graph is marked dirty; call calculate before reading positions.

Defaults to FlowDirection::Rtl (the original hard-coded behavior).

Source

pub fn direction(&self) -> FlowDirection

The graph’s configured FlowDirection.

Source

pub fn highlight_style(self, style: Style) -> Self

Set the highlight Style applied to the selected node’s border during stateful render (see FlowState::selection). The hover node’s border gets this style with an added Modifier::DIM so the two states stay visually distinct. Defaults to bold yellow foreground.

Only the border is recolored (the port glyphs on the border keep their symbols and pick up the highlight’s fg/modifier); node content is never touched.

Source

pub fn show_arrows(self, on: bool) -> Self

Toggle drawing a direction arrow on each connection’s to (in) port, pointing in the flow direction (e.g. ◄ for Rtl, ▶ for Ltr, ▼ for Ttb). Off by default — pass true to replace the in-port connection glyph (┤/┴ family) with an arrow so the flow direction reads at a glance. The from (out) port always keeps its ├/┬ glyph.

Source

pub fn add_node(&mut self, node: NodeLayout<'a>) -> NodeId

Append a node, assigning it the next fresh NodeId (and bumping the internal counter). The graph is marked dirty; you must call calculate before reading positions.

node.title/content strings should outlive the graph; using a &'static str is the common case.

Source

pub fn add_node_with_id( &mut self, id: NodeId, node: NodeLayout<'a>, ) -> Result<(), AddNodeError>

Append a node with a caller-chosen NodeId. Returns AddNodeError::ConflictingId if id is already in use; otherwise the node is inserted (preserving render order) and the graph is marked dirty.

The id need not be contiguous — this is the path used to support non-contiguous node ids. The auto-increment counter is bumped past id only if id would otherwise collide with a future add_node result.

Source

pub fn add_connection(&mut self, conn: Connection<'a>)

Append a Connection. The graph is marked dirty.

Source

pub fn remove_node(&mut self, id: NodeId) -> bool

Remove the node with the given NodeId and cascade-delete every Connection that references it (on either endpoint). No-op (returns false) if id is not in the graph. The graph is marked dirty.

Returns true if a node was actually removed.

Source

pub fn remove_connection( &mut self, from: NodeId, from_port: PortId, to: NodeId, to_port: PortId, ) -> bool

Remove the first Connection matching the given endpoints. No-op (returns false) if no such connection exists. The graph is marked dirty.

Returns true if a connection was removed.

Source

pub fn has_node(&self, id: NodeId) -> bool

true if a node with the given NodeId exists in the graph.

Source

pub fn is_dirty(&self) -> bool

true if the graph changed since the last calculate (any node/connection added or removed). Mutators do not re-run layout automatically, so a dirty graph means positions / split may be stale until you call calculate.

Source

pub fn positions(&self) -> &Map<NodeId, Rect>

All node placements from the last calculate, keyed by NodeId. Each Rect is the node’s full frame (border included) in canvas coordinates (origin top-left of the off-screen canvas, x growing leftwards — see split for the mirroring).

Source

pub fn node_rect(&self, id: NodeId) -> Option<Rect>

The full-frame Rect (border included, canvas coordinates) of a single node, or None if the node was not placed (unreachable / unknown / never calculated).

Source

pub fn calculate(&mut self)

Source

pub fn diagnostics(&self) -> &[Diagnostic]

Non-fatal problems detected during the most recent calculate: unreachable (unplaced) nodes, ignored out-of-bounds connections, and connections that couldn’t be routed.

The slice is cleared at the start of each calculate, so it always reflects the latest run. Each entry is also emitted via log::warn!.

Source

pub fn split(&self, area: Rect) -> Vec<Rect>

Source

pub fn split_named(&self, area: Rect) -> Vec<(NodeId, Rect)>

Like split, but each entry is tagged with its NodeId. The order matches [split] (nodes in render order) and the rects are the same content rects (border removed, mirrored to screen coordinates within area).

Useful when you need to map a placed rect back to the node it belongs to (e.g. hit testing).

Source

pub fn hit_test(&self, area: Rect, x: u16, y: u16) -> Option<NodeId>

Hit-test a screen coordinate against the placed nodes and return the NodeId of the node whose full frame (border included) contains (x, y), or None if the point falls in empty space or on a node that was never placed.

(x, y) is interpreted relative to area’s top-left corner (i.e. they are screen coordinates within area), exactly like the rects returned by split / split_named. The hit area is each node’s bordered rect (a point on the border counts as a hit), matching the granularity used by the off-screen canvas — so the inner content rects from split_named are strictly contained in the hit rects.

Must be called after calculate; before that, positions is empty and hit_test always returns None. The transformation honors direction (the same canvas→screen mirror as split_named), so hit testing is correct for Ltr/Rtl/Ttb/Btt.

Nodes never overlap in canvas space, so at most one node can contain a given point; the first match (in render order) is returned. Viewport offset is not applied here — for a scrolled graph use the upcoming hit_test_viewport (Step 6), or pre-translate (x, y) by the offset.

Source

pub fn split_viewport(&self, area: Rect, viewport: &Viewport) -> Vec<Rect>

👎Deprecated since 0.1.0:

use split_stateful(area, &FlowState) with a FlowState whose view_offset is set

Like split, but returns each node’s content rect in screen coordinates for a scrolled viewport.

Each rect is the node’s canvas-rect (mirrored, inner border removed), translated by -viewport.offset + area.origin, then clipped to area. A node fully scrolled off-screen yields a 0×0 rect (render it only when width > 0 && height > 0).

Typical per-frame usage:

ⓘ
let zones = graph.split_viewport(view_area, &viewport);
for (i, z) in zones.iter().enumerate() {
    if z.width > 0 && z.height > 0 {
        f.render_widget(my_content[i], *z);
    }
}
f.render_widget(NodeGraphView::new(&graph).viewport(viewport), view_area);
Source

pub fn split_stateful( &self, area: Rect, state: &FlowState, ) -> Vec<(NodeId, Rect)>

Like split_named, but each content rect is translated by state.view_offset and clipped to area — i.e. it returns the screen-coordinate content rects for a scrolled/panned view driven by a FlowState.

This is the stateful-path counterpart of split_viewport: it yields (NodeId, Rect) pairs (so the caller can route content to the right node), and the rect math is identical — each node’s canvas-rect (mirrored, inner border removed) is shifted by -view_offset + area.origin and clipped to area. A node fully scrolled off-screen yields a 0×0 rect (render it only when width > 0 && height > 0).

Typical per-frame usage with the stateful render path:

ⓘ
let zones = graph.split_stateful(view_area, &flow_state);
for (id, z) in &zones {
    if z.width > 0 && z.height > 0 {
        f.render_widget(my_content_for(id), *z);
    }
}
f.render_stateful_widget(graph, view_area, &mut flow_state);

Trait Implementations§

Source§

impl<'a> Clone for NodeGraph<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for NodeGraph<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> StatefulWidget for NodeGraph<'a>

Source§

type State = FlowState

State associated with the stateful widget. Read more
Source§

fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State)

Draws the current state of the widget in the given buffer. That is the only method required to implement a custom stateful widget.

Auto Trait Implementations§

§

impl<'a> Freeze for NodeGraph<'a>

§

impl<'a> RefUnwindSafe for NodeGraph<'a>

§

impl<'a> Send for NodeGraph<'a>

§

impl<'a> Sync for NodeGraph<'a>

§

impl<'a> Unpin for NodeGraph<'a>

§

impl<'a> UnsafeUnpin for NodeGraph<'a>

§

impl<'a> UnwindSafe for NodeGraph<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.