1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! User interaction handling for the chart widget.
//!
//! Implements tracking mode (auto-scroll to latest bar) and keyboard shortcuts
//! (arrow keys for pan, +/- for zoom, Home for jump-to-latest, F for fit-all).
//! These methods are called by the main widget paint loop.
use egui::{Response, Ui};
use crate::widget::Chart;
impl Chart {
/// Manage tracking mode — auto-scroll to the latest bar while active.
///
/// Tracking mode keeps the chart viewport anchored to real-time data.
/// It exits automatically based on the configured
/// [`TrackingModeExitMode`](crate::config::TrackingModeExitMode): on mouse leave,
/// on next tap, or on touch end. Any manual scroll/zoom/drag also deactivates it.
pub fn handle_tracking_mode(&mut self, ui: &Ui, response: &Response) {
// Track mouse entering/leaving chart area for tracking mode
let was_in_chart = self.mouse_in_chart;
self.mouse_in_chart = response.hovered();
// Handle tracking mode exit conditions
use crate::config::TrackingModeExitMode;
if self.tracking_mode_active {
match self.chart_options.tracking_mode.exit_mode {
TrackingModeExitMode::OnMouseLeave => {
// Exit when mouse leaves the chart
if was_in_chart && !self.mouse_in_chart {
self.tracking_mode_active = false;
}
}
TrackingModeExitMode::OnNextTap => {
// Exit on any click
if response.clicked() {
self.tracking_mode_active = false;
}
}
TrackingModeExitMode::OnTouchEnd => {
// Exit when drag/touch ends
if !response.dragged() && self.scroll_start_pos.is_some() {
self.tracking_mode_active = false;
}
}
}
// Exit tracking mode on any manual interaction (scroll, zoom, drag)
if response.dragged()
|| ui.input(|i| {
i.raw_scroll_delta.length_sq() > 0.0 || i.smooth_scroll_delta.length_sq() > 0.0
})
{
self.tracking_mode_active = false;
}
}
// If tracking mode is active, keep scrolling to latest
if self.tracking_mode_active {
self.state.time_scale_mut().scroll_to_realtime();
}
}
/// Process keyboard shortcuts when the chart has focus.
///
/// | Key | Action |
/// |-----|--------|
/// | Left/Right | Pan by configured `pan_amount` bars |
/// | +/- | Zoom in/out by `zoom_step` centered on chart |
/// | Home | Scroll to latest (real-time) data |
/// | F | Fit all data into the viewport |
/// | PageUp/PageDown | Zoom in/out by `3x zoom_step` |
///
/// Shortcuts are only active when `chart_options.keyboard.enabled` is `true`
/// and the chart response has focus.
pub fn handle_keyboard_shortcuts(
&mut self,
ui: &Ui,
response: &Response,
chart_width: f32,
chart_rect_min_x: f32,
) {
if !self.chart_options.keyboard.enabled || !response.has_focus() {
return;
}
ui.input(|i| {
use egui::Key;
// Pan left (Left arrow)
if i.key_pressed(Key::ArrowLeft) {
self.state
.time_scale_mut()
.scroll_bars(-self.chart_options.keyboard.pan_amount);
}
// Pan right (Right arrow)
if i.key_pressed(Key::ArrowRight) {
self.state
.time_scale_mut()
.scroll_bars(self.chart_options.keyboard.pan_amount);
}
// Zoom in (+)
if i.key_pressed(Key::Plus) || i.key_pressed(Key::Equals) {
let zoom_point_x = chart_width / 2.0;
self.state.time_scale_mut().zoom(
self.chart_options.keyboard.zoom_step,
zoom_point_x,
chart_rect_min_x,
chart_width,
);
}
// Zoom out (-)
if i.key_pressed(Key::Minus) {
let zoom_point_x = chart_width / 2.0;
self.state.time_scale_mut().zoom(
-self.chart_options.keyboard.zoom_step,
zoom_point_x,
chart_rect_min_x,
chart_width,
);
}
// Scroll to real-time / latest data (Home)
if i.key_pressed(Key::Home) {
self.state.time_scale_mut().scroll_to_realtime();
}
// Fit content (F) - zoom to show all data
if i.key_pressed(Key::F) {
self.state.time_scale_mut().fit_content();
}
// Page Up (zoom in more)
if i.key_pressed(Key::PageUp) {
let zoom_point_x = chart_width / 2.0;
self.state.time_scale_mut().zoom(
self.chart_options.keyboard.zoom_step * 3.0,
zoom_point_x,
chart_rect_min_x,
chart_width,
);
}
// Page Down (zoom out more)
if i.key_pressed(Key::PageDown) {
let zoom_point_x = chart_width / 2.0;
self.state.time_scale_mut().zoom(
-self.chart_options.keyboard.zoom_step * 3.0,
zoom_point_x,
chart_rect_min_x,
chart_width,
);
}
});
}
/// Request keyboard focus when the user actually interacts with the chart
/// (click, pointer-down, or drag), so keyboard shortcuts become active.
///
/// Focus is deliberately *not* taken on bare hover: doing so would paint the
/// keyboard focus ring whenever the pointer merely passes over the chart,
/// which reads as a spurious selection. Tab/keyboard navigation still routes
/// focus here through egui's own focus machinery, so the focus ring remains a
/// genuine keyboard affordance.
pub fn request_focus_if_needed(&self, response: &mut Response) {
if self.chart_options.keyboard.enabled
&& (response.clicked() || response.dragged() || response.is_pointer_button_down_on())
{
response.request_focus();
}
}
/// Display a grabbing cursor while the user is actively dragging (panning) the chart.
pub fn set_panning_cursor(&self, ui: &Ui, response: &Response) {
if response.dragged() {
ui.ctx().set_cursor_icon(egui::CursorIcon::Grabbing);
}
}
}