Skip to main content

DrawingManager

Struct DrawingManager 

Source
pub struct DrawingManager {
    pub registry: DrawingToolRegistry,
    pub tool_defaults: HashMap<DrawingToolType, DrawingTemplate>,
    pub drawings: Vec<Drawing>,
    pub options: DrawingManagerOptions,
    pub stay_in_drawing_mode: bool,
    pub curr_timeframe: String,
    pub pending_sync: Arc<Mutex<Vec<DrawingSyncAction>>>,
    /* private fields */
}
Expand description

Central coordinator for chart drawings.

DrawingManager owns the list of completed drawings and the in-progress drawing, and delegates business logic to specialized services:

§Frame loop integration

// 1. Sync state from app
manager.sync_from_app_state(drawing_state);

// 2. Update screen coordinates from chart coordinates
manager.update_all_screen_coords(bar_to_x, price_to_y);

// 3. Handle user input (clicks, drags, keyboard)
// ... (start_drawing_with_coords, add_point_with_coords, etc.)

// 4. Render against the chart's bars and coordinate mapping
let view = ChartView { price_formatter: None, time_formatter: None, bar_seconds: 0, instrument: None, bars, revision, bar_to_x: &bar_to_x, price_to_y: &price_to_y, y_to_price: &y_to_price, time_axis: true, executions: None, timeframe: None, finer: &[] };
manager.render_all(painter, price_rect, &view);

Fields§

§registry: DrawingToolRegistry

Host tools and built-in availability. Register tools before restoring their drawings.

§tool_defaults: HashMap<DrawingToolType, DrawingTemplate>

Per-tool defaults, exportable with serde for host-owned application storage. New drawings use these properties without copying geometry, identity or layer.

§drawings: Vec<Drawing>

All completed drawings on the chart.

§options: DrawingManagerOptions

Manager configuration (colors, snapping, handles).

§stay_in_drawing_mode: bool

Whether to stay in drawing mode after completing a drawing. A finished measurement always returns to selection mode.

§curr_timeframe: String

Current chart timeframe string (e.g., "1D", "1h"): drawings hidden on it are neither drawn nor hit. Chart::set_timeframe keeps it in step with the chart.

§pending_sync: Arc<Mutex<Vec<DrawingSyncAction>>>

Changes to the stored drawings, oldest first, for the host to drain, for example with std::mem::take(&mut *manager.pending_sync.lock().unwrap()). See DrawingSyncAction.

Implementations§

Source§

impl DrawingManager

Source

pub fn delete_drawing(&mut self, id: usize)

Deletes the drawing with the given ID from the drawing list.

The deleted drawing is pushed to the undo history and a cloud sync delete action is queued. If the deleted drawing was selected, the selection is cleared. A locked drawing is not deleted.

Source

pub fn delete_selected(&mut self)

Deletes the selected drawings, keeping any locked ones.

Source

pub fn clear_all(&mut self)

Removes every drawing that is not locked and clears the current drawing and selection.

Queues a Delete for each drawing it removes, so the host deletes their stored records. To unload drawings without deleting them, clear Self::drawings, then call Self::cancel_curr_drawing and Self::deselect. A bulk removal is one undo step.

Source

pub fn bring_to_front(&mut self, id: usize)

Bring a drawing to the front (drawn last, on top), above the highest z-order

Source

pub fn send_to_back(&mut self, id: usize)

Send a drawing to the back (drawn first, behind others), below the lowest z-order

Source

pub fn reorder_drawings(&mut self, id: usize, layers: &[(usize, i32)])

Apply a layer move as one undo step, saving each changed drawing.

Source

pub fn duplicate_drawing(&mut self, id: usize)

Duplicate a drawing three bars to the right, recording its creation for undo and saving. Adds nothing for a drawing of a disabled tool or a copy that saving would refuse.

Source

pub fn paste_drawings(&mut self, drawings: &[Drawing])

Paste copies three bars to the right with fresh identities, selecting them as one undoable addition. The caller maps clipboard anchors into this manager’s chart coordinates first. Leaves out a copy of a disabled tool or an unregistered custom tool and a copy that saving would refuse; when it leaves out every copy, it selects nothing and records no undo step.

Source

pub fn edit_drawing(&mut self, id: usize, edit: impl FnOnce(&mut Drawing))

Applies edit to the drawing with the given ID, records it for undo and queues it to be saved. Edit a drawing through this rather than Self::drawings so the change is saved. A locked drawing accepts only visibility, name and folder changes, or an edit that unlocks it. Does nothing if there is no such drawing.

Source

pub fn edit_drawings(&mut self, ids: &[usize], edit: impl FnMut(&mut Drawing))

Edits live drawing ids as one undo step and queues their saved changes. Locked drawings accept visibility, name and folder changes, or an edit that unlocks them.

Source

pub fn drag_edit_drawing(&mut self, id: usize, edit: impl FnOnce(&mut Drawing))

Applies edit as one frame of a continuous edit, such as dragging a colour picker: the first frame records the drawing’s state and nothing is queued until Self::end_drag_handle, which makes the whole gesture one undo step and queues one update. Does nothing if there is no such drawing or it is locked.

Source

pub fn update_all_screen_coords<F, G>(&mut self, bar_to_x: F, price_to_y: G)
where F: Fn(f32) -> f32 + Copy, G: Fn(f64) -> f32 + Copy,

Recomputes screen coordinates for all drawings (completed and in-progress) from their persistent chart coordinates using the current pan/zoom transforms.

Must be called each frame before rendering.

Source

pub fn update_pane_anchors(&mut self, pane: Rect)

Projects anchored annotations into pane after chart-coordinate updates. The chart calls this automatically; standalone managers call it before rendering.

Source

pub fn shift_bar_indices(&mut self, from: f32, shift: f32)

Shifts every bar index at or after from by shift, across all drawings, the in-progress drawing, the state a handle drag restores on undo, and the undo history.

This is called when bars are inserted in front of existing ones, causing the bar indices from there on to shift right. Pass f32::NEG_INFINITY for bars inserted in front of the first bar.

Source

pub fn toggle_visibility_all(&mut self)

Toggles the visibility of all drawings. If all are visible, hides them all; otherwise shows them all.

Source

pub fn set_visibility_all(&mut self, visible: bool)

Set every drawing’s visibility as one undoable edit.

Source

pub fn toggle_lock_all(&mut self)

Toggles the lock state of all drawings. If all are locked, unlocks them all; otherwise locks them all.

Source

pub fn add_drawing(&mut self, drawing: Drawing) -> Result<usize, &'static str>

Adds a completed drawing with validated chart coordinates and properties as one undo step. Assigns a fresh id, retains the supplied layer, and queues a save if saveable. Use Chart::create_drawing when the anchors are UTC times rather than bar indices.

Source

pub fn listed_drawings(&self) -> impl Iterator<Item = &Drawing>

Drawings included in host object lists, in drawing-list order.

Source

pub fn group_drawings<'a>( &'a self, group: &'a str, ) -> impl Iterator<Item = &'a Drawing>

Members of a host-defined group, including hidden and locked drawings.

Source

pub fn set_group(&mut self, ids: &[usize], group: Option<String>)

Assigns or removes group membership for live ids as one undo step. Locked drawings keep their membership. Group names are host-owned and are saved with each member.

Source

pub fn select_group(&mut self, group: &str)

Selects all selectable members of a group for the existing move, edit and delete operations. Those operations retain their lock checks and single undo step.

Source

pub fn add_synced_drawing( &mut self, drawing: Drawing, ) -> Result<usize, &'static str>

Add a drawing from another source (for multi-chart sync, or restored from storage) under a new id, and return the id. A drawing restored from storage keeps its stored identity (stored_id, created_at, notes) and its z-order. A drawing that was never stored goes on top, and so does a copy, one stored under the id of a drawing already here, which is saved as a new drawing.

Refuses, and adds nothing, a drawing that saving would refuse: a cursor tool, fewer anchors than the tool draws with, a non-finite coordinate, an anchor too far out to map to a time or a setting out of range. Tool availability does not apply: drawings of a disabled tool are restored and synced.

Source§

impl DrawingManager

Source

pub fn get_handles(&self, drawing: &Drawing) -> Vec<(HandlePos, Pos2)>

Returns the selection handle positions for a drawing (tool-type-aware). Regression trends use the fitted endpoints from the last Self::render_all.

Source

pub fn hit_test_handle( &self, drawing: &Drawing, point: Pos2, ) -> Option<HandlePos>

Hit-tests a point against the handles of a drawing.

Returns the HandlePos if a handle was hit, None otherwise.

Source

pub fn start_drag_handle(&mut self, drawing_id: usize, handle: HandlePos)

Begins a handle drag operation, saving the drawing’s current state for undo. A locked drawing is not dragged.

Source

pub fn start_drag_body(&mut self, drawing_id: usize, origin: Pos2)

Begins moving the anchors that move with the body (all but a bars pattern’s source) from a grab at origin, retaining the grab offset. Locked drawings ignore the grab. The move shares the handle drag’s undo and sync lifecycle.

Source

pub fn update_drag_handle<F, G, H>( &mut self, new_pos: Pos2, x_to_bar: F, y_to_price: G, translate_price: H, )
where F: Fn(f32) -> f32, G: Fn(f32) -> f64, H: Fn(f64, f32, f32) -> f64,

Updates the position of the currently dragged handle to new_pos, applying snap behavior and coordinate conversion. translate_price(price, from_y, to_y) shifts whole-drawing prices in scale units; y_to_price retains the pointer bounds for individual handles.

Source

pub fn end_drag_handle(&mut self)

Ends the current handle drag or Self::drag_edit_drawing gesture, pushing one modify command to the undo history and queuing one update for the whole gesture. A copy drag records the additions instead, so undo removes every copy.

Source

pub fn set_active_tool(&mut self, tool: Option<DrawingToolType>) -> bool

Sets the active drawing tool and returns whether it is now active. Pass None to exit drawing mode and return to selection/pointer mode. A tool cancels any in-progress drawing and leaves eraser mode. A built-in tool the registry disables is not set: the manager returns to selection mode, as with None, and the call returns false.

Source

pub fn active_custom_tool(&self) -> Option<&str>

The registered tool selected for the next placement, or None for a built-in tool.

Source

pub fn active_tool(&self) -> Option<DrawingToolType>

The built-in tool the next pointer placement uses, or None in selection mode. A registered tool is active as DrawingToolType::Polyline with its id in Self::active_custom_tool. Set it with Self::set_active_tool or Self::set_custom_tool.

Source

pub fn is_eraser_mode(&self) -> bool

Whether a click deletes the drawing under the pointer instead of selecting it. Switch it with Self::set_eraser_mode.

Source

pub fn curr_drawing(&self) -> Option<&Drawing>

The drawing being placed. It joins Self::drawings when completed, except for the temporary DrawingToolType::Measure ruler.

Source

pub fn curr_drawing_mut(&mut self) -> Option<&mut Drawing>

The drawing being placed, to adjust its settings before it is completed, such as the glyph size of an icon that Self::start_icon_insertion prepared.

Source

pub fn dragging_handle(&self) -> Option<(usize, HandlePos)>

The drawing and handle being dragged, as (drawing_id, handle), from Self::start_drag_handle or Self::start_drag_body until Self::end_drag_handle.

Source

pub fn set_custom_tool(&mut self, id: &str) -> Result<(), &'static str>

Starts a registered tool’s multi-click construction. One-anchor tools complete on the first click. The host picker uses registry.custom_tools() for its ids and names.

Source

pub fn show_custom_settings(&mut self, ui: &mut Ui, id: usize) -> bool

Shows a registered tool’s settings and records a changed draft as one undoable edit. Unknown tools and locked drawings show no editor and return false.

Source

pub fn set_eraser_mode(&mut self, on: bool)

Switches eraser mode on or off. In eraser mode a click on the chart deletes the drawing under the pointer, unless it is locked. Switching it on leaves the active tool and cancels any in-progress drawing; choosing a tool with Self::set_active_tool switches it off.

Source

pub fn start_text_annotation(&mut self)

Start text annotation mode

Activates the Note drawing tool, allowing the user to click the bar and price to annotate, then where the label goes.

Source

pub fn start_icon_insertion(&mut self, icon_name: String)

Start icon/emoji insertion mode

Prepares a bundled icon, Unicode emoji or sticker: glyph, allowing the user to click on the chart to place it.

Source

pub fn start_post_insertion(&mut self, tool: DrawingToolType, post: PostLink)

Prepares a post or idea card showing post, placed by the next click on the chart, as Self::start_icon_insertion does for a picked icon. The tool’s saved defaults apply, and the inline editor does not open, since the card has its content. Does nothing for other tools or a disabled one.

Source

pub fn start_drawing_with_coords<F, G>( &mut self, tool_type: DrawingToolType, point: Pos2, x_to_bar: F, y_to_price: G, )
where F: Fn(f32) -> f32, G: Fn(f32) -> f64,

Starts a new drawing of the given tool type at the given screen position, or places the drawing Self::start_icon_insertion prepared for it. Does nothing for a cursor tool or a disabled built-in tool. A completed drawing that saving would refuse, such as one whose anchor maps to a non-finite position, is dropped.

The coordinate conversion closures (x_to_bar, y_to_price) convert screen coordinates to chart coordinates for persistent storage. If the tool requires only one point, the drawing is immediately completed and added to the drawing list.

Source

pub fn add_point_with_coords<F, G>( &mut self, point: Pos2, x_to_bar: F, y_to_price: G, )
where F: Fn(f32) -> f32, G: Fn(f32) -> f64,

Adds a subsequent point to the in-progress drawing.

If the added point completes the drawing (based on DrawingToolType::required_points), the drawing is finalized, pushed to the drawing list, and a cloud sync action is queued.

Source

pub fn update_last_point_with_coords<F, G>( &mut self, point: Pos2, x_to_bar: F, y_to_price: G, )
where F: Fn(f32) -> f32, G: Fn(f32) -> f64,

Moves the pointer preview of the in-progress drawing to point, adding it after the placed anchors if there is none yet. The next click or the end of a drag replaces it with an anchor; finishing a multi-point drawing drops it.

Source

pub fn complete_curr_drawing(&mut self)

Manually completes the in-progress drawing (e.g., on double-click or Enter for multi-point tools), without its pointer preview. Requires at least 2 points, and as many as the tool draws with, so that what it adds can be saved. A drawing that saving would still refuse is dropped, and the tool stays active.

Source

pub fn cancel_curr_drawing(&mut self)

Cancels the in-progress drawing without adding it to the drawing list, and clears a finished measurement.

Source

pub fn complete_drag_drawing(&mut self)

Complete a drag-to-draw drawing (see Self::complete_curr_drawing)

Source

pub fn update_drag_handle_with_coords<F, G, H>( &mut self, new_pos: Pos2, x_to_bar: F, y_to_price: G, translate_price: H, )
where F: Fn(f32) -> f32, G: Fn(f32) -> f64, H: Fn(f64, f32, f32) -> f64,

Alias for Self::update_drag_handle, with the same pointer mapping and price translation.

Source

pub fn hit_test_handle_by_id( &self, point: Pos2, drawing_id: usize, ) -> Option<HandlePos>

Hit test a handle by drawing ID Returns the handle position if hit, None otherwise

Source§

impl DrawingManager

Source

pub fn render_handles(&self, painter: &Painter, drawing: &Drawing)

Renders selection handles for a drawing, highlighting the currently dragged handle (if any).

Source

pub fn hit_test(&self, point: Pos2) -> Option<usize>

Hit-tests a screen point against all visible drawings (back-to-front).

Returns the ID of the topmost drawing at point, or None if no drawing was hit. Delegates to DrawingInteraction::hit_test, the single source of truth for per-segment / per-level geometry hit testing, so that channels, Fibonacci levels, fans, and pitchforks are selectable along their drawn geometry rather than only near a control point. Honors the active timeframe so drawings hidden on the current timeframe are not hit. After Self::render_all, uses the actual clipped paint geometry, including text and data-driven bodies. Before the first paint, falls back to the tool’s geometric outline.

Source

pub fn render_all( &self, painter: &Painter, price_rect: Rect, view: &ChartView<'_>, )

Render all drawings against the chart’s bars and coordinate mapping

Source§

impl DrawingManager

Source

pub fn set_sync_enabled(&mut self, enabled: bool)

Enables or disables per-drawing storage notifications. Disabling clears pending actions. Automatic chart storage disables them; enable them again to also consume notifications. Changes made while disabled are not reported.

Source

pub fn new() -> Self

Creates a new DrawingManager with default options and empty drawing list.

Source

pub fn sync_from_app_state(&mut self, drawing_state: &DrawingState)

Apply the host’s tool, snapping, color, eraser and selection state. A state with both a tool and the eraser leaves only the eraser on, as Self::set_eraser_mode does.

Source

pub fn sel_drawing(&self) -> Option<usize>

Returns the ID of the currently selected (primary) drawing, if any.

Source

pub fn select(&mut self, id: usize)

Selects the drawing with the given ID (deselects all others).

Source

pub fn select_drawings(&mut self, ids: impl IntoIterator<Item = usize>)

Replace the selected drawings with the supplied live ids.

Source

pub fn selected_ids(&self) -> impl Iterator<Item = usize> + '_

Selected live drawing ids, in drawing-list order.

Source

pub fn is_selected(&self, id: usize) -> bool

Whether a drawing belongs to the current selection.

Source

pub fn toggle_selection(&mut self, id: usize)

Toggle a live drawing in the selection, retaining the other selected drawings.

Source

pub fn edit_selected(&mut self, edit: impl FnMut(&mut Drawing))

Apply a style edit to every unlocked selected drawing as one undo step.

Source

pub fn deselect(&mut self)

Deselects all drawings.

Source

pub fn select_at(&mut self, point: Pos2) -> bool

Hit-tests at point and selects the topmost drawing found.

Returns true if a drawing was selected, false if the click hit empty space (which deselects all drawings).

Source

pub fn undo(&mut self) -> bool

Undoes the last drawing operation. Returns true if an operation was undone.

Source

pub fn redo(&mut self) -> bool

Redoes the last undone operation. Returns true if an operation was redone.

Source

pub fn can_undo(&self) -> bool

Returns true if there are operations available to undo.

Source

pub fn can_redo(&self) -> bool

Returns true if there are operations available to redo.

Source

pub fn update_snap_options(&mut self)

Rebuilds the internal SnapService from the current manager options. Call this after changing snap-related options.

Source

pub fn snap_point(&self, point: Pos2, drawings: &[Drawing]) -> Pos2

Applies snap behavior to a screen point, returning the snapped position.

With magnet mode on, snaps to nearby chart values and visible anchors of drawings. Targets keep their time and price paired. A dragged drawing cannot snap to itself. Drawing anchors are borrowed without flattening or cloning their geometry.

Source

pub fn new_drawing(&self, id: usize, tool: DrawingToolType) -> Drawing

A new drawing of tool in its default style: the tool’s own, recoloured by DrawingManagerOptions::default_color when set, then the default saved for the tool.

Trait Implementations§

Source§

impl Default for DrawingManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> MaybeSend for T
where T: Send + Sync + ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.