[−][src]Trait conrod_core::widget::Widget
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:
- init_state
- style
- update
Methods that can be optionally overridden:
- default_x_position
- default_y_position
- default_width
- default_height
- drag_area
- kid_area
Methods that should not be overridden:
- floating
- scroll_kids
- scroll_kids_vertically
- scroll_kids_horizontally
- place_widget_on_kid_area
- parent
- no_parent
- set
Associated Types
type State: Any + Send
[−]
State to be stored within the Ui
s 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.
type Style: Style + Send
[−]
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:
- First, the method will attempt to return the value directly if the field is
Some
. - If the field is
None
, the method will fall back to theWidget::Style
stored within theTheme
'swidget_styling
map. - If there are no style defaults for the widget in the
Theme
, or if there is but the default field is alsoNone
, 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_core::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>, }
type Event
[−]
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
fn init_state(&self, _: Generator<'_>) -> Self::State
[−]
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.
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 we need to re-draw the widget.
fn update(self, args: UpdateArgs<'_, '_, '_, '_, Self>) -> Self::Event
[−]
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 (whetherPublic
orInternal
). - 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 initial 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 forUiCell
for more details.
Provided methods
fn default_x_position(&self, ui: &Ui) -> Position
[−]
The default Position for the widget along the x axis.
This is used when no Position is explicitly given when instantiating the Widget.
fn default_y_position(&self, ui: &Ui) -> Position
[−]
The default Position for the widget along the y axis.
This is used when no Position is explicitly given when instantiating the Widget.
fn default_x_dimension(&self, ui: &Ui) -> Dimension
[−]
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.
fn default_y_dimension(&self, ui: &Ui) -> Dimension
[−]
The default height of the widget.
By default, this simply calls default_dimension with a fallback absolute dimension of 0.0.
fn drag_area(
&self,
_dim: Dimensions,
_style: &Self::Style,
_theme: &Theme
) -> Option<Rect>
[−]
&self,
_dim: Dimensions,
_style: &Self::Style,
_theme: &Theme
) -> Option<Rect>
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.
fn kid_area(&self, args: KidAreaArgs<'_, Self>) -> KidArea
[−]
The area on which child widgets will be placed when using the Place
Position
methods.
fn is_over(&self) -> IsOverFn
[−]
Returns either of the following:
- A
Function
that can be used to describe whether or not a given point is over the widget. - The
Id
of anotherWidget
that can be used to determine if the point is over this widget.
By default, this is a function returns true
if the given Point
is over the bounding
Rect
and returns false
otherwise.
NOTE: It could be worth removing this in favour of adding a widget::State
trait, adding
an is_over
method to it and then refactoring the Container
to store a
Box<widget::State>
and Box<widget::Style>
rather than Box<Any>
. This would however
involve some significant breakage (which could perhaps be mitigated by adding a
derive(WidgetState)
macro - a fair chunk of work) so this might be the easiest temporary
way forward for now.
fn parent(self, parent_id: Id) -> 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 no_parent(self) -> Self
[−]
Specify that this widget has no parent widgets.
fn place_on_kid_area(self, b: bool) -> Self
[−]
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.
fn graphics_for(self, id: Id) -> Self
[−]
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.
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 crop_kids(self) -> Self
[−]
Indicates that all widgets who are children of this widget should be cropped to the
kid_area
of this widget.
fn scroll_kids(self) -> Self
[−]
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.
fn scroll_kids_vertically(self) -> Self
[−]
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.
fn scroll_kids_horizontally(self) -> 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.
This method calls Widget::crop_kids
internally.
fn and<F>(self, build: F) -> Self where
F: FnOnce(Self) -> Self,
[−]
F: FnOnce(Self) -> Self,
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.
fn and_mut<F>(self, mutate: F) -> Self where
F: FnOnce(&mut Self),
[−]
F: FnOnce(&mut Self),
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.
fn and_if<F>(self, cond: bool, build: F) -> Self where
F: FnOnce(Self) -> Self,
[−]
F: FnOnce(Self) -> Self,
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.
fn and_then<T, F>(self, maybe: Option<T>, build: F) -> Self where
F: FnOnce(Self, T) -> Self,
[−]
F: FnOnce(Self, T) -> Self,
A method that optionally builds the Widget with the given build
function.
If maybe
is Some(t)
, build(self, t)
is evaluated and returned.
If None
, self
is returned.
fn set<'a, 'b>(self, id: Id, ui_cell: &'a mut UiCell<'b>) -> Self::Event
[−]
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 theWidget::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
impl Widget for BorderedRectangle
[src][+]
impl Widget for Matrix
[src][+]
impl Widget for Image
[src][+]
impl Widget for Line
[src][+]
impl Widget for Rectangle
[src][+]
impl Widget for RoundedRectangle
[src][+]
impl<'a> Widget for Button<'a, Flat>
[src][+]
impl<'a> Widget for Button<'a, Image>
[src][+]
impl<'a> Widget for Canvas<'a>
[src][+]
impl<'a> Widget for CollapsibleArea<'a>
[src][+]
impl<'a> Widget for DirectoryView<'a>
[src][+]
impl<'a> Widget for FileNavigator<'a>
[src][+]
impl<'a> Widget for Text<'a>
[src][+]
impl<'a> Widget for Tabs<'a>
[src][+]
impl<'a> Widget for TextBox<'a>
[src][+]
impl<'a> Widget for TextEdit<'a>
[src][+]
impl<'a> Widget for TitleBar<'a>
[src][+]
impl<'a> Widget for Toggle<'a>
[src][+]
impl<'a, E> Widget for EnvelopeEditor<'a, E> where
E: EnvelopePoint,
[src][+]
E: EnvelopePoint,
impl<'a, N, E> Widget for Graph<'a, N, E> where
N: Iterator,
N::Item: NodeId,
E: Iterator<Item = (NodeSocket<N::Item>, NodeSocket<N::Item>)>,
[src][+]
N: Iterator,
N::Item: NodeId,
E: Iterator<Item = (NodeSocket<N::Item>, NodeSocket<N::Item>)>,
impl<'a, T> Widget for DropDownList<'a, T> where
T: AsRef<str>,
[src][+]
T: AsRef<str>,
impl<'a, T> Widget for NumberDialer<'a, T> where
T: Float + NumCast + ToString,
[src][+]
T: Float + NumCast + ToString,
impl<'a, T> Widget for RangeSlider<'a, T> where
T: Float,
[src][+]
T: Float,
impl<'a, T> Widget for Slider<'a, T> where
T: Float + NumCast + ToPrimitive,
[src][+]
T: Float + NumCast + ToPrimitive,
impl<'a, X, Y> Widget for XYPad<'a, X, Y> where
X: Float + ToString + Debug + Any,
Y: Float + ToString + Debug + Any,
[src][+]
X: Float + ToString + Debug + Any,
Y: Float + ToString + Debug + Any,
impl<A> Widget for Scrollbar<A> where
A: Axis,
[src][+]
A: Axis,
impl<D, S> Widget for List<D, S> where
D: Direction,
S: ItemSize,
[src][+]
D: Direction,
S: ItemSize,
impl<I> Widget for PointPath<I> where
I: IntoIterator<Item = Point>,
[src][+]
I: IntoIterator<Item = Point>,
impl<I> Widget for Polygon<I> where
I: IntoIterator<Item = Point>,
[src][+]
I: IntoIterator<Item = Point>,
impl<M, D, S> Widget for ListSelect<M, D, S> where
M: Mode,
D: Direction,
S: ItemSize,
[src][+]
M: Mode,
D: Direction,
S: ItemSize,
impl<S> Widget for Oval<S> where
S: OvalSection,
[src][+]
S: OvalSection,
impl<S, I> Widget for Triangles<S, I> where
S: Style,
I: IntoIterator<Item = Triangle<S::Vertex>>,
[src][+]
S: Style,
I: IntoIterator<Item = Triangle<S::Vertex>>,
impl<W> Widget for Node<W> where
W: Widget,
[src][+]
W: Widget,
impl<X, Y, F> Widget for PlotPath<X, Y, F> where
X: NumCast + Clone,
Y: NumCast + Clone,
F: FnMut(X) -> Y,
[src][+]
X: NumCast + Clone,
Y: NumCast + Clone,
F: FnMut(X) -> Y,
impl<X, Y, I> Widget for Grid<X, Y, I> where
X: Into<Scalar>,
Y: Into<Scalar>,
I: Iterator<Item = Axis<X, Y>>,
[src][+]
X: Into<Scalar>,
Y: Into<Scalar>,
I: Iterator<Item = Axis<X, Y>>,