pub struct Button<M> { /* private fields */ }Expand description
A button that emits a message when it is activated.
Activation is a release inside the button, or Enter/Space while it holds focus. A press that is dragged off and released elsewhere is cancelled, which is what makes a touchscreen usable — a finger that lands on the wrong control can be slid away rather than committing.
Implementations§
Source§impl<M> Button<M>
impl<M> Button<M>
Sourcepub fn inert(label: impl Into<String>) -> Self
pub fn inert(label: impl Into<String>) -> Self
A button that emits nothing. Useful as a disabled affordance, or when the application only cares about focus.
Sourcepub fn no_focus(self) -> Self
pub fn no_focus(self) -> Self
Presses without touching focus: the button takes none, and costs none.
An ordinary button takes focus when pressed, which is right for a button somebody tabs to and wrong for a key on an on-screen keyboard — that key is pressed while a field is being typed into, and the field has to keep the caret. Making the key merely unfocusable is not enough either, since pressing an unfocusable node is what drops focus and commits a field.
So this asks for neither. The button still presses, still paints pressed, still emits its message; Tab skips it, and the focus ring never moves.
ui.add(root, Button::new("q", Msg::Key('q')).no_focus(), Rect::new(0, 100, 40, 40));
assert_eq!(ui.focused(), Some(field));Sourcepub fn with_repeat(self, delay_ms: u64, interval_ms: u64) -> Self
pub fn with_repeat(self, delay_ms: u64, interval_ms: u64) -> Self
Emits again, and again, while a finger stays on it.
A repeating button acts on press rather than on release, which is the only way it could work: repeats have to start while the finger is still down. That is a real change in feel and the reason this is opt-in — an ordinary button emits on release precisely so that sliding off it before letting go cancels the press, and a repeating one gives that up.
Reach for it where holding means more of the same: Backspace on an on-screen keyboard, a stepper’s arrows, a scrollbar’s ends. Not for anything whose second press means something different from its first.
delay_ms is the pause before the first repeat — long enough that an
ordinary tap never triggers one — and interval_ms the gap between the
rest. The repeats are counted, not emitted: the button has no message
channel while it is animating, so whoever owns it collects them with
take_repeats once a frame.
Costs nothing when nothing is held. The button asks the tree to wake it
only between a press and its release, and answers
Wake::Never the moment the finger goes.
Sourcepub fn with_icon(self, icon: &'static Icon) -> Self
pub fn with_icon(self, icon: &'static Icon) -> Self
A shape drawn in place of the label.
For the button whose meaning is a picture — a Backspace key, a
scrollbar’s arrow — and specifically for the case where that picture is
not reliably in the font. An Icon is filled polygons this crate
draws itself, so it is the same on a machine with no fonts installed at
all as it is on one with DejaVu.
It takes the label’s place rather than sitting beside it, and it is
drawn in the same content colour the label would have used, so it
follows the theme and the button’s state without being told. Keep the
label anyway: it is what label still reports, which is
what a test and an accessibility pass read.
Sized from the button rather than fixed, and kept square — the shortest side decides, so a wide key gets a centred square icon rather than a stretched one.
Sourcepub const fn icon(&self) -> Option<&'static Icon>
pub const fn icon(&self) -> Option<&'static Icon>
The shape drawn in place of the label, if any.
Sourcepub fn with_corner(self, corner: impl Into<String>) -> Self
pub fn with_corner(self, corner: impl Into<String>) -> Self
A small second label in the top-right corner.
What a key on a real keyboard has printed above the character it types:
the ! over the 1, the ? over the +. It says what the other
state of this button would give, which is the whole reason a keyboard
prints it — you cannot discover Shift by pressing Shift if pressing it
is what changes the legend.
Drawn at two thirds the label’s size in the same content colour, so it reads as an annotation rather than as a second button. Empty is the normal case and costs nothing.
Sourcepub fn set_corner(&mut self, corner: impl Into<String>)
pub fn set_corner(&mut self, corner: impl Into<String>)
Replaces the corner label.
Sourcepub fn take_repeats(&mut self) -> u32
pub fn take_repeats(&mut self) -> u32
Repeats owed since this was last called, and clears the tally.
Zero unless with_repeat was asked for and a finger
has been resting on the button for longer than its delay.
Reach for repeats_pending first when polling
several buttons: taking needs &mut, and getting one out of the tree
costs a repaint of the node whether or not anything had changed.
Sourcepub const fn watching_hold(self) -> Self
pub const fn watching_hold(self) -> Self
Reports how long a finger has been resting on it.
The other half of press-and-hold. with_repeat
answers “again, and again”; this answers “how long”, which is what a
gesture that fires once after a delay needs — a key offering its
alternates, a button revealing a menu.
Costs the same as repeating and no more: the button asks the tree to
wake it only between a press and its release, so a screen nobody is
touching schedules nothing. Read it with held_ms.
Sourcepub const fn held_ms(&self) -> Option<u64>
pub const fn held_ms(&self) -> Option<u64>
How long the current press has lasted, in milliseconds.
None when nothing is on it. Updated on each tick while held, so it is
as fresh as the last one — which for a wake-driven tree means as fresh
as whatever asked to be woken.
A free read: unlike Ui::widget_mut, looking does not repaint.
Sourcepub const fn repeats_pending(&self) -> u32
pub const fn repeats_pending(&self) -> u32
Repeats owed, without taking them.
The read that costs nothing. Ui::widget_mut damages the node it hands
out — it cannot know whether the caller changed anything — so polling a
keyboard’s sixty keys through it repaints the whole keyboard on every
frame. This is how a caller finds the one key that owes something before
asking for it mutably.
Sourcepub fn with_role(self, role: Role) -> Self
pub fn with_role(self, role: Role) -> Self
Sets the colour role. The content colour comes from the theme’s pairing, so the label stays readable whichever role and theme are chosen.
Sourcepub fn with_radius(self, radius: Radius) -> Self
pub fn with_radius(self, radius: Radius) -> Self
Sets the corner rounding token.
Sourcepub fn with_style(self, style: TextStyle) -> Self
pub fn with_style(self, style: TextStyle) -> Self
Sets the font and size.
Sourcepub fn set_message(&mut self, message: Option<M>)
pub fn set_message(&mut self, message: Option<M>)
Replaces the message emitted on activation.
Sourcepub const fn role(&self) -> Role
pub const fn role(&self) -> Role
The colour role it is drawn in.
Worth reading before writing: Ui::widget_mut repaints whatever it
hands out, so a caller restyling a row of buttons at once should skip
the ones already right.
Sourcepub fn set_role(&mut self, role: Role)
pub fn set_role(&mut self, role: Role)
Replaces the colour role.
What a list of buttons uses to show which one is selected, since a role survives a theme change and a colour does not.
Sourcepub fn set_style(&mut self, style: TextStyle)
pub fn set_style(&mut self, style: TextStyle)
Replaces the font and size.
For an application that registers a font after building its tree, which is the ordinary case: the tree has to exist before anyone knows whether the font file was there.
Sourcepub fn preferred_width(&self, engine: &mut TextEngine) -> i32
pub fn preferred_width(&self, engine: &mut TextEngine) -> i32
Width this button needs for its label plus comfortable padding.
Takes the engine because with a proportional font the answer is not the character count times anything, and guessing is how a button ends up one letter too narrow in the language it was not tested in.
Trait Implementations§
Source§impl<M> Describe for Button<M>
impl<M> Describe for Button<M>
Source§const DOC: &'static str = "A rectangle somebody presses to make something happen."
const DOC: &'static str = "A rectangle somebody presses to make something happen."
Source§const ICON: &'static Icon
const ICON: &'static Icon
Source§const PROPERTIES: &'static [Property]
const PROPERTIES: &'static [Property]
Source§impl<M: Clone + 'static> Widget<M> for Button<M>
impl<M: Clone + 'static> Widget<M> for Button<M>
Source§fn animate(&mut self, now_ms: u64) -> Animation
fn animate(&mut self, now_ms: u64) -> Animation
Counts the repeats a held finger has earned, and asks for the next wake.
Counted from the press rather than accumulated from the last tick, so a clock that jumped — a loop that blocked, a snapshot ticking straight past a second — yields the repeats that time actually covered and no more.
Source§fn snap(&mut self, now_ms: u64) -> Animation
fn snap(&mut self, now_ms: u64) -> Animation
A held key under reduced motion still repeats.
Motion::None is about movement, not about clocks — the same rule the
carousel’s auto-advance follows. Backspace that stopped deleting because
somebody turned animations off would be a bug wearing a setting’s
clothes.
Source§fn describe(&self) -> Option<&dyn DynDescribe>
fn describe(&self) -> Option<&dyn DynDescribe>
Source§fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>
fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>
describe.Source§fn measure(&self, ctx: &mut MeasureCtx<'_>, _offered: Offer) -> Measured
fn measure(&self, ctx: &mut MeasureCtx<'_>, _offered: Offer) -> Measured
Source§fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)
fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)
canvas, which is already clipped to this widget’s bounds
intersected with the damage region being repainted. Read moreSource§fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled
fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled
Source§fn accepts_pointer(&self) -> bool
fn accepts_pointer(&self) -> bool
true if the pointer can hit this widget. Read moreSource§fn preserves_focus(&self) -> bool
fn preserves_focus(&self) -> bool
true if a press on this widget should leave focus exactly where
it is. Read moreAuto Trait Implementations§
impl<M> Freeze for Button<M>
impl<M> RefUnwindSafe for Button<M>where
Option<M>: RefUnwindSafe,
impl<M> Send for Button<M>
impl<M> Sync for Button<M>
impl<M> Unpin for Button<M>
impl<M> UnsafeUnpin for Button<M>where
Option<M>: UnsafeUnpin,
impl<M> UnwindSafe for Button<M>where
Option<M>: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DynDescribe for Twhere
T: Describe,
impl<T> DynDescribe for Twhere
T: Describe,
Source§fn kind(&self) -> &'static str
fn kind(&self) -> &'static str
Describe::KIND.Source§fn properties(&self) -> &'static [Property]
fn properties(&self) -> &'static [Property]
Describe::PROPERTIES.Source§fn get_property(&self, name: &str) -> Option<Value>
fn get_property(&self, name: &str) -> Option<Value>
Describe::get.Source§fn set_property(
&mut self,
name: &str,
value: Value,
) -> Result<(), PropertyError>
fn set_property( &mut self, name: &str, value: Value, ) -> Result<(), PropertyError>
Describe::set.