Skip to main content

StyleComponent

Struct StyleComponent 

Source
pub struct StyleComponent {
Show 35 fields pub display: Option<Display>, pub width: SizeDimension, pub height: SizeDimension, pub min_width: Option<f32>, pub max_width: Option<f32>, pub min_height: Option<f32>, pub max_height: Option<f32>, pub margin: EdgeInsets, pub padding: EdgeInsets, pub box_sizing: BoxSizing, pub flex_direction: FlexDirection, pub justify_content: JustifyContent, pub align_items: AlignItems, pub flex_wrap: FlexWrap, pub row_gap: f32, pub column_gap: f32, pub flex_grow: f32, pub flex_shrink: f32, pub flex_basis: SizeDimension, pub position: Position, pub top: Option<SizeDimension>, pub right: Option<SizeDimension>, pub bottom: Option<SizeDimension>, pub left: Option<SizeDimension>, pub line_height: f32, pub font_size: SizeDimension, pub text_align: TextAlign, pub vertical_align: VerticalAlign, pub overflow: Overflow, pub z_index: Option<i32>, pub background_color: Option<[f32; 4]>, pub background_z: Option<f32>, pub color: Option<[f32; 4]>, pub word_wrap: Option<WordWrapMode>, pub word_wrap_tokens: Option<Vec<String>>, /* private fields */
}
Expand description

All CSS layout properties for a node, in one struct.

This mirrors the browser’s “computed style” record — a single bundle per element rather than dozens of separate ECS components. Paired with HtmlElementComponent (semantic role) and LayoutComponent at the subtree root.

All size values are in glyph units (1.0 = one monospace character cell).

Style resolution order for any property:

  1. StyleComponent value (if not the type’s Default)
  2. HtmlElementComponent.element_type UA-default (e.g. DivDisplay::Block)
  3. Layout system built-in fallback

Fields§

§display: Option<Display>

None = inherit from HtmlElementComponent UA default.

§width: SizeDimension§height: SizeDimension§min_width: Option<f32>§max_width: Option<f32>§min_height: Option<f32>§max_height: Option<f32>§margin: EdgeInsets§padding: EdgeInsets§box_sizing: BoxSizing

box-sizing. Default: BoxSizing::BorderBox (cat-engine default).

§flex_direction: FlexDirection§justify_content: JustifyContent§align_items: AlignItems§flex_wrap: FlexWrap§row_gap: f32§column_gap: f32§flex_grow: f32§flex_shrink: f32§flex_basis: SizeDimension§position: Position§top: Option<SizeDimension>§right: Option<SizeDimension>§bottom: Option<SizeDimension>§left: Option<SizeDimension>§line_height: f32

Line height in glyph units. Default: 1.0.

§font_size: SizeDimension

Visual glyph scale applied by descendant TextComponents.

Carries its unit: GlyphUnits(1.0) = one row of glyphs per glyph unit (the layout system’s intrinsic measure); WorldUnits(0.08) = each row of glyphs is 0.08 world units tall (the renderer’s glyph quad scale). Layout resolves to world units via the nearest LayoutComponent.unit_scale before stamping the value onto descendant TextComponents. Auto falls back to the descendant’s authored font size.

§text_align: TextAlign

Text alignment within the content box. Default: Auto (no positioning).

§vertical_align: VerticalAlign

Vertical text alignment within the content box.

Auto preserves the legacy behavior: if text_align is set, text is vertically centered; otherwise the authored translation is preserved.

§overflow: Overflow§z_index: Option<i32>§background_color: Option<[f32; 4]>

RGBA background color. When Some, LayoutSystem spawns and manages a background quad (covering the padding box) as a child of this item’s TC. When None, no background quad is created (or an existing one is removed).

§background_z: Option<f32>

Optional override for the __bg quad’s local Z in the item TC’s frame.

None (default) means layout derives the background Z from the item’s resolved stacking layer: resolved_z - 0.5 * LAYER_DISTANCE. Some(z) pins the background to an absolute local Z, overriding the half-step rule. See docs/spec/layout-stacking-z-index.md.

§color: Option<[f32; 4]>

CSS color. Inherited by every descendant glyph via the renderable ancestor color walk (RenderableSystem::inherited_color_for_renderable). When Some, layout spawns/maintains a __text_color ColorComponent as an immediate child of this item’s TC; when None, any existing helper is removed. Nested styled TCs with their own color override naturally because their helper sits closer to the glyph in the walk.

§word_wrap: Option<WordWrapMode>

None = don’t override the descendant TextComponent’s authored mode. Some(_) = write through to the descendant TextComponent during layout. Does not yet cascade through nested TC boundaries (v2).

§word_wrap_tokens: Option<Vec<String>>

Token strings the wrap algorithm may break after when word_wrap == BreakWord. None = inherit the descendant TextComponent’s authored tokens.

Implementations§

Source§

impl StyleComponent

Source

pub fn new() -> Self

Examples found in repository?
examples/router.rs (line 19)
3fn spawn_runtime_text(
4    universe: &mut engine::Universe,
5    owner: engine::ecs::ComponentId,
6    label: &str,
7) {
8    use engine::ecs::component::{
9        ColorComponent, EdgeInsets, SizeDimension, StyleComponent, TextComponent,
10        TransformComponent,
11    };
12
13    let root = universe
14        .world
15        .add_component_boxed_named(label, Box::new(TransformComponent::new()));
16    let style = universe.world.add_component_boxed_named(
17        format!("{label}_style"),
18        Box::new({
19            let mut style = StyleComponent::new();
20            style.margin = EdgeInsets::all(0.5);
21            style.padding = EdgeInsets::axes(0.75, 0.5);
22            style.width = SizeDimension::Auto;
23            style.background_color = Some([0.93, 0.88, 0.98, 1.0]);
24            style
25        }),
26    );
27    let text = universe
28        .world
29        .add_component_boxed_named(format!("{label}_text"), Box::new(TextComponent::new(label)));
30    let color = universe
31        .world
32        .add_component(ColorComponent::rgba(0.38, 0.14, 0.62, 1.0));
33
34    let _ = universe.world.add_child(root, style);
35    let _ = universe.world.add_child(root, text);
36    let _ = universe.world.add_child(text, color);
37
38    universe.attach(owner, root).expect("attach routed child");
39}
More examples
Hide additional examples
examples/diy-panel.rs (line 22)
5fn spawn_runtime_text(
6    universe: &mut engine::Universe,
7    owner: engine::ecs::ComponentId,
8    label: &str,
9) {
10    use engine::ecs::component::{
11        ColorComponent, EdgeInsets, SizeDimension, StyleComponent, TextComponent,
12        TransformComponent,
13    };
14
15    let root = universe.world.add_component_boxed_named(
16        label,
17        Box::new(TransformComponent::new().with_position(0.0, 0.0, 0.2)),
18    );
19    let style = universe.world.add_component_boxed_named(
20        format!("{label}_style"),
21        Box::new({
22            let mut style = StyleComponent::new();
23            style.margin = EdgeInsets::axes(0.25, 0.25);
24            style.padding = EdgeInsets::axes(0.5, 0.5);
25            style.height = SizeDimension::GlyphUnits(2.5);
26            style.width = SizeDimension::Auto;
27            style.background_color = Some([1.0, 0.80, 0.80, 1.0]);
28            style
29        }),
30    );
31    let text_root = universe.world.add_component_boxed_named(
32        format!("{label}_text_root"),
33        Box::new(TransformComponent::new().with_position(0.0, 0.0, 0.2)),
34    );
35    let text = universe
36        .world
37        .add_component_boxed_named(format!("{label}_text"), Box::new(TextComponent::new(label)));
38    let color = universe
39        .world
40        .add_component(ColorComponent::rgba(0.40, 0.05, 0.05, 1.0));
41
42    let _ = universe.world.add_child(root, style);
43    let _ = universe.world.add_child(root, text_root);
44    let _ = universe.world.add_child(text_root, text);
45    let _ = universe.world.add_child(text, color);
46
47    universe.attach(owner, root).expect("attach routed child");
48}
Source

pub fn apply_patch(&mut self, patch: StylePatch)

Apply a StylePatch, updating only fields where the patch has Some(...).

Trait Implementations§

Source§

impl Clone for StyleComponent

Source§

fn clone(&self) -> StyleComponent

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Component for StyleComponent

Source§

fn name(&self) -> &'static str

Short debug/type name for this component kind (e.g. “transform”, “camera”).
Source§

fn set_id(&mut self, id: ComponentId)

Source§

fn as_any(&self) -> &dyn Any

Source§

fn as_any_mut(&mut self) -> &mut dyn Any

Source§

fn to_mms_ast(&self, _world: &World) -> ComponentExpression

Encode this component as an MMS Component Expression AST node. Read more
Source§

fn init(&mut self, _emit: &mut dyn SignalEmitter, _component: ComponentId)

Called when component is added to the World
Source§

fn cleanup(&mut self, _emit: &mut dyn SignalEmitter, _component: ComponentId)

Called when component is removed from the World.
Source§

fn encode(&self) -> HashMap<String, Value>

Encode component data to a HashMap for serialization. Read more
Source§

fn decode(&mut self, _data: &HashMap<String, Value>) -> Result<(), String>

Decode component data from a HashMap after deserialization. Read more
Source§

impl Debug for StyleComponent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for StyleComponent

Source§

fn default() -> Self

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

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more