Struct kas::layout::SizeRules[][src]

pub struct SizeRules { /* fields omitted */ }
Expand description

Widget sizing information

This is the return value of crate::Layout::size_rules and is used to describe size and margin requirements for widgets. This type only concerns size requirements along a single axis.

All units are in pixels. Sizes usually come directly from SizeHandle or from a fixed quantity multiplied by SizeHandle::scale_factor.

Sizes

The widget size model is simple: a rectangular box, plus a margin on each side. Widget sizes are calculated from available space and the SizeRules; these rules currently include:

  • the minimum size required for correct operation
  • the preferred / ideal size
  • a Stretch priority

Available space is distributed between widgets depending on whether the space is below the minimum, between the minimum and preferred, or above the preferred size, with widgets with the highest Stretch priority being prioritised extra space. Usually rows/columns will be stretched to use all available space, the exception being when none have a priority higher than Stretch::None. When expanding a row/column, the highest stretch priority of all contents will be used.

Margins

Required margin sizes are handled separately for each side of a widget. Since SizeRules concerns only one axis, it stores only two margin sizes: “pre” (left/top) and “post” (right/bottom). These are stored as u16 values on the assumption that no margin need exceed 65536.

When widgets are placed next to each other, their margins may be combined; e.g. if a widget with margin of 6px is followed by another with margin 2px, the required margin between the two is the maximum, 6px.

Only the layout engine and parent widgets need consider margins (beyond their specification). For these cases, one needs to be aware that due to margin-merging behaviour, one cannot simply “add” two SizeRules. Instead, when placing one widget next to another, use SizeRules::append or SizeRules::appended; when placing a widget within a frame, use FrameRules::surround_with_margin or FrameRules::surround_as_margin. When calculating the size of a sequence of widgets, one may use the Sum implementation (this assumes that the sequence is in left-to-right or top-to-bottom order).

Alignment

SizeRules concerns calculations of size requirements, which the layout engine uses to assign each widget a Rect; it is up to the widget itself to either fill this rect or align itself within the given space. See crate::Layout::set_rect for more information.

For widgets with a stretch priority of Stretch::None, it is still possible for layout code to assign a size larger than the preference. It is up to the widget to align itself within this space: see crate::Layout::set_rect and crate::layout::AlignHints.

Implementations

Empty (zero size) widget

Warning: appending another size to EMPTY does include margins even though EMPTY itself has zero size. However, EMPTY itself has zero-size margins, so this only affects appending an EMPTY with a non-empty SizeRules.

Empty space with the given stretch priority

See warning on SizeRules::EMPTY.

A fixed size with given (pre, post) margins

A fixed size, scaled from virtual pixels

This is a shortcut to SizeRules::fixed using virtual-pixel sizes and a scale factor. It also assumes both margins are equal.

Construct rules from given data

Construct fixed-size rules from given data

Construct with custom rules

Region size should meet the given min-imum size and has a given ideal size, plus a given stretch priority.

Expected: ideal >= min (if not, ideal is clamped to min).

Construct with custom rules, scaled from virtual pixels

This is a shortcut around SizeRules::new. It assumes that both margins are equal.

Region size should meet the given min-imum size and has a given ideal size, plus a given stretch priority.

Expected: ideal >= min (if not, ideal is clamped to min).

Get the minimum size

Get the ideal size

Get the max size

With most stretch policies, this returns i32::MAX, but with Stretch::None, this is SizeRules::ideal_size.

Get the (pre, post) margin sizes

Get the (pre, post) margin sizes, cast to i32

Get the stretch priority

Set the stretch priority

Set margins to max of own margins and given margins

Use the maximum size of self and rhs.

Set self = self.max(rhs);

Multiply the (min, ideal) size, including internal margins

E.g. given margin = margins.0 + margins.1 and factors (2, 5), the minimum size is set to min * 2 + margin and the ideal to ideal * 5 + 4 * margin.

Panics if either factor is 0.

Append the rules for rhs to self

This implies that rhs rules concern an element to the right of or below self. Note that order matters since margins may be combined.

Note also that appending SizeRules::EMPTY does include interior margins (those between EMPTY and the other rules) within the result.

Return the rules for self appended by rhs

This implies that rhs rules concern an element to the right of or below self. Note that order matters since margins may be combined.

Note also that appending SizeRules::EMPTY does include interior margins (those between EMPTY and the other rules) within the result.

Return the result of appending all given ranges

Return the result of appending all given ranges (min only)

This is a specialised version of sum: only the minimum is calculated

Set self to self - x + y, clamped to 0 or greater

This is a specialised operation to join two spans, subtracing the common overlap (x), thus margins are self.m.0 and y.m.1.

Reduce the minimum size

If min is greater than the current minimum size, this has no effect.

Adjust a sequence of rules to ensure that the total is at least self

This is used by grids to ensure that cell spans are sufficiently large.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Return the sum over a sequence of rules, assuming these are ordered

Uses SizeRules::appended on all rules in sequence.

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Return the sum over a sequence of rules, assuming these are ordered

Uses SizeRules::appended on all rules in sequence.

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Cast from Self to T

Try converting from Self to T

Cast to integer, truncating Read more

Cast to the nearest integer Read more

Cast the floor to an integer Read more

Cast the ceiling to an integer Read more

Try converting to integer with truncation Read more

Try converting to the nearest integer Read more

Try converting the floor to an integer Read more

Try convert the ceiling to an integer Read more

Convert from T to Self

Try converting from T to Self

Compare self to key and return true if they are equal.

Returns true if the given item matches this filter

Performs the conversion.

Performs the conversion.

Checks that type has a default value.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.