// Copyright 2022 System76 // 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 where Self: Sized, { /// Moves all the elements of `other` into `self`, leaving `other` empty. #[must_use] fn append(self, other: &mut Vec) -> Self where E: Into>; /// Appends all elements in an iterator to the widget. #[must_use] fn extend(mut self, iterator: impl Iterator) -> Self where E: Into>, { for item in iterator { self = self.push(item.into()); } self } /// Pushes an element into the widget. #[must_use] fn push(self, element: impl Into>) -> Self; /// Conditionally pushes an element to the widget. #[must_use] fn push_maybe(self, element: Option>>) -> 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(self, other: &mut Vec) -> Self where E: Into>, { self.extend(other.drain(..)) } fn push(self, element: impl Into>) -> Self { self.push(element) } } impl<'a, Message: 'a> CollectionWidget<'a, Message> for crate::widget::Row<'a, Message> { fn append(self, other: &mut Vec) -> Self where E: Into>, { self.extend(other.drain(..)) } fn push(self, element: impl Into>) -> 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 { 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, } } }