cosmic/widget/
warning.rs

1// Copyright 2022 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4use super::icon;
5use crate::{Element, Renderer, Theme, theme, widget};
6use apply::Apply;
7use iced::{Alignment, Background, Color, Length};
8use iced_core::{Border, Shadow};
9use std::borrow::Cow;
10
11#[must_use]
12pub fn warning<'a, Message>(message: impl Into<Cow<'a, str>>) -> Warning<'a, Message> {
13    Warning {
14        message: message.into(),
15        on_close: None,
16    }
17}
18
19pub struct Warning<'a, Message> {
20    message: Cow<'a, str>,
21    on_close: Option<Message>,
22}
23
24impl<'a, Message: 'static + Clone> Warning<'a, Message> {
25    /// The message to emit on button press.
26    #[must_use]
27    pub fn on_close(mut self, message: Message) -> Self {
28        self.on_close = Some(message);
29        self
30    }
31
32    /// A custom button that has the desired default spacing and padding.
33    pub fn into_widget(self) -> widget::Container<'a, Message, crate::Theme, Renderer> {
34        let label = widget::container(crate::widget::text(self.message)).width(Length::Fill);
35
36        let close_button = icon::from_name("window-close-symbolic")
37            .size(16)
38            .apply(widget::button::icon)
39            .on_press_maybe(self.on_close);
40
41        widget::row::with_capacity(2)
42            .push(label)
43            .push(close_button)
44            .align_y(Alignment::Center)
45            .apply(widget::container)
46            .class(theme::Container::custom(warning_container))
47            .padding(10)
48            .align_y(Alignment::Center)
49            .width(Length::Fill)
50    }
51}
52
53impl<'a, Message: 'static + Clone> From<Warning<'a, Message>> for Element<'a, Message> {
54    fn from(warning: Warning<'a, Message>) -> Self {
55        Self::from(warning.into_widget())
56    }
57}
58
59#[must_use]
60pub fn warning_container(theme: &Theme) -> widget::container::Style {
61    let cosmic = theme.cosmic();
62    widget::container::Style {
63        icon_color: Some(theme.cosmic().warning.on.into()),
64        text_color: Some(theme.cosmic().warning.on.into()),
65        background: Some(Background::Color(theme.cosmic().warning_color().into())),
66        border: Border {
67            color: Color::TRANSPARENT,
68            width: 1.0,
69            radius: cosmic.corner_radii.radius_0.into(),
70        },
71        shadow: Shadow {
72            color: Color::TRANSPARENT,
73            offset: iced::Vector::new(0.0, 0.0),
74            blur_radius: 0.0,
75        },
76        snap: true,
77    }
78}