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
use smithay_client_toolkit::window::ButtonState;
use tiny_skia::{FillRule, PathBuilder, PixmapMut, Rect, Stroke, Transform};

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

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

#[derive(Default, Debug)]
pub(crate) struct Button {
    x: f32,
    y: f32,
    size: f32,
}

impl Button {
    pub fn radius(&self) -> f32 {
        self.size / 2.0
    }

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

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

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

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

impl Button {
    pub fn draw_minimize(
        &self,
        scale: f32,
        colors: &ColorMap,
        mouses: &[Location],
        pixmap: &mut PixmapMut,
    ) -> SkiaResult {
        let btn_state = if mouses.contains(&Location::Button(ButtonKind::Minimize)) {
            ButtonState::Hovered
        } else {
            ButtonState::Idle
        };

        let radius = self.radius();

        let x = self.center_x();
        let y = self.center_y();

        let circle = PathBuilder::from_circle(x, y, radius)?;

        let button_bg = if btn_state == ButtonState::Hovered {
            colors.button_hover_paint()
        } else {
            colors.button_idle_paint()
        };

        pixmap.fill_path(
            &circle,
            &button_bg,
            FillRule::Winding,
            Transform::identity(),
            None,
        );

        let mut button_icon_paint = colors.button_icon_paint();
        button_icon_paint.anti_alias = false;

        let len = 8.0 * scale;
        let hlen = len / 2.0;
        pixmap.fill_rect(
            Rect::from_xywh(x - hlen, y + hlen, len, 1.0 * scale)?,
            &button_icon_paint,
            Transform::identity(),
            None,
        );

        Some(())
    }

    pub fn draw_maximize(
        &self,
        scale: f32,
        colors: &ColorMap,
        mouses: &[Location],
        maximizable: bool,
        is_maximized: bool,
        pixmap: &mut PixmapMut,
    ) -> SkiaResult {
        let btn_state = if !maximizable {
            ButtonState::Disabled
        } else if mouses
            .iter()
            .any(|&l| l == Location::Button(ButtonKind::Maximize))
        {
            ButtonState::Hovered
        } else {
            ButtonState::Idle
        };

        let radius = self.radius();

        let x = self.center_x();
        let y = self.center_y();

        let path1 = {
            let mut pb = PathBuilder::new();
            pb.push_circle(x, y, radius);
            pb.finish()?
        };

        let button_bg = if btn_state == ButtonState::Hovered {
            colors.button_hover_paint()
        } else {
            colors.button_idle_paint()
        };

        pixmap.fill_path(
            &path1,
            &button_bg,
            FillRule::Winding,
            Transform::identity(),
            None,
        );

        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;
            pb.push_rect(x, y, size, size);

            if is_maximized {
                if let Some(rect) = Rect::from_xywh(x + 2.0, y - 2.0, size, size) {
                    pb.move_to(rect.left(), rect.top());
                    pb.line_to(rect.right(), rect.top());
                    pb.line_to(rect.right(), rect.bottom());
                }
            }

            pb.finish()?
        };

        let mut button_icon_paint = colors.button_icon_paint();
        button_icon_paint.anti_alias = false;
        pixmap.stroke_path(
            &path2,
            &button_icon_paint,
            &Stroke {
                width: 1.0 * scale,
                ..Default::default()
            },
            Transform::identity(),
            None,
        );

        Some(())
    }

    pub fn draw_close(
        &self,
        scale: f32,
        colors: &ColorMap,
        mouses: &[Location],
        pixmap: &mut PixmapMut,
    ) -> SkiaResult {
        // Draw the close button
        let btn_state = if mouses
            .iter()
            .any(|&l| l == Location::Button(ButtonKind::Close))
        {
            ButtonState::Hovered
        } else {
            ButtonState::Idle
        };

        let radius = self.radius();

        let x = self.center_x();
        let y = self.center_y();

        let path1 = {
            let mut pb = PathBuilder::new();
            pb.push_circle(x, y, radius);
            pb.finish()?
        };

        let button_bg = if btn_state == ButtonState::Hovered {
            colors.button_hover_paint()
        } else {
            colors.button_idle_paint()
        };

        pixmap.fill_path(
            &path1,
            &button_bg,
            FillRule::Winding,
            Transform::identity(),
            None,
        );

        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()?
        };

        let mut button_icon_paint = colors.button_icon_paint();
        button_icon_paint.anti_alias = true;
        pixmap.stroke_path(
            &x_icon,
            &button_icon_paint,
            &Stroke {
                width: 1.1 * scale,
                ..Default::default()
            },
            Transform::identity(),
            None,
        );

        Some(())
    }
}

#[derive(Debug)]
pub(crate) struct Buttons {
    pub close: Button,
    pub maximize: Button,
    pub minimize: Button,

    w: u32,
    h: u32,

    scale: u32,
}

impl Default for Buttons {
    fn default() -> Self {
        Self {
            close: Default::default(),
            maximize: Default::default(),
            minimize: Default::default(),
            scale: 1,

            w: 0,
            h: super::theme::HEADER_SIZE,
        }
    }
}

impl Buttons {
    pub fn arrange(&mut self, w: u32) {
        self.w = w;

        let scale = self.scale as f32;
        let margin_top = BORDER_SIZE as f32 * scale;
        let margin = 5.0 * scale;
        let spacing = 13.0 * scale;
        let size = 12.0 * 2.0 * scale;

        let mut x = w as f32 * scale - margin - BORDER_SIZE as f32 * scale;
        let y = margin + margin_top;

        x -= size;
        self.close.x = x;
        self.close.y = y;
        self.close.size = size;

        x -= size;
        x -= spacing;
        self.maximize.x = x;
        self.maximize.y = y;
        self.maximize.size = size;

        x -= size;
        x -= spacing;
        self.minimize.x = x;
        self.minimize.y = y;
        self.minimize.size = size;
    }

    pub fn update_scale(&mut self, scale: u32) {
        if self.scale != scale {
            self.scale = scale;
            self.arrange(self.w);
        }
    }

    pub fn find_button(&self, x: f64, y: f64) -> Location {
        let x = x as f32 * self.scale as f32;
        let y = y as f32 * self.scale as f32;
        if self.close.contains(x, y) {
            Location::Button(ButtonKind::Close)
        } else if self.maximize.contains(x, y) {
            Location::Button(ButtonKind::Maximize)
        } else if self.minimize.contains(x, y) {
            Location::Button(ButtonKind::Minimize)
        } else {
            Location::Head
        }
    }

    pub fn scaled_size(&self) -> (u32, u32) {
        (self.w * self.scale, self.h * self.scale)
    }
}