Skip to content

Commit

Permalink
Convert initialize_icons to function
Browse files Browse the repository at this point in the history
  • Loading branch information
dastansam committed Aug 27, 2024
1 parent f24bb69 commit 91adb0e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ mod icon_names {

fn main() {
///...///
relm4_icons::initialize_icons!(icon_names::BASE_RESOURCE_PATH, icon_names::APP_ID);
relm4_icons::initialize_icons(icon_names::GRESOURCE_PATH, icon_names::APP_ID, Some(icon_names::BASE_RESOURCE_PATH));
}
```

Expand Down
4 changes: 4 additions & 0 deletions build_icons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ pub fn bundle_icons<P, I, S>(
"pub(crate) const BASE_RESOURCE_PATH: &str = \"{}\";",
base_resource_path.unwrap_or_default()
)])
.chain([format!(
"pub(crate) const GRESOURCE_PATH: &str = \"{}\";",
format!("{}/{}", out_dir, TARGET_FILE)
)])
.collect();

std::fs::write(Path::new(&out_dir).join(constants_file), constants).unwrap();
Expand Down
37 changes: 23 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,30 @@
#![allow(clippy::negative_feature_names, clippy::multiple_crate_versions)]
#![cfg_attr(docsrs, feature(doc_cfg))]

/// Initialized the icons and registers them globally for your application.
#[macro_export]
macro_rules! initialize_icons {
($base_resource_path:path, $app_id:path) => {
use gtk::{gdk, gio};
use gtk::{
gdk,
gio::{resources_register, Resource},
glib,
};
use std::path::Path;

gio::resources_register_include!("resources.gresource").unwrap();
/// Initialized the icons and registers them globally for your application.
pub fn initialize_icons<P: AsRef<Path>>(
gresource_path: P,
app_id: &str,
base_resource_path: Option<P>,
) {
let gresource_bytes = std::fs::read(gresource_path).unwrap();
let bytes = glib::Bytes::from(&gresource_bytes);
let resource = Resource::from_data(&bytes).unwrap();
resources_register(&resource);

if $base_resource_path.is_empty() && $app_id.is_empty() {
gtk::init().unwrap();
gtk::init().unwrap();

let display = gdk::Display::default().unwrap();
let theme = gtk::IconTheme::for_display(&display);
theme.add_resource_path("/org/gtkrs/icons/");
theme.add_resource_path("/org/gtkrs/icons/scalable/actions/");
}
};
if base_resource_path.is_none() && app_id.is_empty() {
let display = gdk::Display::default().unwrap();
let theme = gtk::IconTheme::for_display(&display);
theme.add_resource_path("/org/gtkrs/icons/");
theme.add_resource_path("/org/gtkrs/icons/scalable/actions/");
}
}

0 comments on commit 91adb0e

Please sign in to comment.