Skip to content

Commit

Permalink
IntoDynamic<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Nov 8, 2023
1 parent 57a689b commit b27b9db
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
24 changes: 24 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,30 @@ impl Generation {
}
}

/// A type that can convert into a `Dynamic<T>`.
pub trait IntoDynamic<T> {
/// Returns `self` as a dynamic.
fn into_dynamic(self) -> Dynamic<T>;
}

impl<T> IntoDynamic<T> for Dynamic<T> {
fn into_dynamic(self) -> Dynamic<T> {
self
}
}

impl<T, F> IntoDynamic<T> for F
where
F: FnMut(&T) + Send + 'static,
T: Default,
{
/// Returns [`Dynamic::default()`] with `self` installed as a for-each
/// callback.
fn into_dynamic(self) -> Dynamic<T> {
Dynamic::default().with_for_each(self)
}
}

/// A value that may be either constant or dynamic.
#[derive(Debug)]
pub enum Value<T> {
Expand Down
8 changes: 5 additions & 3 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::graphics::Graphics;
use crate::styles::components::VisualOrder;
use crate::tree::Tree;
use crate::utils::ModifiersExt;
use crate::value::Dynamic;
use crate::value::{Dynamic, IntoDynamic};
use crate::widget::{EventHandling, ManagedWidget, Widget, WidgetInstance, HANDLED, IGNORED};
use crate::window::sealed::WindowCommand;
use crate::{ConstraintLimit, Run};
Expand Down Expand Up @@ -80,7 +80,8 @@ impl Window<WidgetInstance> {
///
/// `focused` will be initialized with an initial state
/// of `false`.
pub fn with_focused(mut self, focused: Dynamic<bool>) -> Self {
pub fn with_focused(mut self, focused: impl IntoDynamic<bool>) -> Self {
let focused = focused.into_dynamic();
focused.update(false);
self.focused = Some(focused);
self
Expand All @@ -94,7 +95,8 @@ impl Window<WidgetInstance> {
/// visible, this value will contain `true`.
///
/// `occluded` will be initialized with an initial state of `false`.
pub fn with_occluded(mut self, occluded: Dynamic<bool>) -> Self {
pub fn with_occluded(mut self, occluded: impl IntoDynamic<bool>) -> Self {
let occluded = occluded.into_dynamic();
occluded.update(false);
self.occluded = Some(occluded);
self
Expand Down

0 comments on commit b27b9db

Please sign in to comment.