Trait conrod::widget::Widget [] [src]

pub trait Widget: Common + Sized {
    type State: Any + Send;
    type Style: Style + Send;
    type Event;
    fn init_state(&self, _: Generator) -> Self::State;
fn style(&self) -> Self::Style;
fn update(self, args: UpdateArgs<Self>) -> Self::Event; fn default_x_position(&self, ui: &Ui) -> Position { ... }
fn default_y_position(&self, ui: &Ui) -> Position { ... }
fn default_x_dimension(&self, ui: &Ui) -> Dimension { ... }
fn default_y_dimension(&self, ui: &Ui) -> Dimension { ... }
fn drag_area(
        &self,
        _dim: Dimensions,
        _style: &Self::Style,
        _theme: &Theme
    ) -> Option<Rect> { ... }
fn kid_area(&self, args: KidAreaArgs<Self>) -> KidArea { ... }
fn parent(self, parent_id: Id) -> Self { ... }
fn no_parent(self) -> Self { ... }
fn place_on_kid_area(self, b: bool) -> Self { ... }
fn graphics_for(self, id: Id) -> Self { ... }
fn floating(self, is_floating: bool) -> Self { ... }
fn crop_kids(self) -> Self { ... }
fn scroll_kids(self) -> Self { ... }
fn scroll_kids_vertically(self) -> Self { ... }
fn scroll_kids_horizontally(self) -> Self { ... }
fn and<F>(self, build: F) -> Self
    where
        F: FnOnce(Self) -> Self
, { ... }
fn and_mut<F>(self, mutate: F) -> Self
    where
        F: FnOnce(&mut Self)
, { ... }
fn and_if<F>(self, cond: bool, build: F) -> Self
    where
        F: FnOnce(Self) -> Self
, { ... }
fn and_then<T, F>(self, maybe: Option<T>, build: F) -> Self
    where
        F: FnOnce(Self, T) -> Self
, { ... }
fn set<'a, 'b>(self, id: Id, ui_cell: &'a mut UiCell<'b>) -> Self::Event { ... } }

A trait to be implemented by all Widget types.

A type that implements Widget can be thought of as a collection of arguments to the Widget's Widget::update method. They type itself is not stored between updates, but rather is used to update an instance of the Widget's Widget::State, which is stored.

Methods that must be overridden:

Methods that can be optionally overridden:

Methods that should not be overridden:

Associated Types

State to be stored within the Uis widget cache.

Take advantage of this type for any large allocations that you would like to avoid repeating between updates, or any calculations that you'd like to avoid repeating between calls to update.

Conrod will never clone the state, it will only ever be moved.

Every widget is required to have its own associated Style type. This type is intended to contain high-level styling information for the widget that can be optionally specified by a user of the widget.

All Style structs are typically Copy and contain simple, descriptive fields like color, font_size, line_spacing, border_width, etc. These types are also required to be PartialEq. This is so that the Ui may automatically compare the previous style to the new style each time .set is called, allowing conrod to automatically determine whether or not something has changed and if a re-draw is required.

Each field in a Style struct is typically an Option<T>. This is so that each field may be optionally specified, indicating to fall back to defaults if the fields are None upon style retrieval.

The reason this data is required to be in its own Style type (rather than in the widget type itself) is so that conrod can distinguish between default style data that may be stored within the Theme's widget_styling field, and other data that is necessary for the widget's behaviour logic. Having Style be an associated type makes it trivial to retrieve unique, widget-specific styling data for each widget from a single method (see Theme::widget_style).

#[derive(WidgetStyle)]

These Style types are often quite similar and their implementations can involve a lot of boilerplate when written by hand. To get around this, conrod provides #[derive(WidgetStyle)].

This procedural macro generates a "getter"-style method for each struct field that is decorated with a #[conrod(default = "expr")] attribute. The generated methods have the same name as their respective fields and behave as follows:

  1. First, the method will attempt to return the value directly if the field is Some.
  2. If the field is None, the method will fall back to the Widget::Style stored within the Theme's widget_styling map.
  3. If there are no style defaults for the widget in the Theme, or if there is but the default field is also None, the method will fall back to the expression specified within the field's #[conrod(default = "expr")] attribute.

The given "expr" can be a string containing any expression that returns the type specified within the field's Option type parameter. The expression may also utilise the theme and self bindings, where theme is a binding to the borrowed Theme and self is a binding to the borrowed instance of this Style type.

Examples

/// Unique styling for a Button widget.
#[derive(Copy, Clone, Debug, Default, PartialEq, WidgetStyle)]
pub struct Style {
    /// Color of the Button's pressable area.
    #[conrod(default = "theme.shape_color")]
    pub color: Option<Color>,
    /// Width of the border surrounding the button.
    #[conrod(default = "1.0")]
    pub border: Option<Scalar>,
    /// The color of the Button's rectangular border.
    #[conrod(default = "conrod::color::BLACK")]
    pub border_color: Option<Color>,
    /// The color of the Button's label.
    #[conrod(default = "theme.label_color")]
    pub label_color: Option<Color>,
    /// The font size for the Button's label.
    #[conrod(default = "12")]
    pub label_font_size: Option<FontSize>,
}

The type of event yielded by the widget, returned via the Widget::set function.

For a Toggle widget, this might be a bool.

For a non-interactive, purely graphical widget, this might be ().

Required Methods

Return the initial State of the Widget.

The Ui will only call this once, immediately prior to the first time that Widget::update is first called.

Return the styling of the widget.

The Ui will call this once prior to each update. It does this so that it can check for differences in Style in case we need to re-draw the widget.

Update our Widget's unique Widget::State via the State wrapper type (the state field within the UpdateArgs).

Whenever State::update is called, a has_updated flag is set within the State, indicating that there has been some change to the unique Widget::State and that we require re-drawing the Widget. As a result, widget designers should only call State::update when necessary, checking whether or not the state has changed before invoking the method. See the custom_widget.rs example for a demonstration of this.

Arguments

  • id - The Widget's unique index (whether Public or Internal).
  • prev - The previous common state of the Widget. If this is the first time update is called, Widget::init_state will be used to produce some intial state instead.
  • state - A wrapper around the Widget::State. See the State docs for more details.
  • rect - The position (centered) and dimensions of the widget.
  • style - The style produced by the Widget::style method.
  • ui - A wrapper around the Ui, offering restricted access to its functionality. See the docs for UiCell for more details.

Provided Methods

The default Position for the widget along the x axis.

This is used when no Position is explicitly given when instantiating the Widget.

The default Position for the widget along the y axis.

This is used when no Position is explicitly given when instantiating the Widget.

The default width for the Widget.

This method is only used if no height is explicitly given.

By default, this simply calls default_dimension with a fallback absolute dimension of 0.0.

The default height of the widget.

By default, this simply calls default_dimension with a fallback absolute dimension of 0.0.

If the widget is draggable, implement this method and return the position and dimensions of the draggable space. The position should be relative to the center of the widget.

The area on which child widgets will be placed when using the Place Position methods.

Set the parent widget for this Widget by passing the WidgetId of the parent.

This will attach this Widget to the parent widget.

Specify that this widget has no parent widgets.

Set whether or not the Widget should be placed on the kid_area.

If true, the Widget will be placed on the kid_area of the parent Widget if the Widget is given a Place variant for its Position.

If false, the Widget will be placed on the parent Widget's total area.

By default, conrod will automatically determine this for you by checking whether or not the Widget that our Widget is being placed upon returns Some from its Widget::kid_area method.

Indicates that the Widget is used as a non-interactive graphical element for some other widget.

This is useful for Widgets that are used to compose some other Widget.

When adding an edge a -> b where b is considered to be a graphical element of a, several things are implied about b:

  • If b is picked within either Graph::pick_widget or Graph::pick_top_scrollable_widget, it will instead return the index for a.
  • When determining the Graph::scroll_offset for b, a's scrolling (if it is scrollable, that is) will be skipped.
  • b will always be placed upon a's total area, rather than its kid_area which is the default.
  • Any Graphic child of b will be considered as a Graphic child of a.

Set whether or not the widget is floating (the default is false). A typical example of a floating widget would be a pop-up or alert window.

A "floating" widget will always be rendered after its parent tree and all widgets connected to its parent tree. If two sibling widgets are both floating, then the one that was last clicked will be rendered last. If neither are clicked, they will be rendered in the order in which they were cached into the Ui.

Indicates that all widgets who are children of this widget should be cropped to the kid_area of this widget.

Makes the widget's KidArea scrollable.

If a widget is scrollable and it has children widgets that fall outside of its KidArea, the KidArea will become scrollable.

This method calls Widget::crop_kids internally.

Makes the widget's KidArea scrollable.

If a widget is scrollable and it has children widgets that fall outside of its KidArea, the KidArea will become scrollable.

This method calls Widget::crop_kids internally.

Set whether or not the widget's KidArea is scrollable (the default is false).

If a widget is scrollable and it has children widgets that fall outside of its KidArea, the KidArea will become scrollable.

This method calls Widget::crop_kids internally.

A builder method that "lifts" the Widget through the given build function.

This method is solely for providing slight ergonomic improvement by helping to maintain the symmetry of the builder pattern in some cases.

A builder method that mutates the Widget with the given mutate function.

This method is solely for providing slight ergonomic improvement by helping to maintain the symmetry of the builder pattern in some cases.

A method that conditionally builds the Widget with the given build function.

If cond is true, build(self) is evaluated and returned.

If false, self is returned.

A method that optionally builds the the Widget with the given build function.

If maybe is Some(t), build(self, t) is evaluated and returned.

If None, self is returned.

Note: There should be no need to override this method.

After building the widget, you call this method to set its current state into the given Ui. More precisely, the following will occur when calling this method: - The widget's previous state and style will be retrieved. - The widget's current Style will be retrieved (from the Widget::style method). - The widget's state will be updated (using the Widget::udpate method). - If the widget's state or style has changed, the Ui will be notified that the widget needs to be re-drawn. - The new State and Style will be cached within the Ui.

Implementors