Initial implementation for Gresource support.

This commit is contained in:
Jordan Petridis 2017-11-10 19:28:24 +02:00
parent 9bd1341382
commit 72ad266403
No known key found for this signature in database
GPG key ID: CEABAD9F5683B9A6
8 changed files with 51 additions and 7 deletions

View file

@ -2,6 +2,7 @@
authors = ["Daniel Garcia <danigm@wadobo.com>"]
name = "fractal"
version = "0.1.0"
build = "build.rs"
[dependencies]
gdk = "0.6.0"

View file

@ -2,7 +2,7 @@ use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::process::Command;
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
@ -20,4 +20,11 @@ fn main() {
", fractal_res);
f.write_all(code.as_bytes()).unwrap();
// Compile Gresource
Command::new("glib-compile-resources")
.args(&["--generate", "resources.xml"])
.current_dir("res")
.status()
.unwrap();
}

BIN
res/resources.gresource Normal file

Binary file not shown.

8
res/resources.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/gnome/fractal/">
<file preprocess="xml-stripblanks">main_window.glade</file>
<file preprocess="xml-stripblanks">fractal.svg</file>
<file>app.css</file>
</gresource>
</gresources>

View file

@ -1221,7 +1221,7 @@ impl App {
let bk = Backend::new(tx);
let apptx = bk.run();
let gtk_builder = gtk::Builder::new_from_file(&config::datadir("main_window.glade"));
let gtk_builder = gtk::Builder::new_from_resource("/org/gnome/fractal/main_window.glade");
let op = Arc::new(Mutex::new(AppOp {
gtk_builder: gtk_builder.clone(),
load_more_btn: gtk::Button::new_with_label("Load more messages"),
@ -1376,7 +1376,9 @@ impl App {
.expect("Couldn't find main_window in ui file.");
window.set_title("Fractal");
let _ = window.set_icon_from_file(&config::datadir("fractal.svg"));
let pxbf = Pixbuf::new_from_resource("/org/gnome/fractal/fractal.svg").unwrap();
let _ = window.set_icon(&pxbf);
// let _ = window.set_icon_from_file(&config::datadir("fractal.svg"));
window.show_all();
let op = self.op.clone();
@ -1685,10 +1687,11 @@ impl App {
glib::set_prgname(Some("fractal"));
let provider = gtk::CssProvider::new();
let uri = config::datadir("app.css");
if let Err(_) = provider.load_from_path(&uri) {
println!("Error: Failed to add application style");
}
provider.load_from_resource("/org/gnome/fractal/app.css");
// let uri = config::datadir("app.css");
// if let Err(_) = provider.load_from_path(&uri) {
// println!("Error: Failed to add application style");
// }
gtk::StyleContext::add_provider_for_screen(&gdk::Screen::get_default().unwrap(), &provider, 600);
gtk::main();

0
src/build.rs Normal file
View file

View file

@ -1,3 +1,7 @@
extern crate glib;
extern crate gio;
#[macro_use]
extern crate serde_json;
#[macro_use]
@ -11,11 +15,13 @@ mod cache;
mod backend;
mod model;
mod app;
mod static_resources;
use app::App;
fn main() {
static_resources::init().expect("GResource initialization failed.");
let app = App::new();
app.run();
}

19
src/static_resources.rs Normal file
View file

@ -0,0 +1,19 @@
use gio::{resources_register, Error, Resource};
use glib::Bytes;
pub 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");
// Create Resource it will live as long the value lives.
// TODO: change it into Bytes::From_static once the fix lands
// https://bugzilla.gnome.org/show_bug.cgi?id=790030
let gbytes = Bytes::from(&res_bytes.as_ref());
let resource = Resource::new_from_data(&gbytes)?;
// let resource = Resource::new_from_data(&res_bytes.as_ref().into())?;
// Register the resource so It wont be dropped and will continue to live in memory.
resources_register(&resource);
Ok(())
}