iced_aw 0.14.1

Additional widgets for the Iced GUI library
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Displays a [`TabBar`](crate::widget::tab_bar::TabBar) to select the content
//! to be displayed.
//!
//! You have to manage the logic to show the contend by yourself or you may want
//! to use the [`Tabs`](crate::widget::tabs::Tabs) widget instead.
//!
//! *This API requires the following crate features to be activated: `tab_bar`*

use super::{Status, StyleFn};
use iced_core::{Background, Color, Theme, border::Radius};

/// The appearance of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[derive(Clone, Copy, Debug)]
pub struct Style {
    /// The background of the tab bar.
    pub background: Option<Background>,

    /// The border color of the tab bar.
    pub border_color: Option<Color>,

    /// The border width of the tab bar.
    pub border_width: f32,

    /// The border radius of a tab.
    pub tab_border_radius: Radius,

    /// The background of the tab labels.
    pub tab_label_background: Background,

    /// The border color of the tab labels.
    pub tab_label_border_color: Color,

    /// The border with of the tab labels.
    pub tab_label_border_width: f32,

    /// The icon color of the tab labels.
    pub icon_color: Color,

    /// The color of the closing icon border
    pub icon_background: Option<Background>,

    /// How soft/hard the corners of the icon border are
    pub icon_border_radius: Radius,

    /// The text color of the tab labels.
    pub text_color: Color,
}

impl Default for Style {
    fn default() -> Self {
        Self {
            background: None,
            border_color: None,
            border_width: 0.0,
            tab_border_radius: Radius::default(),
            tab_label_background: Background::Color([0.87, 0.87, 0.87].into()),
            tab_label_border_color: [0.7, 0.7, 0.7].into(),
            tab_label_border_width: 1.0,
            icon_color: Color::BLACK,
            icon_background: Some(Background::Color(Color::TRANSPARENT)),
            icon_border_radius: 4.0.into(),
            text_color: Color::BLACK,
        }
    }
}
/// The Catalog of a [`TabBar`](crate::widget::tab_bar::TabBar).
pub trait Catalog {
    ///Style for the trait to use.
    type Class<'a>;

    /// The default class produced by the [`Catalog`].
    fn default<'a>() -> Self::Class<'a>;

    /// The [`Style`] of a class with the given status.
    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}

impl Catalog for Theme {
    type Class<'a> = StyleFn<'a, Self, Style>;

    fn default<'a>() -> Self::Class<'a> {
        Box::new(primary)
    }

    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
        class(self, status)
    }
}

/// The primary theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn primary(theme: &Theme, status: Status) -> Style {
    let mut base = Style::default();
    let palette = theme.extended_palette();

    base.text_color = palette.background.base.text;

    match status {
        Status::Disabled => {
            base.tab_label_background = Background::Color(palette.background.strong.color);
        }
        Status::Hovered => {
            base.tab_label_background = Background::Color(palette.primary.strong.color);
        }
        _ => {
            base.tab_label_background = Background::Color(palette.primary.base.color);
        }
    }

    base
}

/// The dark theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn dark(_theme: &Theme, status: Status) -> Style {
    let mut base = Style {
        tab_label_background: Background::Color([0.1, 0.1, 0.1].into()),
        tab_label_border_color: [0.3, 0.3, 0.3].into(),
        icon_color: Color::WHITE,
        text_color: Color::WHITE,
        ..Default::default()
    };

    if status == Status::Disabled {
        base.tab_label_background = Background::Color([0.13, 0.13, 0.13].into());
    }

    base
}

/// The red theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn red(_theme: &Theme, status: Status) -> Style {
    let mut base = Style {
        tab_label_background: Background::Color([1.0, 0.0, 0.0].into()),
        tab_label_border_color: Color::TRANSPARENT,
        tab_label_border_width: 0.0,
        icon_color: Color::WHITE,
        text_color: Color::WHITE,
        ..Default::default()
    };

    if status == Status::Disabled {
        base.tab_label_background = Background::Color([0.13, 0.13, 0.13].into());
        base.icon_color = Color::BLACK;
        base.text_color = Color::BLACK;
    }

    base
}

/// The blue theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn blue(_theme: &Theme, status: Status) -> Style {
    let mut base = Style {
        tab_label_background: Background::Color([0.0, 0.0, 1.0].into()),
        tab_label_border_color: [0.0, 0.0, 1.0].into(),
        icon_color: Color::WHITE,
        text_color: Color::WHITE,
        ..Default::default()
    };

    if status == Status::Disabled {
        base.tab_label_background = Background::Color([0.5, 0.5, 1.0].into());
        base.tab_label_border_color = [0.5, 0.5, 1.0].into();
    }

    base
}

/// The blue theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn green(_theme: &Theme, status: Status) -> Style {
    let mut base = Style {
        tab_label_background: Color::WHITE.into(),
        icon_color: [0.0, 0.5, 0.0].into(),
        text_color: [0.0, 0.5, 0.0].into(),
        ..Default::default()
    };

    match status {
        Status::Disabled => {
            base.icon_color = [0.7, 0.7, 0.7].into();
            base.text_color = [0.7, 0.7, 0.7].into();
            base.tab_label_border_color = [0.7, 0.7, 0.7].into();
        }
        _ => {
            base.tab_label_border_color = [0.0, 0.5, 0.0].into();
        }
    }

    base
}

/// The purple theme of a [`TabBar`](crate::widget::tab_bar::TabBar).
#[must_use]
pub fn purple(_theme: &Theme, status: Status) -> Style {
    let mut base = Style {
        tab_label_background: Color::WHITE.into(),
        tab_label_border_color: Color::TRANSPARENT,
        icon_color: [0.7, 0.0, 1.0].into(),
        text_color: [0.7, 0.0, 1.0].into(),
        ..Default::default()
    };

    if status == Status::Disabled {
        base.icon_color = Color::BLACK;
        base.text_color = Color::BLACK;
    }

    base
}

#[cfg(test)]
mod tests {
    use super::*;
    use iced_core::{Background, Theme};

    #[test]
    fn style_default() {
        let style = Style::default();

        assert!(style.background.is_none());
        assert!(style.border_color.is_none());
        assert_eq!(style.border_width, 0.0);
        assert!(matches!(style.tab_label_background, Background::Color(_)));
        assert_eq!(style.tab_label_border_width, 1.0);
        assert_eq!(style.icon_color, Color::BLACK);
        assert!(style.icon_background.is_some());
        assert_eq!(style.text_color, Color::BLACK);
    }

    #[test]
    fn primary_theme_active() {
        let theme = Theme::TokyoNight;
        let style = primary(&theme, Status::Active);

        assert!(matches!(style.tab_label_background, Background::Color(_)));
    }

    #[test]
    fn primary_theme_hovered() {
        let theme = Theme::TokyoNight;
        let style = primary(&theme, Status::Hovered);

        assert!(matches!(style.tab_label_background, Background::Color(_)));
    }

    #[test]
    fn primary_theme_disabled() {
        let theme = Theme::TokyoNight;
        let style = primary(&theme, Status::Disabled);

        assert!(matches!(style.tab_label_background, Background::Color(_)));
    }

    #[test]
    fn primary_theme_focused() {
        let theme = Theme::TokyoNight;
        let style = primary(&theme, Status::Focused);

        assert!(matches!(style.tab_label_background, Background::Color(_)));
    }

    #[test]
    fn primary_changes_background_by_status() {
        let theme = Theme::TokyoNight;
        let active = primary(&theme, Status::Active);
        let hovered = primary(&theme, Status::Hovered);
        let disabled = primary(&theme, Status::Disabled);

        // Different statuses should have different backgrounds
        assert_ne!(
            format!("{:?}", active.tab_label_background),
            format!("{:?}", hovered.tab_label_background)
        );
        assert_ne!(
            format!("{:?}", active.tab_label_background),
            format!("{:?}", disabled.tab_label_background)
        );
    }

    #[test]
    fn dark_theme_active() {
        let theme = Theme::TokyoNight;
        let style = dark(&theme, Status::Active);

        assert_eq!(style.icon_color, Color::WHITE);
        assert_eq!(style.text_color, Color::WHITE);
    }

    #[test]
    fn dark_theme_disabled() {
        let theme = Theme::TokyoNight;
        let style = dark(&theme, Status::Disabled);

        assert_eq!(style.icon_color, Color::WHITE);
        assert_eq!(style.text_color, Color::WHITE);
    }

    #[test]
    fn dark_theme_changes_background_when_disabled() {
        let theme = Theme::TokyoNight;
        let active = dark(&theme, Status::Active);
        let disabled = dark(&theme, Status::Disabled);

        assert_ne!(
            format!("{:?}", active.tab_label_background),
            format!("{:?}", disabled.tab_label_background)
        );
    }

    #[test]
    fn red_theme_active() {
        let theme = Theme::TokyoNight;
        let style = red(&theme, Status::Active);

        assert_eq!(
            style.tab_label_background,
            Background::Color([1.0, 0.0, 0.0].into())
        );
        assert_eq!(style.icon_color, Color::WHITE);
        assert_eq!(style.text_color, Color::WHITE);
        assert_eq!(style.tab_label_border_width, 0.0);
    }

    #[test]
    fn red_theme_disabled() {
        let theme = Theme::TokyoNight;
        let style = red(&theme, Status::Disabled);

        assert_eq!(style.icon_color, Color::BLACK);
        assert_eq!(style.text_color, Color::BLACK);
    }

    #[test]
    fn blue_theme_active() {
        let theme = Theme::TokyoNight;
        let style = blue(&theme, Status::Active);

        assert_eq!(
            style.tab_label_background,
            Background::Color([0.0, 0.0, 1.0].into())
        );
        assert_eq!(style.icon_color, Color::WHITE);
        assert_eq!(style.text_color, Color::WHITE);
    }

    #[test]
    fn blue_theme_disabled() {
        let theme = Theme::TokyoNight;
        let style = blue(&theme, Status::Disabled);

        assert_eq!(
            style.tab_label_background,
            Background::Color([0.5, 0.5, 1.0].into())
        );
        assert_eq!(style.tab_label_border_color, [0.5, 0.5, 1.0].into());
    }

    #[test]
    fn green_theme_active() {
        let theme = Theme::TokyoNight;
        let style = green(&theme, Status::Active);

        assert_eq!(style.tab_label_background, Background::Color(Color::WHITE));
        assert_eq!(style.icon_color, [0.0, 0.5, 0.0].into());
        assert_eq!(style.text_color, [0.0, 0.5, 0.0].into());
        assert_eq!(style.tab_label_border_color, [0.0, 0.5, 0.0].into());
    }

    #[test]
    fn green_theme_disabled() {
        let theme = Theme::TokyoNight;
        let style = green(&theme, Status::Disabled);

        assert_eq!(style.icon_color, [0.7, 0.7, 0.7].into());
        assert_eq!(style.text_color, [0.7, 0.7, 0.7].into());
        assert_eq!(style.tab_label_border_color, [0.7, 0.7, 0.7].into());
    }

    #[test]
    fn purple_theme_active() {
        let theme = Theme::TokyoNight;
        let style = purple(&theme, Status::Active);

        assert_eq!(style.tab_label_background, Background::Color(Color::WHITE));
        assert_eq!(style.icon_color, [0.7, 0.0, 1.0].into());
        assert_eq!(style.text_color, [0.7, 0.0, 1.0].into());
        assert_eq!(style.tab_label_border_color, Color::TRANSPARENT);
    }

    #[test]
    fn purple_theme_disabled() {
        let theme = Theme::TokyoNight;
        let style = purple(&theme, Status::Disabled);

        assert_eq!(style.icon_color, Color::BLACK);
        assert_eq!(style.text_color, Color::BLACK);
    }

    #[test]
    fn catalog_default_class() {
        let _class = <Theme as Catalog>::default();
    }

    #[test]
    fn catalog_style() {
        let theme = Theme::TokyoNight;
        let class = <Theme as Catalog>::default();
        let style = theme.style(&class, Status::Active);

        assert!(matches!(style.tab_label_background, Background::Color(_)));
    }

    #[test]
    fn catalog_style_with_different_statuses() {
        let theme = Theme::TokyoNight;
        let class = <Theme as Catalog>::default();

        let active = theme.style(&class, Status::Active);
        let hovered = theme.style(&class, Status::Hovered);
        let disabled = theme.style(&class, Status::Disabled);

        assert!(matches!(active.tab_label_background, Background::Color(_)));
        assert!(matches!(hovered.tab_label_background, Background::Color(_)));
        assert!(matches!(
            disabled.tab_label_background,
            Background::Color(_)
        ));
    }

    #[test]
    fn icon_border_radius_is_set() {
        let style = Style::default();
        // Icon border radius should be set to 4.0 (all corners)
        assert_eq!(style.icon_border_radius.top_left, 4.0);
        assert_eq!(style.icon_border_radius.top_right, 4.0);
        assert_eq!(style.icon_border_radius.bottom_left, 4.0);
        assert_eq!(style.icon_border_radius.bottom_right, 4.0);
    }

    #[test]
    fn different_themes_produce_different_styles() {
        let theme = Theme::TokyoNight;
        let status = Status::Active;

        let _primary_style = primary(&theme, status);
        let dark_style = dark(&theme, status);
        let red_style = red(&theme, status);
        let blue_style = blue(&theme, status);
        let green_style = green(&theme, status);
        let purple_style = purple(&theme, status);

        // All themes should produce different background styles
        assert_ne!(
            format!("{:?}", dark_style.tab_label_background),
            format!("{:?}", red_style.tab_label_background)
        );
        assert_ne!(
            format!("{:?}", red_style.tab_label_background),
            format!("{:?}", blue_style.tab_label_background)
        );
        assert_ne!(
            format!("{:?}", blue_style.tab_label_background),
            format!("{:?}", green_style.tab_label_background)
        );

        // Text colors also differ between some themes
        assert_ne!(green_style.text_color, purple_style.text_color);
    }
}