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
use log::{debug, warn};
use smithay_client_toolkit::reexports::csd_frame::{WindowManagerCapabilities, WindowState};
use tiny_skia::{FillRule, PathBuilder, PixmapMut, Rect, Stroke, Transform};

use crate::{theme::ColorMap, Location, SkiaResult};

/// The size of the button on the header bar in logical points.
const BUTTON_SIZE: f32 = 24.;
const BUTTON_MARGIN: f32 = 5.;
const BUTTON_SPACING: f32 = 13.;

#[derive(Debug)]
pub(crate) struct Buttons {
    // Sorted by order vec of buttons for the left and right sides
    buttons_left: Vec<Button>,
    buttons_right: Vec<Button>,
    layout_config: Option<(String, String)>,
}

type ButtonLayout = (Vec<Button>, Vec<Button>);

impl Default for Buttons {
    fn default() -> Self {
        let (buttons_left, buttons_right) = Buttons::get_default_buttons_layout();

        Self {
            buttons_left,
            buttons_right,
            layout_config: None,
        }
    }
}

impl Buttons {
    pub fn new(layout_config: Option<(String, String)>) -> Self {
        match Buttons::parse_button_layout(layout_config.clone()) {
            Some((buttons_left, buttons_right)) => Self {
                buttons_left,
                buttons_right,
                layout_config,
            },
            _ => Self::default(),
        }
    }

    /// Rearrange the buttons with the new width.
    pub fn arrange(&mut self, width: u32, margin_h: f32) {
        let mut left_x = BUTTON_MARGIN + margin_h;
        let mut right_x = width as f32 - BUTTON_MARGIN;

        for button in &mut self.buttons_left {
            button.offset = left_x;

            // Add the button size plus spacing
            left_x += BUTTON_SIZE + BUTTON_SPACING;
        }

        for button in &mut self.buttons_right {
            // Subtract the button size.
            right_x -= BUTTON_SIZE;

            // Update it
            button.offset = right_x;

            // Subtract spacing for the next button.
            right_x -= BUTTON_SPACING;
        }
    }

    /// Find the coordinate of the button.
    pub fn find_button(&self, x: f64, y: f64) -> Location {
        let x = x as f32;
        let y = y as f32;
        let buttons = self.buttons_left.iter().chain(self.buttons_right.iter());

        for button in buttons {
            if button.contains(x, y) {
                return Location::Button(button.kind);
            }
        }

        Location::Head
    }

    pub fn update_wm_capabilities(&mut self, wm_capabilites: WindowManagerCapabilities) {
        let supports_maximize = wm_capabilites.contains(WindowManagerCapabilities::MAXIMIZE);
        let supports_minimize = wm_capabilites.contains(WindowManagerCapabilities::MINIMIZE);

        self.update_buttons(supports_maximize, supports_minimize);
    }

    pub fn update_buttons(&mut self, supports_maximize: bool, supports_minimize: bool) {
        let is_supported = |button: &Button| match button.kind {
            ButtonKind::Close => true,
            ButtonKind::Maximize => supports_maximize,
            ButtonKind::Minimize => supports_minimize,
        };

        let (buttons_left, buttons_right) =
            Buttons::parse_button_layout(self.layout_config.clone())
                .unwrap_or_else(Buttons::get_default_buttons_layout);

        self.buttons_left = buttons_left.into_iter().filter(is_supported).collect();
        self.buttons_right = buttons_right.into_iter().filter(is_supported).collect();
    }

    pub fn right_buttons_start_x(&self) -> Option<f32> {
        self.buttons_right.last().map(|button| button.x())
    }

    pub fn left_buttons_end_x(&self) -> Option<f32> {
        self.buttons_left.last().map(|button| button.end_x())
    }

    #[allow(clippy::too_many_arguments)]
    pub fn draw(
        &self,
        start_x: f32,
        end_x: f32,
        scale: f32,
        colors: &ColorMap,
        mouse_location: Location,
        pixmap: &mut PixmapMut,
        resizable: bool,
        state: &WindowState,
    ) {
        let left_buttons_right_limit =
            self.right_buttons_start_x().unwrap_or(end_x).min(end_x) - BUTTON_SPACING;
        let buttons_left = self.buttons_left.iter().map(|x| (x, Side::Left));
        let buttons_right = self.buttons_right.iter().map(|x| (x, Side::Right));

        for (button, side) in buttons_left.chain(buttons_right) {
            let is_visible = button.x() > start_x && button.end_x() < end_x
                // If we have buttons from both sides and they overlap, prefer the right side
                && (side == Side::Right || button.end_x() < left_buttons_right_limit);

            if is_visible {
                button.draw(scale, colors, mouse_location, pixmap, resizable, state);
            }
        }
    }

    fn parse_button_layout(sides: Option<(String, String)>) -> Option<ButtonLayout> {
        let Some((left_side, right_side)) = sides else {
            return None;
        };

        let buttons_left = Buttons::parse_button_layout_side(left_side, Side::Left);
        let buttons_right = Buttons::parse_button_layout_side(right_side, Side::Right);

        if buttons_left.is_empty() && buttons_right.is_empty() {
            warn!("No valid buttons found in configuration");
            return None;
        }

        Some((buttons_left, buttons_right))
    }

    fn parse_button_layout_side(config: String, side: Side) -> Vec<Button> {
        let mut buttons: Vec<Button> = vec![];

        for button in config.split(',').take(3) {
            let button_kind = match button {
                "close" => ButtonKind::Close,
                "maximize" => ButtonKind::Maximize,
                "minimize" => ButtonKind::Minimize,
                "appmenu" => {
                    debug!("Ignoring \"appmenu\" button");
                    continue;
                }
                _ => {
                    warn!("Ignoring unknown button type: {button}");
                    continue;
                }
            };

            buttons.push(Button::new(button_kind));
        }

        // For the right side, we need to revert the order
        if side == Side::Right {
            buttons.into_iter().rev().collect()
        } else {
            buttons
        }
    }

    fn get_default_buttons_layout() -> ButtonLayout {
        (
            vec![],
            vec![
                Button::new(ButtonKind::Close),
                Button::new(ButtonKind::Maximize),
                Button::new(ButtonKind::Minimize),
            ],
        )
    }
}

#[derive(Debug, Clone)]
pub(crate) struct Button {
    /// The button offset into the header bar canvas.
    offset: f32,
    /// The kind of the button.
    kind: ButtonKind,
}

impl Button {
    pub fn new(kind: ButtonKind) -> Self {
        Self { offset: 0., kind }
    }

    pub fn radius(&self) -> f32 {
        BUTTON_SIZE / 2.0
    }

    pub fn x(&self) -> f32 {
        self.offset
    }

    pub fn center_x(&self) -> f32 {
        self.offset + self.radius()
    }

    pub fn center_y(&self) -> f32 {
        BUTTON_MARGIN + self.radius()
    }

    pub fn end_x(&self) -> f32 {
        self.offset + BUTTON_SIZE
    }

    fn contains(&self, x: f32, y: f32) -> bool {
        x > self.offset
            && x < self.offset + BUTTON_SIZE
            && y > BUTTON_MARGIN
            && y < BUTTON_MARGIN + BUTTON_SIZE
    }

    pub fn draw(
        &self,
        scale: f32,
        colors: &ColorMap,
        mouse_location: Location,
        pixmap: &mut PixmapMut,
        resizable: bool,
        state: &WindowState,
    ) -> SkiaResult {
        let button_bg = if mouse_location == Location::Button(self.kind)
            && (resizable || self.kind != ButtonKind::Maximize)
        {
            colors.button_hover_paint()
        } else {
            colors.button_idle_paint()
        };

        // Convert to pixels.
        let x = self.center_x() * scale;
        let y = self.center_y() * scale;
        let radius = self.radius() * scale;

        // Draw the button background.
        let circle = PathBuilder::from_circle(x, y, radius)?;
        pixmap.fill_path(
            &circle,
            &button_bg,
            FillRule::Winding,
            Transform::identity(),
            None,
        );

        let mut button_icon_paint = colors.button_icon_paint();
        // Do AA only for diagonal lines.
        button_icon_paint.anti_alias = self.kind == ButtonKind::Close;

        // Draw the icon.
        match self.kind {
            ButtonKind::Close => {
                let x_icon = {
                    let size = 3.5 * scale;
                    let mut pb = PathBuilder::new();

                    {
                        let sx = x - size;
                        let sy = y - size;
                        let ex = x + size;
                        let ey = y + size;

                        pb.move_to(sx, sy);
                        pb.line_to(ex, ey);
                        pb.close();
                    }

                    {
                        let sx = x - size;
                        let sy = y + size;
                        let ex = x + size;
                        let ey = y - size;

                        pb.move_to(sx, sy);
                        pb.line_to(ex, ey);
                        pb.close();
                    }

                    pb.finish()?
                };

                pixmap.stroke_path(
                    &x_icon,
                    &button_icon_paint,
                    &Stroke {
                        width: 1.1 * scale,
                        ..Default::default()
                    },
                    Transform::identity(),
                    None,
                );
            }
            ButtonKind::Maximize => {
                let path2 = {
                    let size = 8.0 * scale;
                    let hsize = size / 2.0;
                    let mut pb = PathBuilder::new();

                    let x = x - hsize;
                    let y = y - hsize;
                    if state.contains(WindowState::MAXIMIZED) {
                        let offset = 2.0 * scale;
                        if let Some(rect) =
                            Rect::from_xywh(x, y + offset, size - offset, size - offset)
                        {
                            pb.push_rect(rect);
                            pb.move_to(rect.left() + offset, rect.top() - offset);
                            pb.line_to(rect.right() + offset, rect.top() - offset);
                            pb.line_to(rect.right() + offset, rect.bottom() - offset + 0.5);
                        }
                    } else if let Some(rect) = Rect::from_xywh(x, y, size, size) {
                        pb.push_rect(rect);
                    }

                    pb.finish()?
                };

                pixmap.stroke_path(
                    &path2,
                    &button_icon_paint,
                    &Stroke {
                        width: 1.0 * scale,
                        ..Default::default()
                    },
                    Transform::identity(),
                    None,
                );
            }
            ButtonKind::Minimize => {
                let len = 8.0 * scale;
                let hlen = len / 2.0;
                pixmap.fill_rect(
                    Rect::from_xywh(x - hlen, y + hlen, len, scale)?,
                    &button_icon_paint,
                    Transform::identity(),
                    None,
                );
            }
        }

        Some(())
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ButtonKind {
    Close,
    Maximize,
    Minimize,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Side {
    Left,
    Right,
}