rectangle
Paint a rectangular shape with an optional corner radius, border, and constrained size.
Use rectangle(paint) -> Rectangle to describe a painted region whose size comes from layout. The paint can be a solid Color or another value convertible to Paint, with rectangle-specific methods available to refine its radius, border, and fill.
Shape Construction
use hera_kit::{Color, ViewExt, rectangle};
let swatch = rectangle(Color::rgba(0.20, 0.45, 0.80, 1.0))
.radius(12.0)
.border(1.0, Color::rgba(0.12, 0.30, 0.60, 1.0))
.frame(120.0, 80.0);The example configures the rectangle's radius and border before applying its frame. Follow that order when adding .radius, .border, or .fill, because common modifiers can return a wrapper that no longer exposes rectangle-specific methods. Calling .fill replaces the paint supplied at construction.
Sizing
A rectangle has no content from which to measure a natural size, so it accepts the finite space proposed by its parent. If an axis is unbounded, the shape cannot infer an arbitrary visible extent along it. Give the rectangle a frame when a particular size is part of the design.
For a decoration behind measured content, a background modifier avoids making a fill primitive participate in the parent's intrinsic measurement.
Box Styling
The common .box_style modifier applies protocol BoxStyle styling. Keep the supported style types and validation limits in mind when using shadows, gradients, or more complex box treatment.
Box styling changes how the region is drawn, while layout determines the room reserved for it. If a border or shadow needs separation from neighboring content, provide that room through padding or spacing rather than assuming the visual effect enlarges the stack allocation.
Interaction Composition
To make a rectangular element interactive, compose it within a semantic control such as a button label. The rectangle supplies appearance, and the control supplies activation, focus, and cancellation. Specialized interactions can instead use the low-level hit-testing facilities exposed by backend integration.
See button, modifiers, and damage and effects.
Component-Specific Reference
These methods configure Rectangle directly and return Self. Apply them before common modifiers such as frame or padding, whose wrapper types no longer expose rectangle-specific methods.
| Method | Purpose |
|---|---|
radius | Sets a uniform corner radius. |
border | Sets a uniform border width and color. |
fill | Replaces the rectangle’s paint. |
radius
fn radius(self, radius: f32) -> SelfSets the same corner radius on every corner, in logical pixels. The default is zero, and the builder clamps negative values to zero. Use a finite value and apply box_style when corners need independent radii. The radius describes the shape’s appearance rather than adding space to its layout.
border
fn border(self, width: f32, color: Color) -> SelfAssigns a uniform border using a finite, nonnegative logical width and a solid color. A later call replaces the previous border configuration; the border is absent by default. Independent edge widths or colors belong in BoxStyle, which overrides the uniform border when supplied.
fill
fn fill(self, paint: impl Into<Paint>) -> SelfReplaces the paint supplied to rectangle(paint) or a previous fill call. The argument may be a solid Color or a prepared Paint, including a gradient. This changes the rectangle’s content paint; the common background modifier instead introduces a decoration behind a view.
Configuration Example
use hera_kit::{rectangle, Color, ViewExt};
let swatch = rectangle(Color::rgba(0.2, 0.4, 0.7, 1.0))
.fill(Color::rgba(0.3, 0.5, 0.8, 1.0))
.radius(10.0)
.border(1.0, Color::rgba(0.1, 0.2, 0.4, 1.0))
.frame(120.0, 80.0);The final fill replaces the construction paint, and the frame supplies the region in which the styled rectangle is laid out.
Source Reference
The following files in the Hera checkout define the behavior described on this page.
crates/kit/src/view/primitives.rscrates/protocol/src/scene/box_style.rscrates/kit/src/view/dsl.rs