cosmic/
ext.rs

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
// Copyright 2022 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0

use iced::Color;
use iced_core::Widget;

pub trait ElementExt {
    #[must_use]
    fn debug(self, debug: bool) -> Self;
}

impl<'a, Message: 'static> ElementExt for crate::Element<'a, Message> {
    fn debug(self, debug: bool) -> Self {
        if debug {
            self.explain(Color::WHITE)
        } else {
            self
        }
    }
}

/// Additional methods for the [`Column`] and [`Row`] widgets.
pub trait CollectionWidget<'a, Message: 'a>:
    Widget<Message, crate::Theme, crate::Renderer>
where
    Self: Sized,
{
    /// Moves all the elements of `other` into `self`, leaving `other` empty.
    #[must_use]
    fn append<E>(self, other: &mut Vec<E>) -> Self
    where
        E: Into<crate::Element<'a, Message>>;

    /// Appends all elements in an iterator to the widget.
    #[must_use]
    fn extend<E>(mut self, iterator: impl Iterator<Item = E>) -> Self
    where
        E: Into<crate::Element<'a, Message>>,
    {
        for item in iterator {
            self = self.push(item.into());
        }

        self
    }

    /// Pushes an element into the widget.
    #[must_use]
    fn push(self, element: impl Into<crate::Element<'a, Message>>) -> Self;

    /// Conditionally pushes an element to the widget.
    #[must_use]
    fn push_maybe(self, element: Option<impl Into<crate::Element<'a, Message>>>) -> Self {
        if let Some(element) = element {
            self.push(element.into())
        } else {
            self
        }
    }
}

impl<'a, Message: 'a> CollectionWidget<'a, Message> for crate::widget::Column<'a, Message> {
    fn append<E>(self, other: &mut Vec<E>) -> Self
    where
        E: Into<crate::Element<'a, Message>>,
    {
        self.extend(other.drain(..).map(Into::into))
    }

    fn push(self, element: impl Into<crate::Element<'a, Message>>) -> Self {
        self.push(element)
    }
}

impl<'a, Message: 'a> CollectionWidget<'a, Message> for crate::widget::Row<'a, Message> {
    fn append<E>(self, other: &mut Vec<E>) -> Self
    where
        E: Into<crate::Element<'a, Message>>,
    {
        self.extend(other.drain(..).map(Into::into))
    }

    fn push(self, element: impl Into<crate::Element<'a, Message>>) -> Self {
        self.push(element)
    }
}

pub trait ColorExt {
    /// Combines color with background to create appearance of transparency.
    #[must_use]
    fn blend_alpha(self, background: Self, alpha: f32) -> Self;
}

impl ColorExt for iced::Color {
    fn blend_alpha(self, background: Self, alpha: f32) -> Self {
        Self {
            a: 1.0,
            r: (self.r - background.r).mul_add(alpha, background.r),
            g: (self.g - background.g).mul_add(alpha, background.g),
            b: (self.b - background.b).mul_add(alpha, background.b),
        }
    }
}