Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Improve `cx.on_action` method to support chaining (#44353)

Jason Leeb633de66f793
diff --git a/crates/client/src/client.rs b/crates/client/src/client.rs
index 6d6d229b940..14311d6bbf5 100644
--- a/crates/client/src/client.rs
+++ b/crates/client/src/client.rs
@@ -150,9 +150,8 @@ pub fn init(client: &Arc<Client>, cx: &mut App) {
.detach_and_log_err(cx);
}
}
- });
-
- cx.on_action({
+ })
+ .on_action({
let client = client.clone();
move |_: &SignOut, cx| {
if let Some(client) = client.upgrade() {
@@ -162,9 +161,8 @@ pub fn init(client: &Arc<Client>, cx: &mut App) {
.detach();
}
}
- });
-
- cx.on_action({
+ })
+ .on_action({
let client = client;
move |_: &Reconnect, cx| {
if let Some(client) = client.upgrade() {
diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs
index 923b5dc1540..29be039cdd1 100644
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@ -351,8 +351,8 @@ pub fn init(cx: &mut App) {
)
.detach();
}
- });
- cx.on_action(move |_: &workspace::NewWindow, cx| {
+ })
+ .on_action(move |_: &workspace::NewWindow, cx| {
let app_state = workspace::AppState::global(cx);
if let Some(app_state) = app_state.upgrade() {
workspace::open_new(
diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs
index f7c57ef015e..aa1acae33b8 100644
--- a/crates/gpui/src/app.rs
+++ b/crates/gpui/src/app.rs
@@ -1777,7 +1777,10 @@ impl App {
/// Register a global handler for actions invoked via the keyboard. These handlers are run at
/// the end of the bubble phase for actions, and so will only be invoked if there are no other
/// handlers or if they called `cx.propagate()`.
- pub fn on_action<A: Action>(&mut self, listener: impl Fn(&A, &mut Self) + 'static) {
+ pub fn on_action<A: Action>(
+ &mut self,
+ listener: impl Fn(&A, &mut Self) + 'static,
+ ) -> &mut Self {
self.global_action_listeners
.entry(TypeId::of::<A>())
.or_default()
@@ -1787,6 +1790,7 @@ impl App {
listener(action, cx)
}
}));
+ self
}
/// Event handlers propagate events by default. Call this method to stop dispatching to
diff --git a/crates/keymap_editor/src/keymap_editor.rs b/crates/keymap_editor/src/keymap_editor.rs
index 113d5026eb8..e81b1077c70 100644
--- a/crates/keymap_editor/src/keymap_editor.rs
+++ b/crates/keymap_editor/src/keymap_editor.rs
@@ -123,8 +123,8 @@ pub fn init(cx: &mut App) {
})
}
- cx.on_action(|_: &OpenKeymap, cx| common(None, cx));
- cx.on_action(|action: &ChangeKeybinding, cx| common(Some(action.action.clone()), cx));
+ cx.on_action(|_: &OpenKeymap, cx| common(None, cx))
+ .on_action(|action: &ChangeKeybinding, cx| common(Some(action.action.clone()), cx));
register_serializable_item::<KeymapEditor>(cx);
}
diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs
index 56dfb239899..0a50faf867c 100644
--- a/crates/workspace/src/workspace.rs
+++ b/crates/workspace/src/workspace.rs
@@ -576,44 +576,43 @@ pub fn init(app_state: Arc<AppState>, cx: &mut App) {
toast_layer::init(cx);
history_manager::init(cx);
- cx.on_action(|_: &CloseWindow, cx| Workspace::close_global(cx));
- cx.on_action(|_: &Reload, cx| reload(cx));
-
- cx.on_action({
- let app_state = Arc::downgrade(&app_state);
- move |_: &Open, cx: &mut App| {
- if let Some(app_state) = app_state.upgrade() {
- prompt_and_open_paths(
- app_state,
- PathPromptOptions {
- files: true,
- directories: true,
- multiple: true,
- prompt: None,
- },
- cx,
- );
+ cx.on_action(|_: &CloseWindow, cx| Workspace::close_global(cx))
+ .on_action(|_: &Reload, cx| reload(cx))
+ .on_action({
+ let app_state = Arc::downgrade(&app_state);
+ move |_: &Open, cx: &mut App| {
+ if let Some(app_state) = app_state.upgrade() {
+ prompt_and_open_paths(
+ app_state,
+ PathPromptOptions {
+ files: true,
+ directories: true,
+ multiple: true,
+ prompt: None,
+ },
+ cx,
+ );
+ }
}
- }
- });
- cx.on_action({
- let app_state = Arc::downgrade(&app_state);
- move |_: &OpenFiles, cx: &mut App| {
- let directories = cx.can_select_mixed_files_and_dirs();
- if let Some(app_state) = app_state.upgrade() {
- prompt_and_open_paths(
- app_state,
- PathPromptOptions {
- files: true,
- directories,
- multiple: true,
- prompt: None,
- },
- cx,
- );
+ })
+ .on_action({
+ let app_state = Arc::downgrade(&app_state);
+ move |_: &OpenFiles, cx: &mut App| {
+ let directories = cx.can_select_mixed_files_and_dirs();
+ if let Some(app_state) = app_state.upgrade() {
+ prompt_and_open_paths(
+ app_state,
+ PathPromptOptions {
+ files: true,
+ directories,
+ multiple: true,
+ prompt: None,
+ },
+ cx,
+ );
+ }
}
- }
- });
+ });
}
type BuildProjectItemFn =
diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs
index 3bc05ef5407..ed22d7ef510 100644
--- a/crates/zed/src/zed.rs
+++ b/crates/zed/src/zed.rs
@@ -161,15 +161,15 @@ pub fn init(cx: &mut App) {
|| flag.await
{
cx.update(|cx| {
- cx.on_action(|_: &TestPanic, _| panic!("Ran the TestPanic action"));
- cx.on_action(|_: &TestCrash, _| {
- unsafe extern "C" {
- fn puts(s: *const i8);
- }
- unsafe {
- puts(0xabad1d3a as *const i8);
- }
- });
+ cx.on_action(|_: &TestPanic, _| panic!("Ran the TestPanic action"))
+ .on_action(|_: &TestCrash, _| {
+ unsafe extern "C" {
+ fn puts(s: *const i8);
+ }
+ unsafe {
+ puts(0xabad1d3a as *const i8);
+ }
+ });
})
.ok();
};
@@ -179,11 +179,11 @@ pub fn init(cx: &mut App) {
with_active_or_new_workspace(cx, |workspace, window, cx| {
open_log_file(workspace, window, cx);
});
- });
- cx.on_action(|_: &workspace::RevealLogInFileManager, cx| {
+ })
+ .on_action(|_: &workspace::RevealLogInFileManager, cx| {
cx.reveal_path(paths::log_file().as_path());
- });
- cx.on_action(|_: &zed_actions::OpenLicenses, cx| {
+ })
+ .on_action(|_: &zed_actions::OpenLicenses, cx| {
with_active_or_new_workspace(cx, |workspace, window, cx| {
open_bundled_file(
workspace,
@@ -194,13 +194,13 @@ pub fn init(cx: &mut App) {
cx,
);
});
- });
- cx.on_action(|_: &zed_actions::OpenTelemetryLog, cx| {
+ })
+ .on_action(|_: &zed_actions::OpenTelemetryLog, cx| {
with_active_or_new_workspace(cx, |workspace, window, cx| {
open_telemetry_log_file(workspace, window, cx);
});
- });
- cx.on_action(|&zed_actions::OpenKeymapFile, cx| {
+ })
+ .on_action(|&zed_actions::OpenKeymapFile, cx| {
with_active_or_new_workspace(cx, |_, window, cx| {
open_settings_file(
paths::keymap_file(),
@@ -209,8 +209,8 @@ pub fn init(cx: &mut App) {
cx,
);
});
- });
- cx.on_action(|_: &OpenSettingsFile, cx| {
+ })
+ .on_action(|_: &OpenSettingsFile, cx| {
with_active_or_new_workspace(cx, |_, window, cx| {
open_settings_file(
paths::settings_file(),
@@ -219,13 +219,13 @@ pub fn init(cx: &mut App) {
cx,
);
});
- });
- cx.on_action(|_: &OpenAccountSettings, cx| {
+ })
+ .on_action(|_: &OpenAccountSettings, cx| {
with_active_or_new_workspace(cx, |_, _, cx| {
cx.open_url(&zed_urls::account_url(cx));
});
- });
- cx.on_action(|_: &OpenTasks, cx| {
+ })
+ .on_action(|_: &OpenTasks, cx| {
with_active_or_new_workspace(cx, |_, window, cx| {
open_settings_file(
paths::tasks_file(),
@@ -234,8 +234,8 @@ pub fn init(cx: &mut App) {
cx,
);
});
- });
- cx.on_action(|_: &OpenDebugTasks, cx| {
+ })
+ .on_action(|_: &OpenDebugTasks, cx| {
with_active_or_new_workspace(cx, |_, window, cx| {
open_settings_file(
paths::debug_scenarios_file(),
@@ -244,8 +244,8 @@ pub fn init(cx: &mut App) {
cx,
);
});
- });
- cx.on_action(|_: &OpenDefaultSettings, cx| {
+ })
+ .on_action(|_: &OpenDefaultSettings, cx| {
with_active_or_new_workspace(cx, |workspace, window, cx| {
open_bundled_file(
workspace,
@@ -256,8 +256,8 @@ pub fn init(cx: &mut App) {
cx,
);
});
- });
- cx.on_action(|_: &zed_actions::OpenDefaultKeymap, cx| {
+ })
+ .on_action(|_: &zed_actions::OpenDefaultKeymap, cx| {
with_active_or_new_workspace(cx, |workspace, window, cx| {
open_bundled_file(
workspace,
@@ -268,8 +268,8 @@ pub fn init(cx: &mut App) {
cx,
);
});
- });
- cx.on_action(|_: &zed_actions::About, cx| {
+ })
+ .on_action(|_: &zed_actions::About, cx| {
with_active_or_new_workspace(cx, |workspace, window, cx| {
about(workspace, window, cx);
});