use super::{Tree, find_node_by_path};
use crate::node::{Node, node_kind::NodeKind};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) enum InputModality {
#[default]
Pointer,
Keyboard,
}
const MODIFIER_KEYS: [&str; 6] = ["Shift", "Control", "Alt", "AltGraph", "Super", "Meta"];
fn is_keyboard_use(key: &str, ctrl: bool, alt: bool, meta: bool) -> bool {
!(ctrl || alt || meta || MODIFIER_KEYS.contains(&key))
}
pub(super) fn shows_focus_ring(modality: InputModality, input: &Node) -> bool {
modality == InputModality::Keyboard || matches!(input.node_kind(), NodeKind::TextInput { .. })
}
pub(super) fn set_node_focus_visible(node: &Node, visible: bool) {
node.set_style_focus_visible(visible);
if let Some(state) = &node.interaction {
state.set_focus_visible(visible);
}
if let Some(element_ref) = &node.element_ref {
element_ref.set_focus_visible(visible);
}
}
impl Tree {
pub fn focus_visible(&self) -> bool {
let Some(root) = self.root.as_ref() else {
return false;
};
self
.focused_path
.as_deref()
.and_then(|path| find_node_by_path(root, path))
.is_some_and(Node::is_style_focus_visible)
}
pub(super) fn note_key_press_modality(&mut self, key: &str, ctrl: bool, alt: bool, meta: bool) {
if is_keyboard_use(key, ctrl, alt, meta) {
self.set_input_modality(InputModality::Keyboard);
}
}
pub(super) fn set_input_modality(&mut self, modality: InputModality) {
if self.input_modality == modality {
return;
}
self.input_modality = modality;
if self.focused_node.is_some() {
self.refresh_focus_ids();
self.needs_redraw = true;
}
}
}