Skip to content

Commit

Permalink
document decorators (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmoulton authored Nov 1, 2024
1 parent aaa3c9d commit 6f0bc39
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/views/clip.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![deny(missing_docs)]

use peniko::kurbo::Size;

use crate::{
Expand Down Expand Up @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions src/views/container.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![deny(missing_docs)]

use crate::{
id::ViewId,
view::{IntoView, View},
Expand Down Expand Up @@ -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;
}

Expand Down
85 changes: 85 additions & 0 deletions src/views/decorator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![deny(missing_docs)]

//! # Decorator
//!
//! The decorator trait is the primary interface for extending the appearance and functionality of ['View']s.
Expand All @@ -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<V = Self::DV> + 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.
Expand Down Expand Up @@ -55,6 +60,9 @@ pub trait Decorators: IntoView<V = Self::DV> + 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<String>) -> Self::DV {
let view = self.into_view();
let view_id = view.id();
Expand All @@ -63,6 +71,10 @@ pub trait Decorators: IntoView<V = Self::DV> + 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<S: Into<String>>(
self,
apply: impl Fn() -> bool + 'static,
Expand Down Expand Up @@ -97,12 +109,14 @@ pub trait Decorators: IntoView<V = Self::DV> + Sized {
view
}

/// Add a style class to the view
fn class<C: StyleClass>(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<C: StyleClass>(self, apply: impl Fn() -> bool + 'static, _class: C) -> Self::DV {
let view = self.into_view();
let id = view.id();
Expand All @@ -117,6 +131,7 @@ pub trait Decorators: IntoView<V = Self::DV> + Sized {
view
}

/// Remove a style class from the view
fn remove_class<C: StyleClass>(self, _class: C) -> Self::DV {
let view = self.into_view();
view.id().remove_class(C::class_ref());
Expand Down Expand Up @@ -153,12 +168,17 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand Down Expand Up @@ -309,6 +329,12 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -317,6 +343,12 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -325,6 +357,14 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -333,6 +373,14 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -354,6 +402,10 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -364,6 +416,10 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -374,6 +430,12 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -382,6 +444,12 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -390,6 +458,17 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -399,6 +478,9 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand All @@ -407,6 +489,9 @@ pub trait Decorators: IntoView<V = Self::DV> + 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();
Expand Down

0 comments on commit 6f0bc39

Please sign in to comment.