egui_charts/ui/app_state.rs
1//! Application state trait for UI synchronization.
2//!
3//! UI components need to read from centralized app state. Instead of
4//! depending on a concrete AppState type, components accept any type
5//! implementing [`ChartAppState`].
6
7use crate::model::{ChartType, Timeframe};
8
9/// Trait for application state that chart UI components can read from.
10///
11/// Library consumers implement this on their app state type to connect
12/// the UI components to their data layer.
13pub trait ChartAppState {
14 /// Currently active trading symbol (e.g., "AAPL", "BTC/USD").
15 fn active_symbol(&self) -> &str;
16 /// Current chart timeframe.
17 fn active_timeframe(&self) -> &Timeframe;
18 /// Current chart visualization type.
19 fn chart_type(&self) -> ChartType;
20 /// Whether the data connection is active.
21 fn is_connected(&self) -> bool;
22 /// Whether replay mode is active.
23 fn is_replay_active(&self) -> bool;
24}
25
26/// A control the terminal or a host can hide. Hidden controls are not drawn and emit nothing.
27///
28/// Each component that draws one of these has a `set_control_visible(control, visible)`
29/// method; every control starts visible. This controls presentation, not authorization:
30/// hosts choose which services, commands and shortcut bindings they provide.
31/// Chart behavior switches live in [`ChartOptions`](crate::config::ChartOptions);
32/// component `config` fields override dimensions and [`Theme`](crate::theme::Theme) overrides
33/// colours and text sizes. [`ChartBuilder::with_theme`](crate::widget::ChartBuilder::with_theme)
34/// keeps those theme properties local to one chart.
35///
36/// ```
37/// use egui_charts::ui::{Control, context_menu::ChartMenus, top_toolbar::TopToolbar};
38/// let mut toolbar = TopToolbar::new();
39/// let mut menus = ChartMenus::default();
40/// for control in [Control::Alerts, Control::Trading, Control::Settings] {
41/// toolbar.set_control_visible(control, false);
42/// menus.set_control_visible(control, false);
43/// }
44/// toolbar.set_control_visible(Control::Snapshot, false);
45/// ```
46#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
47#[non_exhaustive]
48pub enum Control {
49 /// The top toolbar's menu button or host menu slot.
50 Menu,
51 /// The top toolbar's symbol search button.
52 SymbolSearch,
53 /// The top toolbar's interval selector.
54 Interval,
55 /// The top toolbar's chart type selector.
56 ChartType,
57 /// Study selection and removal entries in the toolbar and series/chart menus.
58 Indicators,
59 /// The top toolbar's undo button. Shortcut bindings remain host configurable.
60 Undo,
61 /// The top toolbar's redo button. Shortcut bindings remain host configurable.
62 Redo,
63 /// The top toolbar's theme toggle.
64 Theme,
65 /// The top toolbar's quick search button.
66 QuickSearch,
67 /// Chart and series settings buttons and menu entries.
68 Settings,
69 /// The top toolbar's fullscreen button.
70 Fullscreen,
71 /// The top toolbar's snapshot button and menu.
72 Snapshot,
73 /// Object Tree buttons and chart-menu entries.
74 ObjectTree,
75 /// The right rail's help button.
76 Help,
77 /// All chart, series, drawing and axis context menus.
78 ContextMenus,
79 /// Reset-view entries in the chart menu.
80 ResetView,
81 /// Copy-price entries in chart and series menus.
82 CopyPrice,
83 /// CSV export entries in the chart menu.
84 ExportData,
85 /// Alert creation: the top toolbar's Alert button, the right rail's Alerts button and the
86 /// add alert rows of the chart and series menus.
87 Alerts,
88 /// Positions and orders: the top toolbar's Trade button, the drawing toolbar's hide rows for
89 /// them and the chart menu's order rows.
90 Trading,
91 /// The top toolbar's Publish button.
92 Publish,
93 /// The top toolbar's Replay button.
94 Replay,
95 /// The top toolbar's Save and Layout setup buttons.
96 Layouts,
97 /// Templates: the top toolbar's indicator templates button, the drawing toolbar's templates
98 /// button and menu, and the chart settings dialog's template button.
99 Templates,
100 /// The top toolbar's Compare button.
101 Compare,
102 /// The bottom toolbar's time zone menu.
103 TimeZone,
104 /// The bottom toolbar's session menu.
105 Session,
106 /// The bottom toolbar's dividend adjustment toggle.
107 DividendAdjustment,
108}
109
110/// Records in `hidden` whether `control` is shown.
111pub(crate) fn set_control_visible(
112 hidden: &mut std::collections::HashSet<Control>,
113 control: Control,
114 visible: bool,
115) {
116 if visible {
117 hidden.remove(&control);
118 } else {
119 hidden.insert(control);
120 }
121}