Implement width and height for responsive
This commit is contained in:
parent
7a7d562b03
commit
5050fcc7ac
2 changed files with 36 additions and 15 deletions
|
|
@ -2146,9 +2146,9 @@ where
|
||||||
/// Creates a new [`Responsive`] widget with a closure that produces its
|
/// Creates a new [`Responsive`] widget with a closure that produces its
|
||||||
/// contents.
|
/// contents.
|
||||||
///
|
///
|
||||||
/// The `view` closure will be provided with the current [`Size`] of
|
/// The `view` closure will receive the maximum available space for
|
||||||
/// the [`Responsive`] widget and, therefore, can be used to build the
|
/// the [`Responsive`] during layout. You can use this [`Size`] to
|
||||||
/// contents of the widget in a responsive way.
|
/// conditionally build the contents.
|
||||||
pub fn responsive<'a, Message, Theme, Renderer>(
|
pub fn responsive<'a, Message, Theme, Renderer>(
|
||||||
f: impl Fn(Size) -> Element<'a, Message, Theme, Renderer> + 'a,
|
f: impl Fn(Size) -> Element<'a, Message, Theme, Renderer> + 'a,
|
||||||
) -> Responsive<'a, Message, Theme, Renderer>
|
) -> Responsive<'a, Message, Theme, Renderer>
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@ pub struct Responsive<
|
||||||
Renderer = crate::Renderer,
|
Renderer = crate::Renderer,
|
||||||
> {
|
> {
|
||||||
view: Box<dyn Fn(Size) -> Element<'a, Message, Theme, Renderer> + 'a>,
|
view: Box<dyn Fn(Size) -> Element<'a, Message, Theme, Renderer> + 'a>,
|
||||||
|
width: Length,
|
||||||
|
height: Length,
|
||||||
content: Element<'a, Message, Theme, Renderer>,
|
content: Element<'a, Message, Theme, Renderer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,17 +34,31 @@ where
|
||||||
/// Creates a new [`Responsive`] widget with a closure that produces its
|
/// Creates a new [`Responsive`] widget with a closure that produces its
|
||||||
/// contents.
|
/// contents.
|
||||||
///
|
///
|
||||||
/// The `view` closure will be provided with the current [`Size`] of
|
/// The `view` closure will receive the maximum available space for
|
||||||
/// the [`Responsive`] widget and, therefore, can be used to build the
|
/// the [`Responsive`] during layout. You can use this [`Size`] to
|
||||||
/// contents of the widget in a responsive way.
|
/// conditionally build the contents.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
view: impl Fn(Size) -> Element<'a, Message, Theme, Renderer> + 'a,
|
view: impl Fn(Size) -> Element<'a, Message, Theme, Renderer> + 'a,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
Self {
|
Self {
|
||||||
view: Box::new(view),
|
view: Box::new(view),
|
||||||
|
width: Length::Fill,
|
||||||
|
height: Length::Fill,
|
||||||
content: Element::new(horizontal_space().width(0)),
|
content: Element::new(horizontal_space().width(0)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the width of the [`Responsive`].
|
||||||
|
pub fn width(mut self, width: impl Into<Length>) -> Self {
|
||||||
|
self.width = width.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the height of the [`Responsive`].
|
||||||
|
pub fn height(mut self, height: impl Into<Length>) -> Self {
|
||||||
|
self.height = height.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||||
|
|
@ -61,8 +77,8 @@ where
|
||||||
|
|
||||||
fn size(&self) -> Size<Length> {
|
fn size(&self) -> Size<Length> {
|
||||||
Size {
|
Size {
|
||||||
width: Length::Fill,
|
width: self.width,
|
||||||
height: Length::Fill,
|
height: self.height,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,16 +88,21 @@ where
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
limits: &layout::Limits,
|
limits: &layout::Limits,
|
||||||
) -> layout::Node {
|
) -> layout::Node {
|
||||||
|
let limits = limits.width(self.width).height(self.height);
|
||||||
let size = limits.max();
|
let size = limits.max();
|
||||||
|
|
||||||
self.content = (self.view)(size);
|
self.content = (self.view)(size);
|
||||||
tree.diff_children(std::slice::from_mut(&mut self.content));
|
tree.diff_children(std::slice::from_mut(&mut self.content));
|
||||||
|
|
||||||
self.content.as_widget_mut().layout(
|
let node = self.content.as_widget_mut().layout(
|
||||||
&mut tree.children[0],
|
&mut tree.children[0],
|
||||||
renderer,
|
renderer,
|
||||||
&limits.loose(),
|
&limits.loose(),
|
||||||
)
|
);
|
||||||
|
|
||||||
|
let size = limits.resolve(self.width, self.height, node.size());
|
||||||
|
|
||||||
|
layout::Node::with_children(size, vec![node])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(
|
fn update(
|
||||||
|
|
@ -98,7 +119,7 @@ where
|
||||||
self.content.as_widget_mut().update(
|
self.content.as_widget_mut().update(
|
||||||
&mut tree.children[0],
|
&mut tree.children[0],
|
||||||
event,
|
event,
|
||||||
layout,
|
layout.children().next().unwrap(),
|
||||||
cursor,
|
cursor,
|
||||||
renderer,
|
renderer,
|
||||||
clipboard,
|
clipboard,
|
||||||
|
|
@ -122,7 +143,7 @@ where
|
||||||
renderer,
|
renderer,
|
||||||
theme,
|
theme,
|
||||||
style,
|
style,
|
||||||
layout,
|
layout.children().next().unwrap(),
|
||||||
cursor,
|
cursor,
|
||||||
viewport,
|
viewport,
|
||||||
);
|
);
|
||||||
|
|
@ -138,7 +159,7 @@ where
|
||||||
) -> mouse::Interaction {
|
) -> mouse::Interaction {
|
||||||
self.content.as_widget().mouse_interaction(
|
self.content.as_widget().mouse_interaction(
|
||||||
&tree.children[0],
|
&tree.children[0],
|
||||||
layout,
|
layout.children().next().unwrap(),
|
||||||
cursor,
|
cursor,
|
||||||
viewport,
|
viewport,
|
||||||
renderer,
|
renderer,
|
||||||
|
|
@ -154,7 +175,7 @@ where
|
||||||
) {
|
) {
|
||||||
self.content.as_widget_mut().operate(
|
self.content.as_widget_mut().operate(
|
||||||
&mut tree.children[0],
|
&mut tree.children[0],
|
||||||
layout,
|
layout.children().next().unwrap(),
|
||||||
renderer,
|
renderer,
|
||||||
operation,
|
operation,
|
||||||
);
|
);
|
||||||
|
|
@ -170,7 +191,7 @@ where
|
||||||
) -> Option<overlay::Element<'a, Message, Theme, Renderer>> {
|
) -> Option<overlay::Element<'a, Message, Theme, Renderer>> {
|
||||||
self.content.as_widget_mut().overlay(
|
self.content.as_widget_mut().overlay(
|
||||||
&mut tree.children[0],
|
&mut tree.children[0],
|
||||||
layout,
|
layout.children().next().unwrap(),
|
||||||
renderer,
|
renderer,
|
||||||
viewport,
|
viewport,
|
||||||
translation,
|
translation,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue