Add application menu item to open the default settings

This commit is contained in:
Max Brunsfeld 2022-07-12 13:38:24 -07:00
parent 8464c03e65
commit 7750054a45
3 changed files with 115 additions and 22 deletions

View file

@ -52,6 +52,7 @@ actions!(
DebugElements,
OpenSettings,
OpenKeymap,
OpenDefaultSettings,
OpenDefaultKeymap,
IncreaseBufferFontSize,
DecreaseBufferFontSize,
@ -111,27 +112,27 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
cx.add_action({
let app_state = app_state.clone();
move |workspace: &mut Workspace, _: &OpenDefaultKeymap, cx: &mut ViewContext<Workspace>| {
workspace.with_local_workspace(cx, app_state.clone(), |workspace, cx| {
let project = workspace.project().clone();
let buffer = project.update(cx, |project, cx| {
let text = Assets::get("keymaps/default.json").unwrap().data;
let text = str::from_utf8(text.as_ref()).unwrap();
project
.create_buffer(text, project.languages().get_language("JSON"), cx)
.expect("creating buffers on a local workspace always succeeds")
});
let buffer = cx.add_model(|cx| {
MultiBuffer::singleton(buffer, cx).with_title("Default Key Bindings".into())
});
workspace.add_item(
Box::new(
cx.add_view(|cx| {
Editor::for_multibuffer(buffer, Some(project.clone()), cx)
}),
),
cx,
);
});
open_bundled_config_file(
workspace,
app_state.clone(),
"keymaps/default.json",
"Default Key Bindings",
cx,
);
}
});
cx.add_action({
let app_state = app_state.clone();
move |workspace: &mut Workspace,
_: &OpenDefaultSettings,
cx: &mut ViewContext<Workspace>| {
open_bundled_config_file(
workspace,
app_state.clone(),
"default-settings.json",
"Default Settings",
cx,
);
}
});
cx.add_action(
@ -386,6 +387,30 @@ fn open_config_file(
.detach_and_log_err(cx)
}
fn open_bundled_config_file(
workspace: &mut Workspace,
app_state: Arc<AppState>,
asset_path: &'static str,
title: &str,
cx: &mut ViewContext<Workspace>,
) {
workspace.with_local_workspace(cx, app_state.clone(), |workspace, cx| {
let project = workspace.project().clone();
let buffer = project.update(cx, |project, cx| {
let text = Assets::get(asset_path).unwrap().data;
let text = str::from_utf8(text.as_ref()).unwrap();
project
.create_buffer(text, project.languages().get_language("JSON"), cx)
.expect("creating buffers on a local workspace always succeeds")
});
let buffer = cx.add_model(|cx| MultiBuffer::singleton(buffer, cx).with_title(title.into()));
workspace.add_item(
Box::new(cx.add_view(|cx| Editor::for_multibuffer(buffer, Some(project.clone()), cx))),
cx,
);
});
}
#[cfg(test)]
mod tests {
use super::*;