Skip to main content

Module geometry

Module geometry 

Source
Expand description

Units and geometry, typed as GPUI’s gpui::geometry: a length is Points (GPUI’s Pixels), Rems, a Percentage (a fraction: 0.5 is 50%) or one of the lengths styles give (AbsoluteLength, DefiniteLength, Length); an angle is Radians; shapes are generic over their unit (Point, Size, Bounds, Edges, Corners). Public API takes and returns these, never a bare f32 for a length, an angle or a fraction (held by the unit_types test; docs/dev/geometry.md).

NSH lays out in points; DevicePixels and ScaledPixels exist only where a platform draws or reports pixels. Conversions happen at the boundaries: taffy here (taffy.rs, the one place), the platforms where they hand values to their toolkits, nsh-js where it decodes JS numbers.

The units are #[repr(transparent)] newtypes and the shapes #[repr(C)] structs of them: zero cost on hot paths. Deviations from GPUI are deliberate and listed in docs/dev/geometry.md (points rather than pixels, f32’s comparisons rather than a total order, a public .0, CSS angle constructors).

Structs§

Bounds
A rectangle: its top left corner (origin) and its size. A view’s frame is its bounds relative to its parent.
Corners
A value for each corner of a box: radii.
DevicePixels
Physical pixels of a display: what a platform’s bitmaps, swap chains and pointer events in pixels count. NSH’s own values are Points; conversions happen at the platform boundary, with the surface’s scale factor.
Edges
A value for each edge of a box: insets, paddings, borders.
Percentage
A fraction of a whole: 0.5 is 50%, 1.0 all of it. NSH keeps CSS percentages as fractions everywhere (styles, gradients, transform origins). Values outside 0..=1 are taken (a gradient stop at 150%, a -50% translation).
Point
A point: x to the right, y down, from the origin of what it is relative to.
Points
A length in points, the unit NSH lays out in (GPUI’s Pixels): the platform’s density-independent unit (iOS / macOS points, Android dp, CSS px on the web, DIPs on Windows). Style strings’ px and bare numbers are points. Device pixels are DevicePixels / ScaledPixels, met only where a platform draws or reports them.
Radians
An angle in radians. Which way it turns and where 0 points is the API’s that takes it (CSS: 0 points up, clockwise). deg, turn and grad make one from CSS’s other angle units.
Rems
A length in rems: multiples of a root font size (16 points in style strings, REM_SIZE).
ScaledPixels
Points scaled to device pixels without rounding: a fractional device position, as a platform draws at (Points::scale).
Size
A width and a height.

Enums§

AbsoluteLength
A length known without layout: points, or rems of a root font size.
Axis
A direction in two dimensions ("vertical" or "horizontal" in JS: a prop that names one, a split’s or a resizable group’s).
DefiniteLength
A length that is not auto: absolute, or a fraction of a reference length (the parent’s size for a box, the gradient line for a color stop).
Length
A length a style gives a box: definite, or auto (sized by layout).

Constants§

REM_SIZE
The points of one rem in NSH’s style strings and Tailwind’s default theme (CSS’s 16px).

Traits§

Along
A two-dimensional value read or changed along one Axis.
Half
A value that can be halved (the center of a Bounds).
IsZero
A value that can be zero.

Functions§

auto
auto: sized by layout.
bounds
Bounds at origin of size.
deg
degrees degrees, in radians (CSS deg).
grad
gradians gradians, in radians: 400 to a turn (CSS grad).
percentage
value as a fraction of a whole (percentage(0.5) is 50%).
point
A point at x, y.
px
points points. The name is GPUI’s and CSS’s: a CSS px is a point.
radians
value radians.
relative
fraction of the reference length, the parent’s size for a box (relative(0.5) is 50%).
rems
rems rems.
size
A size of width × height.
turn
turns whole turns, in radians (CSS turn).