Make command dispatching work
This commit is contained in:
@@ -104,7 +104,17 @@ impl dyn Action {
|
||||
pub fn type_id(&self) -> TypeId {
|
||||
self.as_any().type_id()
|
||||
}
|
||||
|
||||
pub fn name(&self) -> SharedString {
|
||||
ACTION_REGISTRY
|
||||
.read()
|
||||
.names_by_type_id
|
||||
.get(&self.type_id())
|
||||
.expect("type is not a registered action")
|
||||
.clone()
|
||||
}
|
||||
}
|
||||
|
||||
type ActionBuilder = fn(json: Option<serde_json::Value>) -> anyhow::Result<Box<dyn Action>>;
|
||||
|
||||
lazy_static! {
|
||||
@@ -114,7 +124,7 @@ lazy_static! {
|
||||
#[derive(Default)]
|
||||
struct ActionRegistry {
|
||||
builders_by_name: HashMap<SharedString, ActionBuilder>,
|
||||
builders_by_type_id: HashMap<TypeId, ActionBuilder>,
|
||||
names_by_type_id: HashMap<TypeId, SharedString>,
|
||||
all_names: Vec<SharedString>, // So we can return a static slice.
|
||||
}
|
||||
|
||||
@@ -123,20 +133,22 @@ pub fn register_action<A: Action>() {
|
||||
let name = A::qualified_name();
|
||||
let mut lock = ACTION_REGISTRY.write();
|
||||
lock.builders_by_name.insert(name.clone(), A::build);
|
||||
lock.builders_by_type_id.insert(TypeId::of::<A>(), A::build);
|
||||
lock.names_by_type_id
|
||||
.insert(TypeId::of::<A>(), name.clone());
|
||||
lock.all_names.push(name);
|
||||
}
|
||||
|
||||
/// Construct an action based on its name and optional JSON parameters sourced from the keymap.
|
||||
pub fn build_action_from_type(type_id: &TypeId) -> Result<Box<dyn Action>> {
|
||||
let lock = ACTION_REGISTRY.read();
|
||||
|
||||
let build_action = lock
|
||||
.builders_by_type_id
|
||||
let name = lock
|
||||
.names_by_type_id
|
||||
.get(type_id)
|
||||
.ok_or_else(|| anyhow!("no action type registered for {:?}", type_id))?;
|
||||
.ok_or_else(|| anyhow!("no action type registered for {:?}", type_id))?
|
||||
.clone();
|
||||
drop(lock);
|
||||
|
||||
(build_action)(None)
|
||||
build_action(&name, None)
|
||||
}
|
||||
|
||||
/// Construct an action based on its name and optional JSON parameters sourced from the keymap.
|
||||
|
||||
@@ -414,7 +414,6 @@ pub trait ElementInteractivity<V: 'static>: 'static {
|
||||
Box::new(move |_, key_down, context, phase, cx| {
|
||||
if phase == DispatchPhase::Bubble {
|
||||
let key_down = key_down.downcast_ref::<KeyDownEvent>().unwrap();
|
||||
dbg!(&context);
|
||||
if let KeyMatch::Some(action) =
|
||||
cx.match_keystroke(&global_id, &key_down.keystroke, context)
|
||||
{
|
||||
|
||||
@@ -44,19 +44,6 @@ impl KeyBinding {
|
||||
pending_keystrokes: &[Keystroke],
|
||||
contexts: &[&DispatchContext],
|
||||
) -> KeyMatch {
|
||||
let should_debug = self.keystrokes.len() == 1
|
||||
&& self.keystrokes[0].key == "p"
|
||||
&& self.keystrokes[0].modifiers.command == true
|
||||
&& self.keystrokes[0].modifiers.shift == true;
|
||||
|
||||
if false && should_debug {
|
||||
dbg!(
|
||||
&self.keystrokes,
|
||||
&pending_keystrokes,
|
||||
&contexts,
|
||||
&self.matches_context(contexts)
|
||||
);
|
||||
}
|
||||
if self.keystrokes.as_ref().starts_with(&pending_keystrokes)
|
||||
&& self.matches_context(contexts)
|
||||
{
|
||||
|
||||
@@ -46,7 +46,6 @@ impl KeyMatcher {
|
||||
keystroke: &Keystroke,
|
||||
context_stack: &[&DispatchContext],
|
||||
) -> KeyMatch {
|
||||
dbg!(keystroke, &context_stack);
|
||||
let keymap = self.keymap.lock();
|
||||
// Clear pending keystrokes if the keymap has changed since the last matched keystroke.
|
||||
if keymap.version() != self.keymap_version {
|
||||
|
||||
@@ -145,7 +145,7 @@ impl<V> Eq for WeakView<V> {}
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct AnyView {
|
||||
model: AnyModel,
|
||||
initialize: fn(&AnyView, &mut WindowContext) -> AnyBox,
|
||||
pub initialize: fn(&AnyView, &mut WindowContext) -> AnyBox,
|
||||
layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId,
|
||||
paint: fn(&AnyView, &mut AnyBox, &mut WindowContext),
|
||||
}
|
||||
@@ -184,6 +184,10 @@ impl AnyView {
|
||||
.compute_layout(layout_id, available_space);
|
||||
(self.paint)(self, &mut rendered_element, cx);
|
||||
}
|
||||
|
||||
pub(crate) fn draw_dispatch_stack(&self, cx: &mut WindowContext) {
|
||||
(self.initialize)(self, cx);
|
||||
}
|
||||
}
|
||||
|
||||
impl<V: 'static> Component<V> for AnyView {
|
||||
|
||||
+46
-16
@@ -228,7 +228,7 @@ pub(crate) struct Frame {
|
||||
key_matchers: HashMap<GlobalElementId, KeyMatcher>,
|
||||
mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyListener)>>,
|
||||
pub(crate) focus_listeners: Vec<AnyFocusListener>,
|
||||
key_dispatch_stack: Vec<KeyDispatchStackFrame>,
|
||||
pub(crate) key_dispatch_stack: Vec<KeyDispatchStackFrame>,
|
||||
freeze_key_dispatch_stack: bool,
|
||||
focus_parents_by_child: HashMap<FocusId, FocusId>,
|
||||
pub(crate) scene_builder: SceneBuilder,
|
||||
@@ -327,7 +327,7 @@ impl Window {
|
||||
/// find the focused element. We interleave key listeners with dispatch contexts so we can use the
|
||||
/// contexts when matching key events against the keymap. A key listener can be either an action
|
||||
/// handler or a [KeyDown] / [KeyUp] event listener.
|
||||
enum KeyDispatchStackFrame {
|
||||
pub(crate) enum KeyDispatchStackFrame {
|
||||
Listener {
|
||||
event_type: TypeId,
|
||||
listener: AnyKeyListener,
|
||||
@@ -407,6 +407,9 @@ impl<'a> WindowContext<'a> {
|
||||
}
|
||||
|
||||
self.window.focus = Some(handle.id);
|
||||
|
||||
// self.window.current_frame.key_dispatch_stack.clear()
|
||||
// self.window.root_view.initialize()
|
||||
self.app.push_effect(Effect::FocusChanged {
|
||||
window_handle: self.window.handle,
|
||||
focused: Some(handle.id),
|
||||
@@ -428,6 +431,14 @@ impl<'a> WindowContext<'a> {
|
||||
self.notify();
|
||||
}
|
||||
|
||||
pub fn dispatch_action(&mut self, action: Box<dyn Action>) {
|
||||
self.defer(|cx| {
|
||||
cx.app.propagate_event = true;
|
||||
let stack = cx.dispatch_stack();
|
||||
cx.dispatch_action_internal(action, &stack[..])
|
||||
})
|
||||
}
|
||||
|
||||
/// Schedules the given function to be run at the end of the current effect cycle, allowing entities
|
||||
/// that are currently on the stack to be returned to the app.
|
||||
pub fn defer(&mut self, f: impl FnOnce(&mut WindowContext) + 'static) {
|
||||
@@ -1055,6 +1066,26 @@ impl<'a> WindowContext<'a> {
|
||||
self.window.dirty = false;
|
||||
}
|
||||
|
||||
pub(crate) fn dispatch_stack(&mut self) -> Vec<KeyDispatchStackFrame> {
|
||||
let root_view = self.window.root_view.take().unwrap();
|
||||
let window = &mut *self.window;
|
||||
let mut spare_frame = Frame::default();
|
||||
mem::swap(&mut spare_frame, &mut window.previous_frame);
|
||||
|
||||
self.start_frame();
|
||||
|
||||
root_view.draw_dispatch_stack(self);
|
||||
|
||||
let window = &mut *self.window;
|
||||
// restore the old values of current and previous frame,
|
||||
// putting the new frame into spare_frame.
|
||||
mem::swap(&mut window.current_frame, &mut window.previous_frame);
|
||||
mem::swap(&mut spare_frame, &mut window.previous_frame);
|
||||
self.window.root_view = Some(root_view);
|
||||
|
||||
spare_frame.key_dispatch_stack
|
||||
}
|
||||
|
||||
/// Rotate the current frame and the previous frame, then clear the current frame.
|
||||
/// We repopulate all state in the current frame during each paint.
|
||||
fn start_frame(&mut self) {
|
||||
@@ -1197,7 +1228,7 @@ impl<'a> WindowContext<'a> {
|
||||
DispatchPhase::Capture,
|
||||
self,
|
||||
) {
|
||||
self.dispatch_action(action, &key_dispatch_stack[..ix]);
|
||||
self.dispatch_action_internal(action, &key_dispatch_stack[..ix]);
|
||||
}
|
||||
if !self.app.propagate_event {
|
||||
break;
|
||||
@@ -1224,7 +1255,10 @@ impl<'a> WindowContext<'a> {
|
||||
DispatchPhase::Bubble,
|
||||
self,
|
||||
) {
|
||||
self.dispatch_action(action, &key_dispatch_stack[..ix]);
|
||||
self.dispatch_action_internal(
|
||||
action,
|
||||
&key_dispatch_stack[..ix],
|
||||
);
|
||||
}
|
||||
|
||||
if !self.app.propagate_event {
|
||||
@@ -1296,11 +1330,9 @@ impl<'a> WindowContext<'a> {
|
||||
self.window.platform_window.prompt(level, msg, answers)
|
||||
}
|
||||
|
||||
pub fn available_actions(&mut self) -> Vec<Box<dyn Action>> {
|
||||
let key_dispatch_stack = &self.window.current_frame.key_dispatch_stack;
|
||||
let mut actions = Vec::new();
|
||||
dbg!(key_dispatch_stack.len());
|
||||
for frame in key_dispatch_stack {
|
||||
pub fn available_actions(&self) -> impl Iterator<Item = Box<dyn Action>> + '_ {
|
||||
let key_dispatch_stack = &self.window.previous_frame.key_dispatch_stack;
|
||||
key_dispatch_stack.iter().filter_map(|frame| {
|
||||
match frame {
|
||||
// todo!factor out a KeyDispatchStackFrame::Action
|
||||
KeyDispatchStackFrame::Listener {
|
||||
@@ -1308,21 +1340,19 @@ impl<'a> WindowContext<'a> {
|
||||
listener: _,
|
||||
} => {
|
||||
match build_action_from_type(event_type) {
|
||||
Ok(action) => {
|
||||
actions.push(action);
|
||||
}
|
||||
Ok(action) => Some(action),
|
||||
Err(err) => {
|
||||
dbg!(err);
|
||||
None
|
||||
} // we'll hit his if TypeId == KeyDown
|
||||
}
|
||||
}
|
||||
KeyDispatchStackFrame::Context(_) => {}
|
||||
KeyDispatchStackFrame::Context(_) => None,
|
||||
}
|
||||
}
|
||||
actions
|
||||
})
|
||||
}
|
||||
|
||||
fn dispatch_action(
|
||||
pub(crate) fn dispatch_action_internal(
|
||||
&mut self,
|
||||
action: Box<dyn Action>,
|
||||
dispatch_stack: &[KeyDispatchStackFrame],
|
||||
|
||||
Reference in New Issue
Block a user