file_icons.rs

  1use std::{path::Path, str, sync::Arc};
  2
  3use collections::HashMap;
  4
  5use gpui::{AppContext, AssetSource, Global};
  6use serde_derive::Deserialize;
  7use util::{maybe, paths::PathExt};
  8
  9#[derive(Deserialize, Debug)]
 10struct TypeConfig {
 11    icon: Arc<str>,
 12}
 13
 14#[derive(Deserialize, Debug)]
 15pub struct FileIcons {
 16    stems: HashMap<String, String>,
 17    suffixes: HashMap<String, String>,
 18    types: HashMap<String, TypeConfig>,
 19}
 20
 21impl Global for FileIcons {}
 22
 23const COLLAPSED_DIRECTORY_TYPE: &str = "collapsed_folder";
 24const EXPANDED_DIRECTORY_TYPE: &str = "expanded_folder";
 25const COLLAPSED_CHEVRON_TYPE: &str = "collapsed_chevron";
 26const EXPANDED_CHEVRON_TYPE: &str = "expanded_chevron";
 27pub const FILE_TYPES_ASSET: &str = "icons/file_icons/file_types.json";
 28
 29pub fn init(assets: impl AssetSource, cx: &mut AppContext) {
 30    cx.set_global(FileIcons::new(assets))
 31}
 32
 33impl FileIcons {
 34    pub fn get(cx: &AppContext) -> &Self {
 35        cx.global::<FileIcons>()
 36    }
 37
 38    pub fn new(assets: impl AssetSource) -> Self {
 39        assets
 40            .load("icons/file_icons/file_types.json")
 41            .and_then(|file| {
 42                serde_json::from_str::<FileIcons>(str::from_utf8(&file).unwrap())
 43                    .map_err(Into::into)
 44            })
 45            .unwrap_or_else(|_| FileIcons {
 46                stems: HashMap::default(),
 47                suffixes: HashMap::default(),
 48                types: HashMap::default(),
 49            })
 50    }
 51
 52    pub fn get_icon(path: &Path, cx: &AppContext) -> Option<Arc<str>> {
 53        let this = cx.try_global::<Self>()?;
 54
 55        // FIXME: Associate a type with the languages and have the file's language
 56        //        override these associations
 57        maybe!({
 58            let suffix = path.icon_stem_or_suffix()?;
 59
 60            if let Some(type_str) = this.stems.get(suffix) {
 61                return this.get_type_icon(type_str);
 62            }
 63
 64            this.suffixes
 65                .get(suffix)
 66                .and_then(|type_str| this.get_type_icon(type_str))
 67        })
 68        .or_else(|| this.get_type_icon("default"))
 69    }
 70
 71    pub fn get_type_icon(&self, typ: &str) -> Option<Arc<str>> {
 72        self.types
 73            .get(typ)
 74            .map(|type_config| type_config.icon.clone())
 75    }
 76
 77    pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> {
 78        let this = cx.try_global::<Self>()?;
 79
 80        let key = if expanded {
 81            EXPANDED_DIRECTORY_TYPE
 82        } else {
 83            COLLAPSED_DIRECTORY_TYPE
 84        };
 85
 86        this.get_type_icon(key)
 87    }
 88
 89    pub fn get_chevron_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> {
 90        let this = cx.try_global::<Self>()?;
 91
 92        let key = if expanded {
 93            EXPANDED_CHEVRON_TYPE
 94        } else {
 95            COLLAPSED_CHEVRON_TYPE
 96        };
 97
 98        this.get_type_icon(key)
 99    }
100}