pub struct DirectTransition<T: StylePropValue> {
pub current_value: T,
/* private fields */
}Expand description
Direct transition controller using TransitionState without the Style system.
This allows you to animate any value that implements StylePropValue by managing
the transition state directly. You control when transitions start, how they’re
configured, and when to step them forward.
§Example
use std::time::{Duration, Instant};
use floem::style::{DirectTransition, Transition};
// Create a transition for animating opacity
let mut opacity = DirectTransition::new(1., None);
// Configure transition timing and easing
opacity.set_transition(Some(
Transition::ease_in_out(Duration::from_millis(300))
));
// Start transition to new value
opacity.transition_to(0.0);
// Animation loop - call this every frame
let start_time = Instant::now();
loop {
let now = Instant::now();
// Step the transition forward
let changed = opacity.step(&now);
// Get current interpolated value
let current_opacity = opacity.get();
// Only update rendering if value changed
if changed {
println!("Current opacity: {:.3}", current_opacity);
// render_with_opacity(current_opacity);
}
// Exit when transition completes
if !opacity.is_active() {
println!("Transition complete!");
break;
}
// Wait for next frame (~60fps)
std::thread::sleep(Duration::from_millis(16));
// Safety timeout
if now.duration_since(start_time) > Duration::from_secs(2) {
break;
}
}
// Chain multiple transitions
opacity.transition_to(0.5); // Start new transition from current position
// ... repeat animation loop
// Or jump immediately without animation
opacity.set_immediate(1.0);Fields§
§current_value: TImplementations§
Source§impl<T: StylePropValue> DirectTransition<T>
impl<T: StylePropValue> DirectTransition<T>
Sourcepub fn new(initial_value: T, transition: Option<Transition>) -> Self
pub fn new(initial_value: T, transition: Option<Transition>) -> Self
Create a new transition starting at the given value
Sourcepub fn set_transition(&mut self, transition: Option<Transition>)
pub fn set_transition(&mut self, transition: Option<Transition>)
Configure the transition timing and easing function
Pass None to disable transitions (values will change immediately)
Sourcepub fn transition_to(&mut self, target: T) -> bool
pub fn transition_to(&mut self, target: T) -> bool
Start transitioning to a new target value
Returns true if a transition was started, false if no transition
is configured or the target equals the current value
Sourcepub fn step(&mut self, now: &Instant) -> bool
pub fn step(&mut self, now: &Instant) -> bool
Step the transition forward in time
Call this every frame with the current time. Returns true if the
interpolated value changed this frame, false otherwise.
You can use the return value to optimize rendering - only update when something actually changed.
Sourcepub fn get(&self) -> T
pub fn get(&self) -> T
Get the current interpolated value
During a transition, this returns the smoothly interpolated value. When no transition is active, returns the target value.
Sourcepub fn set_immediate(&mut self, value: T)
pub fn set_immediate(&mut self, value: T)
Set value immediately without any transition
This cancels any active transition and jumps directly to the new value
Trait Implementations§
Source§impl<T: Clone + StylePropValue> Clone for DirectTransition<T>
impl<T: Clone + StylePropValue> Clone for DirectTransition<T>
Source§fn clone(&self) -> DirectTransition<T>
fn clone(&self) -> DirectTransition<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<T> Freeze for DirectTransition<T>where
T: Freeze,
impl<T> !RefUnwindSafe for DirectTransition<T>
impl<T> !Send for DirectTransition<T>
impl<T> !Sync for DirectTransition<T>
impl<T> Unpin for DirectTransition<T>where
T: Unpin,
impl<T> !UnwindSafe for DirectTransition<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more