#gtk #blueprint #ui #convert #parse #builder

gtk-ui-builder

一个用于解析Blueprint文件并将它们转换为GTK UI文件的Rust库

3个不稳定版本

0.2.0 2022年7月12日
0.1.1 2022年7月11日
0.1.0 2022年7月11日

GUI类别中排名第352

GPL-3.0许可证

53KB
1K SLoC

🦀 gtk-ui-builder

一个用于解析Blueprint文件并将它们转换为GTK UI文件的Rust库

Blueprint项目启发

示例1 - blueprints翻译

Blueprint文件

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!";
                }
            }
        }
    };
}

翻译成XML格式

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);
}

在GTK应用程序中导入blueprint

// 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::<adw::ApplicationWindow>("window").unwrap();

        window.set_application(Some(app));
        window.show();
    });

    // Run app
    application.run();
}

示例2 - rhai事件集成

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"));
                    }
                }
            }
        }
    };
}

此示例需要rhai-events进行解析和gtk-builder进行解释。事件将通过Builder结构自动应用

作者:Nikita Podvirnyy

许可协议:GNU GPL 3.0

依赖关系

~0–4.5MB
~95K SLoC