Struct Button

Source
pub struct Button<T, F> { /* private fields */ }
Expand description

A widget that performs an action when pressed/tapped.

Implementations§

Source§

impl<T, F> Button<T, F>

Source

pub fn new(label: T, action: F) -> Button<T, F>

Source§

impl<F> Button<Text, F>

Source

pub fn with_text(label: impl Into<String>, action: F) -> Button<Text, F>

Examples found in repository?
examples/nested.rs (lines 21-23)
19    fn body(&self) -> Self::Body {
20        let action = self.action.clone();
21        Button::with_text("Increment", move || {
22            action()
23        })
24    }
More examples
Hide additional examples
examples/counter.rs (lines 17-19)
13    fn body(&self) -> Self::Body {
14        let count = self.count.clone();
15        VStack::new((
16            Text::new(format!("Count: {}", count.get())),
17            Button::with_text("Increment", move || {
18                count.set(count.get() + 1);
19            })
20        ))
21    }
examples/picker.rs (lines 22-26)
13    fn body(&self) -> Self::Body {
14        let selection = self.selection.clone();
15        VStack::new((
16            Picker::new("Favorite Color", selection.binding(), (
17                Text::new("Red"),
18                Text::new("Green"),
19                Text::new("Yellow"),
20                Text::new("Blue"),
21            )),
22            Button::with_text("Choose next", clone!(selection => move || {
23                if let Id::Index(i) = selection.get() {
24                    selection.set((i + 1) % 4);
25                }
26            }))
27        ))
28    }
examples/fizz_buzz.rs (lines 24-26)
19    fn body(&self) -> Self::Body {
20        let count = self.count.clone();
21        let fizz = count.get() % 3 == 0;
22        let buzz = count.get() % 5 == 0;
23        VStack::new((
24            Button::with_text("Increment", clone!(count => move || {
25                count.set(count.get() + 1);
26            })),
27            If::new_or_else(fizz || buzz, || {
28                HStack::new((
29                    If::new(fizz, || Text::new("Fizz")),
30                    If::new(buzz, || Text::new("Buzz")),
31                ))
32            }, || {
33                Text::new(format!("{}", self.count.get()))
34            }),
35        ))
36    }
examples/scale_rotate.rs (lines 33-35)
23    fn body(&self) -> Self::Body {
24        let scale = self.scale.clone();
25        let rotation = self.rotation.clone();
26        VStack::with_spacing(100, (
27            Rectangle::new()
28                .overlay(Text::new("Hello world!").foreground_style(Color::GRAY))
29                .rotation_effect(rotation.get())
30                .scale_effect(scale.get())
31                .frame(100),
32            HStack::new((
33                Button::with_text("Rotate", move || {
34                    rotation.set_with(Animation::LINEAR, rotation.get() + Angle::QUARTER);
35                }),
36                Button::with_text("Scale up", clone!(scale => move || {
37                    scale.set_with(Animation::LINEAR, scale.get() + 0.5);
38                })),
39                Button::with_text("Scale down", move || {
40                    scale.set_with(Animation::LINEAR, scale.get() - 0.5);
41                }),
42            ))
43        ))
44    }
examples/animations.rs (lines 47-51)
25    fn body(&self) -> Self::Body {
26        let animations = self.animations;
27        let flips = self.flips.clone();
28        let width = 200.0;
29        let radius = 10.0;
30        let inner_width = width - 2.0 * radius;
31
32        VStack::new((
33            VStack::new(
34                ForEach::with_index_id(animations, |i, animation| {
35                    let factor = if flips.get()[i] { 1.0 } else { -1.0 };
36                    HStack::new((
37                        Text::new(format!("{}", animation))
38                            .frame_with(Alignment::Trailing, Frame::with_width(100)),
39                        ZStack::new((
40                            Rectangle::new()
41                                .frame(Frame::exact(inner_width, 2)),
42                            Circle::new()
43                                .frame(2.0 * radius)
44                                .offset(Vec2::with_x(factor * inner_width / 2.0))
45                        ))
46                        .frame(Frame::with_width(width)),
47                        Button::with_text("Flip", clone!(flips => move || {
48                            let mut value = flips.get();
49                            value[i] = !value[i];
50                            flips.set_with(animation, value);
51                        })),
52                    ))
53                })
54            ),
55        ))
56    }

Trait Implementations§

Source§

impl<T, F> Bind for Button<T, F>

Source§

fn bind(&self, context: &Context)

Source§

impl<T, F> Clone for Button<T, F>
where T: Clone, F: Clone,

Source§

fn clone(&self) -> Button<T, F>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, F> Debug for Button<T, F>
where T: Debug, F: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T, F> PartialEq for Button<T, F>
where T: PartialEq, F: PartialEq,

Source§

fn eq(&self, other: &Button<T, F>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T, F> View for Button<T, F>
where T: View, F: Fn() + 'static,

Source§

fn fire(&self, event: &Event, event_path: &IdPath, context: &Context)

Source§

fn render(&self, context: &Context) -> Node

Source§

type Body = NeverView

Source§

fn body(&self) -> Self::Body

Source§

impl<T, F> Eq for Button<T, F>
where T: Eq, F: Eq,

Source§

impl<T, F> StructuralPartialEq for Button<T, F>

Auto Trait Implementations§

§

impl<T, F> Freeze for Button<T, F>
where T: Freeze, F: Freeze,

§

impl<T, F> RefUnwindSafe for Button<T, F>

§

impl<T, F> Send for Button<T, F>
where T: Send, F: Send,

§

impl<T, F> Sync for Button<T, F>
where T: Sync, F: Sync,

§

impl<T, F> Unpin for Button<T, F>
where T: Unpin, F: Unpin,

§

impl<T, F> UnwindSafe for Button<T, F>
where T: UnwindSafe, F: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> IdentifyExt for T

Source§

fn identify(self, id: impl Into<Id>) -> Identified<T>

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> ViewExt for T
where T: View,

Source§

fn modifier(self, modifier: ModifierNode) -> Modified<Self>

Source§

fn gesture<G>(self, gesture: G) -> Gestured<Self, G>
where G: Gesture,

Source§

fn on_taps<F>(self, count: usize, action: F) -> Gestured<Self, TapGesture<F>>
where F: Fn(),

Source§

fn on_tap<F>(self, action: F) -> Gestured<Self, TapGesture<F>>
where F: Fn(),

Source§

fn on_drag_by<F>( self, minimum_distance: impl Into<f64>, action: F, ) -> Gestured<Self, DragGesture<F>>
where F: Fn(&DragEvent),

Source§

fn on_drag<F>(self, action: F) -> Gestured<Self, DragGesture<F>>
where F: Fn(&DragEvent),

Source§

fn overlay_at<O>(self, alignment: Alignment, overlayed: O) -> Overlay<Self, O>
where O: View,

Source§

fn overlay<O>(self, overlayed: O) -> Overlay<Self, O>
where O: View,

Source§

fn padding(self, insets: impl Into<Insets>) -> Modified<Self>

Source§

fn position(self, position: Vec2<f64>) -> Modified<Self>

Source§

fn offset(self, delta: Vec2<f64>) -> Modified<Self>

Source§

fn opacity(self, opacity: impl Into<f64>) -> Modified<Self>

Source§

fn frame_with( self, alignment: Alignment, frame: impl Into<Frame>, ) -> Modified<Self>

Source§

fn frame(self, frame: impl Into<Frame>) -> Modified<Self>

Source§

fn font(self, font: impl Into<Font>) -> Modified<Self>

Source§

fn foreground_style(self, style: impl Into<Style>) -> Modified<Self>

Source§

fn scale_effect_around( self, anchor: UnitPoint, factor: impl Into<f64>, ) -> Modified<Self>

Source§

fn scale_effect(self, factor: impl Into<f64>) -> Modified<Self>

Source§

fn rotation_effect_around( self, anchor: UnitPoint, angle: impl Into<Angle>, ) -> Modified<Self>

Source§

fn rotation_effect(self, angle: impl Into<Angle>) -> Modified<Self>

Source§

fn on_appear(self, action: impl Fn() + 'static) -> Handler<Self, impl Fn(Event)>

Source§

fn on_disappear( self, action: impl Fn() + 'static, ) -> Handler<Self, impl Fn(Event)>