osui::elements::div
The Div
element is a fundamental building block in OSUI, serving as a generic, transparent container. It doesn't have its own visual representation by default but is primarily used for grouping other elements and applying layout (Transform
) and styling (Style
) to a collection of children.
Div
Struct
pub struct Div {
children: Vec<Arc<Widget>>, // Children widgets contained within this Div
size: (u16, u16), // Internal tracking of the Div's calculated size
}
Associated Methods
Div::new() -> Self
Creates a new Div
instance with no children and default size.
Example:
use osui::prelude::*;
let my_div = Div::new(); // Create a Div programmatically
Element
Trait Implementation
render(&mut self, scope: &mut RenderScope, _: &RenderContext)
This method, for Div
, primarily focuses on setting the RenderScope
's area based on its calculated size or default values. It does not issue any direct drawing commands (e.g., draw_text
, draw_rect
) for itself, as Div
is transparent by default.
after_render(&mut self, scope: &mut RenderScope, render_context: &RenderContext)
This is the crucial method for container elements like Div
. It's called after the Div
itself has been processed by the rendering pipeline.
- It clones the
RenderScope
'sRawTransform
to use as a basis for its children's layout. - It creates a
DivRenderer
(anElementRenderer
helper) which will adjust child positions relative to theDiv
. - It temporarily sets the
RenderScope
'sparent_size
to its own calculated content area. This ensures that children usingDimension::Full
orPosition::Center
/End
resolve correctly within theDiv
's bounds. - It iterates through its
children
and callsscope.render_widget
for each, effectively triggering the rendering pipeline for its nested elements. - After children are rendered, it restores the original
parent_size
to theRenderScope
. - It updates its internal
self.size
based on the accumulated size of its children (as reported byDivRenderer
).
draw_child(&mut self, element: &Arc<Widget>)
This method is called by the rsx!
macro or Rsx::draw_parent
when a widget is declared as a child of this Div
.
- It adds the
element
to its internalchildren
Vec
. - It injects a
NoRenderRoot
component into the child widget. This is critical: it tells the mainScreen
rendering loop not to render this child directly, as theDiv
itself will handle its rendering inafter_render
. This prevents double-rendering and ensures correct layout.
is_ghost(&mut self) -> bool
Returns true
. A Div
is a "ghost" element because it primarily serves as a logical grouping and layout container and does not draw any visual representation (like a background or border) itself. Any visual properties are applied via external Style
components associated with the Div
's widget.
as_any(&self) -> &dyn std::any::Any
/ as_any_mut(&mut self) -> &mut dyn std::any::Any
Standard implementations for downcasting.
DivRenderer
A helper struct that implements ElementRenderer
specifically for Div
to adjust the RenderScope
for its children.
pub struct DivRenderer<'a>(pub &'a mut RawTransform);
ElementRenderer
Trait Implementation for DivRenderer
before_draw(&mut self, scope: &mut RenderScope, _widget: &Arc<Widget>)
This method is called for each child widget of the Div
just before that child is drawn.
- It updates the
Div
's ownRawTransform
(self.0
) to expand itswidth
andheight
to encompass the child's area plus its padding. - It translates the child's
RawTransform
(t
) by theDiv
's absolute position and padding. This ensures children are positioned correctly inside theDiv
. - It updates the child's
RawTransform
padding by adding theDiv
's padding.
Usage in rsx!
use osui::prelude::*;
rsx! {
// A Div with a solid background and padding, containing text and a FlexRow
@Transform::new().dimensions(40, 10).center().padding(2, 1);
@Style { background: Background::Solid(0x333333), foreground: Some(0xFFFFFF) };
Div {
"This is content inside the Div."
"It will respect the Div's padding and dimensions."
FlexRow, gap: 1, {
"Nested"
"FlexRow"
"Elements"
}
}
}
Div
is an essential tool for structuring your UI, applying common styles, and managing the layout of groups of widgets.