pub struct Button { /* private fields */ }
Expand description
A clickable button element
Implementations§
Source§impl Button
impl Button
Sourcepub fn new(parent: &impl Element, flags: u32, label: &str) -> Result<Self>
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 toflags
- Button creation flagslabel
- 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
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}
Sourcepub fn invoke(&self, callback: Box<dyn Fn()>)
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more