https://github.com/krypt0nn/gtk-ui-builder
:crab: Rust library to parse Blueprint files and convert them into GTK UI files
https://github.com/krypt0nn/gtk-ui-builder
gtk rust
Last synced: about 1 month ago
JSON representation
:crab: Rust library to parse Blueprint files and convert them into GTK UI files
- Host: GitHub
- URL: https://github.com/krypt0nn/gtk-ui-builder
- Owner: krypt0nn
- License: gpl-3.0
- Created: 2022-07-11T20:08:40.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-27T20:38:01.000Z (over 2 years ago)
- Last Synced: 2025-03-28T09:45:39.103Z (2 months ago)
- Topics: gtk, rust
- Language: Rust
- Homepage:
- Size: 34.2 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
🦀 gtk-ui-builder
A Rust library to parse Blueprint files and convert them into GTK UI files
Inspired by the [Blueprint](https://gitlab.gnome.org/jwestman/blueprint-compiler) project
# Example 1 - blueprints translation
## Blueprint file
```
using Gtk 4.0;
using Adw 1;Adw.ApplicationWindow window {
default-width: 600;
default-height: 500;content: Gtk.Box {
orientation: vertical;Adw.HeaderBar {
title-widget: Adw.WindowTitle {
title: "Example app";
};
}Adw.PreferencesPage {
Adw.PreferencesGroup {
vexpand: true;
valign: center;Gtk.Button {
label: "Hello, World!";
}
}
}
};
}
```## Translation into XML format
```rs
use gtk_ui_builder::prelude::*;fn main() {
// Read main.blp file
let pattern = std::fs::read_to_string("assets/ui/main.blp")
.expect("Failed to read pattern");// Parse AST
let tree = Parser::parse(pattern)
.expect("Failed to parse blueprint");// Output prettified AST
println!("{}", tree.root.dbg());// Get XML representation of this AST
let ui = tree.get_xml();// Write this representation to the file
// now you can import it as any GTK UI file
std::fs::write("assets/ui/main.ui", &ui);
}
```## Importing blueprint in GTK app
```rs
// We're using gtk-builder feature here
use gtk_ui_builder::prelude::*;fn main() {
gtk4::init().expect("GTK initialization failed");
libadwaita::init();// Create app
let application = gtk::Application::new(
Some("com.github.krypt0nn.gtk-ui-builder"),
Default::default()
);// Init app window and show it
application.connect_activate(|app| {
// You also can parse blueprint with Parser::parse
// and then use it in gtk4::Builder
let builder = Builder::new(include_str!("../assets/ui/main.blp"))
.expect("Failed to parse blueprint");let window = builder.object::("window").unwrap();
window.set_application(Some(app));
window.show();
});// Run app
application.run();
}
```# Example 2 - rhai events integration
```
using Gtk 4.0;
using Adw 1;Adw.ApplicationWindow window {
default-width: 600;
default-height: 500;content: Gtk.Box {
orientation: vertical;Adw.HeaderBar {
title-widget: Adw.WindowTitle window_title {
title: "Example app";
};
}Adw.PreferencesPage {
Adw.PreferencesGroup {
vexpand: true;
valign: center;Gtk.Button {
label: "Hello, World!";clicked => {
window_title.set_str("title", "Button clicked: " + self.get_str("label"));
}
}
}
}
};
}
```This example requires `rhai-events` for parsing and `gtk-builder` for interpreting. Events are automatically applied by the `Builder` struct
Author: [Nikita Podvirnyy](https://github.com/krypt0nn)
Licensed under [GNU GPL 3.0](LICENSE)