Skip to content

Drawing Tools

The DrawingTools trait manages the active drawing tool and undo/redo history on any chart view. Both ChartView and ChartView implement this trait, enabling tool and history management to be written once and used with either view.

Import

rust
use navi_chart::{ChartView, ChartView, DrawingTools, DrawingToolId};

Usage

rust
use navi_chart::{DrawingTools, DrawingToolId};

fn activate_tool<C>(cv: &C, id: DrawingToolId)
where
    C: DrawingTools,
{
    cv.set_tool(id);
}

fn handle_keyboard_shortcut<C>(cv: &C, key: &str)
where
    C: DrawingTools,
{
    match key {
        "z" => { if cv.can_undo() { cv.undo(); } }
        "y" => { if cv.can_redo() { cv.redo(); } }
        "Escape" => cv.cancel_drawing(),
        _ => {}
    }
}

The tool is identified by the [DrawingToolId] enum, so unknown names are rejected at compile time and set_tool returns true for every valid variant.

Method Reference

MethodDescription
undo()Undo the last annotation creation, deletion, or modification. No-op if the history is empty.
redo()Redo the last undone action. No-op if there is nothing to redo.
can_undo()Return true if there is at least one action to undo.
can_redo()Return true if there is at least one action to redo.
finalize_pending_drawing()Force-complete an in-progress multi-point drawing (e.g. a polyline or Elliott wave) at the current number of points. Has no effect if no drawing is in progress.
cancel_drawing()Cancel the current in-progress drawing without creating an annotation. Resets the tool to pointer mode unless sticky mode is active.
set_tool(id)Activate the [DrawingToolId] variant. Pass DrawingToolId::Pointer to return to selection mode. Returns true after the switch is queued.
active_tool()Return the [DrawingToolId] of the currently active tool.
set_sticky_tool(sticky)Enable or disable sticky mode. When true, the tool stays active after each drawing is finalised — useful for placing multiple copies of the same shape without re-selecting the tool.
is_sticky_tool()Return true if sticky mode is enabled.

Tool IDs

DrawingToolId enumerates every tool the chart view can host.

CategoryVariantDescription
ModePointerDefault pan / zoom / select mode
TextEditingInline text editor (entered automatically; rarely set by hand)
LinesTrendlineTrend line
RayHalf-line extending past p1
ExtendedLineInfinite line through two points
HlineHorizontal line at a fixed price
HrayHorizontal ray from p0 rightward
VlineVertical line at a fixed time
CrosslineHorizontal + vertical cross
InfoLineTrend line with Δprice / % / angle info
TrendAngleTrend line with angle arc
ShapesRectAxis-aligned rectangle
CircleCircle
EllipseEllipse
TriangleThree-point triangle
RotatedRectFree-angle rectangle
PolylineMulti-point polyline (double-click to finish)
ArrowsArrowLine with arrowhead
ArrowMarkUpUpward arrow marker
ArrowMarkDownDownward arrow marker
ArrowMarkerFree-rotating arrow marker
ChannelsParallelChannelTwo parallel trend lines
FlatTopBottomTwo horizontal lines
DisjointChannelTwo independent trend lines
RegressionTrendLinear regression channel
Text & LabelsTextEditable text label
NoteText bubble
CalloutSpeech callout
PriceNoteText with price-axis label
PriceLabelAlias for PriceNote accepted by the parser
EmojiEmoji / glyph marker
FibonacciFibRetracementFibonacci retracement levels
FibExtensionFibonacci extension levels
FibChannelFibonacci channel
FibTimezoneFibonacci time zones
FibFanFibonacci fan rays
FibCirclesFibonacci circles
FibArcsFib speed resistance arcs
FibTrendBasedExtensionTrend-based Fibonacci extension
FibWedgeFibonacci wedge
FibSpiralFibonacci spiral
PitchforkPitchforkAndrews' pitchfork
SchiffPitchforkSchiff pitchfork
ModifiedSchiffPitchforkModified Schiff pitchfork
InsidePitchforkInside pitchfork
GannGannBoxGann box
GannSquareGann square
GannSquareFixedGann square (fixed aspect ratio)
GannFanGann fan
MeasurementPriceRangePrice range ruler
DateRangeDate range ruler
DateAndPriceRangeCombined ruler
BrushesBrushFree-hand brush stroke
HighlighterSemi-transparent highlight
PositionLongPositionLong risk/reward box
ShortPositionShort risk/reward box
PositionForecastForecast cone
CyclesCyclicLinesEqually-spaced vertical lines
TimeCyclesSemicircular arc series
SineLineSine wave
Chart PatternsXabcdPattern, CypherPattern, AbcdPattern, HeadAndShoulders, TrianglePattern, ThreeDrivesPatternHarmonic and chart patterns
Elliott WavesElliottImpulseWave, ElliottCorrectiveWave, ElliottTriangleWave, ElliottDoubleCombo, ElliottTripleComboElliott wave counts
Volume ProfileAnchoredVwap, VolumeProfileFixedRange, VolumeProfileAnchoredVolume analysis tools
CurvesPath, Arc, Curve, DoubleCurveMulti-point curves (path is double-click to finish)
MiscBarPattern, GhostFeed, SectorMiscellaneous tools

Sticky Mode

In sticky mode the tool remains active after each drawing is finalised, so the user can place multiple annotations of the same kind consecutively without re-selecting the tool:

rust
// Enable sticky mode before activating a tool
cv.set_sticky_tool(true);
cv.set_tool(DrawingToolId::Trendline);

// The tool stays as Trendline after each line is drawn.
// Revert to normal single-shot mode:
cv.set_sticky_tool(false);

Undo / Redo Integration

Wire undo and redo to keyboard shortcuts and toolbar buttons:

rust
use navi_chart::DrawingTools;

fn build_toolbar_state<C>(cv: &C) -> ToolbarState
where
    C: DrawingTools,
{
    ToolbarState {
        undo_enabled: cv.can_undo(),
        redo_enabled: cv.can_redo(),
        active_tool: cv.active_tool(),
        sticky: cv.is_sticky_tool(),
    }
}

fn on_undo_click<C>(cv: &C)
where
    C: DrawingTools,
{
    cv.undo();
    // Rebuild toolbar state to reflect updated can_undo / can_redo
}

Every add_annotation, update_annotation, remove_annotation, clear_annotations, and selection-mutation call (e.g. delete_selected_annotation) is tracked in the undo history automatically — no extra wiring is needed.

Released under the MIT License.