Define base keymap setting in welcome crate

This commit is contained in:
Max Brunsfeld
2023-05-17 11:23:09 -07:00
parent 89204e85c0
commit 5c729c0e56
12 changed files with 334 additions and 297 deletions
+6 -5
View File
@@ -1,3 +1,4 @@
use super::base_keymap_setting::BaseKeymap;
use fuzzy::{match_strings, StringMatch, StringMatchCandidate};
use gpui::{
actions,
@@ -6,7 +7,7 @@ use gpui::{
};
use picker::{Picker, PickerDelegate, PickerEvent};
use project::Fs;
use settings::{update_settings_file, BaseKeymap, Settings};
use settings::{update_settings_file, Settings};
use std::sync::Arc;
use util::ResultExt;
use workspace::Workspace;
@@ -39,10 +40,10 @@ pub struct BaseKeymapSelectorDelegate {
impl BaseKeymapSelectorDelegate {
fn new(fs: Arc<dyn Fs>, cx: &mut ViewContext<BaseKeymapSelector>) -> Self {
let base = cx.global::<Settings>().base_keymap;
let base = settings::get_setting::<BaseKeymap>(None, cx);
let selected_index = BaseKeymap::OPTIONS
.iter()
.position(|(_, value)| *value == base)
.position(|(_, value)| value == base)
.unwrap_or(0);
Self {
matches: Vec::new(),
@@ -122,8 +123,8 @@ impl PickerDelegate for BaseKeymapSelectorDelegate {
fn confirm(&mut self, cx: &mut ViewContext<BaseKeymapSelector>) {
if let Some(selection) = self.matches.get(self.selected_index) {
let base_keymap = BaseKeymap::from_names(&selection.string);
update_settings_file::<Settings>(self.fs.clone(), cx, move |settings| {
settings.base_keymap = Some(base_keymap)
update_settings_file::<BaseKeymap>(self.fs.clone(), cx, move |setting| {
*setting = Some(base_keymap)
});
}
cx.emit(PickerEvent::Dismiss);
+65
View File
@@ -0,0 +1,65 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use settings::Setting;
#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Default)]
pub enum BaseKeymap {
#[default]
VSCode,
JetBrains,
SublimeText,
Atom,
TextMate,
}
impl BaseKeymap {
pub const OPTIONS: [(&'static str, Self); 5] = [
("VSCode (Default)", Self::VSCode),
("Atom", Self::Atom),
("JetBrains", Self::JetBrains),
("Sublime Text", Self::SublimeText),
("TextMate", Self::TextMate),
];
pub fn asset_path(&self) -> Option<&'static str> {
match self {
BaseKeymap::JetBrains => Some("keymaps/jetbrains.json"),
BaseKeymap::SublimeText => Some("keymaps/sublime_text.json"),
BaseKeymap::Atom => Some("keymaps/atom.json"),
BaseKeymap::TextMate => Some("keymaps/textmate.json"),
BaseKeymap::VSCode => None,
}
}
pub fn names() -> impl Iterator<Item = &'static str> {
Self::OPTIONS.iter().map(|(name, _)| *name)
}
pub fn from_names(option: &str) -> BaseKeymap {
Self::OPTIONS
.iter()
.copied()
.find_map(|(name, value)| (name == option).then(|| value))
.unwrap_or_default()
}
}
impl Setting for BaseKeymap {
const KEY: Option<&'static str> = Some("base_keymap");
type FileContent = Option<Self>;
fn load(
default_value: &Self::FileContent,
user_values: &[&Self::FileContent],
_: &gpui::AppContext,
) -> anyhow::Result<Self>
where
Self: Sized,
{
Ok(user_values
.first()
.and_then(|v| **v)
.unwrap_or(default_value.unwrap()))
}
}
+6 -4
View File
@@ -1,7 +1,7 @@
mod base_keymap_picker;
mod base_keymap_setting;
use std::{borrow::Cow, sync::Arc};
use crate::base_keymap_picker::ToggleBaseKeymapSelector;
use client::TelemetrySettings;
use db::kvp::KEY_VALUE_STORE;
use gpui::{
@@ -9,17 +9,19 @@ use gpui::{
AnyElement, AppContext, Element, Entity, Subscription, View, ViewContext, WeakViewHandle,
};
use settings::{update_settings_file, Settings};
use std::{borrow::Cow, sync::Arc};
use workspace::{
item::Item, open_new, sidebar::SidebarSide, AppState, PaneBackdrop, Welcome, Workspace,
WorkspaceId,
};
use crate::base_keymap_picker::ToggleBaseKeymapSelector;
pub use base_keymap_setting::BaseKeymap;
pub const FIRST_OPEN: &str = "first_open";
pub fn init(cx: &mut AppContext) {
settings::register_setting::<BaseKeymap>(cx);
cx.add_action(|workspace: &mut Workspace, _: &Welcome, cx| {
let welcome_page = cx.add_view(|cx| WelcomePage::new(workspace, cx));
workspace.add_item(Box::new(welcome_page), cx)