mirror of
https://github.com/danbulant/cushy
synced 2026-07-03 10:10:36 +00:00
Fixing nested collapse animations
This commit is contained in:
parent
9a148b8765
commit
94e473cdd3
2 changed files with 45 additions and 22 deletions
|
|
@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
- `Collapse`, `OverlayLayer`, and `Progress` all honor the theme components
|
- `Collapse`, `OverlayLayer`, and `Progress` all honor the theme components
|
||||||
`EasingIn` and `EasingOut` rather than hard-coded easing functions.
|
`EasingIn` and `EasingOut` rather than hard-coded easing functions.
|
||||||
|
- `Collapse` widgets now apply child size changes immediately rather than
|
||||||
|
animating the transition. The previous behavior caused nested collapsed
|
||||||
|
widgets to grow and shrink in an accordian-like fashion rather than animating
|
||||||
|
together.
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ use figures::{Size, Zero};
|
||||||
use crate::animation::{AnimationHandle, AnimationTarget, Spawn};
|
use crate::animation::{AnimationHandle, AnimationTarget, Spawn};
|
||||||
use crate::context::LayoutContext;
|
use crate::context::LayoutContext;
|
||||||
use crate::styles::components::{EasingIn, EasingOut};
|
use crate::styles::components::{EasingIn, EasingOut};
|
||||||
use crate::value::{Dynamic, IntoDynamic, Source};
|
use crate::value::{Destination, Dynamic, Generation, IntoDynamic, Source};
|
||||||
use crate::widget::{MakeWidget, WidgetRef, WrappedLayout, WrapperWidget};
|
use crate::widget::{MakeWidget, WidgetInstance, WidgetRef, WrappedLayout, WrapperWidget};
|
||||||
use crate::ConstraintLimit;
|
use crate::ConstraintLimit;
|
||||||
|
|
||||||
/// A widget that collapses/hides its contents based on a [`Dynamic<bool>`].
|
/// A widget that collapses/hides its contents based on a [`Dynamic<bool>`].
|
||||||
|
|
@ -15,25 +15,32 @@ use crate::ConstraintLimit;
|
||||||
pub struct Collapse {
|
pub struct Collapse {
|
||||||
child: WidgetRef,
|
child: WidgetRef,
|
||||||
collapse: Dynamic<bool>,
|
collapse: Dynamic<bool>,
|
||||||
|
collapse_generation: Generation,
|
||||||
size: Dynamic<Px>,
|
size: Dynamic<Px>,
|
||||||
collapse_animation: Option<CollapseAnimation>,
|
collapse_animation: Option<CollapseAnimation>,
|
||||||
vertical: bool,
|
vertical: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Collapse {
|
impl Collapse {
|
||||||
|
fn new(collapse: Dynamic<bool>, child: WidgetInstance, vertical: bool) -> Self {
|
||||||
|
let collapse_generation = collapse.generation();
|
||||||
|
Self {
|
||||||
|
collapse,
|
||||||
|
collapse_generation,
|
||||||
|
child: WidgetRef::new(child),
|
||||||
|
size: Dynamic::default(),
|
||||||
|
vertical,
|
||||||
|
collapse_animation: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a widget that collapses `child` vertically based on the dynamic
|
/// Returns a widget that collapses `child` vertically based on the dynamic
|
||||||
/// boolean value.
|
/// boolean value.
|
||||||
///
|
///
|
||||||
/// This widget will be collapsed when the dynamic contains `true`, and
|
/// This widget will be collapsed when the dynamic contains `true`, and
|
||||||
/// revealed when the dynamic contains `false`.
|
/// revealed when the dynamic contains `false`.
|
||||||
pub fn vertical(collapse_when: impl IntoDynamic<bool>, child: impl MakeWidget) -> Self {
|
pub fn vertical(collapse_when: impl IntoDynamic<bool>, child: impl MakeWidget) -> Self {
|
||||||
Self {
|
Self::new(collapse_when.into_dynamic(), child.make_widget(), true)
|
||||||
collapse: collapse_when.into_dynamic(),
|
|
||||||
child: WidgetRef::new(child),
|
|
||||||
size: Dynamic::default(),
|
|
||||||
vertical: true,
|
|
||||||
collapse_animation: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a widget that collapses `child` horizontally based on the
|
/// Returns a widget that collapses `child` horizontally based on the
|
||||||
|
|
@ -42,23 +49,33 @@ impl Collapse {
|
||||||
/// This widget will be collapsed when the dynamic contains `true`, and
|
/// This widget will be collapsed when the dynamic contains `true`, and
|
||||||
/// revealed when the dynamic contains `false`.
|
/// revealed when the dynamic contains `false`.
|
||||||
pub fn horizontal(collapse_when: impl IntoDynamic<bool>, child: impl MakeWidget) -> Self {
|
pub fn horizontal(collapse_when: impl IntoDynamic<bool>, child: impl MakeWidget) -> Self {
|
||||||
Self {
|
Self::new(collapse_when.into_dynamic(), child.make_widget(), false)
|
||||||
collapse: collapse_when.into_dynamic(),
|
|
||||||
child: WidgetRef::new(child),
|
|
||||||
size: Dynamic::default(),
|
|
||||||
vertical: false,
|
|
||||||
collapse_animation: None,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn note_child_size(&mut self, size: Px, context: &mut LayoutContext<'_, '_, '_, '_>) {
|
fn note_child_size(
|
||||||
let (easing, target) = if self.collapse.get_tracking_invalidate(context) {
|
&mut self,
|
||||||
|
size: Px,
|
||||||
|
current_size: Px,
|
||||||
|
context: &mut LayoutContext<'_, '_, '_, '_>,
|
||||||
|
) -> Px {
|
||||||
|
context.invalidate_when_changed(&self.collapse);
|
||||||
|
let (generation, collapse) = self.collapse.map_generational(|c| (c.generation(), *c));
|
||||||
|
let (easing, target) = if collapse {
|
||||||
(context.get(&EasingOut), Px::ZERO)
|
(context.get(&EasingOut), Px::ZERO)
|
||||||
} else {
|
} else {
|
||||||
(context.get(&EasingIn), size)
|
(context.get(&EasingIn), size)
|
||||||
};
|
};
|
||||||
|
if generation == self.collapse_generation {}
|
||||||
match &self.collapse_animation {
|
match &self.collapse_animation {
|
||||||
Some(state) if state.target == target => {}
|
Some(state) if state.target == target => {}
|
||||||
|
Some(_) if generation == self.collapse_generation => {
|
||||||
|
// The resize happened from a reason other than our toggle.
|
||||||
|
// Immediately apply it.
|
||||||
|
let mut stored_size = self.size.lock();
|
||||||
|
stored_size.prevent_notifications();
|
||||||
|
*stored_size = target;
|
||||||
|
return target;
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// If this is our first setup, immediately give the child the
|
// If this is our first setup, immediately give the child the
|
||||||
// space they request.
|
// space they request.
|
||||||
|
|
@ -78,6 +95,8 @@ impl Collapse {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.collapse_generation = generation;
|
||||||
|
current_size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -94,13 +113,13 @@ impl WrapperWidget for Collapse {
|
||||||
) -> WrappedLayout {
|
) -> WrappedLayout {
|
||||||
let clip_size = self.size.get_tracking_invalidate(context);
|
let clip_size = self.size.get_tracking_invalidate(context);
|
||||||
if self.vertical {
|
if self.vertical {
|
||||||
self.note_child_size(size.height, context);
|
let height = self.note_child_size(size.height, clip_size, context);
|
||||||
|
|
||||||
Size::new(size.width, clip_size)
|
Size::new(size.width, height)
|
||||||
} else {
|
} else {
|
||||||
self.note_child_size(size.width, context);
|
let width = self.note_child_size(size.width, clip_size, context);
|
||||||
|
|
||||||
Size::new(clip_size, size.height)
|
Size::new(width, size.height)
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue