egui-charts 0.2.0

High-performance financial charting engine for egui — candlesticks, 95 drawing tools, 130+ indicators, and a full design-token theme system
Documentation
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Reusable button components for the top toolbar.
//!
//! Eliminates ~180 lines of repetitive button rendering code.

use crate::icons::Icon;
use crate::styles::{icons as icon_sizes, stroke, typography};
use egui::{Pos2, Rect, Response, Sense, Ui, Vec2};

use super::TopToolbarConfig;
use crate::ext::UiExt;
use crate::theming;
use crate::tokens::DESIGN_TOKENS;

/// Small icon-only button (config-driven size)
///
/// Used for: undo, redo, settings, theme, fullscreen, screenshot, save, etc.
/// This component eliminates ~161 lines of duplication (used 7 times in show()).
///
/// Note: This is the top-toolbar-specific variant that uses [`TopToolbarConfig`]
/// for sizing. For a generic icon button, see [`crate::ui_kit::buttons::IconButton`].
pub struct ToolbarIconButton<'a> {
    pub icon: &'a Icon,
    pub tooltip: &'static str,
    pub config: TopToolbarConfig,
}

impl<'a> ToolbarIconButton<'a> {
    pub fn new(icon: &'a Icon, tooltip: &'static str, config: &TopToolbarConfig) -> Self {
        Self {
            icon,
            tooltip,
            config: config.clone(),
        }
    }

    pub fn show(self, ui: &mut Ui) -> Response {
        let icon_size = self.config.small_icon_size;
        let btn_size = self.config.btn_size;
        let (rect, response) = ui.allocate_exact_size(Vec2::splat(btn_size), Sense::click());

        // Background with theme-aware colors
        let bg_color = if response.is_pointer_button_down_on() {
            theming::btn_bg_pressed(ui)
        } else if response.hovered() {
            theming::btn_bg_hover(ui)
        } else {
            theming::btn_bg_normal(ui)
        };

        ui.painter()
            .rect_filled(rect, self.config.rounding, bg_color);

        // Icon with hover/pressed color change
        let is_hovered = response.hovered();
        let is_active = response.is_pointer_button_down_on();

        // Render icon at exact position (with visibility check)
        let icon_pos = rect.center() - Vec2::splat(icon_size / 2.0);
        let icon_rect = Rect::from_min_size(icon_pos, Vec2::splat(icon_size));
        if ui.is_rect_visible(icon_rect) {
            let icon_color = if is_active {
                theming::icon_active(ui)
            } else if is_hovered {
                theming::icon_hover_color(ui)
            } else {
                theming::icon_normal(ui)
            };
            self.icon
                .as_image_tinted(Vec2::splat(icon_size), icon_color)
                .paint_at(ui, icon_rect);
        }

        response.on_hover_text(self.tooltip)
    }
}

/// Icon button with text label
///
/// Used for: Indicators, Alert, Replay btns.
/// This component eliminates ~117 lines of duplication (used 3 times in show()).
pub struct IconTextButton<'a> {
    pub icon: &'a Icon,
    pub label: &'static str,
    pub config: TopToolbarConfig,
}

impl<'a> IconTextButton<'a> {
    pub fn new(icon: &'a Icon, label: &'static str, config: &TopToolbarConfig) -> Self {
        Self {
            icon,
            label,
            config: config.clone(),
        }
    }

    pub fn show(self, ui: &mut Ui) -> Response {
        let icon_size = self.config.small_icon_size;
        let padding = self.config.padding;
        // Measure actual text width instead of approximation
        let font_id = egui::FontId::proportional(typography::MD);
        let galley = ui.painter().layout_no_wrap(
            self.label.to_string(),
            font_id,
            DESIGN_TOKENS.semantic.ui.text_light,
        );
        let text_width = galley.rect.width();
        let total_width = icon_size + padding * 3.0 + text_width;
        let height = self.config.btn_size;

        let (rect, response) =
            ui.allocate_exact_size(Vec2::new(total_width, height), Sense::click());

        // All paint operations wrapped with visibility check
        if ui.is_rect_visible(rect) {
            // Background
            let bg_color = if response.is_pointer_button_down_on() {
                theming::btn_bg_pressed(ui)
            } else if response.hovered() {
                theming::btn_bg_hover(ui)
            } else {
                theming::btn_bg_normal(ui)
            };

            ui.painter()
                .rect_filled(rect, self.config.rounding, bg_color);

            // Hover/pressed state
            let is_hovered = response.hovered();
            let is_active = response.is_pointer_button_down_on();

            // Icon - using ui.put() for precise positioning
            let icon_pos = Pos2::new(rect.min.x + padding, rect.center().y - icon_size / 2.0);
            let icon_rect = Rect::from_min_size(icon_pos, Vec2::splat(icon_size));
            let icon_color = if is_active {
                theming::icon_active(ui)
            } else if is_hovered {
                theming::icon_hover_color(ui)
            } else {
                theming::icon_normal(ui)
            };
            self.icon
                .as_image_tinted(Vec2::splat(icon_size), icon_color)
                .paint_at(ui, icon_rect);

            // Text color
            let color = if is_hovered || is_active {
                theming::icon_active(ui)
            } else {
                theming::icon_normal(ui)
            };

            // Text
            ui.painter().text(
                Pos2::new(rect.min.x + padding + icon_size + padding, rect.center().y),
                egui::Align2::LEFT_CENTER,
                self.label,
                egui::FontId::proportional(typography::MD),
                color,
            );
        }

        response
    }
}

/// Text-only button (used for interval dropdown)
///
/// Displays text with hover/pressed background.
pub struct TextButton {
    pub text: String,
    pub tooltip: Option<&'static str>,
    pub config: TopToolbarConfig,
}

impl TextButton {
    pub fn new(text: String, config: &TopToolbarConfig) -> Self {
        Self {
            text,
            tooltip: None,
            config: config.clone(),
        }
    }

    pub fn with_tooltip(mut self, tooltip: &'static str) -> Self {
        self.tooltip = Some(tooltip);
        self
    }

    pub fn show(self, ui: &mut Ui) -> Response {
        let padding = self.config.padding;
        // Measure actual text width
        let font_id = egui::FontId::proportional(typography::LG);
        let galley = ui.painter().layout_no_wrap(
            self.text.clone(),
            font_id,
            DESIGN_TOKENS.semantic.ui.text_light,
        );
        let text_width = galley.rect.width();
        let total_width = text_width + padding * 2.0;
        let height = self.config.btn_size;

        let (rect, response) =
            ui.allocate_exact_size(Vec2::new(total_width, height), Sense::click());

        // Background
        let bg_color = if response.is_pointer_button_down_on() {
            theming::btn_bg_pressed(ui)
        } else if response.hovered() {
            theming::btn_bg_hover(ui)
        } else {
            theming::btn_bg_normal(ui)
        };

        ui.painter()
            .rect_filled(rect, self.config.rounding, bg_color);

        // Text
        ui.painter().text(
            rect.center(),
            egui::Align2::CENTER_CENTER,
            &self.text,
            egui::FontId::proportional(typography::LG),
            theming::text_color(ui),
        );

        if let Some(tooltip) = self.tooltip {
            response.on_hover_text(tooltip)
        } else {
            response
        }
    }
}

/// Vertical separator line
pub fn separator(ui: &mut Ui) {
    ui.space_lg();
    let rect = ui
        .allocate_exact_size(Vec2::new(stroke::HAIRLINE, icon_sizes::MD), Sense::hover())
        .0;
    ui.painter().rect_filled(
        rect,
        DESIGN_TOKENS.rounding.none,
        theming::separator_color(ui),
    );
    ui.space_lg();
}

/// Filled pill button (Trade button style)
///
/// Blue-filled rounded pill with white text
pub struct PillButtonFilled {
    pub text: &'static str,
    pub tooltip: &'static str,
}

impl PillButtonFilled {
    pub fn new(text: &'static str, tooltip: &'static str) -> Self {
        Self { text, tooltip }
    }

    pub fn show(self, ui: &mut Ui) -> Response {
        let padding_h = DESIGN_TOKENS.spacing.lg;
        let font_id = egui::FontId::proportional(typography::SM);
        let galley = ui.painter().layout_no_wrap(
            self.text.to_string(),
            font_id,
            theming::publish_button_text(ui),
        );
        let text_width = galley.rect.width();
        let total_width = text_width + padding_h * 2.0;
        let height = DESIGN_TOKENS.sizing.button_sm;

        let (rect, response) =
            ui.allocate_exact_size(Vec2::new(total_width, height), Sense::click());

        if ui.is_rect_visible(rect) {
            // Blue background
            let bg_color = if response.is_pointer_button_down_on() {
                theming::publish_button_bg_pressed(ui)
            } else if response.hovered() {
                theming::publish_button_bg_hover(ui)
            } else {
                theming::publish_button_bg(ui)
            };

            ui.painter()
                .rect_filled(rect, DESIGN_TOKENS.rounding.pill, bg_color);

            // White text
            ui.painter().text(
                rect.center(),
                egui::Align2::CENTER_CENTER,
                self.text,
                egui::FontId::proportional(typography::SM),
                theming::publish_button_text(ui),
            );
        }

        response.on_hover_text(self.tooltip)
    }
}

/// Outlined pill button (Publish button style)
///
/// Transparent with border and text
pub struct PillButtonOutlined {
    pub text: &'static str,
    pub tooltip: &'static str,
}

impl PillButtonOutlined {
    pub fn new(text: &'static str, tooltip: &'static str) -> Self {
        Self { text, tooltip }
    }

    pub fn show(self, ui: &mut Ui) -> Response {
        let padding_h = DESIGN_TOKENS.spacing.lg;
        let font_id = egui::FontId::proportional(typography::SM);
        let galley =
            ui.painter()
                .layout_no_wrap(self.text.to_string(), font_id, theming::text_color(ui));
        let text_width = galley.rect.width();
        let total_width = text_width + padding_h * 2.0;
        let height = DESIGN_TOKENS.sizing.button_sm;

        let (rect, response) =
            ui.allocate_exact_size(Vec2::new(total_width, height), Sense::click());

        if ui.is_rect_visible(rect) {
            // Background: transparent or subtle hover
            let bg_color = if response.is_pointer_button_down_on() {
                theming::btn_bg_pressed(ui)
            } else if response.hovered() {
                theming::btn_bg_hover(ui)
            } else {
                egui::Color32::TRANSPARENT
            };

            ui.painter()
                .rect_filled(rect, DESIGN_TOKENS.rounding.pill, bg_color);

            // Border
            let border_color = if response.hovered() {
                theming::icon_hover_color(ui)
            } else {
                theming::separator_color(ui)
            };
            ui.painter().rect_stroke(
                rect,
                DESIGN_TOKENS.rounding.pill,
                egui::Stroke::new(DESIGN_TOKENS.stroke.hairline, border_color),
                egui::StrokeKind::Inside,
            );

            // Text
            let text_color = if response.hovered() {
                theming::icon_hover_color(ui)
            } else {
                theming::text_color(ui)
            };
            ui.painter().text(
                rect.center(),
                egui::Align2::CENTER_CENTER,
                self.text,
                egui::FontId::proportional(typography::SM),
                text_color,
            );
        }

        response.on_hover_text(self.tooltip)
    }
}

/// Text + icon dropdown button (Save dropdown, etc.)
pub struct TextIconButton<'a> {
    pub text: &'static str,
    pub icon: &'a crate::icons::Icon,
    pub tooltip: &'static str,
    pub config: TopToolbarConfig,
}

impl<'a> TextIconButton<'a> {
    pub fn new(
        text: &'static str,
        icon: &'a crate::icons::Icon,
        tooltip: &'static str,
        config: &TopToolbarConfig,
    ) -> Self {
        Self {
            text,
            icon,
            tooltip,
            config: config.clone(),
        }
    }

    pub fn show(self, ui: &mut Ui) -> Response {
        let padding = self.config.padding;
        let icon_size = icon_sizes::XS;
        let font_id = egui::FontId::proportional(typography::SM);
        let galley = ui.painter().layout_no_wrap(
            self.text.to_string(),
            font_id.clone(),
            theming::text_color(ui),
        );
        let text_width = galley.rect.width();
        let total_width = text_width + padding * 2.0 + icon_size + padding;
        let height = self.config.btn_size;

        let (rect, response) =
            ui.allocate_exact_size(Vec2::new(total_width, height), Sense::click());

        if ui.is_rect_visible(rect) {
            // Background
            let bg_color = if response.is_pointer_button_down_on() {
                theming::btn_bg_pressed(ui)
            } else if response.hovered() {
                theming::btn_bg_hover(ui)
            } else {
                theming::btn_bg_normal(ui)
            };

            ui.painter()
                .rect_filled(rect, self.config.rounding, bg_color);

            // Text
            let text_color = if response.hovered() {
                theming::icon_hover_color(ui)
            } else {
                theming::text_color(ui)
            };
            let text_x = rect.min.x + padding;
            ui.painter().text(
                Pos2::new(text_x, rect.center().y),
                egui::Align2::LEFT_CENTER,
                self.text,
                font_id,
                text_color,
            );

            // Icon (chevron)
            let icon_x = rect.max.x - padding - icon_size;
            let icon_pos = Pos2::new(icon_x, rect.center().y - icon_size / 2.0);
            let icon_rect = Rect::from_min_size(icon_pos, Vec2::splat(icon_size));
            let icon_color = if response.hovered() {
                theming::icon_hover_color(ui)
            } else {
                theming::icon_normal(ui)
            };
            self.icon
                .as_image_tinted(Vec2::splat(icon_size), icon_color)
                .paint_at(ui, icon_rect);
        }

        response.on_hover_text(self.tooltip)
    }
}