Make file types live reload

This commit is contained in:
Mikayla Maki 2023-07-17 13:09:47 -07:00
parent 96ef6ab326
commit 8c855680e7
No known key found for this signature in database
4 changed files with 68 additions and 33 deletions

View file

@ -67,6 +67,9 @@
"log": "log" "log": "log"
}, },
"types": { "types": {
"default": {
"icon": "icons/file_icons/quill/file.svg"
},
"directory": { "directory": {
"icon": "icons/file_icons/quill/folder.svg" "icon": "icons/file_icons/quill/folder.svg"
}, },

View file

@ -4,6 +4,7 @@ use collections::HashMap;
use gpui::{AppContext, AssetSource}; use gpui::{AppContext, AssetSource};
use serde_derive::Deserialize; use serde_derive::Deserialize;
use util::iife;
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
struct TypeConfig { struct TypeConfig {
@ -16,9 +17,9 @@ pub struct FileAssociations {
types: HashMap<String, TypeConfig>, types: HashMap<String, TypeConfig>,
} }
pub const TEXT_FILE_ASSET: &'static str = "icons/file_icons/quill/file.svg";
const DIRECTORY_TYPE: &'static str = "directory"; const DIRECTORY_TYPE: &'static str = "directory";
const EXPANDED_DIRECTORY_TYPE: &'static str = "expanded_directory"; const EXPANDED_DIRECTORY_TYPE: &'static str = "expanded_directory";
pub const FILE_TYPES_ASSET: &'static str = "icons/file_icons/file_types.json";
pub fn init(assets: impl AssetSource, cx: &mut AppContext) { pub fn init(assets: impl AssetSource, cx: &mut AppContext) {
cx.set_global(FileAssociations::new(assets)) cx.set_global(FileAssociations::new(assets))
@ -28,8 +29,9 @@ impl FileAssociations {
pub fn new(assets: impl AssetSource) -> Self { pub fn new(assets: impl AssetSource) -> Self {
assets assets
.load("icons/file_icons/file_types.json") .load("icons/file_icons/file_types.json")
.map(|file| { .and_then(|file| {
serde_json::from_str::<FileAssociations>(str::from_utf8(&file).unwrap()).unwrap() serde_json::from_str::<FileAssociations>(str::from_utf8(&file).unwrap())
.map_err(Into::into)
}) })
.unwrap_or_else(|_| FileAssociations { .unwrap_or_else(|_| FileAssociations {
suffixes: HashMap::default(), suffixes: HashMap::default(),
@ -37,35 +39,37 @@ impl FileAssociations {
}) })
} }
pub fn get_icon(path: &Path, cx: &AppContext) -> Option<Arc<str>> { pub fn get_icon(path: &Path, cx: &AppContext) -> Arc<str> {
if !cx.has_global::<Self>() { iife!({
return None; let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
}
let this = cx.global::<Self>(); iife!({
let suffix = path.extension()?.to_str()?; let suffix = path.extension()?.to_str()?;
this.suffixes this.suffixes
.get(suffix) .get(suffix)
.and_then(|type_str| this.types.get(type_str)) .and_then(|type_str| this.types.get(type_str))
.map(|type_config| type_config.icon.clone()) .map(|type_config| type_config.icon.clone())
})
.or_else(|| this.types.get("default").map(|config| config.icon.clone()))
})
.unwrap_or_else(|| Arc::from("".to_string()))
} }
pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> { pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Arc<str> {
if !cx.has_global::<Self>() { iife!({
return None; let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
}
let this = cx.global::<Self>(); let key = if expanded {
EXPANDED_DIRECTORY_TYPE
} else {
DIRECTORY_TYPE
};
let key = if expanded { this.types
EXPANDED_DIRECTORY_TYPE .get(key)
} else { .map(|type_config| type_config.icon.clone())
DIRECTORY_TYPE })
}; .unwrap_or_else(|| Arc::from("".to_string()))
this.types
.get(key)
.map(|type_config| type_config.icon.clone())
} }
} }

View file

@ -1,11 +1,12 @@
mod file_associations; pub mod file_associations;
mod project_panel_settings; mod project_panel_settings;
use context_menu::{ContextMenu, ContextMenuItem}; use context_menu::{ContextMenu, ContextMenuItem};
use db::kvp::KEY_VALUE_STORE; use db::kvp::KEY_VALUE_STORE;
use drag_and_drop::{DragAndDrop, Draggable}; use drag_and_drop::{DragAndDrop, Draggable};
use editor::{Cancel, Editor}; use editor::{Cancel, Editor};
use file_associations::{FileAssociations, TEXT_FILE_ASSET}; use file_associations::FileAssociations;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use gpui::{ use gpui::{
actions, actions,
@ -234,6 +235,10 @@ impl ProjectPanel {
}) })
.detach(); .detach();
cx.observe_global::<FileAssociations, _>(|_, cx| {
cx.notify();
}).detach();
let view_id = cx.view_id(); let view_id = cx.view_id();
let mut this = Self { let mut this = Self {
project: project.clone(), project: project.clone(),
@ -1189,11 +1194,9 @@ impl ProjectPanel {
let is_expanded = expanded_entry_ids.binary_search(&entry.id).is_ok(); let is_expanded = expanded_entry_ids.binary_search(&entry.id).is_ok();
let icon = show_file_icons let icon = show_file_icons
.then(|| match entry.kind { .then(|| match entry.kind {
EntryKind::File(_) => FileAssociations::get_icon(&entry.path, cx) EntryKind::File(_) => FileAssociations::get_icon(&entry.path, cx),
.or_else(|| Some(TEXT_FILE_ASSET.into())),
_ => FileAssociations::get_folder_icon(is_expanded, cx), _ => FileAssociations::get_folder_icon(is_expanded, cx),
}) });
.flatten();
let mut details = EntryDetails { let mut details = EntryDetails {
filename: entry filename: entry

View file

@ -166,6 +166,7 @@ fn main() {
cx.spawn(|cx| watch_themes(fs.clone(), cx)).detach(); cx.spawn(|cx| watch_themes(fs.clone(), cx)).detach();
cx.spawn(|_| watch_languages(fs.clone(), languages.clone())) cx.spawn(|_| watch_languages(fs.clone(), languages.clone()))
.detach(); .detach();
watch_file_types(fs.clone(), cx);
languages.set_theme(theme::current(cx).clone()); languages.set_theme(theme::current(cx).clone());
cx.observe_global::<SettingsStore, _>({ cx.observe_global::<SettingsStore, _>({
@ -685,6 +686,25 @@ async fn watch_languages(fs: Arc<dyn Fs>, languages: Arc<LanguageRegistry>) -> O
Some(()) Some(())
} }
#[cfg(debug_assertions)]
fn watch_file_types(fs: Arc<dyn Fs>, cx: &mut AppContext) {
cx.spawn(|mut cx| async move {
let mut events = fs
.watch(
"assets/icons/file_icons/file_types.json".as_ref(),
Duration::from_millis(100),
)
.await;
while (events.next().await).is_some() {
cx.update(|cx| {
cx.update_global(|file_types, _| {
*file_types = project_panel::file_associations::FileAssociations::new(Assets);
});
})
}
}).detach()
}
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
async fn watch_themes(_fs: Arc<dyn Fs>, _cx: AsyncAppContext) -> Option<()> { async fn watch_themes(_fs: Arc<dyn Fs>, _cx: AsyncAppContext) -> Option<()> {
None None
@ -695,6 +715,11 @@ async fn watch_languages(_: Arc<dyn Fs>, _: Arc<LanguageRegistry>) -> Option<()>
None None
} }
#[cfg(not(debug_assertions))]
fn watch_file_types(fs: Arc<dyn Fs>, cx: &mut AppContext) {
None
}
fn connect_to_cli( fn connect_to_cli(
server_name: &str, server_name: &str,
) -> Result<(mpsc::Receiver<CliRequest>, IpcSender<CliResponse>)> { ) -> Result<(mpsc::Receiver<CliRequest>, IpcSender<CliResponse>)> {