pub struct Button { /* private fields */ }
Expand description
A Button
widget that can be clicked to perform an action.
Implementations§
Source§impl Button
impl Button
Sourcepub fn new(
width: f32,
height: f32,
text: String,
bg: Color,
fg: Color,
font: Option<Font>,
) -> Self
pub fn new( width: f32, height: f32, text: String, bg: Color, fg: Color, font: Option<Font>, ) -> Self
Creates a new Button
widget.
Examples found in repository?
examples/helloworld.rs (line 8)
5async fn main() {
6 let poppins = load_ttf_font("examples/poppins.ttf").await.unwrap();
7 let label = Label::new("Hello, world!".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()), 36.0);
8 let mut button = Button::new(500.0, 80.0, "Clickity Clickity Click".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.75, 0.5, 1.0), Some(poppins.clone()));
9 let mut toggle = Toggle::new(150.0, 50.0, "Toggle Me".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(1.0, 1.0, 0.5, 1.0), Some(poppins.clone()));
10
11 loop {
12 clear_background(Color::new(0.05, 0.05, 0.1, 1.0));
13
14 label.render(screen_width() / 2.0 - label.width() / 2.0, screen_height() / 2.0 - label.height() / 2.0 - 100.0);
15 button.update(screen_width() / 2.0 - button.width() / 2.0, screen_height() / 2.0 - button.height() / 2.0 + 100.0);
16 button.render(screen_width() / 2.0 - button.width() / 2.0, screen_height() / 2.0 - button.height() / 2.0 + 100.0);
17 toggle.update(screen_width() - toggle.width() - 10.0, 10.0);
18 toggle.render(screen_width() - toggle.width() - 10.0, 10.0);
19
20 if button.is_clicked() {
21 println!("Button clicked!");
22 }
23
24 next_frame().await;
25 }
26}
More examples
examples/cont.rs (line 17)
14async fn main() {
15 let poppins = load_ttf_font("examples/poppins.ttf").await.unwrap();
16 let label = Label::new("Inside a Container!".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()), 32.0);
17 let button = Button::new(400.0, 80.0, "eeeeeeee".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.75, 0.5, 1.0), Some(poppins.clone()));
18
19 let sublabel = Label::new("Inside a SUB Container!".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(0.5, 0.5, 1.0, 1.0), Some(poppins.clone()), 32.0);
20 let subbutton = Toggle::new(300.0, 60.0, "hhhhhhhh".to_string(), Color::new(0.05, 0.05, 0.1, 1.0), Color::new(1.0, 1.0, 0.5, 1.0), Some(poppins.clone()));
21
22 let mut subcontainer = Container::new(
23 flowquad::widgets::container::Direction::Vertical,
24 flowquad::widgets::container::Align::Center,
25 20.0, Color::new(0.05, 0.05, 0.1, 1.0),
26 Some((20.0, 20.0, 20.0, 20.0)), Some((5.0, Color::new(1.0, 0.5, 0.5, 1.0)))
27 );
28 subcontainer.add_child(Box::new(sublabel));
29 subcontainer.add_child(Box::new(subbutton));
30
31 let mut container = Container::new(
32 flowquad::widgets::container::Direction::Horizontal,
33 flowquad::widgets::container::Align::Center,
34 20.0, Color::new(0.05, 0.05, 0.1, 1.0),
35 Some((20.0, 20.0, 20.0, 20.0)), Some((5.0, Color::new(1.0, 0.5, 0.5, 1.0)))
36 );
37
38 container.add_child(Box::new(label));
39 container.add_child(Box::new(button));
40 container.add_child(Box::new(subcontainer)); // This is completely legal, as Container implements Widget trait!
41
42 loop {
43 clear_background(Color::new(0.05, 0.05, 0.1, 1.0));
44
45 container.update(screen_width() / 2.0 - container.width() / 2.0, screen_height() / 2.0 - container.height() / 2.0);
46 container.render(screen_width() / 2.0 - container.width() / 2.0, screen_height() / 2.0 - container.height() / 2.0);
47
48 next_frame().await;
49 }
50}
Trait Implementations§
Source§impl Action for Button
impl Action for Button
Source§fn is_clicked(&self) -> bool
fn is_clicked(&self) -> bool
Returns if the widget is clicked.
Source§fn is_hovered(&self) -> bool
fn is_hovered(&self) -> bool
Returns if the widget is hovered.
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