2023-11-10 21:47:45 +00:00
|
|
|
use crate::{
|
2023-12-15 14:13:32 +00:00
|
|
|
arena::ArenaRef, Action, ActionRegistry, DispatchPhase, FocusId, KeyBinding, KeyContext,
|
|
|
|
KeyMatch, Keymap, Keystroke, KeystrokeMatcher, WindowContext,
|
2023-11-10 21:47:45 +00:00
|
|
|
};
|
|
|
|
use collections::HashMap;
|
|
|
|
use parking_lot::Mutex;
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
use std::{
|
|
|
|
any::{Any, TypeId},
|
2023-11-13 17:29:18 +00:00
|
|
|
rc::Rc,
|
2023-11-10 21:47:45 +00:00
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
|
|
|
|
pub struct DispatchNodeId(usize);
|
|
|
|
|
2023-11-13 17:29:18 +00:00
|
|
|
pub(crate) struct DispatchTree {
|
2023-11-10 21:47:45 +00:00
|
|
|
node_stack: Vec<DispatchNodeId>,
|
2023-12-04 21:28:37 +00:00
|
|
|
pub(crate) context_stack: Vec<KeyContext>,
|
2023-11-10 21:47:45 +00:00
|
|
|
nodes: Vec<DispatchNode>,
|
|
|
|
focusable_node_ids: HashMap<FocusId, DispatchNodeId>,
|
|
|
|
keystroke_matchers: HashMap<SmallVec<[KeyContext; 4]>, KeystrokeMatcher>,
|
|
|
|
keymap: Arc<Mutex<Keymap>>,
|
2023-11-17 01:32:02 +00:00
|
|
|
action_registry: Rc<ActionRegistry>,
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2023-11-13 17:29:18 +00:00
|
|
|
pub(crate) struct DispatchNode {
|
2023-12-12 12:16:09 +00:00
|
|
|
pub key_listeners: Vec<KeyListener>,
|
|
|
|
pub action_listeners: Vec<DispatchActionListener>,
|
2023-12-06 17:02:45 +00:00
|
|
|
pub context: Option<KeyContext>,
|
2023-12-13 17:16:39 +00:00
|
|
|
focus_id: Option<FocusId>,
|
2023-11-10 21:47:45 +00:00
|
|
|
parent: Option<DispatchNodeId>,
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:13:32 +00:00
|
|
|
type KeyListener = ArenaRef<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext)>;
|
2023-11-13 17:29:18 +00:00
|
|
|
|
2023-12-21 08:41:48 +00:00
|
|
|
#[derive(Clone)]
|
2023-11-13 19:48:36 +00:00
|
|
|
pub(crate) struct DispatchActionListener {
|
2023-11-13 17:29:18 +00:00
|
|
|
pub(crate) action_type: TypeId,
|
2023-12-15 14:13:32 +00:00
|
|
|
pub(crate) listener: ArenaRef<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext)>,
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 17:29:18 +00:00
|
|
|
impl DispatchTree {
|
2023-11-17 01:32:02 +00:00
|
|
|
pub fn new(keymap: Arc<Mutex<Keymap>>, action_registry: Rc<ActionRegistry>) -> Self {
|
2023-11-10 21:47:45 +00:00
|
|
|
Self {
|
|
|
|
node_stack: Vec::new(),
|
|
|
|
context_stack: Vec::new(),
|
|
|
|
nodes: Vec::new(),
|
|
|
|
focusable_node_ids: HashMap::default(),
|
|
|
|
keystroke_matchers: HashMap::default(),
|
|
|
|
keymap,
|
2023-11-17 01:32:02 +00:00
|
|
|
action_registry,
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.node_stack.clear();
|
|
|
|
self.nodes.clear();
|
2023-11-13 14:31:35 +00:00
|
|
|
self.context_stack.clear();
|
|
|
|
self.focusable_node_ids.clear();
|
|
|
|
self.keystroke_matchers.clear();
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-06 15:15:53 +00:00
|
|
|
pub fn push_node(&mut self, context: Option<KeyContext>) {
|
2023-11-10 21:47:45 +00:00
|
|
|
let parent = self.node_stack.last().copied();
|
|
|
|
let node_id = DispatchNodeId(self.nodes.len());
|
|
|
|
self.nodes.push(DispatchNode {
|
|
|
|
parent,
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
self.node_stack.push(node_id);
|
2023-12-06 15:15:53 +00:00
|
|
|
if let Some(context) = context {
|
2023-12-06 17:02:45 +00:00
|
|
|
self.active_node().context = Some(context.clone());
|
2023-11-10 21:47:45 +00:00
|
|
|
self.context_stack.push(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pop_node(&mut self) {
|
|
|
|
let node_id = self.node_stack.pop().unwrap();
|
2023-12-06 17:02:45 +00:00
|
|
|
if self.nodes[node_id.0].context.is_some() {
|
2023-11-10 21:47:45 +00:00
|
|
|
self.context_stack.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-05 20:17:59 +00:00
|
|
|
pub fn clear_pending_keystrokes(&mut self) {
|
2023-11-15 19:11:07 +00:00
|
|
|
self.keystroke_matchers.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Preserve keystroke matchers from previous frames to support multi-stroke
|
|
|
|
/// bindings across multiple frames.
|
2023-12-05 20:17:59 +00:00
|
|
|
pub fn preserve_pending_keystrokes(&mut self, old_tree: &mut Self, focus_id: Option<FocusId>) {
|
2023-11-15 19:11:07 +00:00
|
|
|
if let Some(node_id) = focus_id.and_then(|focus_id| self.focusable_node_id(focus_id)) {
|
|
|
|
let dispatch_path = self.dispatch_path(node_id);
|
|
|
|
|
|
|
|
self.context_stack.clear();
|
|
|
|
for node_id in dispatch_path {
|
|
|
|
let node = self.node(node_id);
|
2023-12-06 17:02:45 +00:00
|
|
|
if let Some(context) = node.context.clone() {
|
|
|
|
self.context_stack.push(context);
|
2023-11-15 19:11:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some((context_stack, matcher)) = old_tree
|
|
|
|
.keystroke_matchers
|
|
|
|
.remove_entry(self.context_stack.as_slice())
|
|
|
|
{
|
|
|
|
self.keystroke_matchers.insert(context_stack, matcher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 21:47:45 +00:00
|
|
|
pub fn on_key_event(&mut self, listener: KeyListener) {
|
|
|
|
self.active_node().key_listeners.push(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn on_action(
|
|
|
|
&mut self,
|
|
|
|
action_type: TypeId,
|
2023-12-15 14:13:32 +00:00
|
|
|
listener: ArenaRef<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext)>,
|
2023-11-10 21:47:45 +00:00
|
|
|
) {
|
2023-11-13 19:48:36 +00:00
|
|
|
self.active_node()
|
|
|
|
.action_listeners
|
|
|
|
.push(DispatchActionListener {
|
|
|
|
action_type,
|
|
|
|
listener,
|
|
|
|
});
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_focusable(&mut self, focus_id: FocusId) {
|
2023-12-13 17:16:39 +00:00
|
|
|
let node_id = self.active_node_id();
|
|
|
|
self.active_node().focus_id = Some(focus_id);
|
|
|
|
self.focusable_node_ids.insert(focus_id, node_id);
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn focus_contains(&self, parent: FocusId, child: FocusId) -> bool {
|
|
|
|
if parent == child {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(parent_node_id) = self.focusable_node_ids.get(&parent) {
|
|
|
|
let mut current_node_id = self.focusable_node_ids.get(&child).copied();
|
|
|
|
while let Some(node_id) = current_node_id {
|
|
|
|
if node_id == *parent_node_id {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
current_node_id = self.nodes[node_id.0].parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:15:53 +00:00
|
|
|
pub fn available_actions(&self, target: DispatchNodeId) -> Vec<Box<dyn Action>> {
|
2023-12-08 21:02:14 +00:00
|
|
|
let mut actions = Vec::<Box<dyn Action>>::new();
|
2023-12-06 15:15:53 +00:00
|
|
|
for node_id in self.dispatch_path(target) {
|
|
|
|
let node = &self.nodes[node_id.0];
|
|
|
|
for DispatchActionListener { action_type, .. } in &node.action_listeners {
|
2023-12-08 21:02:14 +00:00
|
|
|
if let Err(ix) = actions.binary_search_by_key(action_type, |a| a.as_any().type_id())
|
|
|
|
{
|
|
|
|
// Intentionally silence these errors without logging.
|
|
|
|
// If an action cannot be built by default, it's not available.
|
|
|
|
let action = self.action_registry.build_action_type(action_type).ok();
|
|
|
|
if let Some(action) = action {
|
|
|
|
actions.insert(ix, action);
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
actions
|
|
|
|
}
|
|
|
|
|
2023-12-06 15:52:52 +00:00
|
|
|
pub fn is_action_available(&self, action: &dyn Action, target: DispatchNodeId) -> bool {
|
|
|
|
for node_id in self.dispatch_path(target) {
|
|
|
|
let node = &self.nodes[node_id.0];
|
|
|
|
if node
|
|
|
|
.action_listeners
|
|
|
|
.iter()
|
|
|
|
.any(|listener| listener.action_type == action.as_any().type_id())
|
|
|
|
{
|
|
|
|
return true;
|
2023-12-05 20:17:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2023-12-04 21:28:37 +00:00
|
|
|
pub fn bindings_for_action(
|
|
|
|
&self,
|
|
|
|
action: &dyn Action,
|
|
|
|
context_stack: &Vec<KeyContext>,
|
|
|
|
) -> Vec<KeyBinding> {
|
2023-11-13 22:33:22 +00:00
|
|
|
self.keymap
|
|
|
|
.lock()
|
|
|
|
.bindings_for_action(action.type_id())
|
2023-12-04 20:53:38 +00:00
|
|
|
.filter(|candidate| {
|
2023-12-04 21:28:37 +00:00
|
|
|
if !candidate.action.partial_eq(action) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for i in 1..context_stack.len() {
|
2023-12-04 21:37:47 +00:00
|
|
|
if candidate.matches_context(&context_stack[0..=i]) {
|
2023-12-04 21:28:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2024-01-01 23:09:24 +00:00
|
|
|
false
|
2023-12-04 20:53:38 +00:00
|
|
|
})
|
2023-11-13 22:33:22 +00:00
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:29:18 +00:00
|
|
|
pub fn dispatch_key(
|
2023-11-10 21:47:45 +00:00
|
|
|
&mut self,
|
2023-11-13 17:29:18 +00:00
|
|
|
keystroke: &Keystroke,
|
|
|
|
context: &[KeyContext],
|
2023-12-11 20:57:46 +00:00
|
|
|
) -> Vec<Box<dyn Action>> {
|
2023-11-13 19:48:36 +00:00
|
|
|
if !self.keystroke_matchers.contains_key(context) {
|
|
|
|
let keystroke_contexts = context.iter().cloned().collect();
|
2023-11-13 17:29:18 +00:00
|
|
|
self.keystroke_matchers.insert(
|
|
|
|
keystroke_contexts,
|
|
|
|
KeystrokeMatcher::new(self.keymap.clone()),
|
|
|
|
);
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 19:48:36 +00:00
|
|
|
let keystroke_matcher = self.keystroke_matchers.get_mut(context).unwrap();
|
2023-12-11 20:57:46 +00:00
|
|
|
if let KeyMatch::Some(actions) = keystroke_matcher.match_keystroke(keystroke, context) {
|
2023-11-13 17:29:18 +00:00
|
|
|
// Clear all pending keystrokes when an action has been found.
|
|
|
|
for keystroke_matcher in self.keystroke_matchers.values_mut() {
|
|
|
|
keystroke_matcher.clear_pending();
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 20:57:46 +00:00
|
|
|
actions
|
2023-11-13 17:29:18 +00:00
|
|
|
} else {
|
2023-12-11 20:57:46 +00:00
|
|
|
vec![]
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 22:06:33 +00:00
|
|
|
pub fn has_pending_keystrokes(&self) -> bool {
|
|
|
|
self.keystroke_matchers
|
|
|
|
.iter()
|
|
|
|
.any(|(_, matcher)| matcher.has_pending_keystrokes())
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:29:18 +00:00
|
|
|
pub fn dispatch_path(&self, target: DispatchNodeId) -> SmallVec<[DispatchNodeId; 32]> {
|
2023-11-10 21:47:45 +00:00
|
|
|
let mut dispatch_path: SmallVec<[DispatchNodeId; 32]> = SmallVec::new();
|
|
|
|
let mut current_node_id = Some(target);
|
|
|
|
while let Some(node_id) = current_node_id {
|
|
|
|
dispatch_path.push(node_id);
|
|
|
|
current_node_id = self.nodes[node_id.0].parent;
|
|
|
|
}
|
|
|
|
dispatch_path.reverse(); // Reverse the path so it goes from the root to the focused node.
|
|
|
|
dispatch_path
|
|
|
|
}
|
2023-11-13 17:29:18 +00:00
|
|
|
|
2023-12-13 17:16:39 +00:00
|
|
|
pub fn focus_path(&self, focus_id: FocusId) -> SmallVec<[FocusId; 8]> {
|
|
|
|
let mut focus_path: SmallVec<[FocusId; 8]> = SmallVec::new();
|
|
|
|
let mut current_node_id = self.focusable_node_ids.get(&focus_id).copied();
|
|
|
|
while let Some(node_id) = current_node_id {
|
|
|
|
let node = self.node(node_id);
|
|
|
|
if let Some(focus_id) = node.focus_id {
|
|
|
|
focus_path.push(focus_id);
|
|
|
|
}
|
|
|
|
current_node_id = node.parent;
|
|
|
|
}
|
|
|
|
focus_path.reverse(); // Reverse the path so it goes from the root to the focused node.
|
|
|
|
focus_path
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:33:08 +00:00
|
|
|
pub fn node(&self, node_id: DispatchNodeId) -> &DispatchNode {
|
|
|
|
&self.nodes[node_id.0]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn active_node(&mut self) -> &mut DispatchNode {
|
|
|
|
let active_node_id = self.active_node_id();
|
|
|
|
&mut self.nodes[active_node_id.0]
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:29:18 +00:00
|
|
|
pub fn focusable_node_id(&self, target: FocusId) -> Option<DispatchNodeId> {
|
|
|
|
self.focusable_node_ids.get(&target).copied()
|
|
|
|
}
|
2023-11-13 17:33:08 +00:00
|
|
|
|
2023-12-06 15:15:53 +00:00
|
|
|
pub fn root_node_id(&self) -> DispatchNodeId {
|
|
|
|
debug_assert!(!self.nodes.is_empty());
|
|
|
|
DispatchNodeId(0)
|
|
|
|
}
|
|
|
|
|
2023-11-13 17:33:08 +00:00
|
|
|
fn active_node_id(&self) -> DispatchNodeId {
|
|
|
|
*self.node_stack.last().unwrap()
|
|
|
|
}
|
2023-11-10 21:47:45 +00:00
|
|
|
}
|