mirror of
https://github.com/danbulant/cushy
synced 2026-07-09 13:10:57 +00:00
IntoDynamic<T>
This commit is contained in:
parent
57a689b8c8
commit
b27b9db380
2 changed files with 29 additions and 3 deletions
24
src/value.rs
24
src/value.rs
|
|
@ -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.
|
/// A value that may be either constant or dynamic.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Value<T> {
|
pub enum Value<T> {
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ use crate::graphics::Graphics;
|
||||||
use crate::styles::components::VisualOrder;
|
use crate::styles::components::VisualOrder;
|
||||||
use crate::tree::Tree;
|
use crate::tree::Tree;
|
||||||
use crate::utils::ModifiersExt;
|
use crate::utils::ModifiersExt;
|
||||||
use crate::value::Dynamic;
|
use crate::value::{Dynamic, IntoDynamic};
|
||||||
use crate::widget::{EventHandling, ManagedWidget, Widget, WidgetInstance, HANDLED, IGNORED};
|
use crate::widget::{EventHandling, ManagedWidget, Widget, WidgetInstance, HANDLED, IGNORED};
|
||||||
use crate::window::sealed::WindowCommand;
|
use crate::window::sealed::WindowCommand;
|
||||||
use crate::{ConstraintLimit, Run};
|
use crate::{ConstraintLimit, Run};
|
||||||
|
|
@ -80,7 +80,8 @@ impl Window<WidgetInstance> {
|
||||||
///
|
///
|
||||||
/// `focused` will be initialized with an initial state
|
/// `focused` will be initialized with an initial state
|
||||||
/// of `false`.
|
/// 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);
|
focused.update(false);
|
||||||
self.focused = Some(focused);
|
self.focused = Some(focused);
|
||||||
self
|
self
|
||||||
|
|
@ -94,7 +95,8 @@ impl Window<WidgetInstance> {
|
||||||
/// visible, this value will contain `true`.
|
/// visible, this value will contain `true`.
|
||||||
///
|
///
|
||||||
/// `occluded` will be initialized with an initial state of `false`.
|
/// `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);
|
occluded.update(false);
|
||||||
self.occluded = Some(occluded);
|
self.occluded = Some(occluded);
|
||||||
self
|
self
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue