From fed391fe6b9870c36f71ac5e293abbd75bb4dca0 Mon Sep 17 00:00:00 2001 From: KCaverly Date: Tue, 31 Oct 2023 12:00:45 -0400 Subject: [PATCH] wip --- crates/workspace2/src/workspace2.rs | 249 ++++++++++++++-------------- crates/zed2/src/main.rs | 50 +++--- crates/zed2/src/zed2.rs | 30 +++- 3 files changed, 181 insertions(+), 148 deletions(-) diff --git a/crates/workspace2/src/workspace2.rs b/crates/workspace2/src/workspace2.rs index 52a9971bc5..a5d73b2904 100644 --- a/crates/workspace2/src/workspace2.rs +++ b/crates/workspace2/src/workspace2.rs @@ -14,15 +14,18 @@ use anyhow::{anyhow, Result}; use call2::ActiveCall; use client2::{ proto::{self, PeerId}, - Client, UserStore, + Client, TypedEnvelope, UserStore, }; use collections::{HashMap, HashSet}; use dock::Dock; -use futures::{channel::oneshot, FutureExt}; +use futures::{ + channel::{mpsc, oneshot}, + FutureExt, +}; use gpui2::{ - AnyModel, AnyView, AppContext, AsyncAppContext, DisplayId, EventEmitter, MainThread, Model, - Subscription, Task, View, ViewContext, VisualContext, WeakModel, WeakView, WindowBounds, - WindowHandle, WindowOptions, + AnyModel, AnyView, AppContext, AsyncAppContext, AsyncWindowContext, DisplayId, EventEmitter, + MainThread, Model, ModelContext, Subscription, Task, View, ViewContext, VisualContext, + WeakModel, WeakView, WindowBounds, WindowHandle, WindowOptions, }; use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ProjectItem}; use language2::LanguageRegistry; @@ -418,7 +421,7 @@ pub struct AppState { } pub struct WorkspaceStore { - workspaces: HashSet>, + workspaces: HashSet>, followers: Vec, client: Arc, _subscriptions: Vec, @@ -539,7 +542,7 @@ pub struct Workspace { last_leaders_by_pane: HashMap, PeerId>, // window_edited: bool, active_call: Option<(Model, Vec)>, - // leader_updates_tx: mpsc::UnboundedSender<(PeerId, proto::UpdateFollowers)>, + leader_updates_tx: mpsc::UnboundedSender<(PeerId, proto::UpdateFollowers)>, database_id: WorkspaceId, app_state: Arc, // subscriptions: Vec, @@ -2853,16 +2856,16 @@ impl Workspace { // } // } - // fn handle_update_followers( - // &mut self, - // leader_id: PeerId, - // message: proto::UpdateFollowers, - // _cx: &mut ViewContext, - // ) { - // self.leader_updates_tx - // .unbounded_send((leader_id, message)) - // .ok(); - // } + fn handle_update_followers( + &mut self, + leader_id: PeerId, + message: proto::UpdateFollowers, + _cx: &mut ViewContext, + ) { + self.leader_updates_tx + .unbounded_send((leader_id, message)) + .ok(); + } // async fn process_leader_update( // this: &WeakView, @@ -3886,18 +3889,19 @@ impl EventEmitter for Workspace { // } impl WorkspaceStore { - // pub fn new(client: Arc, cx: &mut ModelContext) -> Self { - // Self { - // workspaces: Default::default(), - // followers: Default::default(), - // _subscriptions: vec![ - // client.add_request_handler(cx.handle(), Self::handle_follow), - // client.add_message_handler(cx.handle(), Self::handle_unfollow), - // client.add_message_handler(cx.handle(), Self::handle_update_followers), - // ], - // client, - // } - // } + pub fn new(client: Arc, cx: &mut ModelContext) -> Self { + // Self { + // workspaces: Default::default(), + // followers: Default::default(), + // _subscriptions: vec![ + // client.add_request_handler(cx.weak_model(), Self::handle_follow), + // client.add_message_handler(cx.weak_model(), Self::handle_unfollow), + // client.add_message_handler(cx.weak_model(), Self::handle_update_followers), + // ], + // client, + // } + todo!() + } pub fn update_followers( &self, @@ -3933,100 +3937,101 @@ impl WorkspaceStore { }) .log_err() } + + // async fn handle_follow( + // this: Model, + // envelope: TypedEnvelope, + // _: Arc, + // mut cx: AsyncAppContext, + // ) -> Result { + // this.update(&mut cx, |this, cx| { + // let follower = Follower { + // project_id: envelope.payload.project_id, + // peer_id: envelope.original_sender_id()?, + // }; + // let active_project = ActiveCall::global(cx) + // .read(cx) + // .location() + // .map(|project| project.id()); + + // let mut response = proto::FollowResponse::default(); + // for workspace in &this.workspaces { + // let Some(workspace) = workspace.upgrade(cx) else { + // continue; + // }; + + // workspace.update(cx.as_mut(), |workspace, cx| { + // let handler_response = workspace.handle_follow(follower.project_id, cx); + // if response.views.is_empty() { + // response.views = handler_response.views; + // } else { + // response.views.extend_from_slice(&handler_response.views); + // } + + // if let Some(active_view_id) = handler_response.active_view_id.clone() { + // if response.active_view_id.is_none() + // || Some(workspace.project.id()) == active_project + // { + // response.active_view_id = Some(active_view_id); + // } + // } + // }); + // } + + // if let Err(ix) = this.followers.binary_search(&follower) { + // this.followers.insert(ix, follower); + // } + + // Ok(response) + // }) + // } + + async fn handle_unfollow( + model: Model, + envelope: TypedEnvelope, + _: Arc, + mut cx: AsyncAppContext, + ) -> Result<()> { + model.update(&mut cx, |this, _| { + let follower = Follower { + project_id: envelope.payload.project_id, + peer_id: envelope.original_sender_id()?, + }; + if let Ok(ix) = this.followers.binary_search(&follower) { + this.followers.remove(ix); + } + Ok(()) + })? + } + + async fn handle_update_followers( + this: Model, + envelope: TypedEnvelope, + _: Arc, + mut cx: AsyncWindowContext, + ) -> Result<()> { + // let leader_id = envelope.original_sender_id()?; + // let update = envelope.payload; + + // this.update(&mut cx, |this, cx| { + // for workspace in &this.workspaces { + // let Some(workspace) = workspace.upgrade() else { + // continue; + // }; + // workspace.update(cx, |workspace, cx| { + // let project_id = workspace.project.read(cx).remote_id(); + // if update.project_id != project_id && update.project_id.is_some() { + // return; + // } + // workspace.handle_update_followers(leader_id, update.clone(), cx); + // }); + // } + // Ok(()) + // })? + todo!() + } } -// async fn handle_follow( -// this: ModelHandle, -// envelope: TypedEnvelope, -// _: Arc, -// mut cx: AsyncAppContext, -// ) -> Result { -// this.update(&mut cx, |this, cx| { -// let follower = Follower { -// project_id: envelope.payload.project_id, -// peer_id: envelope.original_sender_id()?, -// }; -// let active_project = ActiveCall::global(cx) -// .read(cx) -// .location() -// .map(|project| project.id()); - -// let mut response = proto::FollowResponse::default(); -// for workspace in &this.workspaces { -// let Some(workspace) = workspace.upgrade(cx) else { -// continue; -// }; - -// workspace.update(cx.as_mut(), |workspace, cx| { -// let handler_response = workspace.handle_follow(follower.project_id, cx); -// if response.views.is_empty() { -// response.views = handler_response.views; -// } else { -// response.views.extend_from_slice(&handler_response.views); -// } - -// if let Some(active_view_id) = handler_response.active_view_id.clone() { -// if response.active_view_id.is_none() -// || Some(workspace.project.id()) == active_project -// { -// response.active_view_id = Some(active_view_id); -// } -// } -// }); -// } - -// if let Err(ix) = this.followers.binary_search(&follower) { -// this.followers.insert(ix, follower); -// } - -// Ok(response) -// }) -// } - -// async fn handle_unfollow( -// this: ModelHandle, -// envelope: TypedEnvelope, -// _: Arc, -// mut cx: AsyncAppContext, -// ) -> Result<()> { -// this.update(&mut cx, |this, _| { -// let follower = Follower { -// project_id: envelope.payload.project_id, -// peer_id: envelope.original_sender_id()?, -// }; -// if let Ok(ix) = this.followers.binary_search(&follower) { -// this.followers.remove(ix); -// } -// Ok(()) -// }) -// } - -// async fn handle_update_followers( -// this: ModelHandle, -// envelope: TypedEnvelope, -// _: Arc, -// mut cx: AsyncAppContext, -// ) -> Result<()> { -// let leader_id = envelope.original_sender_id()?; -// let update = envelope.payload; -// this.update(&mut cx, |this, cx| { -// for workspace in &this.workspaces { -// let Some(workspace) = workspace.upgrade(cx) else { -// continue; -// }; -// workspace.update(cx.as_mut(), |workspace, cx| { -// let project_id = workspace.project.read(cx).remote_id(); -// if update.project_id != project_id && update.project_id.is_some() { -// return; -// } -// workspace.handle_update_followers(leader_id, update.clone(), cx); -// }); -// } -// Ok(()) -// }) -// } -// } - // impl Entity for WorkspaceStore { // type Event = (); // } @@ -4320,7 +4325,7 @@ pub fn open_paths( .await; if let Some(existing) = existing { - // Ok(( + // // Ok(( // existing.clone(), // cx.update_window_root(&existing, |workspace, cx| { // workspace.open_paths(abs_paths, true, cx) diff --git a/crates/zed2/src/main.rs b/crates/zed2/src/main.rs index 64946e1829..a736a20574 100644 --- a/crates/zed2/src/main.rs +++ b/crates/zed2/src/main.rs @@ -12,7 +12,7 @@ use client2::UserStore; use db2::kvp::KEY_VALUE_STORE; use fs2::RealFs; use futures::{channel::mpsc, SinkExt, StreamExt}; -use gpui2::{App, AppContext, AsyncAppContext, Context, SemanticVersion, Task}; +use gpui2::{Action, App, AppContext, AsyncAppContext, Context, SemanticVersion, Task}; use isahc::{prelude::Configurable, Request}; use language2::LanguageRegistry; use log::LevelFilter; @@ -45,7 +45,7 @@ use util::{ paths, ResultExt, }; use uuid::Uuid; -use workspace2::AppState; +use workspace2::{AppState, WorkspaceStore}; use zed2::languages; use zed2::{ensure_only_instance, Assets, IsOnlyInstance}; @@ -120,7 +120,7 @@ fn main() { language2::init(cx); languages::init(languages.clone(), node_runtime.clone(), cx); let user_store = cx.build_model(|cx| UserStore::new(client.clone(), http.clone(), cx)); - // let workspace_store = cx.add_model(|cx| WorkspaceStore::new(client.clone(), cx)); + let workspace_store = cx.build_model(|cx| WorkspaceStore::new(client.clone(), cx)); cx.set_global(client.clone()); @@ -165,20 +165,18 @@ fn main() { // client.telemetry().start(installation_id, session_id, cx); - // todo!("app_state") - let app_state: Arc = todo!(); - // let app_state = Arc::new(AppState { - // languages, - // client: client.clone(), - // user_store, - // fs, - // build_window_options, - // initialize_workspace, - // background_actions, - // workspace_store, - // node_runtime, - // }); - // cx.set_global(Arc::downgrade(&app_state)); + let app_state = Arc::new(AppState { + languages, + client: client.clone(), + user_store, + fs, + build_window_options, + initialize_workspace, + background_actions, + workspace_store, + node_runtime, + }); + cx.set_global(Arc::downgrade(&app_state)); // audio::init(Assets, cx); // auto_update::init(http.clone(), client::ZED_SERVER_URL.clone(), cx); @@ -914,11 +912,13 @@ async fn handle_cli_connection( } } -// pub fn background_actions() -> &'static [(&'static str, &'static dyn Action)] { -// &[ -// ("Go to file", &file_finder::Toggle), -// ("Open command palette", &command_palette::Toggle), -// ("Open recent projects", &recent_projects::OpenRecent), -// ("Change your settings", &zed_actions::OpenSettings), -// ] -// } +pub fn background_actions() -> &'static [(&'static str, &'static dyn Action)] { + // &[ + // ("Go to file", &file_finder::Toggle), + // ("Open command palette", &command_palette::Toggle), + // ("Open recent projects", &recent_projects::OpenRecent), + // ("Change your settings", &zed_actions::OpenSettings), + // ] + // todo!() + &[] +} diff --git a/crates/zed2/src/zed2.rs b/crates/zed2/src/zed2.rs index 729102046f..4f9aad042f 100644 --- a/crates/zed2/src/zed2.rs +++ b/crates/zed2/src/zed2.rs @@ -5,7 +5,10 @@ mod open_listener; pub use assets::*; use collections::HashMap; -use gpui2::{AsyncAppContext, Point}; +use gpui2::{ + point, px, AsyncAppContext, Point, Styled, TitlebarOptions, WindowBounds, WindowKind, + WindowOptions, +}; pub use only_instance::*; pub use open_listener::*; @@ -20,6 +23,7 @@ use futures::{ }; use std::{path::Path, sync::Arc, thread, time::Duration}; use util::{paths::PathLikeWithPosition, ResultExt}; +use uuid::Uuid; use workspace2::AppState; pub fn connect_to_cli( @@ -211,3 +215,27 @@ pub async fn handle_cli_connection( } } } + +pub fn build_window_options( + bounds: Option, + display: Option, + platform: &dyn Platform, +) -> WindowOptions { + let bounds = bounds.unwrap_or(WindowBounds::Maximized); + let display_id = display.and_then(|display| platform.screen_by_id(display)); + + WindowOptions { + bounds, + titlebar: Some(TitlebarOptions { + title: None, + appears_transparent: true, + traffic_light_position: Some(point(px(8.), px(8.))), + }), + center: false, + focus: false, + show: false, + kind: WindowKind::Normal, + is_movable: false, + display_id, + } +}