0.1.6 |
|
---|---|
0.1.5 |
|
0.1.4 |
|
0.1.2 |
|
0.1.0 |
|
#6 in #gio
4KB
58 行
GTK Resources

用于轻松加载 gtk gresources 的过程性 derive 宏。
此包依赖于 gtk-rs 项目。
文档
在 Docs.rs 上找到。
示例
将 gtk-resources
添加到您的 Cargo.toml
依赖中
[dependencies]
gio = "0.9"
gtk = "0.9"
glib = "0.10"
gtk_resources = "0.1.6"
然后在您的 gtk-rs
项目中。
use gtk_resources::UIResource;
#[derive(UIResource, Debug)]
#[resource = "/com/name/project/window.ui"]
struct WindowResource {
window: gtk::ApplicationWindow,
display_label: gtk::Label,
hello_btn: gtk::Button,
}
fn main() {
gtk::init().unwrap();
// Register resource bundles
let res_bytes = include_bytes!("../test/test.gresource");
let data = glib::Bytes::from(&res_bytes[..]);
let resource = gio::Resource::from_data(&data).unwrap();
gio::resources_register(&resource);
let res = WindowResource::load().unwrap();
println!("res: {:?}", res);
// ...
}
文件 test/test.gresource.xml 定义了由本示例使用的资源包。
有关资源包的更多信息,请参阅 Gio::Resource 和 glib-compile-resources。
#[resource = "/com/name/project/window.ui"]
属性在 WindowResource
结构体中指定了对应资源的路径。
UIResource 结构体的字段名称和类型必须与导出资源对象的 id 和类型匹配。否则,gtk::Builder
在运行时无法加载它们。
前一个示例生成的代码如下
impl UIResource for WindowResource {
fn load() -> Result<WindowResource, ()> {
use gtk::BuilderExtManual;
let b = gtk::Builder::from_resource("/com/name/project/window.ui");
Ok (WindowResource {
window: b.get_object::<gtk::ApplicationWindow>("window").ok_or(())?,
display_label: b.get_object::<gtk::Label>("display_label").ok_or(())?,
hello_btn: b.get_object::<gtk::Button>("hello_btn").ok_or(())?,
})
}
}
依赖关系
~1.5MB
~35K SLoC