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§

source§

impl<'b, 'a: 'b, 'c> ButtonBuilder<'a, 'c>

source

pub fn new() -> Self

source

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);
source

pub fn padding_top(self, padding: f64) -> Self

Extra spacing around a button’s items (label and/or image).

source

pub fn padding_left(self, padding: f64) -> Self

Extra spacing around a button’s items (label and/or image).

source

pub fn padding_bottom(self, padding: f64) -> Self

Extra spacing around a button’s items (label and/or image).

source

pub fn padding_right(self, padding: f64) -> Self

Extra spacing around a button’s items (label and/or image).

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

pub fn image(self, image: Image<'a, 'c>) -> Self

source

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 with [include_labeled_bytes!]
  • label: a label to describe the bytes for debugging purposes
  • bytes: UTF-8 encoded bytes of the SVG
source

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.

source

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.

source

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.

source

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.

source

pub fn override_style(self, style: &ButtonStyle) -> Self

source

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.

source

pub fn image_corner_rounding<R: Into<CornerRounding>>(self, value: R) -> Self

Set independent rounding for each of the button’s image’s corners

source

pub fn image_padding<EI: Into<EdgeInsets>>(self, value: EI) -> Self

Set padding for the image

source

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.

source

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.

source

pub fn outline_color(self, color: Color, for_state: ControlState) -> Self

source

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.

source

pub fn hotkey<MK: Into<Option<MultiKey>>>(self, key: MK) -> Self

Set a hotkey for the button

source

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.

source

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.

source

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.

source

pub fn maybe_disabled_tooltip(self, tooltip: Option<impl Into<Text>>) -> Self

Like disabled_tooltip, but the tooltip may not exist.

source

pub fn tooltip_and_disabled(self, tooltip: impl Into<Text>) -> Self

Sets a tooltip to appear whether the button is disabled or not.

source

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.

source

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.

source

pub fn disabled(self, is_disabled: bool) -> Self

The button cannot be clicked and will be styled as ControlState::Disabled

source

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.

source

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.

source

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.

source

pub fn corner_rounding<R: Into<CornerRounding>>(self, value: R) -> Self

Set independent rounding for each of the button’s corners

source

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");
}
source

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

source

pub fn get_action(&self) -> Option<&String>

Get the button’s text label, if defined

source

pub fn build_def(&self, ctx: &EventCtx<'_>) -> Widget

Shorthand method to build a default widget whose action is derived from the label’s text.

source

fn style_mut( &'b mut self, state: ControlState ) -> &'b mut ButtonStateStyle<'a, 'c>

source

fn style(&'b self, state: ControlState) -> &'b ButtonStateStyle<'a, 'c>

source

pub fn batch(&self, ctx: &EventCtx<'_>, for_state: ControlState) -> GeomBatch

Trait Implementations§

source§

impl<'a, 'c> Clone for ButtonBuilder<'a, 'c>

source§

fn clone(&self) -> ButtonBuilder<'a, 'c>

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<'a, 'c> Debug for ButtonBuilder<'a, 'c>

source§

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

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

impl<'a, 'c> Default for ButtonBuilder<'a, 'c>

source§

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>

§

impl<'a, 'c> UnwindSafe for ButtonBuilder<'a, 'c>

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
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

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.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

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.
§

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.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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,

§

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>,

§

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>,

§

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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<G1, G2> Within<G2> for G1
where G2: Contains<G1>,

§

fn is_within(&self, b: &G2) -> bool