Skip to content

Commit

Permalink
Write docs for responsive in iced_lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jan 12, 2022
1 parent 241e123 commit f6c436a
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lazy/src/responsive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//! Build responsive widgets.
use crate::{Cache, CacheBuilder};

use iced_native::event::{self, Event};
Expand All @@ -13,6 +14,7 @@ use std::cell::RefCell;
use std::hash::{Hash, Hasher as _};
use std::ops::Deref;

/// The state of a [`Responsive`] widget.
#[derive(Debug, Clone, Default)]
pub struct State {
last_size: Option<Size>,
Expand All @@ -25,20 +27,30 @@ impl State {
State::default()
}

pub fn layout(&self, parent: Layout<'_>) -> Layout<'_> {
fn layout(&self, parent: Layout<'_>) -> Layout<'_> {
Layout::with_offset(
parent.position() - Point::ORIGIN,
&self.last_layout,
)
}
}

/// A widget that is aware of its dimensions.
///
/// A [`Responsive`] widget will always try to fill all the available space of
/// its parent.
#[allow(missing_debug_implementations)]
pub struct Responsive<'a, Message, Renderer>(
RefCell<Internal<'a, Message, Renderer>>,
);

impl<'a, Message, Renderer> Responsive<'a, Message, Renderer> {
/// Creates a new [`Responsive`] widget with the given [`State`] and a
/// closure that produces its contents.
///
/// The `view` closure will be provided with the current [`Size`] of
/// the [`Responsive`] widget and, therefore, can be used to build the
/// contents of the widget in a responsive way.
pub fn new(
state: &'a mut State,
view: impl FnOnce(Size) -> Element<'a, Message, Renderer> + 'a,
Expand Down

0 comments on commit f6c436a

Please sign in to comment.