zed2.rs

  1#![allow(unused_variables, dead_code, unused_mut)]
  2// todo!() this is to make transition easier.
  3
  4mod assets;
  5pub mod languages;
  6mod only_instance;
  7mod open_listener;
  8
  9pub use assets::*;
 10use gpui::{
 11    point, px, AppContext, AsyncWindowContext, Task, TitlebarOptions, WeakView, WindowBounds,
 12    WindowKind, WindowOptions,
 13};
 14pub use only_instance::*;
 15pub use open_listener::*;
 16
 17use anyhow::Result;
 18use settings::Settings;
 19use std::sync::Arc;
 20use uuid::Uuid;
 21use workspace::{AppState, Workspace};
 22
 23pub fn build_window_options(
 24    bounds: Option<WindowBounds>,
 25    display_uuid: Option<Uuid>,
 26    cx: &mut AppContext,
 27) -> WindowOptions {
 28    let bounds = bounds.unwrap_or(WindowBounds::Maximized);
 29    let display = display_uuid.and_then(|uuid| {
 30        cx.displays()
 31            .into_iter()
 32            .find(|display| display.uuid().ok() == Some(uuid))
 33    });
 34
 35    WindowOptions {
 36        bounds,
 37        titlebar: Some(TitlebarOptions {
 38            title: None,
 39            appears_transparent: true,
 40            traffic_light_position: Some(point(px(8.), px(8.))),
 41        }),
 42        center: false,
 43        focus: false,
 44        show: false,
 45        kind: WindowKind::Normal,
 46        is_movable: true,
 47        display_id: display.map(|display| display.id()),
 48    }
 49}
 50
 51pub fn init_zed_actions(cx: &mut AppContext) {
 52    cx.observe_new_views(|workspace: &mut Workspace, cx| {
 53        workspace
 54            // cx.add_action(about);
 55            // cx.add_global_action(|_: &Hide, cx: &mut gpui::AppContext| {
 56            //     cx.platform().hide();
 57            // });
 58            // cx.add_global_action(|_: &HideOthers, cx: &mut gpui::AppContext| {
 59            //     cx.platform().hide_other_apps();
 60            // });
 61            // cx.add_global_action(|_: &ShowAll, cx: &mut gpui::AppContext| {
 62            //     cx.platform().unhide_other_apps();
 63            // });
 64            // cx.add_action(
 65            //     |_: &mut Workspace, _: &Minimize, cx: &mut ViewContext<Workspace>| {
 66            //         cx.minimize_window();
 67            //     },
 68            // );
 69            // cx.add_action(
 70            //     |_: &mut Workspace, _: &Zoom, cx: &mut ViewContext<Workspace>| {
 71            //         cx.zoom_window();
 72            //     },
 73            // );
 74            // cx.add_action(
 75            //     |_: &mut Workspace, _: &ToggleFullScreen, cx: &mut ViewContext<Workspace>| {
 76            //         cx.toggle_full_screen();
 77            //     },
 78            // );
 79            .register_action(|workspace, _: &zed_actions::Quit, cx| quit(cx));
 80        // cx.add_global_action(move |action: &OpenZedURL, cx| {
 81        //     cx.global::<Arc<OpenListener>>()
 82        //         .open_urls(vec![action.url.clone()])
 83        // });
 84        // cx.add_global_action(move |action: &OpenBrowser, cx| cx.platform().open_url(&action.url));
 85        // cx.add_global_action(move |_: &IncreaseBufferFontSize, cx| {
 86        //     theme::adjust_font_size(cx, |size| *size += 1.0)
 87        // });
 88        // cx.add_global_action(move |_: &DecreaseBufferFontSize, cx| {
 89        //     theme::adjust_font_size(cx, |size| *size -= 1.0)
 90        // });
 91        // cx.add_global_action(move |_: &ResetBufferFontSize, cx| theme::reset_font_size(cx));
 92        // cx.add_global_action(move |_: &install_cli::Install, cx| {
 93        //     cx.spawn(|cx| async move {
 94        //         install_cli::install_cli(&cx)
 95        //             .await
 96        //             .context("error creating CLI symlink")
 97        //     })
 98        //     .detach_and_log_err(cx);
 99        // });
100        // cx.add_action(
101        //     move |workspace: &mut Workspace, _: &OpenLog, cx: &mut ViewContext<Workspace>| {
102        //         open_log_file(workspace, cx);
103        //     },
104        // );
105        // cx.add_action(
106        //     move |workspace: &mut Workspace, _: &OpenLicenses, cx: &mut ViewContext<Workspace>| {
107        //         open_bundled_file(
108        //             workspace,
109        //             asset_str::<Assets>("licenses.md"),
110        //             "Open Source License Attribution",
111        //             "Markdown",
112        //             cx,
113        //         );
114        //     },
115        // );
116        // cx.add_action(
117        //     move |workspace: &mut Workspace, _: &OpenTelemetryLog, cx: &mut ViewContext<Workspace>| {
118        //         open_telemetry_log_file(workspace, cx);
119        //     },
120        // );
121        // cx.add_action(
122        //     move |_: &mut Workspace, _: &OpenKeymap, cx: &mut ViewContext<Workspace>| {
123        //         create_and_open_local_file(&paths::KEYMAP, cx, Default::default).detach_and_log_err(cx);
124        //     },
125        // );
126        // cx.add_action(
127        //     move |_: &mut Workspace, _: &OpenSettings, cx: &mut ViewContext<Workspace>| {
128        //         create_and_open_local_file(&paths::SETTINGS, cx, || {
129        //             settings::initial_user_settings_content().as_ref().into()
130        //         })
131        //         .detach_and_log_err(cx);
132        //     },
133        // );
134        // cx.add_action(open_local_settings_file);
135        // cx.add_action(
136        //     move |workspace: &mut Workspace, _: &OpenDefaultKeymap, cx: &mut ViewContext<Workspace>| {
137        //         open_bundled_file(
138        //             workspace,
139        //             settings::default_keymap(),
140        //             "Default Key Bindings",
141        //             "JSON",
142        //             cx,
143        //         );
144        //     },
145        // );
146        // cx.add_action(
147        //     move |workspace: &mut Workspace,
148        //           _: &OpenDefaultSettings,
149        //           cx: &mut ViewContext<Workspace>| {
150        //         open_bundled_file(
151        //             workspace,
152        //             settings::default_settings(),
153        //             "Default Settings",
154        //             "JSON",
155        //             cx,
156        //         );
157        //     },
158        // );
159        // cx.add_action({
160        //     move |workspace: &mut Workspace, _: &DebugElements, cx: &mut ViewContext<Workspace>| {
161        //         let app_state = workspace.app_state().clone();
162        //         let markdown = app_state.languages.language_for_name("JSON");
163        //         let window = cx.window();
164        //         cx.spawn(|workspace, mut cx| async move {
165        //             let markdown = markdown.await.log_err();
166        //             let content = to_string_pretty(&window.debug_elements(&cx).ok_or_else(|| {
167        //                 anyhow!("could not debug elements for window {}", window.id())
168        //             })?)
169        //             .unwrap();
170        //             workspace
171        //                 .update(&mut cx, |workspace, cx| {
172        //                     workspace.with_local_workspace(cx, move |workspace, cx| {
173        //                         let project = workspace.project().clone();
174
175        //                         let buffer = project
176        //                             .update(cx, |project, cx| {
177        //                                 project.create_buffer(&content, markdown, cx)
178        //                             })
179        //                             .expect("creating buffers on a local workspace always succeeds");
180        //                         let buffer = cx.add_model(|cx| {
181        //                             MultiBuffer::singleton(buffer, cx)
182        //                                 .with_title("Debug Elements".into())
183        //                         });
184        //                         workspace.add_item(
185        //                             Box::new(cx.add_view(|cx| {
186        //                                 Editor::for_multibuffer(buffer, Some(project.clone()), cx)
187        //                             })),
188        //                             cx,
189        //                         );
190        //                     })
191        //                 })?
192        //                 .await
193        //         })
194        //         .detach_and_log_err(cx);
195        //     }
196        // });
197        // cx.add_action(
198        //     |workspace: &mut Workspace,
199        //      _: &project_panel::ToggleFocus,
200        //      cx: &mut ViewContext<Workspace>| {
201        //         workspace.toggle_panel_focus::<ProjectPanel>(cx);
202        //     },
203        // );
204        // cx.add_action(
205        //     |workspace: &mut Workspace,
206        //      _: &collab_ui::collab_panel::ToggleFocus,
207        //      cx: &mut ViewContext<Workspace>| {
208        //         workspace.toggle_panel_focus::<collab_ui::collab_panel::CollabPanel>(cx);
209        //     },
210        // );
211        // cx.add_action(
212        //     |workspace: &mut Workspace,
213        //      _: &collab_ui::chat_panel::ToggleFocus,
214        //      cx: &mut ViewContext<Workspace>| {
215        //         workspace.toggle_panel_focus::<collab_ui::chat_panel::ChatPanel>(cx);
216        //     },
217        // );
218        // cx.add_action(
219        //     |workspace: &mut Workspace,
220        //      _: &collab_ui::notification_panel::ToggleFocus,
221        //      cx: &mut ViewContext<Workspace>| {
222        //         workspace.toggle_panel_focus::<collab_ui::notification_panel::NotificationPanel>(cx);
223        //     },
224        // );
225        // cx.add_action(
226        //     |workspace: &mut Workspace,
227        //      _: &terminal_panel::ToggleFocus,
228        //      cx: &mut ViewContext<Workspace>| {
229        //         workspace.toggle_panel_focus::<TerminalPanel>(cx);
230        //     },
231        // );
232        // cx.add_global_action({
233        //     let app_state = Arc::downgrade(&app_state);
234        //     move |_: &NewWindow, cx: &mut AppContext| {
235        //         if let Some(app_state) = app_state.upgrade() {
236        //             open_new(&app_state, cx, |workspace, cx| {
237        //                 Editor::new_file(workspace, &Default::default(), cx)
238        //             })
239        //             .detach();
240        //         }
241        //     }
242        // });
243        // cx.add_global_action({
244        //     let app_state = Arc::downgrade(&app_state);
245        //     move |_: &NewFile, cx: &mut AppContext| {
246        //         if let Some(app_state) = app_state.upgrade() {
247        //             open_new(&app_state, cx, |workspace, cx| {
248        //                 Editor::new_file(workspace, &Default::default(), cx)
249        //             })
250        //             .detach();
251        //         }
252        //     }
253        // });
254        // load_default_keymap(cx);
255    })
256    .detach();
257}
258
259pub fn initialize_workspace(
260    workspace_handle: WeakView<Workspace>,
261    was_deserialized: bool,
262    app_state: Arc<AppState>,
263    cx: AsyncWindowContext,
264) -> Task<Result<()>> {
265    cx.spawn(|mut cx| async move {
266        workspace_handle.update(&mut cx, |workspace, cx| {
267            let workspace_handle = cx.view().clone();
268            cx.subscribe(&workspace_handle, {
269                move |workspace, _, event, cx| {
270                    if let workspace::Event::PaneAdded(pane) = event {
271                        pane.update(cx, |pane, cx| {
272                            pane.toolbar().update(cx, |toolbar, cx| {
273                                // todo!()
274                                //     let breadcrumbs = cx.add_view(|_| Breadcrumbs::new(workspace));
275                                //     toolbar.add_item(breadcrumbs, cx);
276                                //     let buffer_search_bar = cx.add_view(BufferSearchBar::new);
277                                //     toolbar.add_item(buffer_search_bar.clone(), cx);
278                                //     let quick_action_bar = cx.add_view(|_| {
279                                //         QuickActionBar::new(buffer_search_bar, workspace)
280                                //     });
281                                //     toolbar.add_item(quick_action_bar, cx);
282                                //     let diagnostic_editor_controls =
283                                //         cx.add_view(|_| diagnostics2::ToolbarControls::new());
284                                //     toolbar.add_item(diagnostic_editor_controls, cx);
285                                //     let project_search_bar = cx.add_view(|_| ProjectSearchBar::new());
286                                //     toolbar.add_item(project_search_bar, cx);
287                                //     let submit_feedback_button =
288                                //         cx.add_view(|_| SubmitFeedbackButton::new());
289                                //     toolbar.add_item(submit_feedback_button, cx);
290                                //     let feedback_info_text = cx.add_view(|_| FeedbackInfoText::new());
291                                //     toolbar.add_item(feedback_info_text, cx);
292                                //     let lsp_log_item =
293                                //         cx.add_view(|_| language_tools::LspLogToolbarItemView::new());
294                                //     toolbar.add_item(lsp_log_item, cx);
295                                //     let syntax_tree_item = cx
296                                //         .add_view(|_| language_tools::SyntaxTreeToolbarItemView::new());
297                                //     toolbar.add_item(syntax_tree_item, cx);
298                            })
299                        });
300                    }
301                }
302            })
303            .detach();
304
305            //     cx.emit(workspace2::Event::PaneAdded(
306            //         workspace.active_pane().clone(),
307            //     ));
308
309            //     let collab_titlebar_item =
310            //         cx.add_view(|cx| CollabTitlebarItem::new(workspace, &workspace_handle, cx));
311            //     workspace.set_titlebar_item(collab_titlebar_item.into_any(), cx);
312
313            //     let copilot =
314            //         cx.add_view(|cx| copilot_button::CopilotButton::new(app_state.fs.clone(), cx));
315            //     let diagnostic_summary =
316            //         cx.add_view(|cx| diagnostics::items::DiagnosticIndicator::new(workspace, cx));
317            //     let activity_indicator = activity_indicator::ActivityIndicator::new(
318            //         workspace,
319            //         app_state.languages.clone(),
320            //         cx,
321            //     );
322            //     let active_buffer_language =
323            //         cx.add_view(|_| language_selector::ActiveBufferLanguage::new(workspace));
324            //     let vim_mode_indicator = cx.add_view(|cx| vim::ModeIndicator::new(cx));
325            //     let feedback_button = cx.add_view(|_| {
326            //         feedback::deploy_feedback_button::DeployFeedbackButton::new(workspace)
327            //     });
328            //     let cursor_position = cx.add_view(|_| editor::items::CursorPosition::new());
329            workspace.status_bar().update(cx, |status_bar, cx| {
330                // status_bar.add_left_item(diagnostic_summary, cx);
331                // status_bar.add_left_item(activity_indicator, cx);
332
333                // status_bar.add_right_item(feedback_button, cx);
334                // status_bar.add_right_item(copilot, cx);
335                // status_bar.add_right_item(active_buffer_language, cx);
336                // status_bar.add_right_item(vim_mode_indicator, cx);
337                // status_bar.add_right_item(cursor_position, cx);
338            });
339
340            //     auto_update::notify_of_any_new_update(cx.weak_handle(), cx);
341
342            //     vim::observe_keystrokes(cx);
343
344            //     cx.on_window_should_close(|workspace, cx| {
345            //         if let Some(task) = workspace.close(&Default::default(), cx) {
346            //             task.detach_and_log_err(cx);
347            //         }
348            //         false
349            //     });
350            // })?;
351
352            // let project_panel = ProjectPanel::load(workspace_handle.clone(), cx.clone());
353            // let terminal_panel = TerminalPanel::load(workspace_handle.clone(), cx.clone());
354            // let assistant_panel = AssistantPanel::load(workspace_handle.clone(), cx.clone());
355            // let channels_panel =
356            //     collab_ui::collab_panel::CollabPanel::load(workspace_handle.clone(), cx.clone());
357            // let chat_panel =
358            //     collab_ui::chat_panel::ChatPanel::load(workspace_handle.clone(), cx.clone());
359            // let notification_panel = collab_ui::notification_panel::NotificationPanel::load(
360            //     workspace_handle.clone(),
361            //     cx.clone(),
362            // );
363            // let (
364            //     project_panel,
365            //     terminal_panel,
366            //     assistant_panel,
367            //     channels_panel,
368            //     chat_panel,
369            //     notification_panel,
370            // ) = futures::try_join!(
371            //     project_panel,
372            //     terminal_panel,
373            //     assistant_panel,
374            //     channels_panel,
375            //     chat_panel,
376            //     notification_panel,
377            // )?;
378            // workspace_handle.update(&mut cx, |workspace, cx| {
379            //     let project_panel_position = project_panel.position(cx);
380            //     workspace.add_panel_with_extra_event_handler(
381            //         project_panel,
382            //         cx,
383            //         |workspace, _, event, cx| match event {
384            //             project_panel::Event::NewSearchInDirectory { dir_entry } => {
385            //                 search::ProjectSearchView::new_search_in_directory(workspace, dir_entry, cx)
386            //             }
387            //             project_panel::Event::ActivatePanel => {
388            //                 workspace.focus_panel::<ProjectPanel>(cx);
389            //             }
390            //             _ => {}
391            //         },
392            //     );
393            //     workspace.add_panel(terminal_panel, cx);
394            //     workspace.add_panel(assistant_panel, cx);
395            //     workspace.add_panel(channels_panel, cx);
396            //     workspace.add_panel(chat_panel, cx);
397            //     workspace.add_panel(notification_panel, cx);
398
399            //     if !was_deserialized
400            //         && workspace
401            //             .project()
402            //             .read(cx)
403            //             .visible_worktrees(cx)
404            //             .any(|tree| {
405            //                 tree.read(cx)
406            //                     .root_entry()
407            //                     .map_or(false, |entry| entry.is_dir())
408            //             })
409            //     {
410            //         workspace.toggle_dock(project_panel_position, cx);
411            //     }
412            //     cx.focus_self();
413        })?;
414        Ok(())
415    })
416}
417
418fn quit(cx: &mut gpui::AppContext) {
419    let should_confirm = workspace::WorkspaceSettings::get_global(cx).confirm_quit;
420    cx.spawn(|mut cx| async move {
421        // let mut workspace_windows = cx
422        //     .windows()
423        //     .into_iter()
424        //     .filter_map(|window| window.downcast::<Workspace>())
425        //     .collect::<Vec<_>>();
426
427        //     // If multiple windows have unsaved changes, and need a save prompt,
428        //     // prompt in the active window before switching to a different window.
429        //     workspace_windows.sort_by_key(|window| window.is_active(&cx) == Some(false));
430
431        //     if let (true, Some(window)) = (should_confirm, workspace_windows.first().copied()) {
432        //         let answer = window.prompt(
433        //             PromptLevel::Info,
434        //             "Are you sure you want to quit?",
435        //             &["Quit", "Cancel"],
436        //             &mut cx,
437        //         );
438
439        //         if let Some(mut answer) = answer {
440        //             let answer = answer.next().await;
441        //             if answer != Some(0) {
442        //                 return Ok(());
443        //             }
444        //         }
445        //     }
446
447        //     // If the user cancels any save prompt, then keep the app open.
448        //     for window in workspace_windows {
449        //         if let Some(should_close) = window.update_root(&mut cx, |workspace, cx| {
450        //             workspace.prepare_to_close(true, cx)
451        //         }) {
452        //             if !should_close.await? {
453        //                 return Ok(());
454        //             }
455        //         }
456        //     }
457        cx.update(|cx| {
458            cx.quit();
459        })?;
460
461        anyhow::Ok(())
462    })
463    .detach_and_log_err(cx);
464}