egui-cha-ds 0.6.0

Design System for egui-cha (Atoms, Molecules, Theme)
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Custom window titlebar component
//!
//! Provides a customizable titlebar for frameless/transparent windows.
//!
//! # Features
//!
//! - Window drag to move
//! - Close, minimize, maximize buttons
//! - Custom content area (title, icons, etc.)
//! - Platform-aware button placement (macOS left, Windows/Linux right)
//!
//! # Example
//!
//! ```ignore
//! use egui_cha_ds::titlebar::{TitleBar, TitleBarStyle};
//!
//! TitleBar::new("My App")
//!     .style(TitleBarStyle::default())
//!     .show(ui, |ui| {
//!         // Optional custom content
//!         ui.label("Custom content");
//!     });
//! ```

use egui::{Color32, Pos2, Rect, Response, Sense, Ui, Vec2};

/// Titlebar button type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TitleBarButton {
    Close,
    Minimize,
    Maximize,
}

/// Titlebar button action result
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct TitleBarResponse {
    pub close_clicked: bool,
    pub minimize_clicked: bool,
    pub maximize_clicked: bool,
    pub dragged: bool,
}

/// Titlebar style configuration
#[derive(Debug, Clone)]
pub struct TitleBarStyle {
    /// Height of the titlebar
    pub height: f32,
    /// Background color (None = transparent)
    pub background: Option<Color32>,
    /// Title text color
    pub title_color: Color32,
    /// Button size
    pub button_size: f32,
    /// Button spacing
    pub button_spacing: f32,
    /// Padding from edges
    pub padding: f32,
    /// Show window control buttons
    pub show_buttons: bool,
    /// Button style
    pub button_style: TitleBarButtonStyle,
}

impl Default for TitleBarStyle {
    fn default() -> Self {
        Self {
            height: 32.0,
            background: None,
            title_color: Color32::from_rgb(200, 200, 200),
            button_size: 12.0,
            button_spacing: 8.0,
            padding: 12.0,
            show_buttons: true,
            button_style: TitleBarButtonStyle::default(),
        }
    }
}

impl TitleBarStyle {
    /// Create style from theme
    pub fn from_theme(theme: &crate::Theme) -> Self {
        Self {
            height: theme.titlebar_height,
            background: Some(theme.bg_secondary.linear_multiply(theme.glass_opacity)),
            title_color: theme.text_primary,
            button_size: 12.0,
            button_spacing: 8.0,
            padding: theme.spacing_sm,
            show_buttons: true,
            button_style: TitleBarButtonStyle::default(),
        }
    }

    /// Create transparent style from theme (for vibrancy windows)
    pub fn transparent_from_theme(theme: &crate::Theme) -> Self {
        Self {
            height: theme.titlebar_height,
            background: None,
            title_color: theme.text_primary,
            button_size: 12.0,
            button_spacing: 8.0,
            padding: theme.spacing_sm,
            show_buttons: true,
            button_style: TitleBarButtonStyle::default(),
        }
    }

    /// Transparent style (for vibrancy windows)
    pub fn transparent() -> Self {
        Self {
            background: None,
            ..Default::default()
        }
    }

    /// Compact style
    pub fn compact() -> Self {
        Self {
            height: 28.0,
            button_size: 10.0,
            padding: 8.0,
            ..Default::default()
        }
    }
}

/// Button visual style
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TitleBarButtonStyle {
    /// macOS-style traffic lights (colored circles)
    #[default]
    TrafficLights,
    /// Windows-style icons
    WindowsIcons,
    /// Minimal dots
    Minimal,
}

/// Custom titlebar component
pub struct TitleBar<'a> {
    title: &'a str,
    style: TitleBarStyle,
    buttons: Vec<TitleBarButton>,
    id: Option<egui::Id>,
}

impl<'a> TitleBar<'a> {
    /// Create a new titlebar with title
    pub fn new(title: &'a str) -> Self {
        Self {
            title,
            style: TitleBarStyle::default(),
            buttons: vec![
                TitleBarButton::Close,
                TitleBarButton::Minimize,
                TitleBarButton::Maximize,
            ],
            id: None,
        }
    }

    /// Set a unique ID for this titlebar (required when multiple titlebars in same UI)
    pub fn id(mut self, id: impl std::hash::Hash) -> Self {
        self.id = Some(egui::Id::new(id));
        self
    }

    /// Set titlebar style
    pub fn style(mut self, style: TitleBarStyle) -> Self {
        self.style = style;
        self
    }

    /// Set which buttons to show
    pub fn buttons(mut self, buttons: Vec<TitleBarButton>) -> Self {
        self.buttons = buttons;
        self
    }

    /// Only show close button
    pub fn close_only(mut self) -> Self {
        self.buttons = vec![TitleBarButton::Close];
        self
    }

    /// Hide all buttons
    pub fn no_buttons(mut self) -> Self {
        self.buttons = vec![];
        self.style.show_buttons = false;
        self
    }

    /// Show the titlebar
    ///
    /// Returns response indicating which buttons were clicked
    pub fn show(self, ui: &mut Ui) -> TitleBarResponse {
        self.show_with_content(ui, |_| {})
    }

    /// Show the titlebar with custom content
    pub fn show_with_content<R>(
        self,
        ui: &mut Ui,
        add_contents: impl FnOnce(&mut Ui) -> R,
    ) -> TitleBarResponse {
        let mut response = TitleBarResponse::default();

        // Use provided ID or generate from title
        let base_id = self.id.unwrap_or_else(|| ui.id().with(self.title));

        // Allocate titlebar area
        let (rect, drag_response) = ui.allocate_exact_size(
            Vec2::new(ui.available_width(), self.style.height),
            Sense::click_and_drag(),
        );

        // Handle drag for window movement
        if drag_response.drag_started() {
            response.dragged = true;
            ui.ctx().send_viewport_cmd(egui::ViewportCommand::StartDrag);
        }

        // Draw background
        if let Some(bg) = self.style.background {
            ui.painter().rect_filled(rect, 0.0, bg);
        }

        // Determine button position based on platform
        let buttons_on_left = cfg!(target_os = "macos");

        // Calculate positions
        let button_area_width = if self.style.show_buttons && !self.buttons.is_empty() {
            self.buttons.len() as f32 * (self.style.button_size + self.style.button_spacing)
        } else {
            0.0
        };

        // Draw buttons
        if self.style.show_buttons && !self.buttons.is_empty() {
            let buttons_start = if buttons_on_left {
                rect.min + Vec2::new(self.style.padding, self.style.height / 2.0)
            } else {
                rect.max
                    - Vec2::new(
                        self.style.padding + button_area_width,
                        -self.style.height / 2.0,
                    )
            };

            for (i, button) in self.buttons.iter().enumerate() {
                let offset = i as f32 * (self.style.button_size + self.style.button_spacing);
                let center = buttons_start + Vec2::new(offset + self.style.button_size / 2.0, 0.0);

                let button_rect =
                    Rect::from_center_size(center, Vec2::splat(self.style.button_size));

                let button_response = ui.interact(
                    button_rect,
                    base_id.with(format!("btn_{:?}", button)),
                    Sense::click(),
                );

                // Draw button
                self.draw_button(ui, button_rect, *button, button_response.hovered());

                // Handle click
                if button_response.clicked() {
                    match button {
                        TitleBarButton::Close => {
                            response.close_clicked = true;
                            ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
                        }
                        TitleBarButton::Minimize => {
                            response.minimize_clicked = true;
                            ui.ctx()
                                .send_viewport_cmd(egui::ViewportCommand::Minimized(true));
                        }
                        TitleBarButton::Maximize => {
                            response.maximize_clicked = true;
                            ui.ctx().send_viewport_cmd(egui::ViewportCommand::Maximized(
                                !ui.input(|i| i.viewport().maximized.unwrap_or(false)),
                            ));
                        }
                    }
                }
            }
        }

        // Draw title
        let title_x = if buttons_on_left {
            rect.min.x + self.style.padding + button_area_width + self.style.button_spacing
        } else {
            rect.min.x + self.style.padding
        };

        let title_rect = Rect::from_min_max(
            Pos2::new(title_x, rect.min.y),
            Pos2::new(
                if buttons_on_left {
                    rect.max.x - self.style.padding
                } else {
                    rect.max.x - self.style.padding - button_area_width - self.style.button_spacing
                },
                rect.max.y,
            ),
        );

        // Title text
        ui.painter().text(
            title_rect.center(),
            egui::Align2::CENTER_CENTER,
            self.title,
            egui::FontId::proportional(14.0),
            self.style.title_color,
        );

        // Custom content area (between title and buttons on non-macOS, or after title on macOS)
        let content_rect = if buttons_on_left {
            // macOS: content after title
            Rect::from_min_max(
                Pos2::new(title_rect.max.x, rect.min.y),
                Pos2::new(rect.max.x - self.style.padding, rect.max.y),
            )
        } else {
            // Windows/Linux: content between padding and buttons
            Rect::from_min_max(
                Pos2::new(
                    rect.min.x + self.style.padding + 100.0, // After title
                    rect.min.y,
                ),
                Pos2::new(
                    rect.max.x - self.style.padding - button_area_width - self.style.button_spacing,
                    rect.max.y,
                ),
            )
        };

        if content_rect.width() > 20.0 {
            let mut content_ui = ui.child_ui(
                content_rect,
                egui::Layout::left_to_right(egui::Align::Center),
                None,
            );
            add_contents(&mut content_ui);
        }

        response
    }

    /// Draw a single button
    fn draw_button(&self, ui: &Ui, rect: Rect, button: TitleBarButton, hovered: bool) {
        let painter = ui.painter();
        match self.style.button_style {
            TitleBarButtonStyle::TrafficLights => {
                // macOS-style colored circles
                let (color, hover_color) = match button {
                    TitleBarButton::Close => (
                        Color32::from_rgb(255, 95, 87),
                        Color32::from_rgb(255, 59, 48),
                    ),
                    TitleBarButton::Minimize => (
                        Color32::from_rgb(255, 189, 46),
                        Color32::from_rgb(255, 204, 0),
                    ),
                    TitleBarButton::Maximize => (
                        Color32::from_rgb(39, 201, 63),
                        Color32::from_rgb(40, 205, 65),
                    ),
                };

                let color = if hovered { hover_color } else { color };
                painter.circle_filled(rect.center(), rect.width() / 2.0, color);

                // Draw icon on hover
                if hovered {
                    let icon_color = Color32::from_rgba_unmultiplied(0, 0, 0, 180);
                    let center = rect.center();
                    let size = rect.width() * 0.3;

                    match button {
                        TitleBarButton::Close => {
                            // X icon
                            painter.line_segment(
                                [center - Vec2::splat(size), center + Vec2::splat(size)],
                                egui::Stroke::new(1.5, icon_color),
                            );
                            painter.line_segment(
                                [
                                    center + Vec2::new(-size, size),
                                    center + Vec2::new(size, -size),
                                ],
                                egui::Stroke::new(1.5, icon_color),
                            );
                        }
                        TitleBarButton::Minimize => {
                            // - icon
                            painter.line_segment(
                                [center - Vec2::new(size, 0.0), center + Vec2::new(size, 0.0)],
                                egui::Stroke::new(1.5, icon_color),
                            );
                        }
                        TitleBarButton::Maximize => {
                            // + or expand icon
                            painter.line_segment(
                                [center - Vec2::new(size, 0.0), center + Vec2::new(size, 0.0)],
                                egui::Stroke::new(1.5, icon_color),
                            );
                            painter.line_segment(
                                [center - Vec2::new(0.0, size), center + Vec2::new(0.0, size)],
                                egui::Stroke::new(1.5, icon_color),
                            );
                        }
                    }
                }
            }

            TitleBarButtonStyle::WindowsIcons => {
                // Windows-style icons
                let bg_color = if hovered {
                    match button {
                        TitleBarButton::Close => Color32::from_rgb(232, 17, 35),
                        _ => Color32::from_rgba_unmultiplied(255, 255, 255, 30),
                    }
                } else {
                    Color32::TRANSPARENT
                };

                let icon_color = if hovered && button == TitleBarButton::Close {
                    Color32::WHITE
                } else {
                    self.style.title_color
                };

                // Background
                painter.rect_filled(rect, 0.0, bg_color);

                let center = rect.center();
                let size = rect.width() * 0.25;

                match button {
                    TitleBarButton::Close => {
                        painter.line_segment(
                            [center - Vec2::splat(size), center + Vec2::splat(size)],
                            egui::Stroke::new(1.0, icon_color),
                        );
                        painter.line_segment(
                            [
                                center + Vec2::new(-size, size),
                                center + Vec2::new(size, -size),
                            ],
                            egui::Stroke::new(1.0, icon_color),
                        );
                    }
                    TitleBarButton::Minimize => {
                        painter.line_segment(
                            [center - Vec2::new(size, 0.0), center + Vec2::new(size, 0.0)],
                            egui::Stroke::new(1.0, icon_color),
                        );
                    }
                    TitleBarButton::Maximize => {
                        painter.rect_stroke(
                            Rect::from_center_size(center, Vec2::splat(size * 2.0)),
                            0.0,
                            egui::Stroke::new(1.0, icon_color),
                            egui::StrokeKind::Inside,
                        );
                    }
                }
            }

            TitleBarButtonStyle::Minimal => {
                // Simple dots
                let color = if hovered {
                    self.style.title_color
                } else {
                    self.style.title_color.linear_multiply(0.5)
                };
                painter.circle_filled(rect.center(), rect.width() / 4.0, color);
            }
        }
    }
}

/// Simple titlebar that just provides drag area
///
/// Use when you want custom button placement or no buttons at all.
pub fn drag_area(ui: &mut Ui, height: f32) -> Response {
    let (_rect, response) = ui.allocate_exact_size(
        Vec2::new(ui.available_width(), height),
        Sense::click_and_drag(),
    );

    if response.drag_started() {
        ui.ctx().send_viewport_cmd(egui::ViewportCommand::StartDrag);
    }

    response
}