Layout and Styling with OSUI
OSUI provides a robust system for controlling the position, size, and visual appearance of your UI elements using Transform and Style components.
1. Controlling Layout with Transform
The Transform component defines how a widget is positioned and sized relative to its parent. It's designed for flexibility, allowing both fixed values and dynamic rules.
// Defined using the component! macro in src/style.rs
component!(Transform {
pub x: Position,
pub y: Position,
pub mx: i32,
pub my: i32,
pub px: u16,
pub py: u16,
pub width: Dimension,
pub height: Dimension,
});
x,y: Horizontal and vertical position, defined by thePositionenum.mx,my: Margins (offsets) from the calculated position. Can be positive or negative.px,py: Padding (internal spacing) around the content within the widget's bounds.width,height: Sizing rules for dimensions, defined by theDimensionenum.
Position Enum
Controls horizontal or vertical alignment:
Const(u16): Fixed position in cells from the origin (top-left).x: Position::Const(10)or simplyx: 10(due toFrom<u16> for Positionimpl).
Center: Centers the widget in the parent's available space.End: Aligns the widget to the end (right or bottom) of the parent.
Dimension Enum
Controls sizing:
Full: Fills the available space from the parent.Content: Automatically sized to fit content. This is the default. The element'srendermethod will typically update theRenderScope's size to match its content.Const(u16): Fixed size in cells.width: Dimension::Const(50)or simplywidth: 50.