GtkButton

GtkButton

GtkButton is a GTK widget mirrored in XFrame in the “Buttons” section, built from the Button archetype (kind “Button”). A clickable push button. Pressing it with mouse button 1 while the pointer is inside (or pressing Return or Space while it is focused) fires its click handler; releasing outside does not fire. Disabled buttons draw grey text and ignore input.

Category: XFrame   Family: Gtk   Section: Buttons   Archetype: Button   Kind: Button   Default size: 150×36

GtkButton running under Xvfb
GtkButton running in an 800×600 window under Xvfb.

Properties

PropertyTypeDefaultRust setter
backgroundColor—set_style_name
foregroundColor—set_style_name
tooltip-textStr——
visibleBooltrueset_visible
enabledBooltrueset_enabled
labelStr—set_text
reliefEnum (Normal, Half, None)0—
focus-on-clickBooltrue—

“—” in the setter column means the Rust widget code has no setter for that property: it is set in the designer (Inspector) and carried in the saved design, not changed from Rust code.

Button archetype settings (Rust)

Settings on the shared Button struct in crates/xframe-widgets/src/archetypes.rs. One struct serves every kind of the archetype, so only the settings matching this kind apply to it (a calendar uses set_date, a notebook uses the tab settings, a grid uses the grid settings). “—” as a default means the data gives no Inspector default for that setting.

PropertyTypeDefaultRust setter
disabled-text-colorColor—set_disabled_text_color
onBool—set_on
defaultBool—set_default
cancelBool—set_cancel
modal-resultInt—set_modal_result
kindInt—set_kind
layoutInt—set_layout
styleInt—set_style
default-colorColor—set_default_color
none-colorColor—set_none_color
directionInt—set_direction

Working example

Minimal complete Rust program — this is the file examples/GtkButton.rs in lightgui_rust. Run it with:

cargo run -p xframe-demo --example GtkButton
//! GtkButton — minimal complete XFrame example.
//!
//! Run it from the workspace root:
//!     cargo run -p xframe-demo --example GtkButton
//! It opens an 800x600 window showing one GtkButton widget (press Esc
//! or `q`, or close the window, to quit). The widget is the real
//! `xframe-widgets` implementation for this registry kind, created by
//! `xframe_widgets::create("GtkButton")` inside `xframe_demo::run_widget`
//! — the same code path the `xframe-demo` binary uses.

use std::process::ExitCode;

fn main() -> ExitCode {
    match xframe_demo::run_widget("GtkButton", false, None, None) {
        Ok(code) => ExitCode::from(code as u8),
        Err(e) => {
            eprintln!("GtkButton example: {}", e);
            ExitCode::from(1)
        }
    }
}

Event handling

Every XFrame widget dispatches the same four input events through the Widget trait: button press, button release, pointer motion, and key press. The generated event loop routes a release to the widget that got the press (even if the pointer moved off it), motion to the pressed widget while dragging and otherwise to the topmost widget under the pointer, and keys only to the focused widget. A press that starts and ends inside an enabled widget fires its click handler (on_click); Return or Space fires it when the widget is focused.

SignalHandlerWhat it meansKind
clickedon_clickedClickedSimple

Connect clicked in the designer; the generated program wires each signal to a stub handler (on_<signal>) you fill in.

Button pushes: wire the clicked signal to on_clicked. The handler runs on release-inside (or Return/Space while focused). A latched button also keeps its on state — read it after the click.

Font and labels

Change the displayed text at any time with set_text("...") — the widget redraws with the new label. The font comes from the active style: styles are registered on the StyleManager with add_style_ex(name, fill, outline, text, line, radius, line_width, font), and a widget switches style with set_style_name("dark"). So a font change is a style change: register a style with the font you want and apply it by name.

Styling: colours, border radius, line width, line colour

Styling is per-style, not per-widget: a Style carries fill_color, outline_color, line_color, text_color, radius (border radius), line_width, and font_name. Register a style with add_style_ex (argument order: fill, outline, text, line, radius, line width, font) and apply it with set_style_name("name") on any widget — the same widget can be square or rounded, thin- or thick-lined, just by switching style. The demo's default style uses radius 6 and line width 1; dark is the dark variant.


All Rust widgets · lightgui_rust on GitHub

Read more