Skip to content

Instantly share code, notes, and snippets.

@bugaevc
Created August 16, 2018 09:10
Show Gist options
  • Save bugaevc/5950fc996c46b9d16c4e330bcb653fee to your computer and use it in GitHub Desktop.
Save bugaevc/5950fc996c46b9d16c4e330bcb653fee to your computer and use it in GitHub Desktop.
gio-rs resources
diff --git a/src/main.rs b/src/main.rs
index ac542de..e40605d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,12 +16,10 @@ use futures_timer::*;
use gio::prelude::*;
use gtk::prelude::*;
use librgs::ServerEntry;
-use static_resources::Resources;
use std::sync::{
mpsc::{channel, TryRecvError},
Arc, Mutex,
};
-use std::rc::Rc;
mod static_resources;
mod treemodel;
@@ -49,14 +47,13 @@ fn build_filters(builder: &gtk::Builder) {
});
}
-fn build_refresher(builder: &gtk::Builder, resources: &Rc<Resources>) {
+fn build_refresher(builder: &gtk::Builder) {
let refresher = builder.get_object::<gtk::Button>("refresh_button").unwrap();
let server_list = builder.get_object::<gtk::ListStore>("server-list-store").unwrap();
refresher.connect_clicked({
let refresher = refresher.clone();
- let resources = resources.clone();
move |_| {
refresher.set_sensitive(false);
server_list.clear();
@@ -69,7 +66,6 @@ fn build_refresher(builder: &gtk::Builder, resources: &Rc<Resources>) {
// Do the UI part of the server fetch
gtk::timeout_add(25, {
- let resources = resources.clone();
let refresher = refresher.clone();
let server_list = server_list.clone();
move || {
@@ -78,12 +74,16 @@ fn build_refresher(builder: &gtk::Builder, resources: &Rc<Resources>) {
glib::Continue(match rx.try_recv() {
// Add and continue
Ok(entry) => {
+ let icon = gdk_pixbuf::Pixbuf::new_from_resource_at_scale(
+ "/io/obozrenie/game_icons/q3a.png",
+ 24, 24, false
+ );
treemodel::append_server(
&server_list,
&[treemodel::GameEntry {
name: "q3a".into(),
p: p.clone(),
- icon: resources.game_icons.get("q3a").unwrap().clone(),
+ icon: icon.unwrap(),
}],
entry,
);
@@ -155,10 +155,10 @@ fn build_refresher(builder: &gtk::Builder, resources: &Rc<Resources>) {
});
}
-fn build_ui(app: &gtk::Application, resources: &Rc<Resources>) -> gtk::Builder {
+fn build_ui(app: &gtk::Application) -> gtk::Builder {
let builder = gtk::Builder::new_from_string(include_str!("obozrenie.ui"));
- build_refresher(&builder, resources);
+ build_refresher(&builder);
build_filters(&builder);
let window = builder.get_object::<gtk::ApplicationWindow>("main_window").unwrap();
@@ -172,11 +172,11 @@ fn build_ui(app: &gtk::Application, resources: &Rc<Resources>) -> gtk::Builder {
}
fn main() {
- let resources = static_resources::init().expect("GResource initialization failed.");
+ static_resources::init().expect("GResource initialization failed.");
let application = gtk::Application::new("io.obozrenie", gio::ApplicationFlags::empty()).unwrap();
application.connect_startup({
move |app| {
- let builder = build_ui(app, &resources);
+ let builder = build_ui(app);
builder.get_object::<gtk::Button>("refresh_button").unwrap().clicked();
}
});
diff --git a/src/static_resources.rs b/src/static_resources.rs
index fa0a072..87cc426 100644
--- a/src/static_resources.rs
+++ b/src/static_resources.rs
@@ -1,16 +1,7 @@
-use gdk_pixbuf::Pixbuf;
use gio::{resources_register, Error, Resource, ResourceLookupFlags};
use glib::Bytes;
-use std::collections::HashMap;
-use std::rc::Rc;
-const RES_ROOT_PATH: &str = "/io/obozrenie";
-
-pub struct Resources {
- pub game_icons: HashMap<String, Pixbuf>,
-}
-
-pub(crate) fn init() -> Result<Rc<Resources>, Error> {
+pub(crate) fn init() -> Result<(), Error> {
// load the gresource binary at build time and include/link it into the final binary.
let res_bytes = include_bytes!("../res/resources.gresource");
@@ -22,21 +13,5 @@ pub(crate) fn init() -> Result<Rc<Resources>, Error> {
// Register the resource so It wont be dropped and will continue to live in memory.
resources_register(&resource);
-
- let out = Rc::new(Resources {
- game_icons: ["q3a"]
- .into_iter()
- .map(|entry| {
- for format in ["png", "svg"].into_iter() {
- if let Ok(pixbuf) = Pixbuf::new_from_resource_at_scale(&format!("{}/game_icons/{}.{}", RES_ROOT_PATH, entry, format), 24, 24, false) {
- return (entry.to_string(), pixbuf);
- }
- }
-
- panic!("Failed to load icon for game {}", entry);
- })
- .collect(),
- });
-
- Ok(out)
+ Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment