From 0a75789845d6a3b15665f00cc023a11d7212c69c Mon Sep 17 00:00:00 2001 From: jrmoulton Date: Fri, 1 Nov 2024 12:24:30 -0600 Subject: [PATCH] document decorators --- src/views/clip.rs | 4 ++ src/views/container.rs | 5 +++ src/views/decorator.rs | 85 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/src/views/clip.rs b/src/views/clip.rs index 4b62d821..7215d992 100644 --- a/src/views/clip.rs +++ b/src/views/clip.rs @@ -1,3 +1,5 @@ +#![deny(missing_docs)] + use peniko::kurbo::Size; use crate::{ @@ -54,7 +56,9 @@ impl View for Clip { } } +/// A trait that adds a `clip` method to any type that implements `IntoView`. pub trait ClipExt { + /// Wrap the view in a clip view. fn clip(self) -> Clip; } diff --git a/src/views/container.rs b/src/views/container.rs index a63e9606..328c9185 100644 --- a/src/views/container.rs +++ b/src/views/container.rs @@ -1,3 +1,5 @@ +#![deny(missing_docs)] + use crate::{ id::ViewId, view::{IntoView, View}, @@ -28,7 +30,10 @@ impl View for Container { "Container".into() } } + +/// A trait that adds a `container` method to any type that implements `IntoView`. pub trait ContainerExt { + /// Wrap the view in a container. fn container(self) -> Container; } diff --git a/src/views/decorator.rs b/src/views/decorator.rs index 33e3439e..3107987b 100644 --- a/src/views/decorator.rs +++ b/src/views/decorator.rs @@ -1,3 +1,5 @@ +#![deny(missing_docs)] + //! # Decorator //! //! The decorator trait is the primary interface for extending the appearance and functionality of ['View']s. @@ -18,6 +20,9 @@ use crate::{ /// A trait that extends the appearance and functionality of Views through styling and event handling. pub trait Decorators: IntoView + Sized { + /// The type of the decorated view. + /// + /// Using this type allows for chaining of decorators as well as maintaining the original type of the view which allows you to call methods that were a part of the original view even after calling a decorators method. type DV: View; /// Alter the style of the view. @@ -55,6 +60,9 @@ pub trait Decorators: IntoView + Sized { view } + /// Add a debug name to the view that will be shown in the inspector. + /// + /// This can be called multiple times and each name will be shown in the inspector with the most recent name showing first. fn debug_name(self, name: impl Into) -> Self::DV { let view = self.into_view(); let view_id = view.id(); @@ -63,6 +71,10 @@ pub trait Decorators: IntoView + Sized { view } + /// Conditionally add a debug name to the view that will be shown in the inspector. + /// + /// # Reactivity + /// Both the `apply` and `name` functions are reactive. fn debug_name_if>( self, apply: impl Fn() -> bool + 'static, @@ -97,12 +109,14 @@ pub trait Decorators: IntoView + Sized { view } + /// Add a style class to the view fn class(self, _class: C) -> Self::DV { let view = self.into_view(); view.id().add_class(C::class_ref()); view } + /// Conditionally add a style class to the view fn class_if(self, apply: impl Fn() -> bool + 'static, _class: C) -> Self::DV { let view = self.into_view(); let id = view.id(); @@ -117,6 +131,7 @@ pub trait Decorators: IntoView + Sized { view } + /// Remove a style class from the view fn remove_class(self, _class: C) -> Self::DV { let view = self.into_view(); view.id().remove_class(C::class_ref()); @@ -153,12 +168,17 @@ pub trait Decorators: IntoView + Sized { view } + /// Mark the view as draggable fn draggable(self) -> Self::DV { let view = self.into_view(); view.id().draggable(); view } + /// Mark the view as disabled + /// + /// # Reactivity + /// The `disabled_fn` is reactive. fn disabled(self, disabled_fn: impl Fn() -> bool + 'static) -> Self::DV { let view = self.into_view(); let id = view.id(); @@ -309,6 +329,12 @@ pub trait Decorators: IntoView + Sized { }) } + /// Set the event handler for resize events for this view. + /// + /// There can only be one resize event handler for a view. + /// + /// # Reactivity + /// The action will be called whenever the view is resized but will not rerun automatically in response to signal changes fn on_resize(self, action: impl Fn(Rect) + 'static) -> Self::DV { let view = self.into_view(); let id = view.id(); @@ -317,6 +343,12 @@ pub trait Decorators: IntoView + Sized { view } + /// Set the event handler for move events for this view. + /// + /// There can only be one move event handler for a view. + /// + /// # Reactivity + /// The action will be called whenever the view is moved but will not rerun automatically in response to signal changes fn on_move(self, action: impl Fn(Point) + 'static) -> Self::DV { let view = self.into_view(); let id = view.id(); @@ -325,6 +357,14 @@ pub trait Decorators: IntoView + Sized { view } + /// Set the event handler for cleanup events for this view. + /// + /// The cleanup event is called when the view is removed from the view tree. + /// + /// There can only be one cleanup event handler for a view. + /// + /// # Reactivity + /// The action will be called when the view is removed from the view tree but will not rerun automatically in response to signal changes fn on_cleanup(self, action: impl Fn() + 'static) -> Self::DV { let view = self.into_view(); let id = view.id(); @@ -333,6 +373,14 @@ pub trait Decorators: IntoView + Sized { view } + /// Add an animation to the view. + /// + /// You can add more than one animation to a view and all of them can be active at the same time. + /// + /// See the [Animation] struct for more information on how to create animations. + /// + /// # Reactivity + /// The animation function will be updated in response to signal changes in the function. The behavior is the same as the [Decorators::style] method. fn animation(self, animation: impl Fn(Animation) -> Animation + 'static) -> Self::DV { let view = self.into_view(); let view_id = view.id(); @@ -354,6 +402,10 @@ pub trait Decorators: IntoView + Sized { view } + /// Clear the focus from the window. + /// + /// # Reactivity + /// The when function is reactive and will rereun in response to any signal changes in the function. fn clear_focus(self, when: impl Fn() + 'static) -> Self::DV { let view = self.into_view(); let id = view.id(); @@ -364,6 +416,10 @@ pub trait Decorators: IntoView + Sized { view } + /// Request that this view gets the focus for the window. + /// + /// # Reactivity + /// The when function is reactive and will rereun in response to any signal changes in the function. fn request_focus(self, when: impl Fn() + 'static) -> Self::DV { let view = self.into_view(); let id = view.id(); @@ -374,6 +430,12 @@ pub trait Decorators: IntoView + Sized { view } + /// Set the window scale factor. + /// + /// This internally calls the [crate::action::set_window_scale] function. + /// + /// # Reactivity + /// The scale function is reactive and will rereun in response to any signal changes in the function. fn window_scale(self, scale_fn: impl Fn() -> f64 + 'static) -> Self { create_effect(move |_| { let window_scale = scale_fn(); @@ -382,6 +444,12 @@ pub trait Decorators: IntoView + Sized { self } + /// Set the window title. + /// + /// This internally calls the [crate::action::set_window_title] function. + /// + /// # Reactivity + /// The title function is reactive and will rereun in response to any signal changes in the function. fn window_title(self, title_fn: impl Fn() -> String + 'static) -> Self { create_effect(move |_| { let window_title = title_fn(); @@ -390,6 +458,17 @@ pub trait Decorators: IntoView + Sized { self } + /// Set the system window menu + /// + /// This internally calls the [crate::action::set_window_menu] function. + /// + /// Platform support: + /// - Windows: No + /// - macOS: Yes (not currently implemented) + /// - Linux: No + /// + /// # Reactivity + /// The menu function is reactive and will rereun in response to any signal changes in the function. fn window_menu(self, menu_fn: impl Fn() -> Menu + 'static) -> Self { create_effect(move |_| { let menu = menu_fn(); @@ -399,6 +478,9 @@ pub trait Decorators: IntoView + Sized { } /// Adds a secondary-click context menu to the view, which opens at the mouse position. + /// + /// # Reactivity + /// The menu function is not reactive and will not rerun automatically in response to signal changes while the menu is showing and will only update the menu items each time that it is created fn context_menu(self, menu: impl Fn() -> Menu + 'static) -> Self::DV { let view = self.into_view(); let id = view.id(); @@ -407,6 +489,9 @@ pub trait Decorators: IntoView + Sized { } /// Adds a primary-click context menu, which opens below the view. + /// + /// # Reactivity + /// The menu function is not reactive and will not rerun automatically in response to signal changes while the menu is showing and will only update the menu items each time that it is created fn popout_menu(self, menu: impl Fn() -> Menu + 'static) -> Self::DV { let view = self.into_view(); let id = view.id();