Implement From<Option<T>> for Element
This commit is contained in:
parent
dcea10f707
commit
e0d9078334
9 changed files with 110 additions and 74 deletions
|
|
@ -532,3 +532,49 @@ where
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, Message, Theme, Renderer> From<Option<T>>
|
||||
for Element<'a, Message, Theme, Renderer>
|
||||
where
|
||||
T: Into<Self>,
|
||||
Renderer: crate::Renderer,
|
||||
{
|
||||
fn from(element: Option<T>) -> Self {
|
||||
struct Void;
|
||||
|
||||
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Void
|
||||
where
|
||||
Renderer: crate::Renderer,
|
||||
{
|
||||
fn size(&self) -> Size<Length> {
|
||||
Size {
|
||||
width: Length::Fixed(0.0),
|
||||
height: Length::Fixed(0.0),
|
||||
}
|
||||
}
|
||||
|
||||
fn layout(
|
||||
&self,
|
||||
_tree: &mut Tree,
|
||||
_renderer: &Renderer,
|
||||
_limits: &layout::Limits,
|
||||
) -> layout::Node {
|
||||
layout::Node::new(Size::ZERO)
|
||||
}
|
||||
|
||||
fn draw(
|
||||
&self,
|
||||
_tree: &Tree,
|
||||
_renderer: &mut Renderer,
|
||||
_theme: &Theme,
|
||||
_style: &renderer::Style,
|
||||
_layout: Layout<'_>,
|
||||
_cursor: mouse::Cursor,
|
||||
_viewport: &Rectangle,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
element.map(T::into).unwrap_or_else(|| Element::new(Void))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,6 +57,7 @@ impl Length {
|
|||
|
||||
/// Adapts the [`Length`] so it can contain the other [`Length`] and
|
||||
/// match its fluidity.
|
||||
#[inline]
|
||||
pub fn enclose(self, other: Length) -> Self {
|
||||
match (self, other) {
|
||||
(Length::Shrink, Length::Fill | Length::FillPortion(_)) => other,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{Radians, Vector};
|
||||
use crate::{Length, Radians, Vector};
|
||||
|
||||
/// An amount of space in 2 dimensions.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||
|
|
@ -66,6 +66,15 @@ impl Size {
|
|||
}
|
||||
}
|
||||
|
||||
impl Size<Length> {
|
||||
/// Returns true if either `width` or `height` are 0-sized.
|
||||
#[inline]
|
||||
pub fn is_void(&self) -> bool {
|
||||
matches!(self.width, Length::Fixed(0.0))
|
||||
|| matches!(self.height, Length::Fixed(0.0))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> From<[T; 2]> for Size<T> {
|
||||
fn from([width, height]: [T; 2]) -> Self {
|
||||
Size { width, height }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue