3 unstable releases

0.2.0 Oct 2, 2022
0.1.1 Sep 27, 2022
0.1.0 Sep 23, 2022

#1253 in Procedural macros

GPL-3.0 license

18KB
120 lines

GTK Blueprint for Rust

This crate lets you use the Blueprint syntax for the UI in GTK applications built with Rust.

For this to work the Blueprint Compiler directory must be found in the $PATH environtment variable as "blueprint-compiler", or it can be included in the project root in blueprint-compiler/blueprint-compiler.py.

If the Blueprint Compiler is not found, your project will not compile. If the Blueprint compiler fails to compile the file to UI XML for whatever reason (file not found, bad syntax, etc.) your program will also fail to compile.


Include Blueprint

Use the include_blp!() macro to compile the Blueprint to the format that GtkBuilder can read (UI XML). The macro takes a file path relative to the project root. Similarly to how the include_str!() macro works, the compiled UI XML is embedded in the Rust source file as a &'static str.

Example

let builder = gtk::Builder::from_string(include_blp!("./src/window.blp"));
// OR without ./ prefixed to the path
let builder = gtk::Builder::from_string(include_blp!("src/window.blp"));

Blueprint Map

This macro generates a static Map of all the blueprints (files ending in .blp) in your project. It compiles the files and uses them as the Values for the map, with the Keys being the path of that blueprint relative to the Project Root. Then use get_blp to use the &str of the compiled blueprint.

The input for this macro is the path (relative to the Project Root) that it should start looking for blueprint files.

The advantage of this is when there is a blueprint that is used multiple times in your code, it will only be embedded once in the binary, where include_blp would embed it as many times as it is used.

Example

use gtk_blueprint::get_blp;

gtk_blueprint::gen_blp_map!("");

fn main() {
    let builder = gtk::Builder::from_string(get_blp!("./src/window.blp"));
    // OR without ./ prefixed to the path
    let builder = gtk::Builder::from_string(get_blp!("src/window.blp"));
}

Bug

If you make changes to any of your Blueprint files but don't make changes to the Rust code, cargo will think that it does not need to recompile the project because the code hasn't changed and it will run with the old version of the blueprint. As a work-around, create a file build.rs at the root of your project and put this in it:

fn main() {
    println!("cargo:rerun-if-changed=**/*.blp");
}

Dependencies

~2MB
~40K SLoC