Trait conrod::Widget [] [src]

pub trait Widget: Sized {
    type State: Any + PartialEq + Debug;
    type Style: Any + PartialEq + Debug;
    fn common(&self) -> &CommonBuilder;
    fn common_mut(&mut self) -> &mut CommonBuilder;
    fn unique_kind(&self) -> &'static str;
    fn init_state(&self) -> Self::State;
    fn style(&self) -> Self::Style;
    fn update<'a, 'b, C>(self, args: UpdateArgs<'a, 'b, Self, C>) -> Option<Self::State> where C: CharacterCache;
    fn draw<'a, C>(args: DrawArgs<'a, Self, C>) -> Element where C: CharacterCache;

    fn default_position(&self, _theme: &Theme) -> Position { ... }
    fn default_h_align(&self, theme: &Theme) -> HorizontalAlign { ... }
    fn default_v_align(&self, theme: &Theme) -> VerticalAlign { ... }
    fn default_width<C: CharacterCache>(&self, _theme: &Theme, _: &GlyphCache<C>) -> Scalar { ... }
    fn default_height(&self, _theme: &Theme) -> Scalar { ... }
    fn capture_mouse(_prev: &Self::State, _new: &Self::State) -> bool { ... }
    fn uncapture_mouse(_prev: &Self::State, _new: &Self::State) -> bool { ... }
    fn capture_keyboard(_prev: &Self::State, _new: &Self::State) -> bool { ... }
    fn uncapture_keyboard(_prev: &Self::State, _new: &Self::State) -> bool { ... }
    fn drag_area(&self, _dim: Dimensions, _style: &Self::Style, _theme: &Theme) -> Option<Area> { ... }
    fn kid_area(state: &State<Self::State>, _style: &Self::Style, _theme: &Theme) -> KidArea { ... }
    fn parent(self, maybe_parent_id: Option<WidgetId>) -> Self { ... }
    fn floating(self, is_floating: bool) -> Self { ... }
    fn scrolling(self, scrollable: bool) -> Self { ... }
    fn vertical_scrolling(self, scrollable: bool) -> Self { ... }
    fn horizontal_scrolling(self, scrollable: bool) -> Self { ... }
    fn set_children<C>(_id: WidgetId, _state: &State<Self::State>, _style: &Self::Style, _ui: &mut Ui<C>) where C: CharacterCache { ... }
    fn set<C>(self, id: WidgetId, ui: &mut Ui<C>) where C: CharacterCache { ... }
}

A trait to be implemented by all Widget types.

Methods that must be overridden: - common_mut - common - unique_kind - init_state - style - update - draw

Methods that can be optionally overridden: - parent_id - capture_mouse - uncapture_mouse - capture_keyboard - uncapture_keyboard - default_position - default_width - default_height - default_h_align - default_v_align

Methods that should not be overridden: - parent - set

Associated Types

type State: Any + PartialEq + Debug

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 and draw. Conrod will never clone the state, it will only ever be moved.

type Style: Any + PartialEq + Debug

Styling used by the widget to construct an Element. Styling is useful to have in its own abstraction in order to making Theme serializing easier. Conrod doesn't yet support serializing non-internal widget styling with the Theme type, but we hope to soon.

Required Methods

fn common(&self) -> &CommonBuilder

Return a reference to a CommonBuilder struct owned by the Widget. This method allows us to do a blanket impl of Positionable and Sizeable for T: Widget.

fn common_mut(&mut self) -> &mut CommonBuilder

Return a mutable reference to a CommonBuilder struct owned by the Widget. This method allows us to do a blanket impl of Positionable and Sizeable for T: Widget.

fn unique_kind(&self) -> &'static str

Return the kind of the widget as a &'static str. Note that this must be unique from all other widgets' "unique kinds". This is used by conrod to help avoid WidgetId errors and to provide better messages for those that do occur.

fn init_state(&self) -> Self::State

Return the initial State of the Widget. The Ui will only call this once.

fn style(&self) -> Self::Style

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 a new Element needs to be constructed.

fn update<'a, 'b, C>(self, args: UpdateArgs<'a, 'b, Self, C>) -> Option<Self::State> where C: CharacterCache

Your widget's previous state is given to you as a parameter and it is your job to construct and return an Update that will be used to update the widget's cached state. You only have to return Some state if the resulting state would be different to prev. If Some new state was returned, Widget::draw will be called in order to construct an up to date Element.

Arguments

  • prev - The previous state of the Widget. If none existed, Widget::init_state will be used to pass the initial state instead.
  • xy - The coordinates representing the middle of the widget.
  • dim - The dimensions of the widget.
  • input - A view into the current state of the user input (i.e. mouse and keyboard).
  • current_style - The style just produced by the Widget::style method.
  • theme - The currently active Theme within the Ui.
  • glyph_cache - Used for determining the size of rendered text if necessary.

fn draw<'a, C>(args: DrawArgs<'a, Self, C>) -> Element where C: CharacterCache

Construct a renderable Element from the current styling and new state. This will only be called on the occasion that the widget's Style or State has changed. Keep this in mind when designing your widget's Style and State types.

Arguments

  • new_state - The freshly produced State which contains the unique widget info necessary for rendering.
  • current_style - The freshly produced Style of the widget.
  • theme - The currently active Theme within the Ui.
  • glyph_cache - Used for determining the size of rendered text if necessary.

Provided Methods

fn default_position(&self, _theme: &Theme) -> Position

The default Position for the widget. This is used when no Position is explicitly given when instantiating the Widget.

fn default_h_align(&self, theme: &Theme) -> HorizontalAlign

The default horizontal alignment for the widget. This is used when no HorizontalAlign is explicitly given when instantiating a Widget.

fn default_v_align(&self, theme: &Theme) -> VerticalAlign

The default vertical alignment for the widget. This is used when no VerticalAlign is explicitly given when instantiating a Widget.

fn default_width<C: CharacterCache>(&self, _theme: &Theme, _: &GlyphCache<C>) -> Scalar

The default width of the widget. A reference to the GlyphCache is provided in case the width should adjust to some text len. This method is only used if no width or dimensions are given.

fn default_height(&self, _theme: &Theme) -> Scalar

The default height of the widget. This method is only used if no height or dimensions are given.

fn capture_mouse(_prev: &Self::State, _new: &Self::State) -> bool

Optionally override with the case that the widget should capture the mouse.

fn uncapture_mouse(_prev: &Self::State, _new: &Self::State) -> bool

Optionally override with the case that the widget should capture the mouse.

fn capture_keyboard(_prev: &Self::State, _new: &Self::State) -> bool

Optionally override with the case that the widget should capture the mouse.

fn uncapture_keyboard(_prev: &Self::State, _new: &Self::State) -> bool

Optionally override with the case that the widget should capture the mouse.

fn drag_area(&self, _dim: Dimensions, _style: &Self::Style, _theme: &Theme) -> Option<Area>

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

fn kid_area(state: &State<Self::State>, _style: &Self::Style, _theme: &Theme) -> KidArea

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

fn parent(self, maybe_parent_id: Option<WidgetId>) -> Self

Set the parent widget for this Widget by passing the WidgetId of the parent. This will attach this Widget to the parent widget.

fn floating(self, is_floating: bool) -> Self

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.

fn scrolling(self, scrollable: bool) -> Self

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.

fn vertical_scrolling(self, scrollable: bool) -> Self

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.

fn horizontal_scrolling(self, scrollable: bool) -> Self

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.

fn set_children<C>(_id: WidgetId, _state: &State<Self::State>, _style: &Self::Style, _ui: &mut Ui<C>) where C: CharacterCache

An optionally overridable method for setting child widgets. This will be called immediately after the widget itself is updated. NOTE: The API for this will probably changed somehow, as there is probably a nicer way to do this than give the widget designer mutable access to the entire Ui.

fn set<C>(self, id: WidgetId, ui: &mut Ui<C>) where C: CharacterCache

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, Widget::draw will be called to create the new Element for rendering. - The new State, Style and Element (if there is one) will be cached within the Ui.

Implementors