generated from cosmic-utils/cosmic-app-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from ahoneybun/fix-blocking-code
fix: blocking code
- Loading branch information
Showing
6 changed files
with
120 additions
and
17 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
use cosmic::widget::icon; | ||
use std::collections::HashMap; | ||
use std::sync::{Mutex, OnceLock}; | ||
|
||
pub(crate) static ICON_CACHE: OnceLock<Mutex<IconCache>> = OnceLock::new(); | ||
|
||
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] | ||
pub struct IconCacheKey { | ||
name: &'static str, | ||
size: u16, | ||
} | ||
|
||
pub struct IconCache { | ||
cache: HashMap<IconCacheKey, icon::Handle>, | ||
} | ||
|
||
impl IconCache { | ||
pub fn new() -> Self { | ||
let mut cache = HashMap::new(); | ||
|
||
macro_rules! bundle { | ||
($name:expr, $size:expr) => { | ||
let data: &'static [u8] = | ||
include_bytes!(concat!("../../res/icons/bundled/", $name, ".svg")); | ||
cache.insert( | ||
IconCacheKey { | ||
name: $name, | ||
size: $size, | ||
}, | ||
icon::from_svg_bytes(data).symbolic(true), | ||
); | ||
}; | ||
} | ||
|
||
bundle!("timer-sand-symbolic", 18); | ||
bundle!("harddisk-symbolic", 18); | ||
|
||
Self { cache } | ||
} | ||
|
||
fn get_icon(&mut self, name: &'static str, size: u16) -> icon::Icon { | ||
let handle = self | ||
.cache | ||
.entry(IconCacheKey { name, size }) | ||
.or_insert_with(|| icon::from_name(name).size(size).handle()) | ||
.clone(); | ||
icon::icon(handle).size(size) | ||
} | ||
|
||
pub fn get(name: &'static str, size: u16) -> icon::Icon { | ||
let mut icon_cache = ICON_CACHE.get().unwrap().lock().unwrap(); | ||
icon_cache.get_icon(name, size) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters