Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

AspectRatio

AspectRatio preview

AspectRatio — a single-child wrapper that constrains layout to a fixed width-to-height ratio.

Public functions

AspectRatio

Detailed description

Given a proposal, AspectRatio computes the largest rectangle that fits within both dimensions while satisfying width / height == ratio. When only one axis is constrained by the parent, the other is derived from the ratio. The child is stretched to the resulting rectangle. The widget publishes no node of its own to assistive technology: it is a bare Role::GenericContainer, which the walker prunes, and its child carries all semantic meaning. It is never hidden, which would take the child out of every platform's tree with it.

When to use

  • Embedding images, thumbnails, or video placeholders that must stay letter-boxed regardless of the available space.
  • Ensuring a square avatar or tile layout against an unconstrained parent axis.
#![allow(unused)]
fn main() {
use teksilo_widgets::primitives::{AspectRatio, RectWidget};
// 16:9 video placeholder
let _thumbnail = AspectRatio::new(16.0 / 9.0)
    .child(RectWidget::new());
}

Density

The picture above is the widget at TargetDensity::Compact, the mouse-and-keyboard ladder. Below is the same subject on the same canvas with only the ladder changed, so what moves is the density and nothing else — where the subject no longer fits, that is what the denser targets cost it at that size. See docs/density-and-targets.md.

Touch

AspectRatio at Touch density

API reference

📖 Full rustdoc API for this module

pub struct AspectRatio

Import: use teksilo::widgets::AspectRatio;

A single-child wrapper that maintains a fixed width/height ratio.

#![allow(unused)]
fn main() {
pub struct AspectRatio { /* fields */ }
}

Methods

pub fn new(ratio: f32) -> Self

Create a new aspect ratio wrapper. Ratio is width / height.

pub fn widescreen() -> Self

Convenience for 16:9 aspect ratio.

pub fn square() -> Self

Convenience for 1:1 aspect ratio.

pub fn child(mut self, widget: impl teksilo_core::IntoTeksiChild) -> Self

Set an inline child widget to constrain; the child is stretched to the computed aspect-ratio rectangle.

pub fn child_opt(self, widget: Option<impl teksilo_core::IntoTeksiChild>) -> Self

Attach widget when it is Some, and do nothing when it is None.

The conditional-child form. teksu!'s if without an else lowers to this, and it is what cond.then(|| w) is for in a builder chain. None adds no arena node, so nothing is laid out, painted, or published to the accessibility tree, and a stack applies no spacing around it.