logo
view!() { /* proc-macro */ }
Expand description

The view! macro allows you to construct your UI easily and cleanly.

It does the same as inside the widget attribute macro, but with less features (no factories, components, etc).

You can even use the relm4-macros crate independently from Relm4 to build your GTK4 UI.

use relm4::gtk;
use gtk::prelude::{BoxExt, ButtonExt};

// Creating a box with a button inside.
relm4_macros::view! {
    vbox = gtk::Box {
        append = &gtk::Button {
            set_label: "Click me!",
            connect_clicked => |_| {
                println!("Hello world!");
            }
        },
    }
}

// You can simply use the vbox created in the macro.
let spacing = vbox.spacing();

Also, the macro doesn’t rely on any special gtk4-rs features so you can even use the macro for other purposes.

In this example, we use it to construct a Command.

use std::process::Command;

let path = "/";

relm4_macros::view! {
    mut process = Command::new("ls") {
        args: ["-la"],
        current_dir = mut &String {
            push_str: path,
        },
        env: args!("HOME", "/home/relm4"),
    }
}

// Output of "ls -la" at "/"
dbg!(process.output());