use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum FocusTarget {
Control(Cow<'static, str>),
TextInput(iced::widget::Id),
}
impl FocusTarget {
pub const fn control(key: &'static str) -> Self {
FocusTarget::Control(Cow::Borrowed(key))
}
pub fn control_dynamic(key: String) -> Self {
FocusTarget::Control(Cow::Owned(key))
}
pub fn text_input(id: iced::widget::Id) -> Self {
FocusTarget::TextInput(id)
}
}
pub type FocusOrder<Message> = Vec<(FocusTarget, Option<Message>)>;
pub struct LayerFocus<Message> {
pub order: FocusOrder<Message>,
pub entry: Option<FocusTarget>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Next,
Previous,
}
pub fn resolve<'a, Message>(
order: &'a FocusOrder<Message>,
current: Option<&FocusTarget>,
) -> Option<&'a FocusTarget> {
let current = current?;
match order.iter().find(|(target, _)| target == current) {
Some((target, _)) => Some(target),
None => order.first().map(|(target, _)| target),
}
}
pub fn advance<'a, Message>(
order: &'a FocusOrder<Message>,
current: Option<&FocusTarget>,
direction: Direction,
) -> Option<&'a FocusTarget> {
if order.is_empty() {
return None;
}
let index =
resolve(order, current).and_then(|target| order.iter().position(|(t, _)| t == target));
let next_index = match (index, direction) {
(None, Direction::Next) => 0,
(None, Direction::Previous) => order.len() - 1,
(Some(i), Direction::Next) => (i + 1) % order.len(),
(Some(i), Direction::Previous) => (i + order.len() - 1) % order.len(),
};
order.get(next_index).map(|(target, _)| target)
}
pub fn activation_message<Message: Clone>(
order: &FocusOrder<Message>,
current: Option<&FocusTarget>,
) -> Option<Message> {
let resolved = resolve(order, current)?;
order
.iter()
.find(|(target, _)| target == resolved)
.and_then(|(_, message)| message.clone())
}
pub fn is_text_input_focused<Message>(
order: &FocusOrder<Message>,
current: Option<&FocusTarget>,
) -> bool {
matches!(resolve(order, current), Some(FocusTarget::TextInput(_)))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Reconciliation {
None,
FocusTextInput(iced::widget::Id),
ClearTextInputFocus,
}
pub fn reconcile(previous: Option<&FocusTarget>, next: Option<&FocusTarget>) -> Reconciliation {
match next {
Some(FocusTarget::TextInput(id)) => Reconciliation::FocusTextInput(id.clone()),
_ => match previous {
Some(FocusTarget::TextInput(_)) => Reconciliation::ClearTextInputFocus,
_ => Reconciliation::None,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug, Clone, PartialEq, Eq)]
enum TestMessage {
A,
B,
C,
}
fn control_order() -> FocusOrder<TestMessage> {
vec![
(FocusTarget::control("a"), Some(TestMessage::A)),
(FocusTarget::control("b"), Some(TestMessage::B)),
(FocusTarget::control("c"), Some(TestMessage::C)),
]
}
#[test]
fn advance_next_from_none_lands_on_first() {
let order = control_order();
assert_eq!(
advance(&order, None, Direction::Next),
Some(&FocusTarget::control("a"))
);
}
#[test]
fn advance_next_wraps_from_last_to_first() {
let order = control_order();
let last = FocusTarget::control("c");
assert_eq!(
advance(&order, Some(&last), Direction::Next),
Some(&FocusTarget::control("a"))
);
}
#[test]
fn advance_previous_wraps_from_first_to_last() {
let order = control_order();
let first = FocusTarget::control("a");
assert_eq!(
advance(&order, Some(&first), Direction::Previous),
Some(&FocusTarget::control("c"))
);
}
#[test]
fn advance_on_empty_order_is_none() {
let order: FocusOrder<TestMessage> = Vec::new();
assert_eq!(advance(&order, None, Direction::Next), None);
}
#[test]
fn resolve_falls_back_to_first_when_current_target_is_gone() {
let order = vec![
(FocusTarget::control("a"), Some(TestMessage::A)),
(FocusTarget::control("c"), Some(TestMessage::C)),
];
let stale = FocusTarget::control("b");
assert_eq!(
resolve(&order, Some(&stale)),
Some(&FocusTarget::control("a"))
);
}
#[test]
fn activation_message_returns_the_current_targets_message() {
let order = control_order();
let b = FocusTarget::control("b");
assert_eq!(activation_message(&order, Some(&b)), Some(TestMessage::B));
}
#[test]
fn activation_message_is_none_for_a_disabled_control() {
let order = vec![
(FocusTarget::control("a"), Some(TestMessage::A)),
(FocusTarget::control("disabled"), None),
];
let disabled = FocusTarget::control("disabled");
assert_eq!(activation_message(&order, Some(&disabled)), None);
assert_eq!(
advance(&order, Some(&FocusTarget::control("a")), Direction::Next),
Some(&disabled)
);
}
#[test]
fn is_text_input_focused_distinguishes_control_from_text_input() {
let text_id = iced::widget::Id::new("field");
let order = vec![
(FocusTarget::control("button"), Some(TestMessage::A)),
(FocusTarget::text_input(text_id.clone()), None),
];
let button = FocusTarget::control("button");
let field = FocusTarget::text_input(text_id);
assert!(!is_text_input_focused(&order, Some(&button)));
assert!(is_text_input_focused(&order, Some(&field)));
}
#[test]
fn reconcile_focuses_iced_when_moving_onto_a_text_input() {
let id = iced::widget::Id::new("field");
let button = FocusTarget::control("button");
let field = FocusTarget::text_input(id.clone());
assert_eq!(
reconcile(Some(&button), Some(&field)),
Reconciliation::FocusTextInput(id)
);
}
#[test]
fn reconcile_clears_iced_focus_when_moving_off_a_text_input() {
let id = iced::widget::Id::new("field");
let button = FocusTarget::control("button");
let field = FocusTarget::text_input(id);
assert_eq!(
reconcile(Some(&field), Some(&button)),
Reconciliation::ClearTextInputFocus
);
}
#[test]
fn reconcile_does_nothing_between_two_controls() {
let a = FocusTarget::control("a");
let b = FocusTarget::control("b");
assert_eq!(reconcile(Some(&a), Some(&b)), Reconciliation::None);
}
#[test]
fn reconcile_does_nothing_between_two_text_inputs_other_than_focusing_the_new_one() {
let id_a = iced::widget::Id::new("a");
let id_b = iced::widget::Id::new("b");
let a = FocusTarget::text_input(id_a);
let b = FocusTarget::text_input(id_b.clone());
assert_eq!(
reconcile(Some(&a), Some(&b)),
Reconciliation::FocusTextInput(id_b)
);
}
}