WIP
This commit is contained in:
+173
-106
@@ -92,6 +92,104 @@ pub trait UpdateView {
|
||||
F: FnOnce(&mut T, &mut ViewContext<T>) -> S;
|
||||
}
|
||||
|
||||
pub trait Action: 'static + AnyAction {
|
||||
type Argument: 'static + Clone;
|
||||
|
||||
const NAME: &'static str;
|
||||
}
|
||||
|
||||
pub trait AnyAction {
|
||||
fn id(&self) -> TypeId;
|
||||
fn arg_as_any(&self) -> &dyn Any;
|
||||
fn boxed_clone(&self) -> Box<dyn AnyAction>;
|
||||
fn boxed_clone_as_any(&self) -> Box<dyn Any>;
|
||||
}
|
||||
|
||||
impl Action for () {
|
||||
type Argument = ();
|
||||
|
||||
const NAME: &'static str = "()";
|
||||
}
|
||||
|
||||
impl AnyAction for () {
|
||||
fn id(&self) -> TypeId {
|
||||
TypeId::of::<()>()
|
||||
}
|
||||
|
||||
fn arg_as_any(&self) -> &dyn Any {
|
||||
&()
|
||||
}
|
||||
|
||||
fn boxed_clone(&self) -> Box<dyn AnyAction> {
|
||||
Box::new(())
|
||||
}
|
||||
|
||||
fn boxed_clone_as_any(&self) -> Box<dyn Any> {
|
||||
Box::new(())
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! action {
|
||||
($name:ident, $arg:ty) => {
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct $name(pub $arg);
|
||||
|
||||
impl $crate::Action for $name {
|
||||
type Argument = $arg;
|
||||
|
||||
const NAME: &'static str = stringify!($name);
|
||||
}
|
||||
|
||||
impl $crate::AnyAction for $name {
|
||||
fn id(&self) -> std::any::TypeId {
|
||||
std::any::TypeId::of::<$name>()
|
||||
}
|
||||
|
||||
fn arg_as_any(&self) -> &dyn std::any::Any {
|
||||
&self.0
|
||||
}
|
||||
|
||||
fn boxed_clone(&self) -> Box<dyn $crate::AnyAction> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn boxed_clone_as_any(&self) -> Box<dyn std::any::Any> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
($name:ident) => {
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct $name;
|
||||
|
||||
impl $crate::Action for $name {
|
||||
type Argument = ();
|
||||
|
||||
const NAME: &'static str = stringify!($name);
|
||||
}
|
||||
|
||||
impl $crate::AnyAction for $name {
|
||||
fn id(&self) -> std::any::TypeId {
|
||||
std::any::TypeId::of::<$name>()
|
||||
}
|
||||
|
||||
fn arg_as_any(&self) -> &dyn std::any::Any {
|
||||
&()
|
||||
}
|
||||
|
||||
fn boxed_clone(&self) -> Box<dyn $crate::AnyAction> {
|
||||
Box::new(())
|
||||
}
|
||||
|
||||
fn boxed_clone_as_any(&self) -> Box<dyn std::any::Any> {
|
||||
Box::new(())
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub struct Menu<'a> {
|
||||
pub name: &'a str,
|
||||
pub items: Vec<MenuItem<'a>>,
|
||||
@@ -101,8 +199,7 @@ pub enum MenuItem<'a> {
|
||||
Action {
|
||||
name: &'a str,
|
||||
keystroke: Option<&'a str>,
|
||||
action: &'a str,
|
||||
arg: Option<Box<dyn Any + 'static>>,
|
||||
action: Box<dyn AnyAction>,
|
||||
},
|
||||
Separator,
|
||||
}
|
||||
@@ -136,19 +233,19 @@ impl App {
|
||||
))));
|
||||
|
||||
let cx = app.0.clone();
|
||||
foreground_platform.on_menu_command(Box::new(move |command, arg| {
|
||||
foreground_platform.on_menu_command(Box::new(move |action| {
|
||||
let mut cx = cx.borrow_mut();
|
||||
if let Some(key_window_id) = cx.cx.platform.key_window_id() {
|
||||
if let Some((presenter, _)) = cx.presenters_and_platform_windows.get(&key_window_id)
|
||||
{
|
||||
let presenter = presenter.clone();
|
||||
let path = presenter.borrow().dispatch_path(cx.as_ref());
|
||||
cx.dispatch_action_any(key_window_id, &path, command, arg.unwrap_or(&()));
|
||||
cx.dispatch_action_any(key_window_id, &path, action);
|
||||
} else {
|
||||
cx.dispatch_global_action_any(command, arg.unwrap_or(&()));
|
||||
cx.dispatch_global_action_any(action);
|
||||
}
|
||||
} else {
|
||||
cx.dispatch_global_action_any(command, arg.unwrap_or(&()));
|
||||
cx.dispatch_global_action_any(action);
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -258,23 +355,19 @@ impl TestAppContext {
|
||||
cx
|
||||
}
|
||||
|
||||
pub fn dispatch_action<T: 'static + Any>(
|
||||
pub fn dispatch_action<A: Action>(
|
||||
&self,
|
||||
window_id: usize,
|
||||
responder_chain: Vec<usize>,
|
||||
name: &str,
|
||||
arg: T,
|
||||
action: A,
|
||||
) {
|
||||
self.cx.borrow_mut().dispatch_action_any(
|
||||
window_id,
|
||||
&responder_chain,
|
||||
name,
|
||||
Box::new(arg).as_ref(),
|
||||
);
|
||||
self.cx
|
||||
.borrow_mut()
|
||||
.dispatch_action_any(window_id, &responder_chain, &action);
|
||||
}
|
||||
|
||||
pub fn dispatch_global_action<T: 'static + Any>(&self, name: &str, arg: T) {
|
||||
self.cx.borrow_mut().dispatch_global_action(name, arg);
|
||||
pub fn dispatch_global_action<A: Action>(&self, action: A) {
|
||||
self.cx.borrow_mut().dispatch_global_action(action);
|
||||
}
|
||||
|
||||
pub fn dispatch_keystroke(
|
||||
@@ -563,17 +656,17 @@ impl ReadViewWith for TestAppContext {
|
||||
}
|
||||
|
||||
type ActionCallback =
|
||||
dyn FnMut(&mut dyn AnyView, &dyn Any, &mut MutableAppContext, usize, usize) -> bool;
|
||||
dyn FnMut(&mut dyn AnyView, &dyn AnyAction, &mut MutableAppContext, usize, usize) -> bool;
|
||||
|
||||
type GlobalActionCallback = dyn FnMut(&dyn Any, &mut MutableAppContext);
|
||||
type GlobalActionCallback = dyn FnMut(&dyn AnyAction, &mut MutableAppContext);
|
||||
|
||||
pub struct MutableAppContext {
|
||||
weak_self: Option<rc::Weak<RefCell<Self>>>,
|
||||
foreground_platform: Rc<dyn platform::ForegroundPlatform>,
|
||||
assets: Arc<AssetCache>,
|
||||
cx: AppContext,
|
||||
actions: HashMap<TypeId, HashMap<String, Vec<Box<ActionCallback>>>>,
|
||||
global_actions: HashMap<String, Vec<Box<GlobalActionCallback>>>,
|
||||
actions: HashMap<TypeId, HashMap<TypeId, Vec<Box<ActionCallback>>>>,
|
||||
global_actions: HashMap<TypeId, Vec<Box<GlobalActionCallback>>>,
|
||||
keystroke_matcher: keymap::Matcher,
|
||||
next_entity_id: usize,
|
||||
next_window_id: usize,
|
||||
@@ -663,69 +756,53 @@ impl MutableAppContext {
|
||||
.map(|debug_elements| debug_elements(&self.cx))
|
||||
}
|
||||
|
||||
pub fn add_action<S, V, T, F>(&mut self, name: S, mut handler: F)
|
||||
pub fn add_action<A, V, F>(&mut self, mut handler: F)
|
||||
where
|
||||
S: Into<String>,
|
||||
A: Action,
|
||||
V: View,
|
||||
T: Any,
|
||||
F: 'static + FnMut(&mut V, &T, &mut ViewContext<V>),
|
||||
F: 'static + FnMut(&mut V, &A, &mut ViewContext<V>),
|
||||
{
|
||||
let name = name.into();
|
||||
let name_clone = name.clone();
|
||||
let handler = Box::new(
|
||||
move |view: &mut dyn AnyView,
|
||||
arg: &dyn Any,
|
||||
action: &dyn AnyAction,
|
||||
cx: &mut MutableAppContext,
|
||||
window_id: usize,
|
||||
view_id: usize| {
|
||||
match arg.downcast_ref() {
|
||||
Some(arg) => {
|
||||
let mut cx = ViewContext::new(cx, window_id, view_id);
|
||||
handler(
|
||||
view.as_any_mut()
|
||||
.downcast_mut()
|
||||
.expect("downcast is type safe"),
|
||||
arg,
|
||||
&mut cx,
|
||||
);
|
||||
cx.halt_action_dispatch
|
||||
}
|
||||
None => {
|
||||
log::error!("Could not downcast argument for action {}", name_clone);
|
||||
false
|
||||
}
|
||||
}
|
||||
let arg = action.arg_as_any().downcast_ref().unwrap();
|
||||
let mut cx = ViewContext::new(cx, window_id, view_id);
|
||||
handler(
|
||||
view.as_any_mut()
|
||||
.downcast_mut()
|
||||
.expect("downcast is type safe"),
|
||||
arg,
|
||||
&mut cx,
|
||||
);
|
||||
cx.halt_action_dispatch
|
||||
},
|
||||
);
|
||||
|
||||
self.actions
|
||||
.entry(TypeId::of::<V>())
|
||||
.or_default()
|
||||
.entry(name)
|
||||
.entry(TypeId::of::<A>())
|
||||
.or_default()
|
||||
.push(handler);
|
||||
}
|
||||
|
||||
pub fn add_global_action<S, T, F>(&mut self, name: S, mut handler: F)
|
||||
pub fn add_global_action<A, F>(&mut self, mut handler: F)
|
||||
where
|
||||
S: Into<String>,
|
||||
T: 'static + Any,
|
||||
F: 'static + FnMut(&T, &mut MutableAppContext),
|
||||
A: Action,
|
||||
F: 'static + FnMut(&A, &mut MutableAppContext),
|
||||
{
|
||||
let name = name.into();
|
||||
let name_clone = name.clone();
|
||||
let handler = Box::new(move |arg: &dyn Any, cx: &mut MutableAppContext| {
|
||||
if let Some(arg) = arg.downcast_ref() {
|
||||
handler(arg, cx);
|
||||
} else {
|
||||
log::error!(
|
||||
"Could not downcast argument for global action {}",
|
||||
name_clone
|
||||
);
|
||||
}
|
||||
let handler = Box::new(move |action: &dyn AnyAction, cx: &mut MutableAppContext| {
|
||||
let arg = action.arg_as_any().downcast_ref().unwrap();
|
||||
handler(arg, cx);
|
||||
});
|
||||
|
||||
self.global_actions.entry(name).or_default().push(handler);
|
||||
self.global_actions
|
||||
.entry(TypeId::of::<A>())
|
||||
.or_default()
|
||||
.push(handler);
|
||||
}
|
||||
|
||||
pub fn window_ids(&self) -> impl Iterator<Item = usize> + '_ {
|
||||
@@ -838,22 +915,20 @@ impl MutableAppContext {
|
||||
self.pending_effects.extend(notifications);
|
||||
}
|
||||
|
||||
pub fn dispatch_action<T: 'static + Any>(
|
||||
pub fn dispatch_action<A: Action>(
|
||||
&mut self,
|
||||
window_id: usize,
|
||||
responder_chain: Vec<usize>,
|
||||
name: &str,
|
||||
arg: T,
|
||||
action: &A,
|
||||
) {
|
||||
self.dispatch_action_any(window_id, &responder_chain, name, Box::new(arg).as_ref());
|
||||
self.dispatch_action_any(window_id, &responder_chain, action);
|
||||
}
|
||||
|
||||
pub(crate) fn dispatch_action_any(
|
||||
&mut self,
|
||||
window_id: usize,
|
||||
path: &[usize],
|
||||
name: &str,
|
||||
arg: &dyn Any,
|
||||
action: &dyn AnyAction,
|
||||
) -> bool {
|
||||
self.pending_flushes += 1;
|
||||
let mut halted_dispatch = false;
|
||||
@@ -865,10 +940,11 @@ impl MutableAppContext {
|
||||
if let Some((name, mut handlers)) = self
|
||||
.actions
|
||||
.get_mut(&type_id)
|
||||
.and_then(|h| h.remove_entry(name))
|
||||
.and_then(|h| h.remove_entry(&action.id()))
|
||||
{
|
||||
for handler in handlers.iter_mut().rev() {
|
||||
let halt_dispatch = handler(view.as_mut(), arg, self, window_id, *view_id);
|
||||
let halt_dispatch =
|
||||
handler(view.as_mut(), action, self, window_id, *view_id);
|
||||
if halt_dispatch {
|
||||
halted_dispatch = true;
|
||||
break;
|
||||
@@ -889,22 +965,22 @@ impl MutableAppContext {
|
||||
}
|
||||
|
||||
if !halted_dispatch {
|
||||
self.dispatch_global_action_any(name, arg);
|
||||
self.dispatch_global_action_any(action);
|
||||
}
|
||||
|
||||
self.flush_effects();
|
||||
halted_dispatch
|
||||
}
|
||||
|
||||
pub fn dispatch_global_action<T: 'static + Any>(&mut self, name: &str, arg: T) {
|
||||
self.dispatch_global_action_any(name, Box::new(arg).as_ref());
|
||||
pub fn dispatch_global_action<A: Action>(&mut self, action: A) {
|
||||
self.dispatch_global_action_any(&action);
|
||||
}
|
||||
|
||||
fn dispatch_global_action_any(&mut self, name: &str, arg: &dyn Any) {
|
||||
if let Some((name, mut handlers)) = self.global_actions.remove_entry(name) {
|
||||
fn dispatch_global_action_any(&mut self, action: &dyn AnyAction) {
|
||||
if let Some((name, mut handlers)) = self.global_actions.remove_entry(&action.id()) {
|
||||
self.pending_flushes += 1;
|
||||
for handler in handlers.iter_mut().rev() {
|
||||
handler(arg, self);
|
||||
handler(action, self);
|
||||
}
|
||||
self.global_actions.insert(name, handlers);
|
||||
self.flush_effects();
|
||||
@@ -943,13 +1019,9 @@ impl MutableAppContext {
|
||||
{
|
||||
MatchResult::None => {}
|
||||
MatchResult::Pending => pending = true,
|
||||
MatchResult::Action { name, arg } => {
|
||||
if self.dispatch_action_any(
|
||||
window_id,
|
||||
&responder_chain[0..=i],
|
||||
&name,
|
||||
arg.as_ref().map(|arg| arg.as_ref()).unwrap_or(&()),
|
||||
) {
|
||||
MatchResult::Action(action) => {
|
||||
if self.dispatch_action_any(window_id, &responder_chain[0..=i], action.as_ref())
|
||||
{
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
@@ -3575,31 +3647,29 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
struct ActionArg {
|
||||
foo: String,
|
||||
}
|
||||
action!(Action, &'static str);
|
||||
|
||||
let actions = Rc::new(RefCell::new(Vec::new()));
|
||||
|
||||
let actions_clone = actions.clone();
|
||||
cx.add_global_action("action", move |_: &ActionArg, _: &mut MutableAppContext| {
|
||||
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
|
||||
actions_clone.borrow_mut().push("global a".to_string());
|
||||
});
|
||||
|
||||
let actions_clone = actions.clone();
|
||||
cx.add_global_action("action", move |_: &ActionArg, _: &mut MutableAppContext| {
|
||||
cx.add_global_action(move |_: &Action, _: &mut MutableAppContext| {
|
||||
actions_clone.borrow_mut().push("global b".to_string());
|
||||
});
|
||||
|
||||
let actions_clone = actions.clone();
|
||||
cx.add_action("action", move |view: &mut ViewA, arg: &ActionArg, cx| {
|
||||
assert_eq!(arg.foo, "bar");
|
||||
cx.add_action(move |view: &mut ViewA, action: &Action, cx| {
|
||||
assert_eq!(action.0, "bar");
|
||||
cx.propagate_action();
|
||||
actions_clone.borrow_mut().push(format!("{} a", view.id));
|
||||
});
|
||||
|
||||
let actions_clone = actions.clone();
|
||||
cx.add_action("action", move |view: &mut ViewA, _: &ActionArg, cx| {
|
||||
cx.add_action(move |view: &mut ViewA, _: &Action, cx| {
|
||||
if view.id != 1 {
|
||||
cx.propagate_action();
|
||||
}
|
||||
@@ -3607,13 +3677,13 @@ mod tests {
|
||||
});
|
||||
|
||||
let actions_clone = actions.clone();
|
||||
cx.add_action("action", move |view: &mut ViewB, _: &ActionArg, cx| {
|
||||
cx.add_action(move |view: &mut ViewB, action: &Action, cx| {
|
||||
cx.propagate_action();
|
||||
actions_clone.borrow_mut().push(format!("{} c", view.id));
|
||||
});
|
||||
|
||||
let actions_clone = actions.clone();
|
||||
cx.add_action("action", move |view: &mut ViewB, _: &ActionArg, cx| {
|
||||
cx.add_action(move |view: &mut ViewB, action: &Action, cx| {
|
||||
cx.propagate_action();
|
||||
actions_clone.borrow_mut().push(format!("{} d", view.id));
|
||||
});
|
||||
@@ -3626,8 +3696,7 @@ mod tests {
|
||||
cx.dispatch_action(
|
||||
window_id,
|
||||
vec![view_1.id(), view_2.id(), view_3.id(), view_4.id()],
|
||||
"action",
|
||||
ActionArg { foo: "bar".into() },
|
||||
&Action("bar"),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
@@ -3640,8 +3709,7 @@ mod tests {
|
||||
cx.dispatch_action(
|
||||
window_id,
|
||||
vec![view_2.id(), view_3.id(), view_4.id()],
|
||||
"action",
|
||||
ActionArg { foo: "bar".into() },
|
||||
&Action("bar"),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
@@ -3654,10 +3722,7 @@ mod tests {
|
||||
fn test_dispatch_keystroke(cx: &mut MutableAppContext) {
|
||||
use std::cell::Cell;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ActionArg {
|
||||
key: String,
|
||||
}
|
||||
action!(Action, &'static str);
|
||||
|
||||
struct View {
|
||||
id: usize,
|
||||
@@ -3704,16 +3769,18 @@ mod tests {
|
||||
|
||||
// This keymap's only binding dispatches an action on view 2 because that view will have
|
||||
// "a" and "b" in its context, but not "c".
|
||||
let binding = keymap::Binding::new("a", "action", Some("a && b && !c"))
|
||||
.with_arg(ActionArg { key: "a".into() });
|
||||
cx.add_bindings(vec![binding]);
|
||||
cx.add_bindings(vec![keymap::Binding::new(
|
||||
"a",
|
||||
Action("a"),
|
||||
Some("a && b && !c"),
|
||||
)]);
|
||||
|
||||
let handled_action = Rc::new(Cell::new(false));
|
||||
let handled_action_clone = handled_action.clone();
|
||||
cx.add_action("action", move |view: &mut View, arg: &ActionArg, _| {
|
||||
cx.add_action(move |view: &mut View, action: &Action, _| {
|
||||
handled_action_clone.set(true);
|
||||
assert_eq!(view.id, 2);
|
||||
assert_eq!(arg.key, "a");
|
||||
assert_eq!(action.0, "a");
|
||||
});
|
||||
|
||||
cx.dispatch_keystroke(
|
||||
|
||||
+36
-49
@@ -5,6 +5,8 @@ use std::{
|
||||
};
|
||||
use tree_sitter::{Language, Node, Parser};
|
||||
|
||||
use crate::{Action, AnyAction};
|
||||
|
||||
extern "C" {
|
||||
fn tree_sitter_context_predicate() -> Language;
|
||||
}
|
||||
@@ -24,8 +26,7 @@ pub struct Keymap(Vec<Binding>);
|
||||
|
||||
pub struct Binding {
|
||||
keystrokes: Vec<Keystroke>,
|
||||
action: String,
|
||||
action_arg: Option<Box<dyn ActionArg>>,
|
||||
action: Box<dyn AnyAction>,
|
||||
context: Option<ContextPredicate>,
|
||||
}
|
||||
|
||||
@@ -70,10 +71,7 @@ where
|
||||
pub enum MatchResult {
|
||||
None,
|
||||
Pending,
|
||||
Action {
|
||||
name: String,
|
||||
arg: Option<Box<dyn Any>>,
|
||||
},
|
||||
Action(Box<dyn AnyAction>),
|
||||
}
|
||||
|
||||
impl Matcher {
|
||||
@@ -117,10 +115,7 @@ impl Matcher {
|
||||
{
|
||||
if binding.keystrokes.len() == pending.keystrokes.len() {
|
||||
self.pending.remove(&view_id);
|
||||
return MatchResult::Action {
|
||||
name: binding.action.clone(),
|
||||
arg: binding.action_arg.as_ref().map(|arg| (*arg).boxed_clone()),
|
||||
};
|
||||
return MatchResult::Action(binding.action.boxed_clone());
|
||||
} else {
|
||||
retain_pending = true;
|
||||
pending.context = Some(cx.clone());
|
||||
@@ -153,19 +148,26 @@ impl Keymap {
|
||||
}
|
||||
}
|
||||
|
||||
mod menu {
|
||||
use crate::action;
|
||||
|
||||
action!(SelectPrev);
|
||||
action!(SelectNext);
|
||||
}
|
||||
|
||||
impl Default for Keymap {
|
||||
fn default() -> Self {
|
||||
Self(vec![
|
||||
Binding::new("up", "menu:select_prev", Some("menu")),
|
||||
Binding::new("ctrl-p", "menu:select_prev", Some("menu")),
|
||||
Binding::new("down", "menu:select_next", Some("menu")),
|
||||
Binding::new("ctrl-n", "menu:select_next", Some("menu")),
|
||||
Binding::new("up", menu::SelectPrev, Some("menu")),
|
||||
Binding::new("ctrl-p", menu::SelectPrev, Some("menu")),
|
||||
Binding::new("down", menu::SelectNext, Some("menu")),
|
||||
Binding::new("ctrl-n", menu::SelectNext, Some("menu")),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
impl Binding {
|
||||
pub fn new<S: Into<String>>(keystrokes: &str, action: S, context: Option<&str>) -> Self {
|
||||
pub fn new<A: Action>(keystrokes: &str, action: A, context: Option<&str>) -> Self {
|
||||
let context = if let Some(context) = context {
|
||||
Some(ContextPredicate::parse(context).unwrap())
|
||||
} else {
|
||||
@@ -177,16 +179,10 @@ impl Binding {
|
||||
.split_whitespace()
|
||||
.map(|key| Keystroke::parse(key).unwrap())
|
||||
.collect(),
|
||||
action: action.into(),
|
||||
action_arg: None,
|
||||
action: Box::new(action),
|
||||
context,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_arg<T: 'static + Any + Clone>(mut self, arg: T) -> Self {
|
||||
self.action_arg = Some(Box::new(arg));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Keystroke {
|
||||
@@ -328,6 +324,8 @@ impl ContextPredicate {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::action;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
@@ -417,15 +415,19 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_matcher() -> anyhow::Result<()> {
|
||||
action!(A, &'static str);
|
||||
action!(B);
|
||||
action!(Ab);
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
struct ActionArg {
|
||||
a: &'static str,
|
||||
}
|
||||
|
||||
let keymap = Keymap(vec![
|
||||
Binding::new("a", "a", Some("a")).with_arg(ActionArg { a: "b" }),
|
||||
Binding::new("b", "b", Some("a")),
|
||||
Binding::new("a b", "a_b", Some("a || b")),
|
||||
Binding::new("a", A("x"), Some("a")),
|
||||
Binding::new("b", B, Some("a")),
|
||||
Binding::new("a b", Ab, Some("a || b")),
|
||||
]);
|
||||
|
||||
let mut ctx_a = Context::default();
|
||||
@@ -437,31 +439,19 @@ mod tests {
|
||||
let mut matcher = Matcher::new(keymap);
|
||||
|
||||
// Basic match
|
||||
assert_eq!(
|
||||
matcher.test_keystroke("a", 1, &ctx_a),
|
||||
Some(("a".to_string(), Some(ActionArg { a: "b" })))
|
||||
);
|
||||
assert_eq!(matcher.test_keystroke("a", 1, &ctx_a), Some(A("x")));
|
||||
|
||||
// Multi-keystroke match
|
||||
assert_eq!(matcher.test_keystroke::<()>("a", 1, &ctx_b), None);
|
||||
assert_eq!(
|
||||
matcher.test_keystroke::<()>("b", 1, &ctx_b),
|
||||
Some(("a_b".to_string(), None))
|
||||
);
|
||||
assert_eq!(matcher.test_keystroke("b", 1, &ctx_b), Some(Ab));
|
||||
|
||||
// Failed matches don't interfere with matching subsequent keys
|
||||
assert_eq!(matcher.test_keystroke::<()>("x", 1, &ctx_a), None);
|
||||
assert_eq!(
|
||||
matcher.test_keystroke("a", 1, &ctx_a),
|
||||
Some(("a".to_string(), Some(ActionArg { a: "b" })))
|
||||
);
|
||||
assert_eq!(matcher.test_keystroke("a", 1, &ctx_a), Some(A("x")));
|
||||
|
||||
// Pending keystrokes are cleared when the context changes
|
||||
assert_eq!(matcher.test_keystroke::<()>("a", 1, &ctx_b), None);
|
||||
assert_eq!(
|
||||
matcher.test_keystroke::<()>("b", 1, &ctx_a),
|
||||
Some(("b".to_string(), None))
|
||||
);
|
||||
assert_eq!(matcher.test_keystroke("b", 1, &ctx_a), Some(B));
|
||||
|
||||
let mut ctx_c = Context::default();
|
||||
ctx_c.set.insert("c".into());
|
||||
@@ -469,25 +459,22 @@ mod tests {
|
||||
// Pending keystrokes are maintained per-view
|
||||
assert_eq!(matcher.test_keystroke::<()>("a", 1, &ctx_b), None);
|
||||
assert_eq!(matcher.test_keystroke::<()>("a", 2, &ctx_c), None);
|
||||
assert_eq!(
|
||||
matcher.test_keystroke::<()>("b", 1, &ctx_b),
|
||||
Some(("a_b".to_string(), None))
|
||||
);
|
||||
assert_eq!(matcher.test_keystroke("b", 1, &ctx_b), Some(Ab));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Matcher {
|
||||
fn test_keystroke<A: Any + Clone>(
|
||||
fn test_keystroke<A: Action>(
|
||||
&mut self,
|
||||
keystroke: &str,
|
||||
view_id: usize,
|
||||
cx: &Context,
|
||||
) -> Option<(String, Option<A>)> {
|
||||
if let MatchResult::Action { name, arg } =
|
||||
) -> Option<A> {
|
||||
if let MatchResult::Action(action) =
|
||||
self.push_keystroke(Keystroke::parse(keystroke).unwrap(), view_id, cx)
|
||||
{
|
||||
Some((name, arg.and_then(|arg| arg.downcast_ref::<A>().cloned())))
|
||||
Some(*action.boxed_clone_as_any().downcast().unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ use crate::{
|
||||
vector::{vec2f, Vector2F},
|
||||
},
|
||||
text_layout::LineLayout,
|
||||
ClipboardItem, Menu, Scene,
|
||||
AnyAction, ClipboardItem, Menu, Scene,
|
||||
};
|
||||
use async_task::Runnable;
|
||||
pub use event::Event;
|
||||
@@ -56,7 +56,7 @@ pub(crate) trait ForegroundPlatform {
|
||||
fn on_open_files(&self, callback: Box<dyn FnMut(Vec<PathBuf>)>);
|
||||
fn run(&self, on_finish_launching: Box<dyn FnOnce() -> ()>);
|
||||
|
||||
fn on_menu_command(&self, callback: Box<dyn FnMut(&str, Option<&dyn Any>)>);
|
||||
fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn AnyAction)>);
|
||||
fn set_menus(&self, menus: Vec<Menu>);
|
||||
fn prompt_for_paths(
|
||||
&self,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use super::{BoolExt as _, Dispatcher, FontSystem, Window};
|
||||
use crate::{executor, keymap::Keystroke, platform, ClipboardItem, Event, Menu, MenuItem};
|
||||
use crate::{
|
||||
executor, keymap::Keystroke, platform, AnyAction, ClipboardItem, Event, Menu, MenuItem,
|
||||
};
|
||||
use block::ConcreteBlock;
|
||||
use cocoa::{
|
||||
appkit::{
|
||||
@@ -90,10 +92,10 @@ pub struct MacForegroundPlatformState {
|
||||
become_active: Option<Box<dyn FnMut()>>,
|
||||
resign_active: Option<Box<dyn FnMut()>>,
|
||||
event: Option<Box<dyn FnMut(crate::Event) -> bool>>,
|
||||
menu_command: Option<Box<dyn FnMut(&str, Option<&dyn Any>)>>,
|
||||
menu_command: Option<Box<dyn FnMut(&dyn AnyAction)>>,
|
||||
open_files: Option<Box<dyn FnMut(Vec<PathBuf>)>>,
|
||||
finish_launching: Option<Box<dyn FnOnce() -> ()>>,
|
||||
menu_actions: Vec<(String, Option<Box<dyn Any>>)>,
|
||||
menu_actions: Vec<Box<dyn AnyAction>>,
|
||||
}
|
||||
|
||||
impl MacForegroundPlatform {
|
||||
@@ -121,7 +123,6 @@ impl MacForegroundPlatform {
|
||||
name,
|
||||
keystroke,
|
||||
action,
|
||||
arg,
|
||||
} => {
|
||||
if let Some(keystroke) = keystroke {
|
||||
let keystroke = Keystroke::parse(keystroke).unwrap_or_else(|err| {
|
||||
@@ -162,7 +163,7 @@ impl MacForegroundPlatform {
|
||||
|
||||
let tag = state.menu_actions.len() as NSInteger;
|
||||
let _: () = msg_send![item, setTag: tag];
|
||||
state.menu_actions.push((action.to_string(), arg));
|
||||
state.menu_actions.push(action);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,7 +216,7 @@ impl platform::ForegroundPlatform for MacForegroundPlatform {
|
||||
}
|
||||
}
|
||||
|
||||
fn on_menu_command(&self, callback: Box<dyn FnMut(&str, Option<&dyn Any>)>) {
|
||||
fn on_menu_command(&self, callback: Box<dyn FnMut(&dyn AnyAction)>) {
|
||||
self.0.borrow_mut().menu_command = Some(callback);
|
||||
}
|
||||
|
||||
@@ -623,8 +624,8 @@ extern "C" fn handle_menu_item(this: &mut Object, _: Sel, item: id) {
|
||||
if let Some(mut callback) = platform.menu_command.take() {
|
||||
let tag: NSInteger = msg_send![item, tag];
|
||||
let index = tag as usize;
|
||||
if let Some((action, arg)) = platform.menu_actions.get(index) {
|
||||
callback(action, arg.as_ref().map(Box::as_ref));
|
||||
if let Some(action) = platform.menu_actions.get(index) {
|
||||
callback(action.as_ref());
|
||||
}
|
||||
platform.menu_command = Some(callback);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::ClipboardItem;
|
||||
use crate::{AnyAction, ClipboardItem};
|
||||
use parking_lot::Mutex;
|
||||
use pathfinder_geometry::vector::Vector2F;
|
||||
use std::{
|
||||
@@ -62,7 +62,7 @@ impl super::ForegroundPlatform for ForegroundPlatform {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn on_menu_command(&self, _: Box<dyn FnMut(&str, Option<&dyn Any>)>) {}
|
||||
fn on_menu_command(&self, _: Box<dyn FnMut(&dyn AnyAction)>) {}
|
||||
|
||||
fn set_menus(&self, _: Vec<crate::Menu>) {}
|
||||
|
||||
|
||||
+11
-19
@@ -5,12 +5,11 @@ use crate::{
|
||||
json::{self, ToJson},
|
||||
platform::Event,
|
||||
text_layout::TextLayoutCache,
|
||||
AssetCache, ElementBox, Scene,
|
||||
Action, AnyAction, AssetCache, ElementBox, Scene,
|
||||
};
|
||||
use pathfinder_geometry::vector::{vec2f, Vector2F};
|
||||
use serde_json::json;
|
||||
use std::{
|
||||
any::Any,
|
||||
collections::{HashMap, HashSet},
|
||||
sync::Arc,
|
||||
};
|
||||
@@ -144,7 +143,7 @@ impl Presenter {
|
||||
|
||||
let mut event_cx = EventContext {
|
||||
rendered_views: &mut self.rendered_views,
|
||||
actions: Default::default(),
|
||||
dispatched_actions: Default::default(),
|
||||
font_cache: &self.font_cache,
|
||||
text_layout_cache: &self.text_layout_cache,
|
||||
view_stack: Default::default(),
|
||||
@@ -154,18 +153,13 @@ impl Presenter {
|
||||
event_cx.dispatch_event(root_view_id, &event);
|
||||
|
||||
let invalidated_views = event_cx.invalidated_views;
|
||||
let actions = event_cx.actions;
|
||||
let dispatch_directives = event_cx.dispatched_actions;
|
||||
|
||||
for view_id in invalidated_views {
|
||||
cx.notify_view(self.window_id, view_id);
|
||||
}
|
||||
for action in actions {
|
||||
cx.dispatch_action_any(
|
||||
self.window_id,
|
||||
&action.path,
|
||||
action.name,
|
||||
action.arg.as_ref(),
|
||||
);
|
||||
for directive in dispatch_directives {
|
||||
cx.dispatch_action_any(self.window_id, &directive.path, directive.action.as_ref());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,10 +177,9 @@ impl Presenter {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ActionToDispatch {
|
||||
pub struct DispatchDirective {
|
||||
pub path: Vec<usize>,
|
||||
pub name: &'static str,
|
||||
pub arg: Box<dyn Any>,
|
||||
pub action: Box<dyn AnyAction>,
|
||||
}
|
||||
|
||||
pub struct LayoutContext<'a> {
|
||||
@@ -249,7 +242,7 @@ impl<'a> PaintContext<'a> {
|
||||
|
||||
pub struct EventContext<'a> {
|
||||
rendered_views: &'a mut HashMap<usize, ElementBox>,
|
||||
actions: Vec<ActionToDispatch>,
|
||||
dispatched_actions: Vec<DispatchDirective>,
|
||||
pub font_cache: &'a FontCache,
|
||||
pub text_layout_cache: &'a TextLayoutCache,
|
||||
pub app: &'a mut MutableAppContext,
|
||||
@@ -270,11 +263,10 @@ impl<'a> EventContext<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn dispatch_action<A: 'static + Any>(&mut self, name: &'static str, arg: A) {
|
||||
self.actions.push(ActionToDispatch {
|
||||
pub fn dispatch_action<A: Action>(&mut self, action: A) {
|
||||
self.dispatched_actions.push(DispatchDirective {
|
||||
path: self.view_stack.clone(),
|
||||
name,
|
||||
arg: Box::new(arg),
|
||||
action: Box::new(action),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user