A Rust library to parse Blueprint files and convert them into GTK UI files

Related tags

GUI gtk
Overview

🦀 gtk-ui-builder

A Rust library to parse Blueprint files and convert them into GTK UI files

Inspired by the Blueprint 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

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

("window").unwrap(); window.set_application(Some(app)); window.show(); }); // Run app application.run(); }">
// 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();
}

Example 2 - rhai events integration

{ window_title.set_str("title", "Button clicked: " + self.get_str("label")); } } } } }; }">
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

Licensed under GNU GPL 3.0

You might also like...
Reactive components in rust, designed to make GTK more manageable

gflux gflux is a tiny experimental reactive component system for rust, designed to make GTK more manageable. gflux: is about 300 lines of code contain

Test bed for gtk-rs-core experiments

Rust GObject Experiments class macro #[gobject::class(final)] mod obj { #[derive(Default)] pub struct MyObj { #[property(get, set)]

A compiler to convert Cairo's intermediate representation "Sierra" code to MLIR.

Cairo Sierra to MLIR compiler A compiler to convert Cairo's intermediate representation "Sierra" code to MLIR. Documentation There is an mdbook in the

Easy pretty print your Rust struct into single element or table

Easy pretty print your Rust struct into single element or table

将 C/C++ 代码转换成流程图 / Turn your C/C++ code into flowchart

cxx2flow 将 C/C++ 代码转换为流程图 效果 更多效果图请参考 GALLERY 两种样式: 折线 平滑 安装 自行编译 cargo install cxx2flow 下载预构建二进制 可以到 GitHub Actions 或 Nightly.link 下载最新构建的二进制,包含 Linu

A little wrapper I've written for creating UI screens from files for the BevyEngine.
A little wrapper I've written for creating UI screens from files for the BevyEngine.

UI Screens for BevyEngine This is a little thing I put together for creating simple UI screens using the BevyEngine. The idea is to define the screens

A TUI to quickly find files in your Google Drive
A TUI to quickly find files in your Google Drive

A TUI to quickly find files in your Google Drive

A small tool to display markdown files as a slideshow.
A small tool to display markdown files as a slideshow.

Rusty Slider A small tool to display markdown files as a slideshow. Demo Try out Rusty Slider online: Example slideshows. Download Rusty Slider is ava

A cross-platform GUI library for Rust focused on simplicity and type-safety
A cross-platform GUI library for Rust focused on simplicity and type-safety

A cross-platform GUI library for Rust, inspired by Elm

Owner
Observer KRypt0n_
An ordinary PHP developer
Observer KRypt0n_
Idiomatic, GTK+-based, GUI library, inspired by Elm, written in Rust

Relm Asynchronous, GTK+-based, GUI library, inspired by Elm, written in Rust. This library is in beta stage: it has not been thoroughly tested and its

null 2.2k Dec 31, 2022
Rust bindings and wrappers for GLib, GDK 3, GTK+ 3 and Cairo.

THIS REPOSITORY IS DEPRECATED SEE: https://github.com/rust-gnome rgtk Rust bindings and wrappers for GLib, GDK 3, GTK+ 3 and Cairo. Building rgtk expe

Jeremy Letang 124 Jul 10, 2022
A powerful color picker and formatter, built with GTK and Rust

Eyedropper A powerful color picker and formatter. More screenshots Features Pick a Color Enter a color in Hex-Format Parse RGBA/ARGB Hex-Colors View c

Jonathan 108 Dec 24, 2022
Highly customizable finder with high performance. Written in Rust and uses GTK

Findex Highly customizable finder with high performance. Written in Rust and uses GTK Installation Automatic Binary Clone from https://aur.archlinux.o

MD Gaziur Rahman Noor 442 Jan 1, 2023
A GUI for NordVPN on Linux that maintains feature parity with the official clients, written with Rust and GTK.

Viking for NordVPN This project aims to provide a fully usable and feature-complete graphical interface for NordVPN on Linux. While it attempts to clo

Jacob Birkett 2 Oct 23, 2022
An example of searching iBeacon using gtk-rs and btleplug.

Beacon Searcher Screenshot Compile & Run Install GTK 3 dev packages: macOS: $ brew install gtk+3 $ brew install gnome-icon-theme Debian / Ubuntu: $ s

Ling, Wei-Cheng 0 Dec 21, 2021
Provides Rust bindings for GTK libraries

The gtk-rs organization aims to provide safe Rust binding over GObject-based libraries

null 431 Dec 30, 2022
Provides Rust bindings for GTK libraries

gtk3-rs The gtk-rs organization aims to provide safe Rust binding over GObject-based libraries. You can find more about it on https://gtk-rs.org. This

null 431 Dec 30, 2022
Graphical font editor (GTK + Rust)

gerb *gerb ʰ-: reconstructed Proto-Indo-European root, meaning to carve gerb: a WIP font editor in gtk3 and rust Introduction gerb is an experimental,

Manos Pitsidianakis 40 Jan 1, 2023
GTK 4 front-end to ChatGPT completions written in Rust

ChatGPT GUI Building git clone [email protected]:teunissenstefan/chatgpt-gui.git cd chatgpt-gui cargo build --release Todo Connect insert_text to only al

Stefan Teunissen 6 Mar 12, 2023