use smallvec::SmallVec;
use crate::chart::{Chart, HANDLE_CSS};
use crate::data::{DrawingId, PaneId};
use crate::drawing::{
Anchor, AnchorCount, FREEHAND_STEP_CSS, HitPart, index_of_time, time_of_index,
};
use crate::frame::Invalidation;
use crate::geom::{Css, PointCss, PointPx, Px};
use crate::pane::{MIN_PANE_CSS, SEPARATOR_HIT_CSS, SEPARATOR_PX, split_stretch};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Button {
#[default]
Left,
Middle,
Right,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[allow(clippy::struct_excessive_bools)]
pub struct Modifiers {
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub meta: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum WheelDelta {
Lines {
x: f32,
y: f32,
},
Pixels {
x: Css,
y: Css,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum GesturePhase {
Start,
Update,
End,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Key {
ArrowLeft,
ArrowRight,
ArrowUp,
ArrowDown,
Home,
End,
Escape,
Delete,
Enter,
Char(char),
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum InputEvent {
PointerDown {
id: u64,
pos: PointCss,
button: Button,
mods: Modifiers,
time_ms: f64,
},
PointerMove {
id: u64,
pos: PointCss,
mods: Modifiers,
time_ms: f64,
},
PointerUp {
id: u64,
pos: PointCss,
button: Button,
mods: Modifiers,
time_ms: f64,
},
PointerLeave,
Wheel {
pos: PointCss,
delta: WheelDelta,
mods: Modifiers,
},
Pinch {
phase: GesturePhase,
center: PointCss,
scale: f32,
},
Key {
key: Key,
pressed: bool,
mods: Modifiers,
},
DoubleClick {
pos: PointCss,
button: Button,
},
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CursorIcon {
#[default]
Default,
Crosshair,
Grab,
Grabbing,
EwResize,
NsResize,
Pointer,
Move,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct InputResult {
pub consumed: bool,
pub cursor: CursorIcon,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CrosshairMode {
#[default]
Normal,
Magnet,
Hidden,
}
const DRAG_THRESHOLD: f32 = 3.0;
pub const KINETIC_TAU_MS: f64 = 325.0;
pub const KINETIC_MIN_SPEED: f64 = 0.05;
const VELOCITY_WINDOW_MS: f64 = 100.0;
const RELEASE_PAUSE_MS: f64 = 50.0;
const LINE_PX: f32 = 40.0;
const WHEEL_ZOOM: f64 = 0.002;
const AXIS_DRAG_ZOOM: f64 = 0.005;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Region {
Plot(PaneId),
PriceAxis(PaneId),
Separator(usize),
TimeAxis,
Outside,
}
#[derive(Clone, Copy, Debug, Default)]
struct Sample {
t: f64,
x: f32,
}
#[derive(Clone, Copy, Debug, Default)]
pub struct VelocityTracker {
samples: [Sample; 8],
n_samples: usize,
}
impl VelocityTracker {
pub fn clear(&mut self) {
self.n_samples = 0;
}
pub fn record(&mut self, t: f64, x: f32) {
self.samples.copy_within(1.., 0);
let last = self.samples.len() - 1;
self.samples[last] = Sample { t, x };
self.n_samples = (self.n_samples + 1).min(self.samples.len());
}
pub fn release_velocity(&self, now: f64) -> f64 {
let recent = &self.samples[self.samples.len() - self.n_samples..];
let Some(last) = recent.last() else {
return 0.0;
};
if now - last.t > RELEASE_PAUSE_MS {
return 0.0;
}
let Some(first) = recent.iter().find(|s| last.t - s.t <= VELOCITY_WINDOW_MS) else {
return 0.0;
};
let dt = last.t - first.t;
if dt <= 0.0 {
return 0.0;
}
f64::from(last.x - first.x) / dt
}
}
#[derive(Clone, Copy, Debug, Default)]
pub(crate) enum Drag {
#[default]
Idle,
PressPending {
id: u64,
start: PointPx,
pane: PaneId,
},
Panning {
id: u64,
last: PointPx,
pane: PaneId,
},
ScalingPrice {
id: u64,
last_y: f32,
pane: PaneId,
},
ScalingTime {
id: u64,
last_x: f32,
},
ResizingPanes {
id: u64,
upper: PaneId,
lower: PaneId,
top: f32,
bottom: f32,
},
EditingDrawing {
id: u64,
drawing: DrawingId,
part: HitPart,
start: PointPx,
pane: PaneId,
},
}
impl Drag {
pub(crate) fn pane(self) -> Option<PaneId> {
match self {
Self::PressPending { pane, .. }
| Self::Panning { pane, .. }
| Self::ScalingPrice { pane, .. }
| Self::EditingDrawing { pane, .. } => Some(pane),
Self::ResizingPanes { upper, .. } => Some(upper),
Self::Idle | Self::ScalingTime { .. } => None,
}
}
pub(crate) fn involves(self, pane: PaneId) -> bool {
match self {
Self::ResizingPanes { upper, lower, .. } => upper == pane || lower == pane,
_ => self.pane() == Some(pane),
}
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Interaction {
pub(crate) drag: Drag,
pub(crate) kinetic: Option<(f64, Option<f64>)>,
velocity: VelocityTracker,
pub(crate) pointer: Option<PointPx>,
pub(crate) crosshair: CrosshairMode,
}
impl Default for Interaction {
fn default() -> Self {
Self {
drag: Drag::Idle,
kinetic: None,
velocity: VelocityTracker::default(),
pointer: None,
crosshair: CrosshairMode::Normal,
}
}
}
impl Chart {
pub(crate) fn region(&self, p: PointPx) -> Region {
let l = self.layout;
let in_width = p.x >= 0.0 && p.x < l.time_axis.right();
if in_width && p.y < l.time_axis.y {
let hit = SEPARATOR_HIT_CSS * self.dpr;
for k in 1..self.panes.len() {
let center = self.panes[k].plot.y - SEPARATOR_PX * 0.5;
if (p.y - center).abs() <= hit {
return Region::Separator(k);
}
}
}
if let Some(i) = self.pane_at(p) {
return Region::Plot(self.panes[i].id);
}
if l.price_axis.contains(p) {
return self
.panes
.iter()
.find(|pane| p.y >= pane.plot.y && p.y < pane.plot.bottom())
.map_or(Region::Outside, |pane| Region::PriceAxis(pane.id));
}
if l.time_axis.contains(p) {
Region::TimeAxis
} else {
Region::Outside
}
}
fn hover_cursor(&self, p: PointPx) -> CursorIcon {
match self.region(p) {
Region::Plot(pane) => {
if self.drawings.armed.is_some() || self.drawings.creating.is_some() {
return CursorIcon::Crosshair;
}
match self.hit_drawing(pane, p) {
Some((_, HitPart::Handle(_))) => CursorIcon::Move,
Some((_, HitPart::Body)) => CursorIcon::Pointer,
None => CursorIcon::Crosshair,
}
}
Region::PriceAxis(_) | Region::Separator(_) => CursorIcon::NsResize,
Region::TimeAxis => CursorIcon::EwResize,
Region::Outside => CursorIcon::Default,
}
}
fn set_pointer(&mut self, p: Option<PointPx>) {
if self.input.pointer != p {
self.input.pointer = p;
if self.input.crosshair != CrosshairMode::Hidden {
self.invalidation = self.invalidation.max(Invalidation::Cursor);
}
}
}
fn plot_x(&self, p: PointPx) -> f32 {
p.x - self.layout.plot.x
}
fn zoom_time(&mut self, anchor_x: f32, factor: f64) {
self.time_scale.zoom(Px(anchor_x), factor);
}
fn zoom_price_at(&mut self, pane: PaneId, y: f32, factor: f64) {
let Some(pane) = self.pane_mut(pane) else {
return;
};
let plot = pane.plot;
let center =
pane.price_scale
.y_to_price(f64::from(y), f64::from(plot.y), f64::from(plot.h));
pane.price_scale.zoom_around(factor, center);
}
fn resize_panes(&mut self, y: f32) {
let Drag::ResizingPanes {
upper,
lower,
top,
bottom,
..
} = self.input.drag
else {
return;
};
let min = MIN_PANE_CSS * self.dpr;
let (lo, hi) = (top + min, bottom - min - SEPARATOR_PX);
if lo > hi {
return;
}
let y = y.round().clamp(lo, hi);
let (Some(su), Some(sl)) = (self.pane_stretch(upper), self.pane_stretch(lower)) else {
return;
};
if let Some((a, b)) = split_stretch(su + sl, y - top, bottom - y - SEPARATOR_PX) {
for (id, s) in [(upper, a), (lower, b)] {
if let Some(p) = self.pane_mut(id) {
p.stretch = s;
}
}
self.invalidation = Invalidation::Full;
}
}
pub(crate) fn anchor_at(&self, pane: PaneId, p: PointPx) -> Option<Anchor> {
let i = self.pane_index(pane)?;
let ctx = self.drawing_ctx(i, &self.text_cache)?;
let plot = ctx.plot;
let idx = self.time_scale.x_to_index(Px(p.x - plot.x)).round();
let time = time_of_index(&self.time_index, idx)?;
let mut y = p.y;
if self.input.crosshair == CrosshairMode::Magnet
&& let Some(bar) = self.magnet_bar(pane, idx as i64)
{
let mut best = f32::INFINITY;
for v in bar {
if let Some(vy) = ctx.y_of(v)
&& (vy - p.y).abs() < (best - p.y).abs()
{
best = vy;
}
}
if best.is_finite() {
y = best;
}
}
let price = ctx.price_at(y);
price.is_finite().then_some(Anchor { time, price })
}
pub(crate) fn free_anchor_at(&self, pane: PaneId, p: PointPx) -> Option<Anchor> {
let i = self.pane_index(pane)?;
let ctx = self.drawing_ctx(i, &self.text_cache)?;
let time = ctx.time_at(p.x)?;
let price = ctx.price_at(p.y);
price.is_finite().then_some(Anchor { time, price })
}
pub(crate) fn hit_drawing(&self, pane: PaneId, p: PointPx) -> Option<(DrawingId, HitPart)> {
let i = self.pane_index(pane)?;
let ctx = self.drawing_ctx(i, &self.text_cache)?;
let tol = ctx.hit_tolerance();
let entries = &self.drawings.entries;
let selected = self
.drawings
.selected()
.and_then(|s| self.drawings.entry(s))
.filter(|e| e.pane == pane && e.visible);
if let Some(e) = selected {
let r = HANDLE_CSS * 0.5 * self.dpr + tol * 0.5;
let hit = self
.handle_buf
.try_borrow_mut()
.ok()
.and_then(|mut handles| {
handles.clear();
e.tool.handles(&ctx, &mut handles);
handles
.iter()
.position(|h| (p.x - h.x).hypot(p.y - h.y) <= r)
});
if let Some(k) = hit {
return Some((e.id, HitPart::Handle(k)));
}
if let Some(part) = e.tool.hit_test(&ctx, p) {
return Some((e.id, part));
}
}
entries.iter().rev().find_map(|e| {
let b = e.bbox;
let near = b.w + b.h > 0.0
&& p.x >= b.x - tol
&& p.x <= b.right() + tol
&& p.y >= b.y - tol
&& p.y <= b.bottom() + tol;
if e.pane != pane || !e.visible || !near {
return None;
}
Some((e.id, e.tool.hit_test(&ctx, p)?))
})
}
fn drawing_press(&mut self, id: u64, pane: PaneId, p: PointPx) -> Option<InputResult> {
let take = |cursor| {
Some(InputResult {
consumed: true,
cursor,
})
};
if let Some(c) = self.drawings.creating {
if let Some(a) = self.anchor_at(c.pane, p) {
self.drawings.creation_click(a, Some(p));
}
return take(CursorIcon::Crosshair);
}
if self.drawings.armed.is_some() {
if let Some(a) = self.anchor_at(pane, p) {
self.drawings.begin_creation(pane, a, p);
}
if self.drawings.creating_count() == Some(AnchorCount::Freehand)
&& let Some(a) = self.free_anchor_at(pane, p)
&& let Some(c) = self.drawings.creating
&& let Some(e) = self.drawings.entry_mut(c.id)
&& let Some(first) = e.tool.anchors_mut().first_mut()
{
*first = a;
}
return take(CursorIcon::Crosshair);
}
let Some((drawing, part)) = self.hit_drawing(pane, p) else {
let _ = self.drawings.select(None);
return None;
};
let _ = self.drawings.select(Some(drawing));
let e = self.drawings.entry(drawing)?;
if e.locked {
return None;
}
let before = e.tool.anchors().to_vec();
self.drawings.edit_before = before;
self.input.drag = Drag::EditingDrawing {
id,
drawing,
part,
start: p,
pane,
};
take(CursorIcon::Move)
}
fn drag_drawing(&mut self, p: PointPx) {
let Drag::EditingDrawing {
drawing,
part,
start,
pane,
..
} = self.input.drag
else {
return;
};
match part {
HitPart::Handle(k) => {
let Some(a) = self.anchor_at(pane, p) else {
return;
};
if let Some(e) = self.drawings.entry_mut(drawing) {
e.tool.move_handle(k, a);
}
}
HitPart::Body => {
let Some(i) = self.pane_index(pane) else {
return;
};
let Some(ctx) = self.drawing_ctx(i, &self.text_cache) else {
return;
};
let spacing = self.time_scale.bar_spacing();
let di = if spacing > 0.0 {
(f64::from(p.x - start.x) / spacing).round()
} else {
0.0
};
let dy = p.y - start.y;
let moved: SmallVec<[Anchor; 4]> = self
.drawings
.edit_before
.iter()
.map(|a| {
let time = if di == 0.0 {
Some(a.time)
} else {
index_of_time(&self.time_index, a.time)
.and_then(|j| time_of_index(&self.time_index, j + di))
};
let price = ctx.y_of(a.price).map(|y| ctx.price_at(y + dy));
match (time, price) {
(Some(time), Some(price)) if price.is_finite() => {
Anchor { time, price }
}
_ => *a,
}
})
.collect();
if let Some(e) = self.drawings.entry_mut(drawing) {
let anchors = e.tool.anchors_mut();
anchors.clear();
anchors.extend_from_slice(&moved);
}
}
}
self.drawings.dirty = true;
}
pub fn crosshair_mode(&self) -> CrosshairMode {
self.input.crosshair
}
pub fn set_crosshair_mode(&mut self, mode: CrosshairMode) {
self.input.crosshair = mode;
self.invalidation = self.invalidation.max(Invalidation::Cursor);
}
pub fn is_animating(&self) -> bool {
self.input.kinetic.is_some()
}
pub(crate) fn step_kinetic(&mut self, now_ms: f64) {
let Some((v, last)) = self.input.kinetic else {
return;
};
let Some(last) = last else {
self.input.kinetic = Some((v, Some(now_ms)));
self.time_scale.changed = true;
return;
};
let dt = now_ms - last;
if !(dt.is_finite() && dt > 0.0) {
self.input.kinetic = Some((v, Some(now_ms)));
return;
}
let decay = (-dt / KINETIC_TAU_MS).exp();
let distance = v * KINETIC_TAU_MS * (1.0 - decay);
let before = self.time_scale.right_offset();
self.time_scale.scroll_by(Px(distance as f32));
let v = v * decay;
let stuck = (self.time_scale.right_offset() - before).abs() < 1e-9;
self.input.kinetic = (v.abs() >= KINETIC_MIN_SPEED && !stuck).then_some((v, Some(now_ms)));
}
pub fn handle_input(&mut self, event: InputEvent) -> InputResult {
let dpr = self.dpr;
let consumed = |cursor| InputResult {
consumed: true,
cursor,
};
match event {
InputEvent::PointerDown {
id,
pos,
button,
time_ms,
..
} => {
let p = pos.to_px(dpr);
self.input.kinetic = None;
if button != Button::Left || !matches!(self.input.drag, Drag::Idle) {
return InputResult {
consumed: false,
cursor: self.hover_cursor(p),
};
}
match self.region(p) {
Region::Plot(pane) => {
if let Some(result) = self.drawing_press(id, pane, p) {
self.set_pointer(Some(p));
return result;
}
self.input.drag = Drag::PressPending { id, start: p, pane };
self.input.velocity.clear();
self.input.velocity.record(time_ms, p.x);
self.set_pointer(Some(p));
consumed(CursorIcon::Crosshair)
}
Region::PriceAxis(pane) => {
self.input.drag = Drag::ScalingPrice {
id,
last_y: p.y,
pane,
};
consumed(CursorIcon::NsResize)
}
Region::Separator(k) => {
let (Some(up), Some(down)) = (self.panes.get(k - 1), self.panes.get(k))
else {
return InputResult::default();
};
self.input.drag = Drag::ResizingPanes {
id,
upper: up.id,
lower: down.id,
top: up.plot.y,
bottom: down.plot.bottom(),
};
consumed(CursorIcon::NsResize)
}
Region::TimeAxis => {
self.input.drag = Drag::ScalingTime { id, last_x: p.x };
consumed(CursorIcon::EwResize)
}
Region::Outside => InputResult::default(),
}
}
InputEvent::PointerMove {
id, pos, time_ms, ..
} => {
let p = pos.to_px(dpr);
let in_plot = matches!(self.region(p), Region::Plot(_));
match self.input.drag {
Drag::Idle if self.drawings.creating.is_some() => {
if let Some(c) = self.drawings.creating {
if c.count == AnchorCount::Freehand {
if let Some(a) = self.free_anchor_at(c.pane, p) {
let step = FREEHAND_STEP_CSS * dpr;
self.drawings.freehand_move(a, p, step);
}
} else if let Some(a) = self.anchor_at(c.pane, p) {
self.drawings.creation_move(a);
}
}
self.set_pointer(in_plot.then_some(p));
consumed(CursorIcon::Crosshair)
}
Drag::EditingDrawing { id: d, .. } if d == id => {
self.drag_drawing(p);
self.set_pointer(in_plot.then_some(p));
consumed(CursorIcon::Move)
}
Drag::Idle => {
self.set_pointer(in_plot.then_some(p));
InputResult {
consumed: false,
cursor: self.hover_cursor(p),
}
}
Drag::PressPending {
id: d, start, pane, ..
} if d == id => {
let moved = (p.x - start.x).hypot(p.y - start.y);
if moved > DRAG_THRESHOLD * dpr {
self.input.drag = Drag::Panning {
id,
last: start,
pane,
};
return self.pan_to(p, time_ms);
}
self.set_pointer(in_plot.then_some(p));
consumed(CursorIcon::Crosshair)
}
Drag::Panning { id: d, .. } if d == id => self.pan_to(p, time_ms),
Drag::ScalingPrice {
id: d,
last_y,
pane,
} if d == id => {
let dy = f64::from((p.y - last_y) / dpr);
if let Some(scale) = self.pane_mut(pane).map(|p| &mut p.price_scale) {
let center = scale.center_price();
scale.zoom_around((dy * AXIS_DRAG_ZOOM).exp(), center);
}
self.input.drag = Drag::ScalingPrice {
id,
last_y: p.y,
pane,
};
consumed(CursorIcon::NsResize)
}
Drag::ResizingPanes { id: d, .. } if d == id => {
self.resize_panes(p.y);
consumed(CursorIcon::NsResize)
}
Drag::ScalingTime { id: d, last_x } if d == id => {
let dx = f64::from((p.x - last_x) / dpr);
let right = self.layout.plot.w;
self.zoom_time(right, (dx * AXIS_DRAG_ZOOM).exp());
self.input.drag = Drag::ScalingTime { id, last_x: p.x };
consumed(CursorIcon::EwResize)
}
_ => InputResult::default(),
}
}
InputEvent::PointerUp {
id, pos, time_ms, ..
} => {
let p = pos.to_px(dpr);
let drag = self.input.drag;
if let Some(c) = self.drawings.creating
&& matches!(drag, Drag::Idle)
{
let far = c
.down
.is_some_and(|d| (p.x - d.x).hypot(p.y - d.y) > DRAG_THRESHOLD * dpr);
if c.count == AnchorCount::Freehand {
if let Some(a) = self.free_anchor_at(c.pane, p) {
self.drawings.freehand_move(a, p, 1.0);
}
self.drawings.freehand_release();
return consumed(self.hover_cursor(p));
}
if let Some(c) = self.drawings.creating.as_mut() {
c.down = None;
}
if far && let Some(a) = self.anchor_at(c.pane, p) {
self.drawings.creation_click(a, None);
}
return consumed(CursorIcon::Crosshair);
}
if let Drag::EditingDrawing {
id: d,
drawing,
start,
pane,
..
} = drag
{
if d != id {
return InputResult::default();
}
self.input.drag = Drag::Idle;
let before = core::mem::take(&mut self.drawings.edit_before);
self.drawings.moved(drawing, before);
if (p.x - start.x).hypot(p.y - start.y) <= DRAG_THRESHOLD * dpr {
self.emit_click(pane, p, Some(drawing));
}
return consumed(self.hover_cursor(p));
}
let (Drag::PressPending { id: d, .. }
| Drag::Panning { id: d, .. }
| Drag::ScalingPrice { id: d, .. }
| Drag::ScalingTime { id: d, .. }
| Drag::ResizingPanes { id: d, .. }) = drag
else {
return InputResult {
consumed: false,
cursor: self.hover_cursor(p),
};
};
if d != id {
return InputResult::default();
}
self.input.drag = Drag::Idle;
if let Drag::PressPending { pane, .. } = drag {
self.emit_click(pane, p, None);
}
if let Drag::Panning { .. } = drag {
let v = self.input.velocity.release_velocity(time_ms);
if v.abs() >= KINETIC_MIN_SPEED {
self.input.kinetic = Some((v, Some(time_ms)));
self.time_scale.changed = true;
}
}
consumed(self.hover_cursor(p))
}
InputEvent::PointerLeave => {
self.set_pointer(None);
InputResult::default()
}
InputEvent::Wheel { pos, delta, mods } => self.wheel(pos.to_px(dpr), delta, mods),
InputEvent::Pinch {
phase,
center,
scale,
} => {
if phase == GesturePhase::Start {
self.input.kinetic = None;
}
let p = center.to_px(dpr);
if scale.is_finite() && scale > 0.0 {
let x = self.plot_x(p);
self.zoom_time(x, f64::from(scale));
}
consumed(self.hover_cursor(p))
}
InputEvent::Key { key, pressed, mods } => {
if !pressed {
return InputResult::default();
}
self.key(key, mods)
}
InputEvent::DoubleClick { pos, button } => {
let p = pos.to_px(dpr);
if button != Button::Left {
return InputResult::default();
}
if self.drawings.finish_open() {
return consumed(self.hover_cursor(p));
}
match self.region(p) {
Region::PriceAxis(pane) => {
if let Some(pane) = self.pane_mut(pane) {
pane.price_scale.set_auto_scale(true);
}
consumed(CursorIcon::NsResize)
}
Region::TimeAxis => {
let spacing = self.default_bar_spacing.to_px(dpr).0;
self.time_scale.set_bar_spacing(f64::from(spacing));
consumed(CursorIcon::EwResize)
}
_ => InputResult::default(),
}
}
}
}
fn pan_to(&mut self, p: PointPx, time_ms: f64) -> InputResult {
let Drag::Panning { id, last, pane } = self.input.drag else {
return InputResult::default();
};
self.time_scale.scroll_by(Px(p.x - last.x));
if let Some(pane) = self.pane_mut(pane)
&& !pane.price_scale.auto_scale()
{
let h = f64::from(pane.plot.h);
pane.price_scale.pan_px(f64::from(p.y - last.y), h);
}
self.input.velocity.record(time_ms, p.x);
self.input.drag = Drag::Panning { id, last: p, pane };
let in_plot = matches!(self.region(p), Region::Plot(_));
self.set_pointer(in_plot.then_some(p));
InputResult {
consumed: true,
cursor: CursorIcon::Grabbing,
}
}
fn wheel(&mut self, p: PointPx, delta: WheelDelta, mods: Modifiers) -> InputResult {
let (dx, dy) = match delta {
WheelDelta::Lines { x, y } => (x * LINE_PX, y * LINE_PX),
WheelDelta::Pixels { x, y } => (x.0, y.0),
};
if !(dx.is_finite() && dy.is_finite()) {
return InputResult::default();
}
let region = self.region(p);
if region == Region::Outside {
return InputResult::default();
}
self.input.kinetic = None;
let dpr = self.dpr;
if mods.shift {
let d = if dx.abs() > dy.abs() { dx } else { dy };
self.time_scale.scroll_by(Px(-d * dpr));
} else if let (Region::PriceAxis(pane), _) | (Region::Plot(pane), true) = (region, mods.alt)
{
self.zoom_price_at(pane, p.y, (f64::from(dy) * WHEEL_ZOOM).exp());
} else if dx.abs() > dy.abs() {
self.time_scale.scroll_by(Px(-dx * dpr));
} else {
let anchor = if region == Region::TimeAxis {
self.layout.plot.w
} else {
self.plot_x(p)
};
self.zoom_time(anchor, (-f64::from(dy) * WHEEL_ZOOM).exp());
}
InputResult {
consumed: true,
cursor: self.hover_cursor(p),
}
}
fn key(&mut self, key: Key, mods: Modifiers) -> InputResult {
let w = self.layout.plot.w;
let command = mods.ctrl || mods.meta;
let done = |consumed| InputResult {
consumed,
cursor: CursorIcon::Default,
};
match key {
Key::Char('z' | 'Z') if command => {
return done(if mods.shift {
self.drawings.redo()
} else {
self.drawings.undo()
});
}
Key::Char('y' | 'Y') if command => return done(self.drawings.redo()),
Key::Escape if self.drawings.creating.is_some() => {
self.drawings.escape_creation();
return done(true);
}
Key::Escape if self.drawings.armed.is_some() => {
self.drawings.cancel_tool();
return done(true);
}
Key::Enter => return done(self.drawings.finish_open()),
Key::Escape if matches!(self.input.drag, Drag::EditingDrawing { .. }) => {
let before = core::mem::take(&mut self.drawings.edit_before);
if let Drag::EditingDrawing { drawing, .. } = self.input.drag
&& let Some(e) = self.drawings.entry_mut(drawing)
{
let anchors = e.tool.anchors_mut();
anchors.clear();
anchors.extend_from_slice(&before);
}
self.drawings.dirty = true;
self.input.drag = Drag::Idle;
return done(true);
}
Key::Delete => {
let Some(sel) = self.drawings.selected() else {
return done(false);
};
if matches!(self.input.drag, Drag::EditingDrawing { .. }) {
self.input.drag = Drag::Idle;
}
return done(self.drawings.remove(sel).is_ok());
}
Key::ArrowLeft => self.time_scale.scroll_by(Px(w * 0.1)),
Key::ArrowRight => self.time_scale.scroll_by(Px(-w * 0.1)),
Key::ArrowUp => self.zoom_time(w, 1.25),
Key::ArrowDown => self.zoom_time(w, 0.8),
Key::Home => self.time_scale.scroll_to_first(),
Key::End => self.scroll_to_realtime(),
Key::Escape => {
let was_dragging = !matches!(self.input.drag, Drag::Idle);
self.input.drag = Drag::Idle;
self.input.kinetic = None;
return InputResult {
consumed: was_dragging,
cursor: CursorIcon::Default,
};
}
Key::Char(_) => return InputResult::default(),
}
InputResult {
consumed: true,
cursor: CursorIcon::Default,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn velocity_uses_recent_samples() {
let mut s = VelocityTracker::default();
for i in 0..10 {
s.record(f64::from(i) * 10.0, i as f32 * 5.0);
}
assert!((s.release_velocity(95.0) - 0.5).abs() < 1e-9);
assert!(s.release_velocity(200.0).abs() < 1e-12);
}
}