skryn 0.0.4

A servo/webrender based Desktop GUI library
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
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::mem;
use std::sync::{Arc, Mutex};

use webrender::api::ColorF;
//use webrender::api::DeviceSize

#[derive(Clone, Debug, PartialEq)]
pub struct Point {
    pub x: f32,
    pub y: f32,
}

impl Point {
    pub fn new() -> Point {
        Point { x: 0.0, y: 0.0 }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Extent {
    pub x: f32,
    pub y: f32,
    pub w: f32,
    pub h: f32,
    pub dpi: f32,
}

impl Extent {
    pub fn new() -> Extent {
        Extent {
            x: 0.0,
            y: 0.0,
            w: 0.0,
            h: 0.0,
            dpi: 0.0,
        }
    }
}

#[derive(Clone, Debug)]
pub enum Unit {
    Natural,
    Extent,
    Pixel(f32),
    Stretch(f32),
}
impl PartialEq for Unit {
    fn eq(&self, other: &Unit) -> bool {
        mem::discriminant(self) == mem::discriminant(other)
    }
}
impl Eq for Unit {}
impl Hash for Unit {
    fn hash<H: Hasher>(&self, state: &mut H) {
        mem::discriminant(self).hash(state)
    }
}

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

#[derive(Clone, Debug)]
pub enum Property {
    Size(i32), //in pixels
    Family(String),
    Left(Unit),   //in pixels or stretches
    Width(Unit),  //in pixels or stretches
    Right(Unit),  //in pixels or stretches
    Top(Unit),    //in pixels or stretches
    Height(Unit), //in pixels or stretches
    Bottom(Unit), //in pixels or stretches
    MinWidth(Unit),
    MinHeight(Unit),
    Color(ColorF),
    BgColor(ColorF),
    HoverColor(ColorF),
    HoverBgColor(ColorF),
    FocusColor(ColorF),
    FocusBgColor(ColorF),
    ActiveColor(ColorF),
    ActiveBgColor(ColorF),
    DisabledColor(ColorF),
    DisabledBgColor(ColorF),
    TextAlign(Align),
}

lazy_static! {
    pub static ref SIZE: Property = Property::Size(0);
    pub static ref FAMILY: Property = Property::Family(String::from(""));
    pub static ref LEFT: Property = Property::Left(Unit::Stretch(0.0));
    pub static ref WIDTH: Property = Property::Width(Unit::Stretch(1.0));
    pub static ref RIGHT: Property = Property::Right(Unit::Stretch(0.0));
    pub static ref TOP: Property = Property::Top(Unit::Stretch(0.0));
    pub static ref HEIGHT: Property = Property::Height(Unit::Stretch(1.0));
    pub static ref BOTTOM: Property = Property::Bottom(Unit::Stretch(0.0));
    pub static ref MIN_WIDTH: Property = Property::MinWidth(Unit::Pixel(0.0));
    pub static ref MIN_HEIGHT: Property = Property::MinHeight(Unit::Pixel(0.0));
    pub static ref COLOR: Property = Property::Color(ColorF {
        r: 0.2,
        g: 0.2,
        b: 0.2,
        a: 1.0,
    });
    pub static ref BG_COLOR: Property = Property::BgColor(ColorF {
        r: 0.9,
        g: 0.9,
        b: 0.9,
        a: 1.0,
    });
    pub static ref HOVER_COLOR: Property = Property::HoverColor(ColorF {
        r: 0.2,
        g: 0.2,
        b: 0.2,
        a: 1.0,
    });
    pub static ref HOVER_BG_COLOR: Property = Property::HoverBgColor(ColorF {
        r: 0.8,
        g: 0.8,
        b: 0.8,
        a: 1.0,
    });
    pub static ref FOCUS_COLOR: Property = Property::FocusColor(ColorF {
        r: 0.0,
        g: 0.0,
        b: 0.0,
        a: 1.0,
    });
    pub static ref FOCUS_BG_COLOR: Property = Property::FocusBgColor(ColorF {
        r: 0.9,
        g: 0.9,
        b: 0.9,
        a: 1.0,
    });
    pub static ref ACTIVE_COLOR: Property = Property::ActiveColor(ColorF {
        r: 0.2,
        g: 0.2,
        b: 0.2,
        a: 1.0,
    });
    pub static ref ACTIVE_BG_COLOR: Property = Property::ActiveBgColor(ColorF {
        r: 1.0,
        g: 1.0,
        b: 1.0,
        a: 1.0,
    });
    pub static ref DISABLED_COLOR: Property = Property::DisabledColor(ColorF {
        r: 0.2,
        g: 0.2,
        b: 0.2,
        a: 1.0,
    });
    pub static ref DISABLED_BG_COLOR: Property = Property::DisabledBgColor(ColorF {
        r: 0.8,
        g: 0.8,
        b: 0.8,
        a: 1.0,
    });
    pub static ref TEXT_ALIGN: Property = Property::TextAlign(Align::Left);
}

impl PartialEq for Property {
    fn eq(&self, other: &Property) -> bool {
        mem::discriminant(self) == mem::discriminant(other)
    }
}
impl Eq for Property {}

impl Hash for Property {
    fn hash<H: Hasher>(&self, state: &mut H) {
        mem::discriminant(self).hash(state)
    }
}

#[derive(Clone, Debug, Default)]
pub struct Properties(HashSet<Property>);

impl Properties {
    pub fn new() -> Properties {
        Properties(HashSet::new())
    }

    pub fn default(&mut self) -> &mut Properties {
        if cfg!(target_os = "linux")
        {
            self.set(Property::Family(String::from("FreeMono")));
        }
        if cfg!(target_os = "windows") || cfg!(target_os = "macos")
        {
            self.set(Property::Family(String::from("Arial")));
        }
        self.set(Property::Size(16))
            .set(Property::Left(Unit::Stretch(0.0)))
            .set(Property::Width(Unit::Stretch(1.0)))
            .set(Property::Right(Unit::Stretch(0.0)))
            .set(Property::Top(Unit::Stretch(0.0)))
            .set(Property::Height(Unit::Stretch(1.0)))
            .set(Property::Bottom(Unit::Stretch(0.0)))
            .set(Property::MinWidth(Unit::Pixel(0.0)))
            .set(Property::MinHeight(Unit::Pixel(0.0)))
            .set(Property::Color(ColorF::new(0.8, 0.8, 0.8, 1.0)))
            .set(Property::BgColor(ColorF::new(1.0, 1.0, 1.0, 0.0)))
            .set(Property::FocusColor(ColorF::new(1.0, 1.0, 1.0, 1.0)))
            .set(Property::FocusBgColor(ColorF::new(0.0, 0.0, 0.0, 0.0)))
            .set(Property::HoverColor(ColorF::new(0.9, 0.9, 0.9, 1.0)))
            .set(Property::HoverBgColor(ColorF::new(0.0, 0.0, 0.0, 0.0)))
            .set(Property::DisabledColor(ColorF::new(0.5, 0.5, 0.5, 1.0)))
            .set(Property::DisabledBgColor(ColorF::new(0.0, 0.0, 0.0, 0.0)))
            .set(Property::TextAlign(Align::Left))
    }

    pub fn set(&mut self, property: Property) -> &mut Properties {
        {
            let x = &mut self.0;
            x.replace(property);
        }
        self
    }

    pub fn get(&self, property: &Property) -> Option<&Property> {
        self.0.get(property)
    }

    pub fn get_size(&self) -> i32 {
        if let Some(Property::Size(x)) = self.get(&SIZE) {
            *x
        } else {
            panic!("Size not found")
        }
    }

    pub fn get_family(&self) -> String {
        if let Some(Property::Family(x)) = self.get(&FAMILY) {
            x.clone()
        } else {
            panic!("Family not found")
        }
    }

    pub fn get_left(&self) -> Unit {
        if let Some(Property::Left(x)) = self.get(&LEFT) {
            x.clone()
        } else {
            panic!("Left not found")
        }
    }

    pub fn get_width(&self) -> Unit {
        if let Some(Property::Width(x)) = self.get(&WIDTH) {
            x.clone()
        } else {
            panic!("Width not found")
        }
    }

    pub fn get_right(&self) -> Unit {
        if let Some(Property::Right(x)) = self.get(&RIGHT) {
            x.clone()
        } else {
            panic!("Right not found")
        }
    }

    pub fn get_top(&self) -> Unit {
        if let Some(Property::Top(x)) = self.get(&TOP) {
            x.clone()
        } else {
            panic!("Top not found")
        }
    }

    pub fn get_height(&self) -> Unit {
        if let Some(Property::Height(x)) = self.get(&HEIGHT) {
            x.clone()
        } else {
            panic!("Height not found")
        }
    }

    pub fn get_bottom(&self) -> Unit {
        if let Some(Property::Bottom(x)) = self.get(&BOTTOM) {
            x.clone()
        } else {
            panic!("Bottom not found")
        }
    }

    pub fn get_min_width(&self) -> Unit {
        if let Some(Property::MinWidth(x)) = self.get(&MIN_WIDTH) {
            x.clone()
        } else {
            panic!("Min Width not found")
        }
    }

    pub fn get_min_height(&self) -> Unit {
        if let Some(Property::MinHeight(x)) = self.get(&MIN_HEIGHT) {
            x.clone()
        } else {
            panic!("Min Height not found")
        }
    }

    pub fn get_color(&self) -> ColorF {
        if let Some(Property::Color(x)) = self.get(&COLOR) {
            *x
        } else {
            panic!("Color not found")
        }
    }

    pub fn get_bg_color(&self) -> ColorF {
        if let Some(Property::BgColor(x)) = self.get(&BG_COLOR) {
            *x
        } else {
            panic!("Background Color not found")
        }
    }

    pub fn get_focus_color(&self) -> ColorF {
        if let Some(Property::FocusColor(x)) = self.get(&FOCUS_COLOR) {
            *x
        } else {
            panic!("Focus Color not found")
        }
    }

    pub fn get_focus_bg_color(&self) -> ColorF {
        if let Some(Property::FocusBgColor(x)) = self.get(&FOCUS_BG_COLOR) {
            *x
        } else {
            panic!("Focus Background Color not found")
        }
    }

    pub fn get_hover_color(&self) -> ColorF {
        if let Some(Property::HoverColor(x)) = self.get(&HOVER_COLOR) {
            *x
        } else {
            panic!("Hover Color not found")
        }
    }

    pub fn get_hover_bg_color(&self) -> ColorF {
        if let Some(Property::HoverBgColor(x)) = self.get(&HOVER_BG_COLOR) {
            *x
        } else {
            panic!("Hover Background Color not found")
        }
    }

    pub fn get_disabled_color(&self) -> ColorF {
        if let Some(Property::DisabledColor(x)) = self.get(&DISABLED_COLOR) {
            *x
        } else {
            panic!("Disabled Color not found")
        }
    }

    pub fn get_disabled_bg_color(&self) -> ColorF {
        if let Some(Property::DisabledBgColor(x)) = self.get(&DISABLED_BG_COLOR) {
            *x
        } else {
            panic!("Disabled Background Color not found")
        }
    }

    pub fn get_text_align(&self) -> Align {
        if let Some(Property::TextAlign(x)) = self.get(&TEXT_ALIGN) {
            x.clone()
        } else {
            panic!("Text Align not found")
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Position {
    pub x: f32,
    pub y: f32,
}

#[derive(Clone, Debug, PartialEq)]
pub struct Modifiers {
    pub shift: bool,
    pub ctrl: bool,
    pub alt: bool,
    pub logo: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub enum ButtonState {
    Pressed,
    Released,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Button {
    Left,
    Middle,
    Right,
    Other,
}

#[derive(Clone, Debug)]
pub struct IdGenerator {
    pub next_id: Arc<Mutex<u64>>,
}

impl IdGenerator {
    pub fn new(start: u64) -> Self {
        IdGenerator {
            next_id: Arc::new(Mutex::new(start)),
        }
    }
    pub fn get(&mut self) -> u64 {
        let mut counter = self.next_id.lock().unwrap();
        *counter += 1;
        *counter
    }
    pub fn zero(&mut self) {
        let mut counter = self.next_id.lock().unwrap();
        *counter = 0;
    }
}