Skip to main content

egui_charts/ui/top_toolbar/
state.rs

1//! Top toolbar selection, visibility and interaction state.
2//!
3//! Keep [`TopToolbarState`] across frames when using the toolbar in a host layout.
4
5/// Replay and action availability shared with the host.
6///
7/// Symbol, interval and chart style come from the host's
8/// [`ChartAppState`](crate::ui::app_state::ChartAppState).
9#[derive(Clone, Debug)]
10pub struct TopToolbarState {
11    /// Whether replay mode is active
12    pub replay_active: bool,
13    /// Whether there is an action to undo; the undo button is disabled while this is `false`.
14    pub can_undo: bool,
15    /// Whether there is an undone action to redo; the redo button is disabled while this is `false`.
16    pub can_redo: bool,
17    /// Whether the host can publish a captured image and return its shareable link.
18    /// Enables the Publish button and Copy link and Post image in the snapshot menu. Handle
19    /// [`TopToolbarAction::CopyImageLink`](super::TopToolbarAction::CopyImageLink) and
20    /// [`TopToolbarAction::Publish`](super::TopToolbarAction::Publish) through that service.
21    pub can_publish_image: bool,
22    /// Whether a broker is attached; the Trade button is hidden while this is `false`.
23    pub can_trade: bool,
24}
25
26impl Default for TopToolbarState {
27    fn default() -> Self {
28        Self {
29            replay_active: false,
30            can_undo: true,
31            can_redo: true,
32            can_trade: false,
33            can_publish_image: false,
34        }
35    }
36}
37
38impl TopToolbarState {
39    /// Create new state with defaults
40    pub fn new() -> Self {
41        Self::default()
42    }
43
44    /// Toggle replay mode
45    pub fn toggle_replay(&mut self) {
46        self.replay_active = !self.replay_active;
47    }
48
49    /// Sync from application state.
50    pub fn sync_from_app_state(&mut self, app_state: &dyn crate::ui::app_state::ChartAppState) {
51        self.replay_active = app_state.is_replay_active();
52    }
53}