Struct widgetry::ButtonBuilder
source · [−]pub struct ButtonBuilder<'a, 'c> {
padding: EdgeInsets,
stack_spacing: f64,
hotkey: Option<MultiKey>,
tooltip: Option<Text>,
stack_axis: Option<Axis>,
is_label_before_image: bool,
corner_rounding: Option<CornerRounding>,
is_disabled: bool,
default_style: ButtonStateStyle<'a, 'c>,
hover_style: ButtonStateStyle<'a, 'c>,
disable_style: ButtonStateStyle<'a, 'c>,
disabled_tooltip: Option<Text>,
}
Fields
padding: EdgeInsets
stack_spacing: f64
hotkey: Option<MultiKey>
tooltip: Option<Text>
stack_axis: Option<Axis>
is_label_before_image: bool
corner_rounding: Option<CornerRounding>
is_disabled: bool
default_style: ButtonStateStyle<'a, 'c>
hover_style: ButtonStateStyle<'a, 'c>
disable_style: ButtonStateStyle<'a, 'c>
disabled_tooltip: Option<Text>
Implementations
sourceimpl<'b, 'a: 'b, 'c> ButtonBuilder<'a, 'c>
impl<'b, 'a: 'b, 'c> ButtonBuilder<'a, 'c>
pub fn new() -> Self
sourcepub fn padding<EI: Into<EdgeInsets>>(self, padding: EI) -> Self
pub fn padding<EI: Into<EdgeInsets>>(self, padding: EI) -> Self
Extra spacing around a button’s items (label and/or image).
If not specified, a default will be applied.
// Custom padding for each inset
let b = ButtonBuilder::new().padding(EdgeInsets{ top: 1.0, bottom: 2.0, left: 12.0, right: 14.0 });
// uniform padding
let b = ButtonBuilder::new().padding(6);
sourcepub fn padding_top(self, padding: f64) -> Self
pub fn padding_top(self, padding: f64) -> Self
Extra spacing around a button’s items (label and/or image).
sourcepub fn padding_left(self, padding: f64) -> Self
pub fn padding_left(self, padding: f64) -> Self
Extra spacing around a button’s items (label and/or image).
sourcepub fn padding_bottom(self, padding: f64) -> Self
pub fn padding_bottom(self, padding: f64) -> Self
Extra spacing around a button’s items (label and/or image).
sourcepub fn padding_right(self, padding: f64) -> Self
pub fn padding_right(self, padding: f64) -> Self
Extra spacing around a button’s items (label and/or image).
sourcepub fn label_text<I: Into<String>>(self, text: I) -> Self
pub fn label_text<I: Into<String>>(self, text: I) -> Self
Set the text of the button’s label.
If label_text
is not set, the button will not have a label.
sourcepub fn label_underlined_text<I: Into<String>>(self, text: I) -> Self
pub fn label_underlined_text<I: Into<String>>(self, text: I) -> Self
Set the text of the button’s label. The text will be decorated with an underline.
See label_styled_text
if you need something more customizable text styling.
sourcepub fn label_styled_text(
self,
styled_text: Text,
for_state: ControlState
) -> Self
pub fn label_styled_text(
self,
styled_text: Text,
for_state: ControlState
) -> Self
Assign a pre-styled Text
instance if your button need something more than uniformly
colored text.
sourcepub fn label_color(self, color: Color, for_state: ControlState) -> Self
pub fn label_color(self, color: Color, for_state: ControlState) -> Self
Set the color of the button’s label.
If not specified, a default font color will be used.
sourcepub fn font(self, font: Font) -> Self
pub fn font(self, font: Font) -> Self
Set the font used by the button’s label.
If not specified, a default font will be used.
sourcepub fn font_size(self, font_size: usize) -> Self
pub fn font_size(self, font_size: usize) -> Self
Set the size of the font of the button’s label.
If not specified, a default font size will be used.
sourcepub fn image_path(self, path: &'a str) -> Self
pub fn image_path(self, path: &'a str) -> Self
Set the image for the button. If not set, the button will have no image.
This will replace any image previously set.
pub fn image(self, image: Image<'a, 'c>) -> Self
sourcepub fn image_bytes(self, labeled_bytes: (&'a str, &'a [u8])) -> Self
pub fn image_bytes(self, labeled_bytes: (&'a str, &'a [u8])) -> Self
Set the image for the button. If not set, the button will have no image.
This will replace any image previously set.
labeled_bytes
: is a (label
,bytes
) tuple you can generate withinclude_labeled_bytes!
label
: a label to describe the bytes for debugging purposesbytes
: UTF-8 encoded bytes of the SVG
sourcepub fn image_batch(self, batch: GeomBatch, bounds: Bounds) -> Self
pub fn image_batch(self, batch: GeomBatch, bounds: Bounds) -> Self
Set the image for the button. If not set, the button will have no image.
This will replace any image previously set.
This method is useful when doing more complex transforms. For example, to re-write more than one color for your button image, do so externally and pass in the resultant GeomBatch here.
sourcepub fn image_color<C: Into<RewriteColor>>(
self,
color: C,
for_state: ControlState
) -> Self
pub fn image_color<C: Into<RewriteColor>>(
self,
color: C,
for_state: ControlState
) -> Self
Rewrite the color of the button’s image.
This has no effect if the button does not have an image.
If the style hasn’t been set for the current ControlState, the style for
ControlState::Default
will be used.
sourcepub fn image_bg_color(self, color: Color, for_state: ControlState) -> Self
pub fn image_bg_color(self, color: Color, for_state: ControlState) -> Self
Set a background color for the image, other than the buttons background.
This has no effect if the button does not have an image.
If the style hasn’t been set for the current ControlState, the style for
ControlState::Default
will be used.
sourcepub fn image_dims<D: Into<ScreenDims>>(self, dims: D) -> Self
pub fn image_dims<D: Into<ScreenDims>>(self, dims: D) -> Self
Scale the bounds containing the image. If image_dims
are not specified, the images
intrinsic size will be used.
See ButtonBuilder::image_content_mode
to control how the image scales to fit
its custom bounds.
pub fn override_style(self, style: &ButtonStyle) -> Self
sourcepub fn image_content_mode(self, content_mode: ContentMode) -> Self
pub fn image_content_mode(self, content_mode: ContentMode) -> Self
If a custom image_dims
was set, control how the image should be scaled to its new bounds
If image_dims
were not specified, the image will not be scaled, so content_mode has no
affect.
The default, ContentMode::ScaleAspectFit
will only grow as much as it can while
maintaining its aspect ratio and not exceeding its bounds.
sourcepub fn image_corner_rounding<R: Into<CornerRounding>>(self, value: R) -> Self
pub fn image_corner_rounding<R: Into<CornerRounding>>(self, value: R) -> Self
Set independent rounding for each of the button’s image’s corners
sourcepub fn image_padding<EI: Into<EdgeInsets>>(self, value: EI) -> Self
pub fn image_padding<EI: Into<EdgeInsets>>(self, value: EI) -> Self
Set padding for the image
sourcepub fn bg_color(self, color: Color, for_state: ControlState) -> Self
pub fn bg_color(self, color: Color, for_state: ControlState) -> Self
Set a background color for the button based on the button’s ControlState
.
If the style hasn’t been set for the current ControlState, the style for
ControlState::Default
will be used.
sourcepub fn outline(self, outline: OutlineStyle, for_state: ControlState) -> Self
pub fn outline(self, outline: OutlineStyle, for_state: ControlState) -> Self
Set an outline for the button based on the button’s ControlState
.
If the style hasn’t been set for the current ControlState, the style for
ControlState::Default
will be used.
pub fn outline_color(self, color: Color, for_state: ControlState) -> Self
sourcepub fn custom_batch(self, batch: GeomBatch, for_state: ControlState) -> Self
pub fn custom_batch(self, batch: GeomBatch, for_state: ControlState) -> Self
Set a pre-rendered GeomBatch to use for the button instead of individual fields.
This is useful for applying one-off button designs that can’t be accommodated by the the existing ButtonBuilder methods.
sourcepub fn tooltip(self, tooltip: impl Into<Text>) -> Self
pub fn tooltip(self, tooltip: impl Into<Text>) -> Self
Set a non-default tooltip Text
to appear when hovering over the button.
If a tooltip
is not specified, a default tooltip will be applied.
sourcepub fn no_tooltip(self) -> Self
pub fn no_tooltip(self) -> Self
If a tooltip
is not specified, a default tooltip will be applied. Use no_tooltip
when
you do not want even the default tooltip to appear.
sourcepub fn disabled_tooltip(self, tooltip: impl Into<Text>) -> Self
pub fn disabled_tooltip(self, tooltip: impl Into<Text>) -> Self
Set a tooltip Text
to appear when hovering over the button, when the button is
disabled.
This tooltip is only displayed when disabled(true)
is also called.
sourcepub fn vertical(self) -> Self
pub fn vertical(self) -> Self
The button’s items will be rendered in a vertical column
If the button doesn’t have both an image and label, this has no effect.
sourcepub fn horizontal(self) -> Self
pub fn horizontal(self) -> Self
The button’s items will be rendered in a horizontal row
If the button doesn’t have both an image and label, this has no effect.
sourcepub fn disabled(self, is_disabled: bool) -> Self
pub fn disabled(self, is_disabled: bool) -> Self
The button cannot be clicked and will be styled as ControlState::Disabled
sourcepub fn label_first(self) -> Self
pub fn label_first(self) -> Self
Display the button’s label before the button’s image.
If the button doesn’t have both an image and label, this has no effect.
sourcepub fn image_first(self) -> Self
pub fn image_first(self) -> Self
Display the button’s image before the button’s label.
If the button doesn’t have both an image and label, this has no effect.
sourcepub fn stack_spacing(self, value: f64) -> Self
pub fn stack_spacing(self, value: f64) -> Self
Spacing between the image and text of a button. Has no effect if the button is text-only or image-only.
sourcepub fn corner_rounding<R: Into<CornerRounding>>(self, value: R) -> Self
pub fn corner_rounding<R: Into<CornerRounding>>(self, value: R) -> Self
Set independent rounding for each of the button’s corners
sourcepub fn build(&self, ctx: &EventCtx<'_>, action: &str) -> Button
pub fn build(&self, ctx: &EventCtx<'_>, action: &str) -> Button
Build a button.
action
: The event that will be fired when clicked
fn build_some_buttons(ctx: &EventCtx) {
let one_off_builder = ButtonBuilder::new().label_text("foo").build(ctx, "foo");
// If you'd like to build a series of similar buttons, `clone` the builder first.
let red_builder = ButtonBuilder::new()
.bg_color(Color::RED, ControlState::Default)
.bg_color(Color::RED.alpha(0.3), ControlState::Disabled)
.outline((2.0, Color::WHITE), ControlState::Default);
let red_button_1 = red_builder.clone().label_text("First red button").build(ctx, "first");
let red_button_2 = red_builder.clone().label_text("Second red button").build(ctx, "second");
let red_button_3 = red_builder.label_text("Last red button").build(ctx, "third");
}
sourcepub fn build_widget<I: AsRef<str>>(
&self,
ctx: &EventCtx<'_>,
action: I
) -> Widget
pub fn build_widget<I: AsRef<str>>(
&self,
ctx: &EventCtx<'_>,
action: I
) -> Widget
Shorthand method to build a Button wrapped in a Widget
action
: The event that will be fired when clicked
sourcepub fn build_def(&self, ctx: &EventCtx<'_>) -> Widget
pub fn build_def(&self, ctx: &EventCtx<'_>) -> Widget
Shorthand method to build a default widget whose action
is derived from the label’s text.
fn style_mut(
&'b mut self,
state: ControlState
) -> &'b mut ButtonStateStyle<'a, 'c>
fn style(&'b self, state: ControlState) -> &'b ButtonStateStyle<'a, 'c>
pub fn batch(&self, ctx: &EventCtx<'_>, for_state: ControlState) -> GeomBatch
Trait Implementations
sourceimpl<'a, 'c> Clone for ButtonBuilder<'a, 'c>
impl<'a, 'c> Clone for ButtonBuilder<'a, 'c>
sourcefn clone(&self) -> ButtonBuilder<'a, 'c>
fn clone(&self) -> ButtonBuilder<'a, 'c>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<'a, 'c> Debug for ButtonBuilder<'a, 'c>
impl<'a, 'c> Debug for ButtonBuilder<'a, 'c>
sourceimpl<'a, 'c> Default for ButtonBuilder<'a, 'c>
impl<'a, 'c> Default for ButtonBuilder<'a, 'c>
sourcefn default() -> ButtonBuilder<'a, 'c>
fn default() -> ButtonBuilder<'a, 'c>
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl<'a, 'c> RefUnwindSafe for ButtonBuilder<'a, 'c>
impl<'a, 'c> Send for ButtonBuilder<'a, 'c>
impl<'a, 'c> Sync for ButtonBuilder<'a, 'c>
impl<'a, 'c> Unpin for ButtonBuilder<'a, 'c> where
'a: 'c,
impl<'a, 'c> UnwindSafe for ButtonBuilder<'a, 'c>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Downcast for T where
T: Any,
impl<T> Downcast for T where
T: Any,
pub fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
pub fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Convert Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
. Read more
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
pub fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Convert Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
. Read more
pub fn as_any(&self) -> &(dyn Any + 'static)
pub fn as_any(&self) -> &(dyn Any + 'static)
Convert &Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s. Read more
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
pub fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert &mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<T> Same<T> for T
impl<T> Same<T> for T
type Output = T
type Output = T
Should always be Self
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more