egui_charts/ui/replay/model.rs
1//! Replay types shared with the host: commands, playback state, speed and progress.
2
3use chrono::{DateTime, Utc};
4
5/// Replay command for the host to apply.
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub enum ReplayCommand {
8 /// Resume playback.
9 Resume,
10 /// Pause playback.
11 Pause,
12 /// Stop playback.
13 Stop,
14 /// Advance playback by one step.
15 StepForward,
16 /// Move playback back by one step.
17 StepBackward,
18 /// Set the playback speed.
19 SetSpeed(PlaybackSpeed),
20}
21
22/// Replay playback state (toolbar-level)
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
24pub enum ToolbarReplayState {
25 /// Playback is advancing.
26 Playing,
27 /// Playback is paused.
28 Paused,
29 #[default]
30 /// No playback session is running.
31 Stopped,
32 /// Playback has reached the end of the available data.
33 Finished,
34 /// Playback is waiting for data.
35 Buffering,
36 /// Playback failed.
37 Error,
38}
39
40impl ToolbarReplayState {
41 /// Whether a replay session is playing, paused or buffering.
42 pub fn is_active(&self) -> bool {
43 matches!(self, Self::Playing | Self::Paused | Self::Buffering)
44 }
45
46 /// Whether the replay can be started from this state
47 pub fn can_start(&self) -> bool {
48 matches!(self, Self::Stopped | Self::Finished)
49 }
50}
51
52/// Playback speed for replay
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
54pub enum PlaybackSpeed {
55 #[default]
56 /// Play at the original speed.
57 RealTime,
58 /// Play at four times the original speed.
59 Fast,
60 /// Play at ten times the original speed.
61 VeryFast,
62 /// Play at one hundred times the original speed.
63 UltraFast,
64}
65
66impl PlaybackSpeed {
67 /// Display name for UI
68 pub fn display_name(&self) -> &'static str {
69 match self {
70 Self::RealTime => "1x",
71 Self::Fast => "4x",
72 Self::VeryFast => "10x",
73 Self::UltraFast => "100x",
74 }
75 }
76
77 /// Speed multiplier as f64
78 pub fn multiplier(&self) -> f64 {
79 match self {
80 Self::RealTime => 1.0,
81 Self::Fast => 4.0,
82 Self::VeryFast => 10.0,
83 Self::UltraFast => 100.0,
84 }
85 }
86}
87
88/// Progress information for replay (tick-file replay)
89#[derive(Debug, Clone)]
90pub struct ReplayProgress {
91 /// Market time reached by playback.
92 pub current_time: DateTime<Utc>,
93 /// Number of bars processed so far.
94 pub bars_played: usize,
95 /// Total number of bars available for playback.
96 pub total_bars: usize,
97 /// Number of executions processed so far.
98 pub ticks_processed: u64,
99 /// Playback progress in percent.
100 pub progress_pct: f32,
101}
102
103impl ReplayProgress {
104 /// Bars played divided by total bars, or zero when the total is zero.
105 ///
106 /// Unlike the `progress_pct` field, this method returns a fraction, not percent.
107 pub fn progress_pct(&self) -> f64 {
108 if self.total_bars == 0 {
109 0.0
110 } else {
111 self.bars_played as f64 / self.total_bars as f64
112 }
113 }
114}