Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docs for container Views #91

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@
//!
//! ## More
//!
//! #### Authoring your own custom [Views](view::View)
//! #### Check out all of the built-in [View](view::View)s
//! See the [Views module](views) for more info.
//!
//! #### Authoring your own custom [View](view::View)s
//! See the [View module](view) for more info.
//!
//! #### Understanding Ids
Expand Down
5 changes: 5 additions & 0 deletions src/views/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ use crate::{
view::{ChangeFlags, View},
};

/// A simple wrapper around another View. See [`container`]
pub struct Container<V: View> {
id: Id,
child: V,
}

/// A simple wrapper around another View
///
/// A [`Container`] is useful for wrapping another [View](crate::view::View). This is often useful for allowing another
/// set of styles completely separate from the View that is being wrapped.
pub fn container<V: View>(child: impl FnOnce() -> V) -> Container<V> {
let (id, child) = ViewContext::new_id_with_child(child);
Container { id, child }
Expand Down
37 changes: 37 additions & 0 deletions src/views/container_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,48 @@ use crate::{
view::{ChangeFlags, View},
};

/// A wrapper around any type that implements View. See [`container_box`]
pub struct ContainerBox {
id: Id,
child: Box<dyn View>,
}

/// A wrapper around any type that implements View.
///
/// Views in Floem are strongly typed. A [`ContainerBox`] allows you to escape the strongly typed View and contain a `Box<dyn View>`.
///
/// ## Bad Example
///```compile_fail
/// use floem::views::*;
/// use floem_reactive::*;
/// let check = true;
///
/// container(|| {
/// if check == true {
/// checkbox(create_rw_signal(true).read_only())
/// } else {
/// label(|| "no check".to_string())
/// }
/// });
/// ```
/// The above example will fail to compile because the container is strongly typed so the if and
/// the else must return the same type. The problem is that checkbox is an [Svg](crate::views::Svg)
/// and the else returns a [Label](crate::views::Label). The solution to this is to use a
/// [`ContainerBox`] to escape the strongly typed requirement.
///
/// ```
/// use floem::views::*;
/// use floem_reactive::*;
/// let check = true;
///
/// container_box(|| {
/// if check == true {
/// Box::new(checkbox(create_rw_signal(true).read_only()))
/// } else {
/// Box::new(label(|| "no check".to_string()))
/// }
/// });
/// ```
pub fn container_box(child: impl FnOnce() -> Box<dyn View>) -> ContainerBox {
let (id, child) = ViewContext::new_id_with_child(child);
ContainerBox { id, child }
Expand Down
3 changes: 2 additions & 1 deletion src/views/dyn_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub struct DynamicContainer<T: 'static> {

/// A container for a dynamically updating View
///
///
/// ## Example
/// ```ignore
/// #[derive(Debug, Clone)]
Expand Down Expand Up @@ -70,6 +69,8 @@ pub struct DynamicContainer<T: 'static> {
/// })
/// }
/// ```
///
/// See [container_box](crate::views::container_box()) for more documentation on a general container
pub fn dyn_container<CF: Fn(T) -> Box<dyn View> + 'static, T: 'static>(
update_view: impl Fn() -> T + 'static,
child_fn: CF,
Expand Down
2 changes: 1 addition & 1 deletion src/views/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! # Floem builtin Views
//!
//! This module contains all of the builting Views or Components of Floem.
//! This module contains all of the built-in Views of Floem.
//!

mod label;
Expand Down
Loading