[][src]Trait kas::prelude::Layout

pub trait Layout: WidgetChildren {
    fn size_rules(
        &mut self,
        size_handle: &mut dyn SizeHandle,
        axis: AxisInfo
    ) -> SizeRules;
fn draw(
        &self,
        draw_handle: &mut dyn DrawHandle,
        mgr: &ManagerState,
        disabled: bool
    ); fn set_rect(&mut self, rect: Rect, _align: AlignHints) { ... }
fn translation(&self, _child_index: usize) -> Coord { ... }
fn spatial_range(&self) -> (usize, usize) { ... }
fn find_id(&self, coord: Coord) -> Option<WidgetId> { ... } }

Positioning and drawing routines for widgets

This trait is part of the Widget family. It may be derived by derive(Widget), but is not by default.

This trait contains methods concerned with positioning of contents as well as low-level event handling.

For parent widgets, the implementation will often be derived (see kas::macros); otherwise, a layout engine may be used (see kas::layout). For leaf widgets, it is implemented directly.

For a description of the widget size model, see SizeRules.

Required methods

fn size_rules(
    &mut self,
    size_handle: &mut dyn SizeHandle,
    axis: AxisInfo
) -> SizeRules

Get size rules for the given axis

This method takes &mut self to allow local caching of child widget configuration for future size_rules and set_rect calls. Fields written by set_rect should not be used for this cache since set_rect may be called multiple times without re-calling size_rules.

To allow automatic flow of content over new lines, the width is sized first, followed by the height; when sizing for height, AxisInfo contains the size of the other axis (i.e. the width).

For widgets with children, a kas::layout::RulesSolver engine may be useful to calculate requirements of complex layouts.

fn draw(
    &self,
    draw_handle: &mut dyn DrawHandle,
    mgr: &ManagerState,
    disabled: bool
)

Draw a widget and its children

This method is invoked each frame to draw visible widgets. It should draw itself and recurse into all visible children.

The disabled argument is passed in from the parent; a widget should use let disabled = disabled || self.is_disabled(); to determine its own disabled state, then pass this value on to children.

WidgetCore::input_state may be used to obtain an InputState to determine active visual effects.

Loading content...

Provided methods

fn set_rect(&mut self, rect: Rect, _align: AlignHints)

Apply a given rect to self

For widgets without children, the trivial default implementation of this method often suffices, though some widgets choose to align themselves within this space. Alignment may be applied in one of two ways:

  1. Shrinking to ideal area and aligning within available space (e.g. CheckBoxBare widget)
  2. Filling available space and applying alignment to contents (e.g. Label widget)

For widgets with children, a kas::layout::RulesSetter engine may be useful (used with a corresponding kas::layout::RulesSolver).

One may assume that size_rules has been called at least once for each axis with current size information before this method, however size_rules might not be re-called before calling set_rect again.

fn translation(&self, _child_index: usize) -> Coord

Get translation of a child

Children may live in a translated coordinate space relative to their parent. This method returns an offset which should be added to a coordinate to translate into the child's coordinate space or subtracted to translate out.

In most cases, the translation will be zero. Widgets should return Coord::ZERO for non-existant children.

fn spatial_range(&self) -> (usize, usize)

Iterate through children in spatial order

Returns a "range" of children, by index, in spatial order. Unlike std::ops::Range this is inclusive and reversible, e.g. (1, 3) means 1, 2, 3 and (5, 2) means 5, 4, 3, 2. As a special case, (_, std::usize::MAX) means the range is empty.

Widgets should return a range over children in spatial order (left-to-right then top-to-bottom). Widgets outside the parent's rect (i.e. popups) should be excluded.

The default implementation should suffice for most widgets (excluding pop-up parents and those with reversed child order).

fn find_id(&self, coord: Coord) -> Option<WidgetId>

Find a widget by coordinate

Returns the identifier of the widget containing this coord, if any. Should only return None when coord is outside the widget's rect, but this is not guaranteed.

Implementations should:

  1. return None if !self.rect().contains(coord)
  2. if, for any child (containing coord), child.find_id(coord) returns Some(id), return that
  3. otherwise, return Some(self.id())

Exceptionally, a widget may deviate from this behaviour, but only when the coord is within the widget's rect (example: CheckBox contains an embedded CheckBoxBare and always forwards this child's id).

This must not be called before Layout::set_rect.

Loading content...

Implementations on Foreign Types

impl<M: 'static> Layout for Box<dyn Widget<Msg = M>>[src]

impl<M: 'static> Layout for Box<dyn Menu<Msg = M>>[src]

Loading content...

Implementors

impl Layout for AccelLabel[src]

impl Layout for DragHandle[src]

This implementation is unusual in that:

  1. size_rules always returns SizeRules::EMPTY
  2. set_rect sets the track within which this handle may move; the parent should call DragHandle::set_size_and_offset after set_rect (otherwise the handle's offset will not be updated)
  3. draw does nothing: the parent is expected to do all drawing

impl Layout for Filler[src]

impl Layout for Label[src]

impl Layout for MessageBox[src]

impl<D: Directional> Layout for ScrollBar<D>[src]

impl<D: Directional, W: Widget> Layout for List<D, W>[src]

impl<D: Directional, W: Widget> Layout for Splitter<D, W>[src]

impl<D: Directional, W: Menu> Layout for MenuBar<D, W>[src]

impl<D: Directional, W: Menu> Layout for SubMenu<D, W>[src]

impl<G: 'static> Layout for EditBox<G>[src]

impl<M: 'static> Layout for CheckBox<M>[src]

impl<M: 'static> Layout for CheckBoxBare<M>[src]

impl<M: 'static> Layout for MenuToggle<M>[src]

impl<M: 'static> Layout for RadioBox<M>[src]

impl<M: 'static> Layout for RadioBoxBare<M>[src]

impl<M: Clone + Debug + 'static> Layout for ComboBox<M>[src]

impl<M: Clone + Debug + 'static> Layout for MenuEntry<M>[src]

impl<M: Clone + Debug + 'static> Layout for TextButton<M>[src]

impl<M: Debug> Layout for Separator<M>[src]

impl<T: SliderType, D: Directional> Layout for Slider<T, D>[src]

impl<W: Widget> Layout for Frame<W>[src]

impl<W: Widget> Layout for MenuFrame<W>[src]

impl<W: Widget> Layout for ScrollRegion<W>[src]

impl<W: Widget> Layout for Stack<W>[src]

impl<W: Widget> Layout for Window<W>[src]

Loading content...