iced_widget/lazy/
helpers.rs

1use crate::core::{self, Element, Size};
2use crate::lazy::component;
3
4use std::hash::Hash;
5
6#[allow(deprecated)]
7pub use crate::lazy::{Component, Lazy, Responsive};
8
9/// Creates a new [`Lazy`] widget with the given data `Dependency` and a
10/// closure that can turn this data into a widget tree.
11#[cfg(feature = "lazy")]
12pub fn lazy<'a, Message, Theme, Renderer, Dependency, View>(
13    dependency: Dependency,
14    view: impl Fn(&Dependency) -> View + 'a,
15) -> Lazy<'a, Message, Theme, Renderer, Dependency, View>
16where
17    Dependency: Hash + 'a,
18    View: Into<Element<'static, Message, Theme, Renderer>>,
19{
20    Lazy::new(dependency, view)
21}
22
23/// Turns an implementor of [`Component`] into an [`Element`] that can be
24/// embedded in any application.
25#[cfg(feature = "lazy")]
26#[deprecated(
27    since = "0.13.0",
28    note = "components introduce encapsulated state and hamper the use of a single source of truth. \
29    Instead, leverage the Elm Architecture directly, or implement a custom widget"
30)]
31#[allow(deprecated)]
32pub fn component<'a, C, Message, Theme, Renderer>(
33    component: C,
34) -> Element<'a, Message, Theme, Renderer>
35where
36    C: Component<Message, Theme, Renderer> + 'a,
37    C::State: 'static,
38    Message: 'a,
39    Theme: 'a,
40    Renderer: core::Renderer + 'a,
41{
42    component::view(component)
43}
44
45/// Creates a new [`Responsive`] widget with a closure that produces its
46/// contents.
47///
48/// The `view` closure will be provided with the current [`Size`] of
49/// the [`Responsive`] widget and, therefore, can be used to build the
50/// contents of the widget in a responsive way.
51#[cfg(feature = "lazy")]
52pub fn responsive<'a, Message, Theme, Renderer>(
53    f: impl Fn(Size) -> Element<'a, Message, Theme, Renderer> + 'a,
54) -> Responsive<'a, Message, Theme, Renderer>
55where
56    Renderer: core::Renderer,
57{
58    Responsive::new(f)
59}