2022-12-06 16:12:59 +01:00
|
|
|
// Copyright 2022 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
|
|
use iced::Color;
|
2023-09-01 07:25:00 +02:00
|
|
|
use iced_core::Widget;
|
2022-12-06 16:12:59 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-03 21:35:35 +01:00
|
|
|
}
|
2023-09-01 07:25:00 +02:00
|
|
|
|
|
|
|
|
/// Additional methods for the [`Column`] and [`Row`] widgets.
|
2023-09-13 15:30:43 +02:00
|
|
|
pub trait CollectionWidget<'a, Message: 'a>: Widget<Message, crate::Renderer>
|
|
|
|
|
where
|
|
|
|
|
Self: Sized,
|
|
|
|
|
{
|
2023-09-01 07:25:00 +02:00
|
|
|
/// 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
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 15:30:43 +02:00
|
|
|
/// 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]
|
2023-09-01 07:25:00 +02:00
|
|
|
fn push_maybe(self, element: Option<impl Into<crate::Element<'a, Message>>>) -> Self {
|
|
|
|
|
if let Some(element) = element {
|
|
|
|
|
self.push(element.into())
|
|
|
|
|
} else {
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 15:30:43 +02:00
|
|
|
impl<'a, Message: 'a> CollectionWidget<'a, Message> for crate::widget::Column<'a, Message> {
|
2023-09-01 07:25:00 +02:00
|
|
|
fn append<E>(self, other: &mut Vec<E>) -> Self
|
|
|
|
|
where
|
|
|
|
|
E: Into<crate::Element<'a, Message>>,
|
|
|
|
|
{
|
|
|
|
|
self.extend(other.drain(..))
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-13 15:30:43 +02:00
|
|
|
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
|
2023-09-01 07:25:00 +02:00
|
|
|
where
|
|
|
|
|
E: Into<crate::Element<'a, Message>>,
|
|
|
|
|
{
|
2023-09-13 15:30:43 +02:00
|
|
|
self.extend(other.drain(..))
|
2023-09-01 07:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
2023-09-13 15:30:43 +02:00
|
|
|
fn push(self, element: impl Into<crate::Element<'a, Message>>) -> Self {
|
|
|
|
|
self.push(element)
|
2023-09-01 07:25:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-09-13 16:06:05 +02:00
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
Color {
|
|
|
|
|
a: 1.0,
|
|
|
|
|
r: background.r + (self.r - background.r) * alpha,
|
|
|
|
|
g: background.g + (self.g - background.g) * alpha,
|
|
|
|
|
b: background.b + (self.b - background.b) * alpha,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|