egui-desktop 0.2.4

Cross-platform GUI for egui applications
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
use egui::{
    Align, Align2, Color32, Context, FontId, Frame, Image, Layout, Margin, PointerButton, Pos2,
    Rect, Rgba, Sense, TextStyle, TopBottomPanel, Vec2, ViewportCommand, vec2,
};

use crate::{TitleBar, titlebar::control_buttons::WindowControlIcon};

/// Get the title bar height based on the platform.
/// - macOS: 28.0 pixels (thinner title bar)
/// - Windows/Linux: 32.0 pixels (standard title bar)
pub fn title_bar_height() -> f32 {
    #[cfg(target_os = "macos")]
    {
        28.0
    }
    #[cfg(not(target_os = "macos"))]
    {
        32.0
    }
}

impl TitleBar {
    /// Display the title bar in the egui context
    ///
    /// This is the main method to render the title bar. It automatically
    /// chooses the appropriate rendering method based on the platform:
    /// - macOS: Uses native traffic light buttons
    /// - Windows/Linux: Uses generic window control buttons
    ///
    /// # Arguments
    /// * `ctx` - The egui context
    ///
    /// # Examples
    ///
    /// ```rust
    /// fn update(&mut self, ctx: &Context, frame: &mut eframe::Frame) {
    ///     self.title_bar.show(ctx);
    ///     
    ///     CentralPanel::default().show(ctx, |ui| {
    ///         ui.label("Main content");
    ///     });
    /// }
    /// ```
    pub fn show(&mut self, ctx: &Context) {
        #[cfg(target_os = "macos")]
        {
            self.render_macos_title_bar(ctx);
        }

        #[cfg(not(target_os = "macos"))]
        {
            self.render_generic_title_bar(ctx);
        }
    }

    /// Render a macOS-style title bar with traffic light controls.
    pub fn render_macos_title_bar(&mut self, ctx: &Context) {
        let content_rect = ctx.content_rect();
        if content_rect.width() < 100.0 || content_rect.height() < 100.0 {
            return;
        }

        TopBottomPanel::top(self.id)
            .exact_height(28.0)
            .frame(
                Frame::new()
                    .fill(self.background_color)
                    .inner_margin(Margin::same(0))
                    .outer_margin(Margin::same(0)),
            )
            .show(ctx, |ui| {
                let title_bar_rect = ui.available_rect_before_wrap();

                if title_bar_rect.width() <= 0.0 || title_bar_rect.height() <= 0.0 {
                    return;
                }

                let title_bar_response =
                    ui.interact(title_bar_rect, self.id, Sense::click_and_drag());

                if title_bar_response.drag_started_by(PointerButton::Primary) {
                    ctx.send_viewport_cmd(ViewportCommand::StartDrag);
                }

                if title_bar_response.double_clicked() {
                    let is_maximized = ctx.input(|i| i.viewport().maximized.unwrap_or(false));
                    ctx.send_viewport_cmd(ViewportCommand::Maximized(!is_maximized));
                }

                ui.horizontal(|ui| {
                    ui.with_layout(Layout::left_to_right(Align::Center), |ui| {
                        let is_fullscreen =
                            ui.ctx().input(|i| i.viewport().fullscreen.unwrap_or(false));
                        if is_fullscreen {
                            // No need to render traffic light controls in fullscreen mode
                            // Just render menu items instead
                            return self.render_menu_items(ui, ctx);
                        }

                        // Save spacing to restore after rendering is done.
                        // We'll use custom spacing so reset current one to be 0
                        let prev_spacing = ui.spacing().item_spacing;
                        ui.spacing_mut().item_spacing = Vec2::ZERO;

                        let spacing_size = 8.0;
                        ui.add_space(spacing_size);

                        // Figure out traffic lights zone geometry
                        let controls_start = Pos2::new(ui.cursor().min.x, spacing_size);
                        let buttons_count = 3.0;
                        let button_size = 12.0;
                        let spacings_count = 2.0;
                        let buttons_width = buttons_count * button_size;
                        let spacing_width = spacings_count * spacing_size;
                        let controls_width = buttons_width + spacing_width;
                        let controls_height = button_size;
                        let rect = Rect::from_min_size(
                            controls_start,
                            vec2(controls_width, controls_height),
                        );

                        // Check if traffic lights controls are hovered.
                        // In this case buttons should be 'active' even in case of not focused window
                        let controls_hovered = ui
                            .ctx()
                            .input(|i| i.pointer.latest_pos().map_or(false, |p| rect.contains(p)));
                        let window_active =
                            ui.ctx().input(|i| i.viewport().focused.unwrap_or(true));
                        let colored = controls_hovered || window_active;
                        let show_icons = controls_hovered;

                        // Figure out inactive color depending on luminance of titlebar
                        let rgba: Rgba = self.background_color.into();
                        let luminance = 0.2126 * rgba.r() + 0.7152 * rgba.g() + 0.0722 * rgba.b();
                        let inactive_color = if luminance < 0.55 {
                            Color32::from_rgb(120, 120, 120)
                        } else {
                            Color32::from_rgb(220, 220, 220)
                        };

                        let close_background_color = if colored {
                            Color32::from_rgb(255, 95, 87)
                        } else {
                            inactive_color
                        };
                        let close_icon_color = Color32::from_rgb(115, 0, 0);
                        let close_response = self.render_traffic_light(
                            ui,
                            close_background_color,
                            show_icons.then(|| WindowControlIcon::Close),
                            close_icon_color,
                            button_size,
                        );

                        if close_response.clicked() {
                            ctx.send_viewport_cmd(ViewportCommand::Close);
                        }

                        ui.add_space(spacing_size);

                        let miniaturize_background_color = if colored {
                            Color32::from_rgb(255, 189, 46)
                        } else {
                            inactive_color
                        };
                        let miniaturize_icon_color = Color32::from_rgb(152, 85, 1);

                        let miniaturize_response = self.render_traffic_light(
                            ui,
                            miniaturize_background_color,
                            show_icons.then(|| WindowControlIcon::Minimize),
                            miniaturize_icon_color,
                            button_size,
                        );

                        if miniaturize_response.clicked() {
                            ctx.send_viewport_cmd(ViewportCommand::Minimized(true));
                        }

                        ui.add_space(spacing_size);

                        let zoom_background_color = if colored {
                            Color32::from_rgb(40, 201, 55)
                        } else {
                            inactive_color
                        };
                        let zoom_icon_color = Color32::from_rgb(0, 97, 0);

                        let option_down = ui.ctx().input(|i| i.modifiers.alt);

                        if option_down {
                            let is_maximized =
                                ui.ctx().input(|i| i.viewport().maximized.unwrap_or(false));
                            let zoom_response = self.render_traffic_light(
                                ui,
                                zoom_background_color,
                                show_icons.then(|| WindowControlIcon::Restore),
                                zoom_icon_color,
                                button_size,
                            );
                            if zoom_response.clicked() {
                                // Handle classic zoom (Option-click)
                                ui.ctx()
                                    .send_viewport_cmd(ViewportCommand::Maximized(!is_maximized));
                            }
                        } else {
                            let is_fullscreen =
                                ui.ctx().input(|i| i.viewport().fullscreen.unwrap_or(false));
                            let zoom_response = self.render_traffic_light(
                                ui,
                                zoom_background_color,
                                show_icons.then(|| WindowControlIcon::Maximize),
                                zoom_icon_color,
                                button_size,
                            );
                            if zoom_response.clicked() {
                                // Handle zoom (fullscreen case)
                                ui.ctx()
                                    .send_viewport_cmd(ViewportCommand::Fullscreen(!is_fullscreen));
                            }
                        }

                        // Add a bit more space after traffic light controls
                        ui.add_space(spacing_size);
                        ui.add_space(spacing_size);

                        // Restore previous spacing configuration
                        ui.spacing_mut().item_spacing = prev_spacing;

                        self.render_menu_items(ui, ctx);
                    });

                    ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
                        self.render_custom_icons(ui);
                        ui.add_space(8.0);
                    });
                });

                if let Some(ref title_text) = self.title {
                    if self.should_show_title() {
                        let font = TextStyle::Body.resolve(ui.style());
                        let galley = ui.fonts_mut(|f| {
                            f.layout_no_wrap(title_text.clone(), font, self.title_color)
                        });

                        let center_x = title_bar_rect.center().x;
                        let center_y = title_bar_rect.min.y + 14.0;

                        let title_pos = Pos2::new(
                            center_x - galley.size().x / 2.0,
                            center_y - galley.size().y / 2.0,
                        );

                        ui.painter().galley(title_pos, galley, self.title_color);
                    }
                }
            });

        self.render_submenu(ctx);
    }

    /// Render a platform-generic title bar (Windows/Linux-style).
    pub fn render_generic_title_bar(&mut self, ctx: &Context) {
        let content_rect = ctx.content_rect();
        if content_rect.width() < 100.0 || content_rect.height() < 100.0 {
            return;
        }

        TopBottomPanel::top(self.id)
            .exact_height(32.0)
            .frame(
                Frame::new()
                    .fill(self.background_color)
                    .inner_margin(Margin::same(0))
                    .outer_margin(Margin::same(0)),
            )
            .show(ctx, |ui| {
                let title_bar_rect = ui.available_rect_before_wrap();

                if title_bar_rect.width() <= 0.0 || title_bar_rect.height() <= 0.0 {
                    return;
                }

                let title_bar_response =
                    ui.interact(title_bar_rect, self.id, Sense::click_and_drag());

                if title_bar_response.drag_started_by(PointerButton::Primary) {
                    ctx.send_viewport_cmd(ViewportCommand::StartDrag);
                }

                if title_bar_response.double_clicked() {
                    let is_maximized = ctx.input(|i| i.viewport().maximized.unwrap_or(false));
                    ctx.send_viewport_cmd(ViewportCommand::Maximized(!is_maximized));
                }

                ui.horizontal(|ui| {
                    ui.with_layout(Layout::left_to_right(Align::Center), |ui| {
                        let icon_size = 20.0;
                        let title_bar_height = 32.0;
                        let icon_center_y = title_bar_height / 2.0;

                        let icon_response = ui.allocate_rect(
                            Rect::from_center_size(
                                Pos2::new(16.0, icon_center_y),
                                Vec2::new(icon_size, icon_size),
                            ),
                            Sense::click(),
                        );

                        ui.put(
                            icon_response.rect,
                            Image::new(self.get_app_icon())
                                .fit_to_exact_size(Vec2::new(icon_size, icon_size)),
                        );

                        if let Some(ref title) = self.title {
                            if self.should_show_title() {
                                let title_width = ui.fonts_mut(|f| {
                                    f.layout_no_wrap(
                                        title.clone(),
                                        FontId::proportional(self.title_font_size),
                                        self.title_color,
                                    )
                                    .size()
                                    .x
                                }) + 8.0;
                                let title_response = ui.allocate_response(
                                    Vec2::new(title_width, 32.0),
                                    Sense::hover(),
                                );

                                let painter = ui.painter();
                                painter.text(
                                    Pos2::new(title_response.rect.left() + 4.0, icon_center_y),
                                    Align2::LEFT_CENTER,
                                    title,
                                    FontId::proportional(self.title_font_size),
                                    self.title_color,
                                );
                            }
                        }

                        self.render_menu_items(ui, ctx);
                    });

                    ui.with_layout(Layout::right_to_left(Align::Center), |ui| {
                        ui.spacing_mut().item_spacing = Vec2::ZERO;

                        let close_response = self
                            .render_window_control_button_with_drawn_icon(
                                ui,
                                WindowControlIcon::Close,
                                self.close_hover_color,
                                self.close_icon_color,
                                16.0,
                            )
                            .on_hover_text("Close");

                        if close_response.clicked() {
                            ctx.send_viewport_cmd(ViewportCommand::Close);
                        }

                        let is_maximized = ctx.input(|i| i.viewport().maximized.unwrap_or(false));

                        let maximize_response = self
                            .render_window_control_button_with_drawn_icon(
                                ui,
                                if is_maximized {
                                    WindowControlIcon::Restore
                                } else {
                                    WindowControlIcon::Maximize
                                },
                                self.hover_color,
                                if is_maximized {
                                    self.restore_icon_color
                                } else {
                                    self.maximize_icon_color
                                },
                                14.0,
                            )
                            .on_hover_text(if is_maximized { "Restore" } else { "Maximize" });

                        if maximize_response.clicked() {
                            ctx.send_viewport_cmd(ViewportCommand::Maximized(!is_maximized));
                        }

                        let minimize_response = self
                            .render_window_control_button_with_drawn_icon(
                                ui,
                                WindowControlIcon::Minimize,
                                self.hover_color,
                                self.minimize_icon_color,
                                14.0,
                            )
                            .on_hover_text("Minimize");

                        if minimize_response.clicked() {
                            ctx.send_viewport_cmd(ViewportCommand::Minimized(true));
                        }

                        self.render_custom_icons(ui);
                    });
                });
            });

        self.render_submenu(ctx);
    }
}