use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(tsify_next::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub enum Interaction {
SelectNext,
SelectPrev,
SelectIndex(u32),
Filter(String),
Confirm,
Cancel,
ScrollDown(u32),
ScrollUp(u32),
PageDown,
PageUp,
Custom(String),
}
impl Interaction {
#[must_use]
pub fn filter(text: impl Into<String>) -> Self {
Self::Filter(text.into())
}
#[must_use]
pub fn custom(action: impl Into<String>) -> Self {
Self::Custom(action.into())
}
#[must_use]
pub const fn is_navigation(&self) -> bool {
matches!(
self,
Self::SelectNext
| Self::SelectPrev
| Self::SelectIndex(_)
| Self::ScrollDown(_)
| Self::ScrollUp(_)
| Self::PageDown
| Self::PageUp
)
}
#[must_use]
pub const fn is_terminal(&self) -> bool {
matches!(self, Self::Confirm | Self::Cancel)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "wasm", derive(tsify_next::Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
pub enum InteractionResult {
StateUpdate(crate::wire::OverlayState),
Confirm(serde_json::Value),
Cancel,
PassThrough,
Consumed,
}
impl InteractionResult {
#[must_use]
pub fn confirm(value: impl Into<serde_json::Value>) -> Self {
Self::Confirm(value.into())
}
#[must_use]
pub const fn state_update(state: crate::wire::OverlayState) -> Self {
Self::StateUpdate(state)
}
#[must_use]
pub const fn closes_overlay(&self) -> bool {
matches!(self, Self::Confirm(_) | Self::Cancel)
}
#[must_use]
pub const fn was_handled(&self) -> bool {
!matches!(self, Self::PassThrough)
}
}
#[cfg(test)]
#[path = "interaction_tests.rs"]
mod tests;