use super::CloseReason;
use crate::{Context, Id, TweenOptions};
#[derive(Clone, Copy)]
pub(super) struct Keys {
pub(super) escape: bool,
pub(super) enter: bool,
}
#[derive(Clone, Copy)]
pub(super) struct CloseInputs {
pub(super) open: bool,
pub(super) escape: bool,
pub(super) overlay: bool,
pub(super) close_button: bool,
pub(super) action: bool,
}
#[derive(Clone, Copy)]
pub(super) struct Dismiss {
pub(super) escape: bool,
pub(super) overlay: bool,
}
fn presence(id: Id) -> Id {
id.with("presence")
}
pub(super) fn mounted(context: &mut Context, id: Id, open: bool) -> bool {
if !open && context.modal_is_open(id) {
context.end_modal(id);
}
open || context.animation_status(presence(id)).is_some()
}
pub(super) fn visibility(
context: &mut Context,
id: Id,
open: bool,
motion: &TweenOptions,
) -> Option<f32> {
let anim = presence(id);
let motion = crate::components::sanitize::forward("Modal motion", motion.clone());
let target = if open { 1.0_f32 } else { 0.0 };
let t = context
.transition_visible(anim, Some(0.0), target, motion, true)
.value
.clamp(0.0, 1.0);
if !open && t <= 0.0 {
context.remove_animation(anim);
return None;
}
Some(t)
}
pub(super) fn begin(context: &mut Context, id: Id, return_focus: Option<Id>, open: bool) -> Keys {
context.begin_modal(id, return_focus, open);
context.push_popup_layer(id);
context.modals.entered = None;
let (escape, enter) = if open {
context.take_modal_keys(id)
} else {
(false, false)
};
if enter {
context.modals.entered = Some(id);
}
Keys { escape, enter }
}
pub(super) fn close_reason(inputs: CloseInputs, dismiss: Dismiss) -> Option<CloseReason> {
if !inputs.open {
None
} else if inputs.escape && dismiss.escape {
Some(CloseReason::Escape)
} else if inputs.overlay && dismiss.overlay {
Some(CloseReason::Overlay)
} else if inputs.close_button {
Some(CloseReason::CloseButton)
} else if inputs.action {
Some(CloseReason::Action)
} else {
None
}
}
pub(super) fn finish(context: &mut Context, id: Id, open: &mut bool, closed: Option<CloseReason>) {
if closed.is_some() {
*open = false;
context.end_modal(id);
}
context.end_modal_build();
}