// 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>: Widget { /// 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(self, iterator: impl Iterator) -> Self where E: Into>; /// Conditionally pushes an element to the widget. #[must_use] fn push_maybe(self, element: Option>>) -> Self; } impl<'a, Message> CollectionWidget<'a, Message> for crate::widget::Column<'a, Message, crate::Renderer> { fn append(self, other: &mut Vec) -> Self where E: Into>, { self.extend(other.drain(..)) } fn extend(mut self, iterator: impl Iterator) -> Self where E: Into>, { for item in iterator { self = self.push(item.into()); } self } fn push_maybe(self, element: Option>>) -> Self { if let Some(element) = element { self.push(element.into()) } else { self } } } impl<'a, Message> CollectionWidget<'a, Message> for crate::widget::Row<'a, Message, crate::Renderer> { fn append(self, other: &mut Vec) -> Self where E: Into>, { self.extend(other.drain(..)) } fn extend(mut self, iterator: impl Iterator) -> Self where E: Into>, { for item in iterator { self = self.push(item.into()); } self } fn push_maybe(self, element: Option>>) -> Self { if let Some(element) = element { self.push(element.into()) } else { self } } }