Skip to main content

Button

ButtonWidget (module pushrod_widgets::button_widget): a push button with a text label that emits Clicked.

A Button, normal and pressed

Creating it​

use pushrod_widgets::button_widget::ButtonWidget;
use pushrod::geometry::{point, size};
use pushrod_widgets::text_widget::TextJustify;
use pushrod::color::{Color, FontStyle};

let button = engine.add_widget(ButtonWidget::new(
point(20, 70),
size(120, 30),
"assets/OpenSans-Regular.ttf".into(),
FontStyle::NORMAL,
16,
Color::BLACK,
TextJustify::Center,
"Click me".into(),
2,
));

engine.on_click(button, |state, ui| {
// runs on every click
});
ParameterTypeMeaning
originPointUpper-left corner, relative to the parent.
sizeSizeWidth and height in pixels.
font_nameStringPath to a TrueType font file.
font_styleFontStyleFontStyle::NORMAL, BOLD, ITALIC, ...
font_sizeu16Size in points.
font_colorColorText color.
justificationTextJustifyLeft, Center, or Right.
msgStringThe label.
border_widthu8Inset of the label from the edges, in pixels.

Methods​

MethodDoes
set_text(text) / get_text()The label.
set_border_width(width) / get_border_width()Label inset.
is_selected()true while drawn pressed.
set_color(color)Background color when not pressed.

Common methods​

Every widget also has the methods of the Widget trait: get_id, get_parent, get_origin/set_origin, get_size/set_size, get_color/set_color, is_hidden/set_hidden, and is_invalidated/set_invalidated. See Creating widgets.

Events and callbacks​

EventEmitted whenCallback
Event::Clicked { id, button, count }The left button is released over the button while it is still pressed.engine.on_click(id, ..)

Notes​

Behavior: pressing the mouse button inverts the button (black background, white text). Dragging off it restores the normal look and dragging back inverts it again. The click only counts if the mouse button is released while the button is still inverted.

Planned​

Default (Return) and cancel (Escape) buttons, toggle buttons, buttons that fire on press, per-state colors, and keyboard mnemonics are planned; see the roadmap.