Files
oak-gpui/zed/src/workspace/mod.rs
T
Nathan Sobo 97a8a8ed43 Remove unsafe code from App::test_async
I don't actually think it was correct to allow the future to borrow a mutable app reference. I went back to passing a wrapper around the refcell to async tests. They'll be a bit more annoying to write but also totally safe.
2021-04-10 00:05:09 -06:00

134 lines
3.8 KiB
Rust

pub mod pane;
pub mod pane_group;
pub mod workspace;
pub mod workspace_view;
pub use pane::*;
pub use pane_group::*;
pub use workspace::*;
pub use workspace_view::*;
use crate::{settings::Settings, watch};
use gpui::MutableAppContext;
use std::path::PathBuf;
pub fn init(app: &mut MutableAppContext) {
app.add_global_action("workspace:open_paths", open_paths);
app.add_global_action("app:quit", quit);
pane::init(app);
workspace_view::init(app);
}
pub struct OpenParams {
pub paths: Vec<PathBuf>,
pub settings: watch::Receiver<Settings>,
}
fn open_paths(params: &OpenParams, app: &mut MutableAppContext) {
log::info!("open paths {:?}", params.paths);
// Open paths in existing workspace if possible
for window_id in app.window_ids().collect::<Vec<_>>() {
if let Some(handle) = app.root_view::<WorkspaceView>(window_id) {
if handle.update(app, |view, ctx| {
if view.contains_paths(&params.paths, ctx.app()) {
view.open_paths(&params.paths, ctx.app_mut());
log::info!("open paths on existing workspace");
true
} else {
false
}
}) {
return;
}
}
}
log::info!("open new workspace");
// Add a new workspace if necessary
let workspace = app.add_model(|ctx| Workspace::new(params.paths.clone(), ctx));
app.add_window(|ctx| WorkspaceView::new(workspace, params.settings.clone(), ctx));
}
fn quit(_: &(), app: &mut MutableAppContext) {
app.platform().quit();
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{settings, test::*};
use gpui::App;
use serde_json::json;
#[test]
fn test_open_paths_action() {
App::test((), |app| {
let settings = settings::channel(&app.font_cache()).unwrap().1;
init(app);
let dir = temp_tree(json!({
"a": {
"aa": null,
"ab": null,
},
"b": {
"ba": null,
"bb": null,
},
"c": {
"ca": null,
"cb": null,
},
}));
app.dispatch_global_action(
"workspace:open_paths",
OpenParams {
paths: vec![
dir.path().join("a").to_path_buf(),
dir.path().join("b").to_path_buf(),
],
settings: settings.clone(),
},
);
assert_eq!(app.window_ids().count(), 1);
app.dispatch_global_action(
"workspace:open_paths",
OpenParams {
paths: vec![dir.path().join("a").to_path_buf()],
settings: settings.clone(),
},
);
assert_eq!(app.window_ids().count(), 1);
let workspace_view_1 = app
.root_view::<WorkspaceView>(app.window_ids().next().unwrap())
.unwrap();
assert_eq!(
workspace_view_1
.read(app)
.workspace
.read(app)
.worktrees()
.len(),
2
);
app.dispatch_global_action(
"workspace:open_paths",
OpenParams {
paths: vec![
dir.path().join("b").to_path_buf(),
dir.path().join("c").to_path_buf(),
],
settings: settings.clone(),
},
);
assert_eq!(app.window_ids().count(), 2);
});
}
}