Pre-parse svgs

This commit is contained in:
Dániel Buga 2022-05-12 22:46:06 +02:00
parent 0dc4bdf7c4
commit be2f1269c8
1 changed files with 17 additions and 23 deletions

View File

@ -1,10 +1,9 @@
use std::{collections::HashMap, ffi::OsStr, path::Path, str::FromStr, sync::Arc}; use std::{collections::HashMap, ffi::OsStr, path::Path, str::FromStr};
use druid::{piet::Svg, Color}; use druid::{piet::Svg, Color};
use include_dir::{include_dir, Dir}; use include_dir::{include_dir, Dir};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use lsp_types::{CompletionItemKind, SymbolKind}; use lsp_types::{CompletionItemKind, SymbolKind};
use parking_lot::Mutex;
use lapce_data::config::{Config, LOGO}; use lapce_data::config::{Config, LOGO};
@ -15,39 +14,34 @@
} }
struct SvgStore { struct SvgStore {
svgs: Arc<Mutex<HashMap<&'static str, Option<Svg>>>>, svgs: HashMap<&'static str, Option<Svg>>,
} }
impl SvgStore { impl SvgStore {
fn new() -> Self { fn new() -> Self {
Self { let mut svgs = HashMap::new();
svgs: Arc::new(Mutex::new(HashMap::new())), svgs.insert("lapce_logo", Svg::from_str(LOGO).ok());
for file in ICONS_DIR.files() {
if let Some(file_name) = file.path().file_name().and_then(OsStr::to_str)
{
let svg =
file.contents_utf8().and_then(|str| Svg::from_str(str).ok());
svgs.insert(file_name, svg);
} }
} }
fn get_svg_or_insert_with( Self { svgs }
&self,
name: &'static str,
create_svg: impl FnOnce() -> Option<Svg>,
) -> Option<Svg> {
self.svgs
.lock()
.entry(name)
.or_insert_with(create_svg)
.clone()
} }
fn get_svg(&self, name: &'static str) -> Option<Svg> { fn get_svg(&self, name: &'static str) -> Option<Svg> {
self.get_svg_or_insert_with(name, || { self.svgs.get(name).and_then(Clone::clone)
Svg::from_str(ICONS_DIR.get_file(name)?.contents_utf8()?).ok()
})
} }
} }
pub fn logo_svg() -> Svg { pub fn logo_svg() -> Svg {
SVG_STORE get_svg("lapce_logo").unwrap()
.get_svg_or_insert_with("lapce_logo", || Svg::from_str(LOGO).ok())
.unwrap()
} }
pub fn get_svg(name: &'static str) -> Option<Svg> { pub fn get_svg(name: &'static str) -> Option<Svg> {