-
Notifications
You must be signed in to change notification settings - Fork 1
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 #6 from luxluth/feat/svg-png-icons-path
Feat: svg png icons path
- Loading branch information
Showing
3 changed files
with
50 additions
and
79 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,13 +1,39 @@ | ||
use std::io::Read; | ||
|
||
use crate::resources; | ||
use gtk::gio; | ||
use gtk::{ | ||
gdk_pixbuf::Pixbuf, | ||
gio::{self, Icon}, | ||
}; | ||
|
||
pub fn load_image_resource(content: &[u8], size: (i32, i32)) -> Icon { | ||
let cursor = gtk::gio::MemoryInputStream::from_bytes(>k::glib::Bytes::from(content)); | ||
Icon::from( | ||
Pixbuf::from_stream_at_scale( | ||
&cursor, | ||
size.0, | ||
size.1, | ||
true, | ||
None::<>k::gio::Cancellable>, | ||
) | ||
.unwrap(), | ||
) | ||
} | ||
|
||
pub fn get_icon(name: &str) -> gio::Icon { | ||
pub fn get_icon(name_or_path: &str) -> Icon { | ||
let mut res = resources::ICON_MAP.get().write().unwrap(); | ||
if let Some(e) = res.get(name) { | ||
if let Some(e) = res.get(name_or_path) { | ||
return e.clone(); | ||
} else { | ||
let icon = gio::Icon::from(gio::ThemedIcon::from_names(&[name])); | ||
res.insert(name.to_string(), icon.clone()); | ||
if std::path::Path::new(name_or_path).exists() { | ||
if let Ok(mut f) = std::fs::File::open(std::path::Path::new(name_or_path)) { | ||
let mut buf = vec![]; | ||
let _ = f.read_to_end(&mut buf); | ||
return load_image_resource(&buf, (512, 512)); | ||
} | ||
} | ||
let icon = Icon::from(gio::ThemedIcon::from_names(&[name_or_path])); | ||
res.insert(name_or_path.to_string(), icon.clone()); | ||
return icon; | ||
} | ||
} |
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