Expand description
§Style
Traits and functions that allow for styling Views.
§The Floem Style System
§The Style struct
The style system is centered around a Style struct.
Style internally is just a hashmap (although one from the im crate so it is cheap to clone).
It maps from a StyleKey to Rc<dyn Any>.
§The StyleKey
StyleKey holds a static reference (that is used as the hash value) to a StyleKeyInfo enum which enumerates the different kinds of values that can be in the map.
Which value is in the StyleKeyInfo enum is used to know how to downcast the Rc<dyn Any.
The key types from the StyleKeyInfo are: (these are all of the different things that can be added to a Style).
- Transition,
- Prop(StylePropInfo),
- Selector(StyleSelectors),
- Class(StyleClassInfo),
- ContextMappings,
Transitions and context mappings don’t hold any extra information, they are just used to know how to downcast the Rc<dyn Any>.
StyleSelectors is a bit mask of which selectors are active.
StyleClassInfo holds a function pointer that returns the name of the class as a String.
The function pointer is basically used as a vtable for the class.
If classes needed more methods other than name, those methods would be added to StyleClassInfo.
StylePropInfo is another vtable, similar to StyleClassInfo and holds function pointers for getting the name of a prop, the props interpolation function from the StylePropValue trait, the associated transition key for the prop, and others.
Props store props. Transitions store transition values. Classes, context mappings, and selectors store nested Style maps.
§Applying Styles to Views
A style can be applied to a view in two different ways.
A single Style can be added to the view_style method of the view trait or multiple Styles can be added by calling style on an IntoView from the Decorators trait.
Calls to style from the decorators trait have a higher precedence than the view_style method, meaning calls to style will override any matching StyleKeyInfo that came from the view_style method.
If you make repeated calls to style from the decorators trait, each will be added separately to the ViewState that is managed by Floem and associated with the ViewId of the view that style was called on.
The ViewState stores a Stack of styles and later calls to style (and thus larger indicies in the style stack) will take precedence over earlier calls.
style from the deocrators trait is reactive and the function that returns the style map with be re-run in response to any reactive updates that it depends on.
If it gets a reactive update, it will have tracked which index into the style stack it had when it was first called and will overrite that index and only that index so that other calls to style are not affected.
§Style Resolution
A final computed_style is resolved in the style_pass of the View trait.
§Context
It first received a Style map that is used as context.
The context is passed down the view tree and carries the inherited properties that were applied to any parent.
Inherited properties include all classes and any prop that has been marked as inherited.
§View Style
The style first gets the Style (if any) from the view_style method.
§Style
Then it gets the style from any calls to style from the decorators trait.
It starts with the first index in the style Stack and applies each successive Style over the combination of any previous ones.
Then the style from the Decorators / ViewState is applied over (overriding any matching props) the style from view_style.
§Nested map resolution
Then any classes that have been applied to the view, and the active selector set are used to resolve nested maps.
Nested maps such as classes and selectors are recursively applied, breadth first. So, deeper / more nested style maps take precendence.
This style map is the combined style of the View.
§Updated context
Finally, the context style is updated using the combined style, applying any style key that is inherited to the context so that the children will have acces to them.
§Prop Extraction
The final computed style of a view will be passed to the style_pass method from the View trait.
Views will store fields that are struct that are prop extractors.
These structs are created using the prop_extractor! macro.
These structs can then be used from in the style_pass to extract props using the read (or read_exact) methods that are created by the prop_extractor macro.
The read methods will take in the combined style for that View and will automatically extract any matching prop values and transitions for those props.
§Transition interpolation
If there is a transition for a prop, the extractor will keep track of the current time and transition state and will set the final extracted value to a properly interpolated value using the state and current time.
§Custom Style Props, Classes, and Extractors.
You can create custom style props with the prop! macro, classes with the [style_class!] macro, and extractors with the prop_extractor! macro.
§Custom Props
You can create custom props.
Doing this allows you to store arbitrary values in the style system.
You can use these to style the view, change it’s behavior, update it’s state, or anything else.
By implementing the StylePropValue trait for your prop (which you must do) you can
-
optionally set how the prop should be interpolated (allowing you to customize what interpolating means in the context of your prop)
-
optionally provide a
debug_viewfor your prop, which debug view will be used in the Floem inspector. This means that you can customize a complex debug experience for your prop with very little effort (and it really can be any arbitrary view. no restrictions.) -
optionally add a custom implementation of how a prop should be combined with another prop. This is different from interpolation and is useful when you want to specify how properties should override each other. The default implementation just replaces the old value with a new value, but if you have a prop with multiple optional fields, you might want to only replace the fields that have a
Somevalue.
§Custom Classes
If you create a custom class, you can apply that class to any view, and when the final style for that view is being resolved, if the style has that class as a nested map, it will be applied, overriding any prviously set values.
§Custom Extractors
You can create custom extractors and embed them in your custom views so that you can get out any built in prop, or any of your custom props from the final combined style that is applied to your View.
Re-exports§
pub use theme::DesignSystem;pub use theme::StyleThemeExt;pub use unit::AnchorAbout;pub use unit::Angle;pub use unit::Auto;pub use unit::DurationUnitExt;pub use unit::Pct;pub use unit::Px;pub use unit::PxPct;pub use unit::PxPctAuto;pub use unit::UnitExt;pub use recalc::InheritedChanges;pub use recalc::InheritedGroups;pub use recalc::Propagate;pub use recalc::RecalcFlags;pub use recalc::StyleRecalcChange;
Modules§
Structs§
- Align
Content Prop - Controls alignment of wrapped flex lines.
- Align
Items Prop - Controls alignment of flex items along the cross axis.
- Align
Self - Controls individual alignment along the cross axis.
- Aspect
Ratio - Sets the preferred aspect ratio for the view.
- Background
- Sets the background color or image.
- Border
- Structure holding border widths for all four sides
- Border
Color - Structure holding border colors for all four sides
- Border
Color Prop - Sets the border color for all sides.
- Border
Progress - Controls the progress/completion of the border animation.
- Border
Prop - Sets the border properties for all sides.
- Border
Radius - Structure holding border radius for all four corners
- Border
Radius Prop - Sets the border radius for all corners.
- BoxShadow
- Structure holding data about the shadow.
- BoxShadow
Prop - Adds one or more drop shadows to the view.
- BoxSizing
Prop - Controls how the total width and height are calculated.
- BoxTree
Props - Builtin
Style - ColGap
- Sets the gap between columns in grid or flex layouts.
- Cursor
- Sets the cursor style when hovering over the view.
- Cursor
Color - Sets the color of the text cursor.
- Dimension
- A unit of linear measurement
- Direct
Transition - Direct transition controller using TransitionState without the Style system.
- Disabled
- Controls the disabled state of the view.
- Display
Prop - Controls the display type of the view.
- Draggable
- Controls whether the view can be dragged.
- Extractor
Field - Flex
Basis - Sets the initial main size of a flex item.
- Flex
Direction Prop - Sets the direction of the main axis for flex items.
- Flex
Grow - Sets the flex grow factor for the flex item.
- Flex
Shrink - Sets the flex shrink factor for the flex item.
- Flex
Wrap Prop - Controls whether flex items wrap to new lines.
- Focusable
- Controls whether the view can receive focus.
- Font
Family - Sets the font family for text content.
- Font
Props - Font
Size - Sets the font size for text content.
- Font
Style - Sets the font style (italic, normal) for text content.
- Font
Weight - Sets the font weight (boldness) for text content.
- Foreground
- Sets the foreground color or pattern.
- Grid
Auto Columns - Specifies the size of implicitly-created grid columns.
- Grid
Auto Flow - Controls how auto-placed items get flowed into the grid.
- Grid
Auto Rows - Specifies the size of implicitly-created grid rows.
- Grid
Column - Specifies a grid item’s location within the grid column.
- GridRow
- Specifies a grid item’s location within the grid row.
- Grid
Template Columns - Defines the line names and track sizing functions of the grid columns.
- Grid
Template Rows - Defines the line names and track sizing functions of the grid rows.
- Height
- Sets the height of the view.
- Inherited
Interaction Cx - Inherited interaction context that is propagated from parent to children.
- Inset
Bottom - Sets the bottom offset for positioned views.
- Inset
Left - Sets the left offset for positioned views.
- Inset
Right - Sets the right offset for positioned views.
- Inset
Top - Sets the top offset for positioned views.
- Interaction
State - The interaction state of a view, used to determine which style selectors apply.
- IsFixed
- Enables fixed positioning relative to the viewport.
- Justify
Content Prop - Controls alignment of flex items along the main axis.
- Justify
Items Prop - Controls default alignment of grid items along the inline axis.
- Justify
Self - Controls individual alignment along the inline axis.
- Line
Height - Sets the line height for text content.
- Margin
- Structure holding margin values for all four sides
- Margin
Prop - Sets the margin for all sides.
- MaxHeight
- Sets the maximum height of the view.
- MaxWidth
- Sets the maximum width of the view.
- MinHeight
- Sets the minimum height of the view.
- MinWidth
- Sets the minimum width of the view.
- Opacity
- Sets the opacity of the view.
- Outline
- Sets the outline stroke properties.
- Outline
Color - Sets the color of the view’s outline.
- Outline
Progress - Controls the progress/completion of the outline animation.
- OverflowX
- How children overflowing their container in Y axis should affect layout
- OverflowY
- How children overflowing their container in X axis should affect layout
- Padding
- Structure holding padding values for all four sides
- Padding
Prop - Sets the padding for all sides.
- Pointer
Events Prop - Controls whether the view can be the target of mouse events.
- Position
Prop - Sets the positioning scheme for the view.
- Rotate
About - Sets the anchor point for rotation transformations.
- Rotation
- Sets the rotation transform angle.
- RowGap
- Sets the gap between rows in grid or flex layouts.
- Scale
About - Sets the anchor point for scaling transformations.
- ScaleX
- Sets the horizontal scale transform.
- ScaleY
- Sets the vertical scale transform.
- Selectable
- Controls whether the view’s text can be selected.
- Selected
- Sets the selected state of the view.
- Selection
Corner Radius - Sets the corner radius of text selections.
- Selection
Style - Stroke
Wrap - Style
- Style
Cache - Cache for style resolution results.
- Style
Cache Key - A cache key for style resolution results.
- Style
Class Info - Style
Class Ref - StyleCx
- Style
Key - Style
Prop Info - Style
Prop Ref - Style
Selectors - Bitmask of active style selectors
- Text
Align Prop - Sets text alignment within the view.
- Text
Color - Sets the text color.
- Text
Overflow Prop - Controls how overflowed text content is handled.
- Transform
Props - Transition
- Transition
State - TranslateX
- Sets the horizontal translation transform.
- TranslateY
- Sets the vertical translation transform.
- Width
- Sets the width of the view.
- ZIndex
- Controls the stack order of positioned views.
Enums§
- Align
Content - Sets the distribution of space between and around content items For Flexbox it controls alignment in the cross axis For Grid it controls alignment in the block axis
- Align
Items - Used to control how child nodes are aligned. For Flexbox it controls alignment in the cross axis For Grid it controls alignment in the block axis
- BoxSizing
- Specifies whether size styles for this node are assigned to the node’s “content box” or “border box”
- Combine
Result - Cursor
Style - Cursor style
- Display
- Sets the layout used for the children of this node
- Flex
Direction - The direction of the flexbox layout main axis.
- Flex
Wrap - Controls whether flex items are forced onto one line or can wrap onto multiple lines.
- Pointer
Events - Pointer event handling mode
- Position
- The positioning strategy for this item.
- Style
KeyInfo - Style
MapValue - Internal storage for style property values in the style map.
- Style
Selector - Pseudo-class selectors for conditional styling
- Style
Value - The value for a [
Style] property in the public API. - Text
Overflow - Text overflow behavior
Traits§
- Custom
Stylable - A trait that enables views to accept custom styling beyond the standard Style properties.
- Custom
Style - A trait for custom styling of specific view types.
- Style
Class - Style
Prop - Style
Prop Reader - Style
Prop Value
Functions§
- resolve_
nested_ maps - the bool in the return is a classes_applied flag. if a new class has been applied, we need to do a request_style_recursive
Type Aliases§
- Justify
Content - Sets the distribution of space between and around content items For Flexbox it controls alignment in the main axis For Grid it controls alignment in the inline axis
- Justify
Items - Used to control how child nodes are aligned. Does not apply to Flexbox, and will be ignored if specified on a flex container For Grid it controls alignment in the inline axis