Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Fix NewWindow flicker by creating buffer synchronously (#44915)
Copilot6f5da5e34e85
diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rsindex 5947cb1703b..516dc867ae1 100644--- a/crates/workspace/src/workspace.rs+++ b/crates/workspace/src/workspace.rs@@ -1627,6 +1627,7 @@ impl Workspace {app_state: Arc<AppState>,requesting_window: Option<WindowHandle<Workspace>>,env: Option<HashMap<String, String>>,+ init: Option<Box<dyn FnOnce(&mut Workspace, &mut Window, &mut Context<Workspace>) + Send>>,cx: &mut App,) -> Task<anyhow::Result<(@@ -1734,6 +1735,12 @@ impl Workspace {);workspace.centered_layout = centered_layout;++ // Call init callback to add items before window renders+ if let Some(init) = init {+ init(&mut workspace, window, cx);+ }+workspace});})?;@@ -1785,6 +1792,12 @@ impl Workspace {cx,);workspace.centered_layout = centered_layout;++ // Call init callback to add items before window renders+ if let Some(init) = init {+ init(&mut workspace, window, cx);+ }+workspace})}@@ -2350,7 +2363,7 @@ impl Workspace {Task::ready(Ok(callback(self, window, cx)))} else {let env = self.project.read(cx).cli_environment(cx);- let task = Self::new_local(Vec::new(), self.app_state.clone(), None, env, cx);+ let task = Self::new_local(Vec::new(), self.app_state.clone(), None, env, None, cx);cx.spawn_in(window, async move |_vh, cx| {let (workspace, _) = task.await?;workspace.update(cx, callback)@@ -7758,7 +7771,14 @@ pub fn join_channel(// no open workspaces, make one to show the error in (blergh)let (window_handle, _) = cx.update(|cx| {- Workspace::new_local(vec![], app_state.clone(), requesting_window, None, cx)+ Workspace::new_local(+ vec![],+ app_state.clone(),+ requesting_window,+ None,+ None,+ cx,+ )})?.await?;@@ -7824,7 +7844,7 @@ pub async fn get_any_active_workspace(// find an existing workspace to focus and show call controlslet active_window = activate_any_workspace_window(&mut cx);if active_window.is_none() {- cx.update(|cx| Workspace::new_local(vec![], app_state.clone(), None, None, cx))?+ cx.update(|cx| Workspace::new_local(vec![], app_state.clone(), None, None, None, cx))?.await?;}activate_any_workspace_window(&mut cx).context("could not open zed")@@ -7991,6 +8011,7 @@ pub fn open_paths(app_state.clone(),open_options.replace_window,open_options.env,+ None,cx,)})?@@ -8035,14 +8056,17 @@ pub fn open_new(cx: &mut App,init: impl FnOnce(&mut Workspace, &mut Window, &mut Context<Workspace>) + 'static + Send,) -> Task<anyhow::Result<()>> {- let task = Workspace::new_local(Vec::new(), app_state, None, open_options.env, cx);- cx.spawn(async move |cx| {- let (workspace, opened_paths) = task.await?;- workspace.update(cx, |workspace, window, cx| {- if opened_paths.is_empty() {- init(workspace, window, cx)- }- })?;+ let task = Workspace::new_local(+ Vec::new(),+ app_state,+ None,+ open_options.env,+ Some(Box::new(init)),+ cx,+ );+ cx.spawn(async move |_cx| {+ let (_workspace, _opened_paths) = task.await?;+ // Init callback is called synchronously during workspace creationOk(())})}diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rsindex fc5e74a7690..d2764c5c334 100644--- a/crates/zed/src/zed.rs+++ b/crates/zed/src/zed.rs@@ -1111,7 +1111,21 @@ fn register_actions(cx,|workspace, window, cx| {cx.activate(true);- Editor::new_file(workspace, &Default::default(), window, cx)+ // Create buffer synchronously to avoid flicker+ let project = workspace.project().clone();+ let buffer = project.update(cx, |project, cx| {+ project.create_local_buffer("", None, true, cx)+ });+ let editor = cx.new(|cx| {+ Editor::for_buffer(buffer, Some(project), window, cx)+ });+ workspace.add_item_to_active_pane(+ Box::new(editor),+ None,+ true,+ window,+ cx,+ );},).detach();