2023-01-16 12:04:39 -07:00
|
|
|
// Copyright 2022 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
use super::icon;
|
2023-09-01 07:29:19 +02:00
|
|
|
use crate::{theme, widget, Element, Renderer, Theme};
|
|
|
|
|
use apply::Apply;
|
2024-11-03 19:16:37 +01:00
|
|
|
use iced::{Alignment, Background, Color, Length};
|
2024-01-30 22:14:00 -05:00
|
|
|
use iced_core::{Border, Shadow};
|
2023-09-01 07:29:19 +02:00
|
|
|
use std::borrow::Cow;
|
2023-01-16 12:04:39 -07:00
|
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn warning<'a, Message>(message: impl Into<Cow<'a, str>>) -> Warning<'a, Message> {
|
|
|
|
|
Warning {
|
|
|
|
|
message: message.into(),
|
|
|
|
|
on_close: None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Warning<'a, Message> {
|
|
|
|
|
message: Cow<'a, str>,
|
|
|
|
|
on_close: Option<Message>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message: 'static + Clone> Warning<'a, Message> {
|
|
|
|
|
/// The message to emit on button press.
|
|
|
|
|
#[must_use]
|
|
|
|
|
pub fn on_close(mut self, message: Message) -> Self {
|
|
|
|
|
self.on_close = Some(message);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A custom button that has the desired default spacing and padding.
|
2024-01-30 22:14:00 -05:00
|
|
|
pub fn into_widget(self) -> widget::Container<'a, Message, crate::Theme, Renderer> {
|
2023-09-01 07:29:19 +02:00
|
|
|
let label = widget::container(crate::widget::text(self.message)).width(Length::Fill);
|
2023-01-16 12:04:39 -07:00
|
|
|
|
2023-09-13 15:47:32 +02:00
|
|
|
let close_button = icon::from_name("window-close-symbolic")
|
2023-09-01 07:29:19 +02:00
|
|
|
.size(16)
|
|
|
|
|
.apply(widget::button::icon)
|
|
|
|
|
.on_press_maybe(self.on_close);
|
2023-01-16 12:04:39 -07:00
|
|
|
|
2023-09-01 07:29:19 +02:00
|
|
|
widget::row::with_capacity(2)
|
|
|
|
|
.push(label)
|
|
|
|
|
.push(close_button)
|
2024-10-16 20:36:46 -04:00
|
|
|
.align_y(Alignment::Center)
|
2023-09-01 07:29:19 +02:00
|
|
|
.apply(widget::container)
|
2024-10-16 20:36:46 -04:00
|
|
|
.class(theme::Container::custom(warning_container))
|
2023-09-01 07:29:19 +02:00
|
|
|
.padding(10)
|
2024-11-03 19:16:37 +01:00
|
|
|
.align_y(Alignment::Center)
|
2023-09-01 07:29:19 +02:00
|
|
|
.width(Length::Fill)
|
2023-01-16 12:04:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, Message: 'static + Clone> From<Warning<'a, Message>> for Element<'a, Message> {
|
|
|
|
|
fn from(warning: Warning<'a, Message>) -> Self {
|
|
|
|
|
Self::from(warning.into_widget())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 12:03:15 -04:00
|
|
|
#[must_use]
|
2024-10-16 20:36:46 -04:00
|
|
|
pub fn warning_container(theme: &Theme) -> widget::container::Style {
|
2023-12-13 12:59:26 -05:00
|
|
|
let cosmic = theme.cosmic();
|
2024-10-16 20:36:46 -04:00
|
|
|
widget::container::Style {
|
2023-09-01 07:29:19 +02:00
|
|
|
icon_color: Some(theme.cosmic().warning.on.into()),
|
2023-02-21 15:23:49 -05:00
|
|
|
text_color: Some(theme.cosmic().warning.on.into()),
|
2023-01-16 12:04:39 -07:00
|
|
|
background: Some(Background::Color(theme.cosmic().warning_color().into())),
|
2024-01-30 22:14:00 -05:00
|
|
|
border: Border {
|
|
|
|
|
color: Color::TRANSPARENT,
|
|
|
|
|
width: 1.0,
|
|
|
|
|
radius: cosmic.corner_radii.radius_0.into(),
|
|
|
|
|
},
|
|
|
|
|
shadow: Shadow {
|
|
|
|
|
color: Color::TRANSPARENT,
|
|
|
|
|
offset: iced::Vector::new(0.0, 0.0),
|
|
|
|
|
blur_radius: 0.0,
|
|
|
|
|
},
|
2023-01-16 12:04:39 -07:00
|
|
|
}
|
|
|
|
|
}
|