Struct Button

Source
pub struct Button { /* private fields */ }
Expand description

A clickable button element

Implementations§

Source§

impl Button

Source

pub fn new(parent: &impl Element, flags: u32, label: &str) -> Result<Self>

Create a new button with the given label and flags

§Arguments
  • parent - Parent element to attach this button to
  • flags - Button creation flags
  • label - Text label for the button
Examples found in repository?
examples/sample.rs (line 14)
3fn main() {
4    // Initialize the UI system
5    ui::init();
6
7    // Create main window
8    let window = Window::new("Rust UI Example", 800, 600, 0).expect("Failed to create window");
9
10    // Create a panel with gray background
11    let panel = Panel::new(&window, ui::UI_PANEL_GRAY).expect("Failed to create panel");
12
13    // Create some buttons
14    let _button1 = Button::new(&panel, 0, "Hello").expect("Failed to create button");
15    let _button2 = Button::new(&panel, 0, "World").expect("Failed to create button");
16    let _button3 = Button::new(&panel, 0, "Click me!").expect("Failed to create button");
17
18    // Start the message loop
19    ui::message_loop();
20}
More examples
Hide additional examples
examples/counter.rs (line 36)
5fn main() {
6    // Initialize UI
7    ui::init();
8
9    // Create main window and save it
10    let window = Window::new("Counter", 200, 150, 0).expect("Failed to create window");
11
12    // Create container panel with white background
13    let panel = Panel::new(&window, ui::UI_PANEL_WHITE | ui::UI_PANEL_MEDIUM_SPACING)
14        .expect("Failed to create panel");
15
16    // Create label and count to be shared between callbacks
17    let label = Rc::new(RefCell::new(
18        Label::new(&panel, 0, &format!("{:>3}", 0)).expect("Failed to create label"),
19    ));
20    let count = Rc::new(RefCell::new(0));
21
22    // Create buttons panel
23    let buttons =
24        Panel::new(&panel, ui::UI_PANEL_HORIZONTAL).expect("Failed to create buttons panel");
25
26    // Create and store minus button callback
27    let label_clone = label.clone();
28    let count_clone = count.clone();
29    let minus_callback = Box::new(move || {
30        *count_clone.borrow_mut() -= 1;
31        let mut label = label_clone.borrow_mut();
32        // Format the number into a string with proper width
33        label.set_content(&format!("{:>3}", *count_clone.borrow()));
34        label.refresh();
35    });
36    let minus = Button::new(&buttons, 0, "-").expect("Failed to create minus button");
37    minus.invoke(minus_callback);
38
39    // Create and store plus button callback
40    let plus_callback = Box::new(move || {
41        *count.borrow_mut() += 1;
42        let mut label = label.borrow_mut();
43        // Format the number into a string with proper width
44        label.set_content(&format!("{:>3}", *count.borrow()));
45        label.refresh();
46    });
47    let plus = Button::new(&buttons, 0, "+").expect("Failed to create plus button");
48    plus.invoke(plus_callback);
49
50    // Start the message loop
51    ui::message_loop();
52}
Source

pub fn invoke(&self, callback: Box<dyn Fn()>)

Examples found in repository?
examples/counter.rs (line 37)
5fn main() {
6    // Initialize UI
7    ui::init();
8
9    // Create main window and save it
10    let window = Window::new("Counter", 200, 150, 0).expect("Failed to create window");
11
12    // Create container panel with white background
13    let panel = Panel::new(&window, ui::UI_PANEL_WHITE | ui::UI_PANEL_MEDIUM_SPACING)
14        .expect("Failed to create panel");
15
16    // Create label and count to be shared between callbacks
17    let label = Rc::new(RefCell::new(
18        Label::new(&panel, 0, &format!("{:>3}", 0)).expect("Failed to create label"),
19    ));
20    let count = Rc::new(RefCell::new(0));
21
22    // Create buttons panel
23    let buttons =
24        Panel::new(&panel, ui::UI_PANEL_HORIZONTAL).expect("Failed to create buttons panel");
25
26    // Create and store minus button callback
27    let label_clone = label.clone();
28    let count_clone = count.clone();
29    let minus_callback = Box::new(move || {
30        *count_clone.borrow_mut() -= 1;
31        let mut label = label_clone.borrow_mut();
32        // Format the number into a string with proper width
33        label.set_content(&format!("{:>3}", *count_clone.borrow()));
34        label.refresh();
35    });
36    let minus = Button::new(&buttons, 0, "-").expect("Failed to create minus button");
37    minus.invoke(minus_callback);
38
39    // Create and store plus button callback
40    let plus_callback = Box::new(move || {
41        *count.borrow_mut() += 1;
42        let mut label = label.borrow_mut();
43        // Format the number into a string with proper width
44        label.set_content(&format!("{:>3}", *count.borrow()));
45        label.refresh();
46    });
47    let plus = Button::new(&buttons, 0, "+").expect("Failed to create plus button");
48    plus.invoke(plus_callback);
49
50    // Start the message loop
51    ui::message_loop();
52}

Trait Implementations§

Source§

impl Element for Button

Source§

fn raw_element(&self) -> *mut UIElement

Get the raw pointer to the underlying UIElement
Source§

fn destroy(&mut self)

Destroy this element and remove it from the UI hierarchy
Source§

fn refresh(&mut self)

Refresh this element’s layout and appearance

Auto Trait Implementations§

§

impl Freeze for Button

§

impl RefUnwindSafe for Button

§

impl !Send for Button

§

impl !Sync for Button

§

impl Unpin for Button

§

impl UnwindSafe for Button

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.