Skip to content

Commit 1a60dbb

Browse files
committed
project: Spawn terminal process on background thread
1 parent a56122e commit 1a60dbb

File tree

9 files changed

+562
-521
lines changed

9 files changed

+562
-521
lines changed

crates/project/src/terminals.rs

Lines changed: 182 additions & 173 deletions
Large diffs are not rendered by default.

crates/terminal/src/terminal.rs

Lines changed: 269 additions & 262 deletions
Large diffs are not rendered by default.

crates/terminal_view/src/terminal_panel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,11 @@ impl TerminalPanel {
462462
cx.spawn_in(window, async move |panel, cx| {
463463
let terminal = project
464464
.update(cx, |project, cx| match terminal_view {
465-
Some(view) => Task::ready(project.clone_terminal(
465+
Some(view) => project.clone_terminal(
466466
&view.read(cx).terminal.clone(),
467467
cx,
468468
working_directory,
469-
)),
469+
),
470470
None => project.create_terminal_shell(working_directory, cx),
471471
})
472472
.ok()?

crates/terminal_view/src/terminal_view.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,27 +1221,27 @@ impl Item for TerminalView {
12211221
window: &mut Window,
12221222
cx: &mut Context<Self>,
12231223
) -> Option<Entity<Self>> {
1224-
let terminal = self
1225-
.project
1226-
.update(cx, |project, cx| {
1227-
let cwd = project
1228-
.active_project_directory(cx)
1229-
.map(|it| it.to_path_buf());
1230-
project.clone_terminal(self.terminal(), cx, cwd)
1231-
})
1232-
.ok()?
1233-
.log_err()?;
1234-
1235-
Some(cx.new(|cx| {
1236-
TerminalView::new(
1237-
terminal,
1238-
self.workspace.clone(),
1239-
workspace_id,
1240-
self.project.clone(),
1241-
window,
1242-
cx,
1243-
)
1244-
}))
1224+
// let terminal = self
1225+
// .project
1226+
// .update(cx, |project, cx| {
1227+
// let cwd = project
1228+
// .active_project_directory(cx)
1229+
// .map(|it| it.to_path_buf());
1230+
// project.clone_terminal(self.terminal(), cx, cwd)
1231+
// })
1232+
// .ok()?;
1233+
1234+
// Some(cx.new(|cx| {
1235+
// TerminalView::new(
1236+
// terminal,
1237+
// self.workspace.clone(),
1238+
// workspace_id,
1239+
// self.project.clone(),
1240+
// window,
1241+
// cx,
1242+
// )
1243+
// }))
1244+
None
12451245
}
12461246

12471247
fn is_dirty(&self, cx: &gpui::App) -> bool {

crates/workspace/src/item.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use anyhow::Result;
1111
use client::{Client, proto};
1212
use futures::{StreamExt, channel::mpsc};
1313
use gpui::{
14-
Action, AnyElement, AnyView, App, Context, Entity, EntityId, EventEmitter, FocusHandle,
15-
Focusable, Font, HighlightStyle, Pixels, Point, Render, SharedString, Task, WeakEntity, Window,
14+
Action, AnyElement, AnyView, App, AppContext, Context, Entity, EntityId, EventEmitter,
15+
FocusHandle, Focusable, Font, HighlightStyle, Pixels, Point, Render, SharedString, Task,
16+
WeakEntity, Window,
1617
};
1718
use project::{Project, ProjectEntryId, ProjectPath};
1819
pub use settings::{
@@ -217,11 +218,11 @@ pub trait Item: Focusable + EventEmitter<Self::Event> + Render + Sized {
217218
_workspace_id: Option<WorkspaceId>,
218219
_window: &mut Window,
219220
_: &mut Context<Self>,
220-
) -> Option<Entity<Self>>
221+
) -> Task<Option<Entity<Self>>>
221222
where
222223
Self: Sized,
223224
{
224-
None
225+
Task::ready(None)
225226
}
226227
fn is_dirty(&self, _: &App) -> bool {
227228
false
@@ -422,7 +423,7 @@ pub trait ItemHandle: 'static + Send {
422423
workspace_id: Option<WorkspaceId>,
423424
window: &mut Window,
424425
cx: &mut App,
425-
) -> Option<Box<dyn ItemHandle>>;
426+
) -> Task<Option<Box<dyn ItemHandle>>>;
426427
fn added_to_pane(
427428
&self,
428429
workspace: &mut Workspace,
@@ -635,9 +636,12 @@ impl<T: Item> ItemHandle for Entity<T> {
635636
workspace_id: Option<WorkspaceId>,
636637
window: &mut Window,
637638
cx: &mut App,
638-
) -> Option<Box<dyn ItemHandle>> {
639-
self.update(cx, |item, cx| item.clone_on_split(workspace_id, window, cx))
640-
.map(|handle| Box::new(handle) as Box<dyn ItemHandle>)
639+
) -> Task<Option<Box<dyn ItemHandle>>> {
640+
let task = self.update(cx, |item, cx| item.clone_on_split(workspace_id, window, cx));
641+
cx.background_spawn(async move {
642+
task.await
643+
.map(|handle| Box::new(handle) as Box<dyn ItemHandle>)
644+
})
641645
}
642646

643647
fn added_to_pane(
@@ -1504,11 +1508,11 @@ pub mod test {
15041508
_workspace_id: Option<WorkspaceId>,
15051509
_: &mut Window,
15061510
cx: &mut Context<Self>,
1507-
) -> Option<Entity<Self>>
1511+
) -> Task<Option<Entity<Self>>>
15081512
where
15091513
Self: Sized,
15101514
{
1511-
Some(cx.new(|cx| Self {
1515+
Task::ready(Some(cx.new(|cx| Self {
15121516
state: self.state.clone(),
15131517
label: self.label.clone(),
15141518
save_count: self.save_count,
@@ -1525,7 +1529,7 @@ pub mod test {
15251529
workspace_id: self.workspace_id,
15261530
focus_handle: cx.focus_handle(),
15271531
serialize: None,
1528-
}))
1532+
})))
15291533
}
15301534

15311535
fn is_dirty(&self, _: &App) -> bool {

crates/workspace/src/pane.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3295,11 +3295,16 @@ impl Pane {
32953295
else {
32963296
return;
32973297
};
3298-
if let Some(item) = item.clone_on_split(database_id, window, cx) {
3299-
to_pane.update(cx, |pane, cx| {
3300-
pane.add_item(item, true, true, None, window, cx);
3301-
})
3302-
}
3298+
let task = item.clone_on_split(database_id, window, cx);
3299+
let to_pane = to_pane.downgrade();
3300+
cx.spawn_in(window, async move |_, cx| {
3301+
if let Some(item) = task.await {
3302+
to_pane.update_in(cx, |pane, window, cx| {
3303+
pane.add_item(item, true, true, None, window, cx)
3304+
});
3305+
}
3306+
})
3307+
.detach();
33033308
} else {
33043309
move_item(&from_pane, &to_pane, item_id, ix, true, window, cx);
33053310
}

crates/workspace/src/shared_screen.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use call::{RemoteVideoTrack, RemoteVideoTrackView, Room};
66
use client::{User, proto::PeerId};
77
use gpui::{
88
AppContext as _, Entity, EventEmitter, FocusHandle, Focusable, InteractiveElement,
9-
ParentElement, Render, SharedString, Styled, div,
9+
ParentElement, Render, SharedString, Styled, Task, div,
1010
};
1111
use std::sync::Arc;
1212
use ui::{Icon, IconName, prelude::*};
@@ -114,14 +114,14 @@ impl Item for SharedScreen {
114114
_workspace_id: Option<WorkspaceId>,
115115
window: &mut Window,
116116
cx: &mut Context<Self>,
117-
) -> Option<Entity<Self>> {
118-
Some(cx.new(|cx| Self {
117+
) -> Task<Option<Entity<Self>>> {
118+
Task::ready(Some(cx.new(|cx| Self {
119119
view: self.view.update(cx, |view, cx| view.clone(window, cx)),
120120
peer_id: self.peer_id,
121121
user: self.user.clone(),
122122
nav_history: Default::default(),
123123
focus: cx.focus_handle(),
124-
}))
124+
})))
125125
}
126126

127127
fn to_item_events(event: &Self::Event, mut f: impl FnMut(ItemEvent)) {

crates/workspace/src/theme_preview.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![allow(unused, dead_code)]
2-
use gpui::{AnyElement, App, Entity, EventEmitter, FocusHandle, Focusable, Hsla, actions, hsla};
2+
use gpui::{
3+
AnyElement, App, Entity, EventEmitter, FocusHandle, Focusable, Hsla, Task, actions, hsla,
4+
};
35
use strum::IntoEnumIterator;
46
use theme::all_theme_colors;
57
use ui::{
@@ -100,11 +102,11 @@ impl Item for ThemePreview {
100102
_workspace_id: Option<crate::WorkspaceId>,
101103
window: &mut Window,
102104
cx: &mut Context<Self>,
103-
) -> Option<Entity<Self>>
105+
) -> Task<Option<Entity<Self>>>
104106
where
105107
Self: Sized,
106108
{
107-
Some(cx.new(|cx| Self::new(window, cx)))
109+
Task::ready(Some(cx.new(|cx| Self::new(window, cx))))
108110
}
109111
}
110112

crates/workspace/src/workspace.rs

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4135,21 +4135,27 @@ impl Workspace {
41354135
direction: SplitDirection,
41364136
window: &mut Window,
41374137
cx: &mut Context<Self>,
4138-
) -> Option<Entity<Pane>> {
4139-
let item = pane.read(cx).active_item()?;
4140-
let maybe_pane_handle =
4141-
if let Some(clone) = item.clone_on_split(self.database_id(), window, cx) {
4142-
let new_pane = self.add_pane(window, cx);
4143-
new_pane.update(cx, |pane, cx| {
4144-
pane.add_item(clone, true, true, None, window, cx)
4145-
});
4146-
self.center.split(&pane, &new_pane, direction).unwrap();
4147-
cx.notify();
4148-
Some(new_pane)
4138+
) -> Task<Option<Entity<Pane>>> {
4139+
let Some(item) = pane.read(cx).active_item() else {
4140+
return Task::ready(None);
4141+
};
4142+
let task = item.clone_on_split(self.database_id(), window, cx);
4143+
cx.spawn_in(window, async move |this, cx| {
4144+
if let Some(clone) = task.await {
4145+
this.update_in(cx, |this, window, cx| {
4146+
let new_pane = this.add_pane(window, cx);
4147+
new_pane.update(cx, |pane, cx| {
4148+
pane.add_item(clone, true, true, None, window, cx)
4149+
});
4150+
this.center.split(&pane, &new_pane, direction).unwrap();
4151+
cx.notify();
4152+
new_pane
4153+
})
4154+
.ok()
41494155
} else {
41504156
None
4151-
};
4152-
maybe_pane_handle
4157+
}
4158+
})
41534159
}
41544160

41554161
pub fn join_all_panes(&mut self, window: &mut Window, cx: &mut Context<Self>) {
@@ -8170,19 +8176,27 @@ pub fn clone_active_item(
81708176
let Some(active_item) = source.read(cx).active_item() else {
81718177
return;
81728178
};
8173-
destination.update(cx, |target_pane, cx| {
8174-
let Some(clone) = active_item.clone_on_split(workspace_id, window, cx) else {
8175-
return;
8176-
};
8177-
target_pane.add_item(
8178-
clone,
8179-
focus_destination,
8180-
focus_destination,
8181-
Some(target_pane.items_len()),
8182-
window,
8183-
cx,
8184-
);
8185-
});
8179+
let destination = destination.downgrade();
8180+
let task = active_item.clone_on_split(workspace_id, window, cx);
8181+
window
8182+
.spawn(cx, async move |cx| {
8183+
let Some(clone) = task.await else {
8184+
return;
8185+
};
8186+
destination
8187+
.update_in(cx, |target_pane, window, cx| {
8188+
target_pane.add_item(
8189+
clone,
8190+
focus_destination,
8191+
focus_destination,
8192+
Some(target_pane.items_len()),
8193+
window,
8194+
cx,
8195+
);
8196+
})
8197+
.log_err();
8198+
})
8199+
.detach();
81868200
}
81878201

81888202
#[derive(Debug)]
@@ -8689,25 +8703,25 @@ mod tests {
86898703
cx,
86908704
);
86918705

8692-
let right_pane = workspace
8693-
.split_and_clone(left_pane.clone(), SplitDirection::Right, window, cx)
8694-
.unwrap();
8706+
let right_pane =
8707+
workspace.split_and_clone(left_pane.clone(), SplitDirection::Right, window, cx);
86958708

8696-
right_pane.update(cx, |pane, cx| {
8697-
pane.add_item(
8698-
single_entry_items[1].boxed_clone(),
8699-
true,
8700-
true,
8701-
None,
8702-
window,
8703-
cx,
8704-
);
8705-
pane.add_item(Box::new(item_3_4.clone()), true, true, None, window, cx);
8709+
let boxed_clone = single_entry_items[1].boxed_clone();
8710+
let right_pane = window.spawn(cx, async move |cx| {
8711+
right_pane.await.map(|right_pane| {
8712+
right_pane
8713+
.update_in(cx, |pane, window, cx| {
8714+
pane.add_item(boxed_clone, true, true, None, window, cx);
8715+
pane.add_item(Box::new(item_3_4.clone()), true, true, None, window, cx);
8716+
})
8717+
.unwrap();
8718+
right_pane
8719+
})
87068720
});
87078721

87088722
(left_pane, right_pane)
87098723
});
8710-
8724+
let right_pane = right_pane.await.unwrap();
87118725
cx.focus(&right_pane);
87128726

87138727
let mut close = right_pane.update_in(cx, |pane, window, cx| {

0 commit comments

Comments
 (0)