use super::Ui;
use crate::{
context::Paint, layout::LayoutCursor, Id, Layout, Rect, Transform, TweenOptions, Vec2,
};
use std::hash::Hash;
pub(crate) struct EffectState {
pub last_frame: u64,
pub revision: u64,
pub value: f32,
pub position: Vec2,
pub built_frame: u64,
}
impl EffectState {
pub(crate) fn new(frame: u64) -> Self {
Self {
last_frame: frame,
revision: 0,
value: 0.0,
position: Vec2::ZERO,
built_frame: frame,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Presence {
pub fade: bool,
pub offset: Option<Vec2>,
pub scale: f32,
pub pivot: Vec2,
pub exit_layout: bool,
pub interactive_enter: bool,
pub motion: Option<TweenOptions>,
}
impl Default for Presence {
fn default() -> Self {
Self::fade()
}
}
impl Presence {
pub fn fade() -> Self {
Self {
fade: true,
offset: None,
scale: 1.0,
pivot: Vec2::splat(0.5),
exit_layout: true,
interactive_enter: true,
motion: None,
}
}
pub fn slide(offset: Vec2) -> Self {
Self::fade().fading(false).offset(offset)
}
pub fn scale(scale: f32) -> Self {
Self::fade().fading(false).scaling(scale)
}
pub fn fading(mut self, fade: bool) -> Self {
self.fade = fade;
self
}
pub fn offset(mut self, offset: Vec2) -> Self {
assert!(offset.is_finite());
self.offset = Some(offset);
self
}
pub fn scaling(mut self, scale: f32) -> Self {
assert!(scale.is_finite() && scale > 0.0);
self.scale = scale;
self
}
pub fn pivot(mut self, pivot: Vec2) -> Self {
assert!(pivot.is_finite());
self.pivot = pivot;
self
}
pub fn exit_layout(mut self, keep: bool) -> Self {
self.exit_layout = keep;
self
}
pub fn interactive_enter(mut self, enabled: bool) -> Self {
self.interactive_enter = enabled;
self
}
pub fn motion(mut self, motion: TweenOptions) -> Self {
self.motion = Some(motion);
self
}
pub fn show<R>(
self,
ui: &mut Ui<'_>,
source: impl Hash,
visible: bool,
build: impl FnOnce(&mut Ui<'_>) -> R,
) -> Option<R> {
if ui.flow.is_some() {
return ui.layout_item(|ui| self.show(ui, source, visible, build));
}
let id = ui.scope.with(("presence", Id::new(source)));
let motion = self
.motion
.unwrap_or_else(|| ui.style().motion.presence.clone());
assert!(
motion.repeat == crate::Repeat::Once && !motion.auto_reverse,
"presence must be a finite forward transition"
);
let pass = ui.context.animation_pass(!ui.clip_rect().is_empty());
let progress = ui.context.animations.transition(
id,
Some(0.0_f32),
if visible { 1.0 } else { 0.0 },
motion,
pass,
);
if !visible && progress.value <= 0.0 {
return None;
}
let interactive = visible && (self.interactive_enter || progress.completed());
let (inner, size, placement) = ui.measure_effect(id, interactive, build);
let rect = Rect::from_min_size(ui.layout.cursor, size);
let t = progress.value.clamp(0.0, 1.0);
let transform = Transform::around(
rect.min + size * self.pivot,
1.0 + (self.scale - 1.0) * (1.0 - t),
self.offset.unwrap_or(Vec2::ZERO) * (1.0 - t),
);
let clip = ui.clip_rect();
if transform.rect(rect).intersect(clip).is_empty() {
ui.context.animations.hide(id);
ui.context.hide_placement_animations(&placement);
}
ui.context.place_visual(
placement,
transform,
if self.fade { t } else { 1.0 },
clip,
interactive,
);
if visible || self.exit_layout {
ui.allocate_space(size);
}
Some(inner)
}
}
impl Ui<'_> {
pub fn visual<R>(
&mut self,
source: impl Hash,
transform: Transform,
opacity: f32,
build: impl FnOnce(&mut Ui<'_>) -> R,
) -> R {
if self.flow.is_some() {
return self.layout_item(|ui| ui.visual(source, transform, opacity, build));
}
assert!(
transform.angle == 0.0
&& transform.scale.is_finite()
&& transform.scale > 0.0
&& transform.translation.is_finite()
&& opacity.is_finite()
);
let id = self.scope.with(("visual", Id::new(source)));
let (inner, size, placement) = self.measure_effect(id, opacity > 0.0, build);
let rect = transform.rect(Rect::from_min_size(self.layout.cursor, size));
if rect.intersect(self.clip_rect()).is_empty() {
self.context.hide_placement_animations(&placement);
}
self.context.place_visual(
placement,
transform,
opacity.clamp(0.0, 1.0),
self.clip_rect(),
opacity > 0.0,
);
self.allocate_space(size);
inner
}
pub(super) fn measure_effect<R>(
&mut self,
id: Id,
interactive: bool,
build: impl FnOnce(&mut Ui<'_>) -> R,
) -> (R, Vec2, crate::context::placement::Placement) {
let bounds = Rect::from_min_max(self.layout.cursor, self.layout.bounds.max);
let spacing = self.style().spacing.max(0.0);
let parent_clip = self.clip_rect();
self.context.begin_placement(self.window);
self.context.visual_depth += 1;
self.context.visual_clips.push((self.window, parent_clip));
let mut child = Ui {
flow: None,
context: self.context,
window: self.window,
scope: id,
sequence: 0,
clip: Rect::from_min_size(Vec2::splat(-1.0e9), Vec2::splat(2.0e9)),
layout: LayoutCursor::new(bounds, Layout::Vertical, spacing),
enabled: self.enabled && interactive,
backdrop_blur: self.backdrop_blur,
hover_style: self.hover_style,
};
child.begin_layout(crate::Align::Start);
let inner = build(&mut child);
child.finish_layout();
let size = child.layout.used;
child.context.visual_depth -= 1;
child.context.visual_clips.pop();
let placement = child.context.end_placement();
(inner, size, placement)
}
pub fn presence<R>(
&mut self,
source: impl Hash,
visible: bool,
build: impl FnOnce(&mut Ui<'_>) -> R,
) -> Option<R> {
Presence::fade().show(self, source, visible, build)
}
pub fn reveal<R>(
&mut self,
source: impl Hash,
open: bool,
build: impl FnOnce(&mut Ui<'_>) -> R,
) -> Option<R> {
self.reveal_with(source, open, self.style().motion.expand.clone(), build)
}
pub fn reveal_with<R>(
&mut self,
source: impl Hash,
open: bool,
motion: TweenOptions,
build: impl FnOnce(&mut Ui<'_>) -> R,
) -> Option<R> {
if self.flow.is_some() {
return self.layout_item(|ui| ui.reveal_with(source, open, motion, build));
}
assert!(
motion.repeat == crate::Repeat::Once && !motion.auto_reverse,
"reveal must be a finite forward transition"
);
let id = self.scope.with(("reveal", Id::new(source)));
let visible = !self.clip_rect().is_empty();
let old_height = self.context.effect_states.get(&id).map_or(0.0, |s| s.value);
let mut state = EffectState::new(self.context.frame);
if !open {
let progress =
self.context
.transition_visible(id, Some(0.0), 0.0_f32, motion.clone(), visible);
state.value = progress.value;
if state.value <= 0.0 {
self.context.effect_states.insert(id, state);
return None;
}
}
let (inner, size, placement) = self.measure_effect(id, open, build);
let height = self
.context
.transition_visible(
id,
Some(old_height),
if open { size.y } else { 0.0 },
motion,
visible,
)
.value
.max(0.0);
state.value = height;
self.context.effect_states.insert(id, state);
let rect = Rect::from_min_size(self.layout.cursor, Vec2::new(size.x, height));
let clip = self.clip_rect().intersect(rect);
let target_bounds = Rect::from_min_size(self.layout.cursor, size);
if target_bounds.intersect(self.clip_rect()).is_empty() || (clip.is_empty() && !open) {
self.context.animations.hide(id);
self.context.hide_placement_animations(&placement);
}
self.context
.place_visual(placement, Transform::IDENTITY, 1.0, clip, open);
if height > 0.0 {
self.allocate_space(Vec2::new(size.x, height));
}
Some(inner)
}
pub fn spring_transition<T: crate::SpringValue>(
&mut self,
source: impl Hash,
target: T,
options: crate::SpringOptions,
) -> crate::Animated<crate::SpringState<T>> {
let pass = self.context.animation_pass(!self.clip_rect().is_empty());
self.context
.animations
.spring(self.animation_id(source), None, target, options, pass)
}
pub fn spring_transition_from<T: crate::SpringValue>(
&mut self,
source: impl Hash,
initial: crate::SpringState<T>,
target: T,
options: crate::SpringOptions,
) -> crate::Animated<crate::SpringState<T>> {
let pass = self.context.animation_pass(!self.clip_rect().is_empty());
self.context.animations.spring(
self.animation_id(source),
Some(initial),
target,
options,
pass,
)
}
pub fn pulse<R>(
&mut self,
source: impl Hash,
active: bool,
build: impl FnOnce(&mut Ui<'_>) -> R,
) -> R {
self.pulse_with(
source,
active,
crate::Pulse::new(self.style().motion.cycle_period),
build,
)
}
pub fn pulse_with<R>(
&mut self,
source: impl Hash,
active: bool,
effect: crate::Pulse,
build: impl FnOnce(&mut Ui<'_>) -> R,
) -> R {
if self.flow.is_some() {
return self.layout_item(|ui| ui.pulse_with(source, active, effect, build));
}
let id = self.scope.with(("pulse", Id::new(source)));
let opacity = if active && !self.style().motion.reduced_motion {
let pass = self.context.animation_pass(!self.clip_rect().is_empty());
self.context
.animations
.animate(id, crate::AnimationOptions::default(), || effect, pass)
.value
} else {
self.context.remove_animation(id);
1.0
};
let (inner, size, placement) = self.measure_effect(id, true, build);
let rect = Rect::from_min_size(self.layout.cursor, size);
if rect.intersect(self.clip_rect()).is_empty() {
self.context.animations.hide(id);
self.context.hide_placement_animations(&placement);
}
self.context.place_visual(
placement,
Transform::IDENTITY,
opacity,
self.clip_rect(),
true,
);
self.allocate_space(size);
inner
}
pub fn highlight(
&mut self,
source: impl Hash,
revision: u64,
rect: Rect,
base: crate::Color,
accent: crate::Color,
) {
self.highlight_with(
source,
revision,
rect,
base,
accent,
self.style().motion.highlight.clone(),
);
}
pub fn highlight_with(
&mut self,
source: impl Hash,
revision: u64,
rect: Rect,
base: crate::Color,
accent: crate::Color,
release: TweenOptions,
) {
assert!(
release.repeat == crate::Repeat::Once && !release.auto_reverse,
"highlight release must be finite"
);
let id = self.scope.with(("highlight", Id::new(source)));
let mut state = self.context.effect_states.remove(&id).unwrap_or_else(|| {
let mut state = EffectState::new(self.context.frame);
state.revision = revision;
state
});
state.last_frame = self.context.frame;
let pass = self
.context
.animation_pass(!rect.intersect(self.clip_rect()).is_empty());
let channel = id.with("amount");
let current = self
.context
.animations
.read::<f32>(channel, pass)
.map_or(0.0, |s| s.value);
if revision != state.revision {
state.revision = revision;
state.value = 1.0;
let effect = crate::Sequence::new(current)
.then(crate::Tween::with_options(
current,
1.0,
self.style().motion.hover.clone(),
))
.then(crate::Tween::with_options(1.0, 0.0, release));
self.context.restart_animation(channel, effect);
}
let alpha = self
.context
.animations
.read::<f32>(channel, pass)
.map_or(0.0, |s| s.value);
let color = if self.style().motion.reduced_motion && state.value > 0.0 {
crate::Interpolate::interpolate(&base, &accent, 0.25)
} else {
crate::Interpolate::interpolate(&base, &accent, alpha)
};
self.context.paint(
id.with("paint"),
self.window,
self.clip,
vec![Paint::Shape(
crate::Shape::rect(rect, color)
.corner_radius(self.style().rounding.top_left)
.into(),
)],
);
self.context.effect_states.insert(id, state);
}
}