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
use egui::{
Color32, Painter, Pos2, Rect, Response, Sense, Shape, Stroke, StrokeKind, Ui, Vec2, vec2,
};
use crate::{TitleBar, titlebar::render_bar::title_bar_height};
/// Window control icon types used by the title bar.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum WindowControlIcon {
/// Close the window.
Close,
/// Maximize the window.
Maximize,
/// Restore the window from maximized state.
Restore,
/// Minimize the window.
Minimize,
}
impl TitleBar {
/// Draw the close button icon (X shape)
///
/// Draws two diagonal lines forming an X shape for the close button.
///
/// # Arguments
/// * `painter` - The egui painter to draw with
/// * `rect` - The bounding rectangle for the icon
/// * `color` - The color of the icon lines
fn draw_close_icon(&self, painter: &egui::Painter, rect: Rect, color: Color32) {
let center = rect.center();
let size = rect.width().min(rect.height()) * 0.6;
let half_size = size / 2.0;
let stroke = Stroke::new(1.5, color);
painter.line_segment(
[
center + Vec2::new(-half_size, -half_size),
center + Vec2::new(half_size, half_size),
],
stroke,
);
painter.line_segment(
[
center + Vec2::new(half_size, -half_size),
center + Vec2::new(-half_size, half_size),
],
stroke,
);
}
/// Draw the maximize button icon (square shape)
///
/// Draws a square outline representing the maximize button.
///
/// # Arguments
/// * `painter` - The egui painter to draw with
/// * `rect` - The bounding rectangle for the icon
/// * `color` - The color of the icon lines
fn draw_maximize_icon(&self, painter: &Painter, rect: egui::Rect, color: Color32) {
let center = rect.center();
let size = rect.width().min(rect.height()) * 0.75;
let stroke = Stroke::new(1.5, color);
let square_rect = Rect::from_center_size(center, Vec2::new(size, size));
painter.rect_stroke(square_rect, 0.0, stroke, StrokeKind::Inside);
}
/// Draw the restore button icon (overlapping squares)
///
/// Draws a main square with two perpendicular lines representing an overlapping
/// second square, indicating the restore down functionality.
///
/// # Arguments
/// * `painter` - The egui painter to draw with
/// * `rect` - The bounding rectangle for the icon
/// * `color` - The color of the icon lines
fn draw_restore_icon(&self, painter: &Painter, rect: Rect, color: Color32) {
let button_size = rect.width().min(rect.height());
let square_size = button_size * 0.85;
let icon_rect = Rect::from_center_size(rect.center(), Vec2::new(square_size, square_size));
let center = icon_rect.center();
let half_size = square_size / 2.0;
let stroke = Stroke::new(1.5, color);
let main_square_size = square_size * 0.7;
let main_square_center = center + Vec2::new(-half_size * 0.2, 0.0);
let main_square = Rect::from_center_size(
main_square_center,
Vec2::new(main_square_size, main_square_size),
);
painter.rect_stroke(main_square, 0.0, stroke, StrokeKind::Inside);
let spacing = half_size * 0.12;
let horizontal_start = center + Vec2::new(-half_size * 0.3, -half_size + spacing);
let horizontal_end = center + Vec2::new(half_size - spacing, -half_size + spacing);
let vertical_start = center + Vec2::new(half_size - spacing, -half_size + spacing);
let vertical_end = center + Vec2::new(half_size - spacing, half_size * 0.2);
painter.line_segment([horizontal_start, horizontal_end], stroke);
painter.line_segment([vertical_start, vertical_end], stroke);
}
/// Draw the minimize button icon (horizontal line)
///
/// Draws a horizontal line representing the minimize button.
///
/// # Arguments
/// * `painter` - The egui painter to draw with
/// * `rect` - The bounding rectangle for the icon
/// * `color` - The color of the icon line
fn draw_minimize_icon(&self, painter: &Painter, rect: Rect, color: Color32) {
let center = rect.center();
let size = rect.width().min(rect.height()) * 0.8;
let half_size = size / 2.0;
let stroke = Stroke::new(2.0, color);
painter.line_segment(
[
center + Vec2::new(-half_size, 0.0),
center + Vec2::new(half_size, 0.0),
],
stroke,
);
}
/// Render a window control button with a drawn icon
///
/// This method creates an interactive button for window controls (close, maximize,
/// restore, minimize) with custom drawn icons instead of SVG images.
///
/// # Arguments
/// * `ui` - The egui UI context
/// * `icon_type` - The type of icon to draw
/// * `hover_color` - The background color when hovering
/// * `icon_color` - The color of the icon
/// * `icon_size` - The size of the icon
///
/// # Returns
/// * `egui::Response` - The interaction response for the button
pub fn render_window_control_button_with_drawn_icon(
&self,
ui: &mut Ui,
icon_type: WindowControlIcon,
hover_color: Color32,
icon_color: Color32,
icon_size: f32,
) -> Response {
let desired_size = Vec2::new(46.0, 32.0);
let (rect, response) = ui.allocate_exact_size(desired_size, Sense::click());
if response.hovered() {
ui.painter().rect_filled(rect, 2.0, hover_color);
ui.ctx().set_cursor_icon(egui::CursorIcon::PointingHand);
}
let icon_rect = Rect::from_center_size(rect.center(), Vec2::new(icon_size, icon_size));
let final_icon_color = if response.hovered() && hover_color == self.close_hover_color {
Color32::WHITE
} else {
icon_color
};
match icon_type {
WindowControlIcon::Close => {
self.draw_close_icon(ui.painter(), icon_rect, final_icon_color)
}
WindowControlIcon::Maximize => {
self.draw_maximize_icon(ui.painter(), icon_rect, final_icon_color)
}
WindowControlIcon::Restore => {
self.draw_restore_icon(ui.painter(), icon_rect, final_icon_color)
}
WindowControlIcon::Minimize => {
self.draw_minimize_icon(ui.painter(), icon_rect, final_icon_color)
}
}
response
}
/// Draw the macOS close button icon (X shape).
///
/// Renders two diagonal lines forming the standard macOS close glyph.
///
/// # Arguments
/// * `painter` - The egui painter used for rendering
/// * `rect` - The bounding rectangle of the icon
/// * `color` - The color of the icon strokes
fn draw_mac_close_icon(&self, painter: &egui::Painter, rect: Rect, color: Color32) {
let stroke = Stroke::new(1.5, color);
painter.line_segment([rect.left_top(), rect.right_bottom()], stroke);
painter.line_segment([rect.right_top(), rect.left_bottom()], stroke);
}
/// Draw the macOS miniaturize button icon (horizontal line).
///
/// Renders a single horizontal line representing the macOS miniaturize glyph.
///
/// # Arguments
/// * `painter` - The egui painter used for rendering
/// * `rect` - The bounding rectangle of the icon
/// * `color` - The color of the icon stroke
fn draw_mac_miniaturize_icon(&self, painter: &Painter, rect: egui::Rect, color: Color32) {
let center = rect.center();
let size = rect.width();
let half_size = size / 2.0;
let stroke = Stroke::new(1.5, color);
painter.line_segment(
[
center + Vec2::new(-half_size, 0.0),
center + Vec2::new(half_size, 0.0),
],
stroke,
);
}
/// Draw the macOS classic zoom button icon (plus sign).
///
/// Renders two perpendicular lines forming a "+" glyph, used for classic zoom
/// when the Option (⌥) key is pressed.
///
/// # Arguments
/// * `painter` - The egui painter used for rendering
/// * `rect` - The bounding rectangle of the icon
/// * `color` - The color of the icon strokes
fn draw_mac_classic_zoom_icon(&self, painter: &Painter, rect: Rect, color: Color32) {
let size = rect.width().min(rect.height());
let center = rect.center();
let half_size = size / 2.0;
let stroke = Stroke::new(1.5, color);
painter.line_segment(
[
center + Vec2::new(0.0, -half_size),
center + Vec2::new(0.0, half_size),
],
stroke,
);
painter.line_segment(
[
center + Vec2::new(-half_size, 0.0),
center + Vec2::new(half_size, 0.0),
],
stroke,
);
}
/// Draw the macOS zoom-to-fullscreen button icon.
///
/// Renders the standard macOS fullscreen zoom glyph composed of two opposing
/// triangular arrows.
///
/// # Arguments
/// * `painter` - The egui painter used for rendering
/// * `rect` - The bounding rectangle of the icon
/// * `color` - The color of the icon strokes
fn draw_mac_zoom_icon(&self, painter: &Painter, rect: Rect, color: Color32) {
// Left top triangle
painter.add(Shape::convex_polygon(
vec![
rect.left_top(),
rect.left_top() + vec2(rect.width() * 0.75, 0.0),
rect.left_top() + vec2(0.0, rect.height() * 0.75),
],
color,
Stroke::NONE,
));
// Right bottom triangle
painter.add(Shape::convex_polygon(
vec![
rect.right_bottom(),
rect.right_bottom() - vec2(rect.width() * 0.75, 0.0),
rect.right_bottom() - vec2(0.0, rect.height() * 0.75),
],
color,
Stroke::NONE,
));
}
/// Render a macOS-style traffic light window control button.
///
/// Creates an interactive traffic light button (close, miniaturize, or zoom)
/// with a custom-drawn icon, matching native macOS appearance and behavior.
/// Icons are rendered procedurally instead of using image assets.
///
/// # Arguments
/// * `ui` - The egui UI context
/// * `button_color` - The background color of the button
/// * `icon_type` - Optional icon to render inside the button
/// * `icon_color` - The color of the icon glyph
/// * `size` - The diameter of the button
///
/// # Returns
/// * `egui::Response` - The interaction response for the button
pub fn render_traffic_light(
&self,
ui: &mut Ui,
button_color: Color32,
icon_type: Option<WindowControlIcon>,
icon_color: Color32,
size: f32,
) -> egui::Response {
let button_size = Vec2::new(size, size);
let (button_id, button_rect) = ui.allocate_space(button_size);
let y_center = title_bar_height() / 2.0;
let centered_pos = Pos2::new(button_rect.center().x, y_center);
let centered_rect = Rect::from_center_size(centered_pos, button_size);
let response = ui.interact(centered_rect, button_id, Sense::click());
let primary_mouse_down = ui
.ctx()
.input(|i| i.pointer.button_down(egui::PointerButton::Primary));
let darken = response.hovered() && primary_mouse_down;
let bkg = if darken {
Color32::from_rgba_premultiplied(
(button_color.r() as f32 * 0.75) as u8,
(button_color.g() as f32 * 0.75) as u8,
(button_color.b() as f32 * 0.75) as u8,
button_color.a(),
)
} else {
button_color
};
ui.painter().circle_filled(centered_pos, size / 2.0, bkg);
ui.painter().circle_stroke(
centered_pos,
size / 2.0,
Stroke::new(0.5, Color32::from_rgba_premultiplied(0, 0, 0, 30)),
);
if let Some(icon) = icon_type {
let icon_color = if darken {
Color32::from_rgba_premultiplied(
(icon_color.r() as f32 * 0.45) as u8,
(icon_color.g() as f32 * 0.45) as u8,
(icon_color.b() as f32 * 0.45) as u8,
icon_color.a(),
)
} else {
icon_color
};
match icon {
WindowControlIcon::Close => {
let rect = Rect::from_center_size(centered_pos, Vec2::new(6.0, 6.0));
self.draw_mac_close_icon(ui.painter(), rect, icon_color)
}
WindowControlIcon::Maximize => {
let rect = Rect::from_center_size(centered_pos, Vec2::new(6.0, 6.0));
self.draw_mac_zoom_icon(ui.painter(), rect, icon_color)
}
WindowControlIcon::Restore => {
let rect = Rect::from_center_size(centered_pos, Vec2::new(6.0, 6.0));
self.draw_mac_classic_zoom_icon(ui.painter(), rect, icon_color)
}
WindowControlIcon::Minimize => {
let rect = Rect::from_center_size(centered_pos, Vec2::new(8.0, 6.0));
self.draw_mac_miniaturize_icon(ui.painter(), rect, icon_color)
}
}
}
response
}
}