unit

package
v0.1.122 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const Infinity = math.MaxInt32

Infinity represents unbounded constraints (max value).

Variables

View Source
var DpInfinity = Dp(float32(math.Inf(1)))

DpInfinity represents infinite dp dimension.

View Source
var DpOffsetUnspecified = DpOffset{X: DpUnspecified, Y: DpUnspecified}
View Source
var DpOffsetZero = DpOffset{X: 0, Y: 0}
View Source
var DpSizeUnspecified = DpSize{Width: DpUnspecified, Height: DpUnspecified}
View Source
var DpSizeZero = DpSize{Width: 0, Height: 0}

DpUnspecified is the constant for an unspecified Dp.

View Source
var TextUnitUnspecified = TextUnit{/* contains filtered or unexported fields */}

1. `TUnspecified` – sentinel (value, not pointer) TextUnitUnspecified is the sentinel value for an unspecified TextUnit. Kotlin uses a packed value with NaN for Unspecified. val Unspecified = pack(UNIT_TYPE_UNSPECIFIED, Float.NaN)

Functions

func DpToGioUnit

func DpToGioUnit(u Dp) (gioUnit.Dp, error)

func DpToGioUnitUnsafe added in v0.1.102

func DpToGioUnitUnsafe(u Dp) gioUnit.Dp

func TextUnitToGioDp added in v0.1.120

func TextUnitToGioDp(tu TextUnit, density float32) (gioUnit.Dp, error)

func TextUnitToGioSp

func TextUnitToGioSp(tu TextUnit) (gioUnit.Sp, error)

func TextUnitToGioSpUnsafe added in v0.1.102

func TextUnitToGioSpUnsafe(tu TextUnit) gioUnit.Sp

Types

type Constraints

type Constraints uint64

Constraints represents immutable constraints for measuring layouts. It packs minWidth, maxWidth, minHeight, maxHeight, and a 2-bit focus indicator into a uint64. The bit allocation is dynamic: one dimension gets 13-18 bits, the other gets 13-16 bits.

Focus modes (2 bits):

  • 0: MaxFocusHeight - height gets 18 bits, width gets 13 bits
  • 1: MinFocusHeight - height gets 16 bits, width gets 15 bits
  • 2: MinFocusWidth - width gets 16 bits, height gets 15 bits
  • 3: MaxFocusWidth - width gets 18 bits, height gets 13 bits

func FitPrioritizingHeight

func FitPrioritizingHeight(minWidth, maxWidth, minHeight, maxHeight int) Constraints

FitPrioritizingHeight creates constraints favoring height bit allocation.

func FitPrioritizingWidth

func FitPrioritizingWidth(minWidth, maxWidth, minHeight, maxHeight int) Constraints

FitPrioritizingWidth creates constraints favoring width bit allocation.

func Fixed

func Fixed(width, height int) Constraints

Fixed creates constraints for a fixed size in both dimensions.

func FixedHeight

func FixedHeight(height int) Constraints

FixedHeight creates constraints with fixed height and unbounded width.

func FixedWidth

func FixedWidth(width int) Constraints

FixedWidth creates constraints with fixed width and unbounded height.

func NewConstraints

func NewConstraints(minWidth, maxWidth, minHeight, maxHeight int) Constraints

NewConstraints creates a new Constraints object with validation.

func (Constraints) Constrain

func (c Constraints) Constrain(other Constraints) Constraints

Constrain clamps another Constraints to these constraints. The result will satisfy these constraints, but may not satisfy the input constraints.

func (Constraints) ConstrainHeight

func (c Constraints) ConstrainHeight(height int) int

ConstrainHeight clamps the height to these constraints.

func (Constraints) ConstrainSize

func (c Constraints) ConstrainSize(size IntSize) IntSize

ConstrainSize clamps an IntSize to these constraints.

func (Constraints) ConstrainWidth

func (c Constraints) ConstrainWidth(width int) int

ConstrainWidth clamps the width to these constraints.

func (Constraints) Copy

func (c Constraints) Copy(minWidth, maxWidth, minHeight, maxHeight int) Constraints

Copy creates new constraints with optional overrides.

func (Constraints) CopyMaxDimensions

func (c Constraints) CopyMaxDimensions() Constraints

CopyMaxDimensions returns constraints with min dimensions zeroed, preserving max and focus.

func (Constraints) HasBoundedHeight

func (c Constraints) HasBoundedHeight() bool

HasBoundedHeight returns false if maxHeight is Infinity.

func (Constraints) HasBoundedWidth

func (c Constraints) HasBoundedWidth() bool

HasBoundedWidth returns false if maxWidth is Infinity.

func (Constraints) HasFixedHeight

func (c Constraints) HasFixedHeight() bool

HasFixedHeight returns true if there's exactly one valid height.

func (Constraints) HasFixedWidth

func (c Constraints) HasFixedWidth() bool

HasFixedWidth returns true if there's exactly one valid width.

func (Constraints) IsSatisfiedBy

func (c Constraints) IsSatisfiedBy(size IntSize) bool

IsSatisfiedBy checks if a size satisfies these constraints.

func (Constraints) IsZero

func (c Constraints) IsZero() bool

IsZero returns true if maxWidth or maxHeight is 0.

func (Constraints) MaxHeight

func (c Constraints) MaxHeight() int

MaxHeight returns the maximum height in pixels, or Infinity.

func (Constraints) MaxWidth

func (c Constraints) MaxWidth() int

MaxWidth returns the maximum width in pixels, or Infinity.

func (Constraints) MinHeight

func (c Constraints) MinHeight() int

MinHeight returns the minimum height in pixels.

func (Constraints) MinWidth

func (c Constraints) MinWidth() int

MinWidth returns the minimum width in pixels.

func (Constraints) Offset

func (c Constraints) Offset(horizontal, vertical int) Constraints

Offset expands constraints by the given deltas.

func (Constraints) String

func (c Constraints) String() string

String returns a readable representation.

type Density

type Density interface {
	// Density returns the logical density of the display.
	// This is a scaling factor for the [Dp] unit.
	Density() float32

	// FontScale returns the current user preference for the scaling factor for fonts.
	FontScale() float32

	// DpToPx converts [Dp] to pixels. Pixels are used to paint to Canvas.
	DpToPx(dp Dp) float32

	// DpRoundToPx converts [Dp] to [int] by rounding.
	DpRoundToPx(dp Dp) int

	// TextUnitToPx converts Sp to pixels. Pixels are used to paint to Canvas.
	// Panics if TextUnit other than SP unit is specified.
	TextUnitToPx(tu TextUnit) float32

	// TextUnitRoundToPx converts Sp to [int] by rounding.
	TextUnitRoundToPx(tu TextUnit) int

	// IntToDp converts an [int] pixel value to [Dp].
	IntToDp(px int) Dp

	// IntToSp converts an [int] pixel value to Sp.
	IntToSp(px int) TextUnit

	// FloatToDp converts a [float32] pixel value to a [Dp].
	FloatToDp(px float32) Dp

	// FloatToSp converts a [float32] pixel value to a Sp.
	FloatToSp(px float32) TextUnit

	// DpRectToRect converts a [DpRect] to a [Rect].
	DpRectToRect(rect DpRect) geometry.Rect

	// DpSizeToSize converts a [DpSize] to a [Size].
	DpSizeToSize(size DpSize) geometry.Size

	// SizeToDpSize converts a [Size] to a [DpSize].
	SizeToDpSize(size geometry.Size) DpSize

	// DpToTextUnit converts [Dp] to [TextUnit] (Sp).
	DpToTextUnit(dp Dp) TextUnit

	// TextUnitToDp converts Sp to [Dp] (non-rounding).
	TextUnitToDp(tu TextUnit) Dp
}

Density provides information about the density of the display. Used for the conversions between pixels, Dp, [int] and TextUnit.

func DensityFromLayoutContext added in v0.1.120

func DensityFromLayoutContext(gtx gioLayout.Context) Density

DensityFromLayoutContext creates a Density from a Gio Layout Context. gtx: The Gio Layout Context.

func NewDensity

func NewDensity(density, fontScale float32) Density
A density of the screen. Used for convert [Dp] to pixels.

density The logical density of the display. This is a scaling factor for the Dp unit. fontScale Current user preference for the scaling factor for fonts.

type Dp

type Dp float32

Dp is a value class that represents a density-independent pixel.

The value is stored as a float32.

const DpHairline Dp = 0

DpHairline is the constant for a hairline Dp.

func LerpDp

func LerpDp(start, stop Dp, fraction float32) Dp

LerpDp linearly interpolates between two Dps.

func MaxDp

func MaxDp(a, b Dp) Dp

MaxDp returns the larger of two Dps.

func MinDp

func MinDp(a, b Dp) Dp

MinDp returns the smaller of two Dps.

func NewDp

func NewDp(value float32) Dp

NewDp creates a new Dp.

func (Dp) Add

func (d Dp) Add(other Dp) Dp

Add adds two Dps.

func (Dp) CoerceAtLeast

func (d Dp) CoerceAtLeast(minimumValue Dp) Dp

CoerceAtLeast ensures value is at least minimumValue.

func (Dp) CoerceAtMost

func (d Dp) CoerceAtMost(maximumValue Dp) Dp

CoerceAtMost ensures value is at most maximumValue.

func (Dp) CoerceIn

func (d Dp) CoerceIn(minimumValue, maximumValue Dp) Dp

CoerceIn ensures value is in range [minimumValue, maximumValue].

func (Dp) CompareTo

func (d Dp) CompareTo(other Dp) int

CompareTo compares this Dp to another Dp.

func (Dp) Div

func (d Dp) Div(other float32) Dp

Div returns d / other (scalar).

func (Dp) DivDp

func (d Dp) DivDp(other Dp) float32

DivDp returns d / other (scalar).

func (Dp) DivInt

func (d Dp) DivInt(other int) Dp

DivInt returns d / other (int).

func (Dp) IsFinite

func (d Dp) IsFinite() bool

IsFinite returns true when it is finite.

func (Dp) IsSpecified

func (d Dp) IsSpecified() bool

IsSpecified checks if the Dp is specified (not NaN).

func (Dp) IsUnspecified

func (d Dp) IsUnspecified() bool

IsUnspecified checks if the Dp is unspecified (NaN).

func (Dp) Negate

func (d Dp) Negate() Dp

Negate returns -d.

func (Dp) String

func (d Dp) String() string

String returns the string representation.

func (Dp) Subtract

func (d Dp) Subtract(other Dp) Dp

Subtract subtracts other Dp from this Dp.

func (Dp) TakeOrElse

func (d Dp) TakeOrElse(def Dp) Dp

TakeOrElse returns this Dp if Specified, otherwise executes the block.

func (Dp) Times

func (d Dp) Times(other float32) Dp

Times returns d * other (scalar).

func (Dp) TimesInt

func (d Dp) TimesInt(other int) Dp

TimesInt returns d * other (int).

func (Dp) Value

func (d Dp) Value() float32

Value returns the float32 value.

type DpOffset

type DpOffset struct {
	X Dp
	Y Dp
}

DpOffset represents a 2D offset using Dp.

func LerpDpOffset

func LerpDpOffset(start, stop DpOffset, fraction float32) DpOffset

func NewDpOffset

func NewDpOffset(x, y Dp) DpOffset

NewDpOffset constructs a DpOffset.

func (DpOffset) Add

func (o DpOffset) Add(other DpOffset) DpOffset

func (DpOffset) Copy

func (o DpOffset) Copy(x, y Dp) DpOffset

func (DpOffset) IsSpecified

func (o DpOffset) IsSpecified() bool

func (DpOffset) IsUnspecified

func (o DpOffset) IsUnspecified() bool

func (DpOffset) String

func (o DpOffset) String() string

func (DpOffset) Subtract

func (o DpOffset) Subtract(other DpOffset) DpOffset

func (DpOffset) TakeOrElse

func (o DpOffset) TakeOrElse(block DpOffset) DpOffset

type DpRect

type DpRect struct {
	Left   Dp
	Top    Dp
	Right  Dp
	Bottom Dp
}

DpRect represents a rectangle with Dp coordinates.

func NewDpRect

func NewDpRect(left, top, right, bottom Dp) DpRect

NewDpRect creates a new DpRect.

func NewDpRectFromOriginSize

func NewDpRectFromOriginSize(origin DpOffset, size DpSize) DpRect

NewDpRectFromOriginSize creates a DpRect from origin and size.

func (DpRect) Height

func (r DpRect) Height() Dp

func (DpRect) Size

func (r DpRect) Size() DpSize

func (DpRect) String

func (r DpRect) String() string

String returns the string representation of DpRect.

func (DpRect) Width

func (r DpRect) Width() Dp

type DpSize

type DpSize struct {
	Width  Dp
	Height Dp
}

DpSize represents a size with Dp dimensions.

func LerpDpSize

func LerpDpSize(start, stop DpSize, fraction float32) DpSize

func NewDpSize

func NewDpSize(width, height Dp) DpSize

NewDpSize creates a new DpSize.

func (DpSize) Add

func (s DpSize) Add(other DpSize) DpSize

func (DpSize) Center

func (s DpSize) Center() DpOffset

func (DpSize) Copy

func (s DpSize) Copy(width, height Dp) DpSize

func (DpSize) Div

func (s DpSize) Div(other float32) DpSize

func (DpSize) DivInt

func (s DpSize) DivInt(other int) DpSize

func (DpSize) IsSpecified

func (s DpSize) IsSpecified() bool

func (DpSize) IsUnspecified

func (s DpSize) IsUnspecified() bool

func (DpSize) String

func (s DpSize) String() string

func (DpSize) Subtract

func (s DpSize) Subtract(other DpSize) DpSize

func (DpSize) TakeOrElse

func (s DpSize) TakeOrElse(block DpSize) DpSize

func (DpSize) Times

func (s DpSize) Times(other float32) DpSize

func (DpSize) TimesInt

func (s DpSize) TimesInt(other int) DpSize

type IntSize

type IntSize struct {
	Width  int
	Height int
}

@TODO implement size fully

type LayoutDirection

type LayoutDirection int

LayoutDirection represents the horizontal layout direction of content.

https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/ui/ui-unit/src/commonMain/kotlin/androidx/compose/ui/unit/LayoutDirection.kt

const (
	// LayoutDirectionLtr represents left-to-right layout direction.
	LayoutDirectionLtr LayoutDirection = iota

	// LayoutDirectionRtl represents right-to-left layout direction.
	LayoutDirectionRtl
)

func (LayoutDirection) String

func (d LayoutDirection) String() string

String returns the string representation of the LayoutDirection.

type TextUnit

type TextUnit struct {
	// contains filtered or unexported fields
}

TextUnit packs unit type (Sp/Em) and float value into 64 bits. Struct wrapper prevents `TextUnit(24)` - must use `Sp(24)` or `Em(1.5)`.

func Em

func Em(value float32) TextUnit

Em creates an EM unit TextUnit.

func LerpTextUnit

func LerpTextUnit(start, stop TextUnit, fraction float32) TextUnit

LerpTextUnit linearly interpolates between two TextUnits.

func LerpTextUnitInheritable

func LerpTextUnitInheritable(a, b TextUnit, t float32) TextUnit

func NewTextUnit

func NewTextUnit(value float32, unitType TextUnitType) TextUnit

NewTextUnit creates a new TextUnit. Note: In Kotlin this is constructor `TextUnit(value: Float, type: TextUnitType)`.

func Sp

func Sp(value float32) TextUnit

Sp creates a SP unit TextUnit.

func (TextUnit) AsGioSp

func (tu TextUnit) AsGioSp() gioUnit.Sp

func (TextUnit) Compare

func (tu TextUnit) Compare(other TextUnit) int

Compare compares this TextUnit with another.

func (TextUnit) Div

func (tu TextUnit) Div(other float32) TextUnit

Div divides a TextUnit by a scalar.

func (TextUnit) Equals

func (tu TextUnit) Equals(other TextUnit) bool

7-9. Equality – Equals method Equals checks if two TextUnits are equal.

func (TextUnit) IsEm

func (tu TextUnit) IsEm() bool

IsEm returns true if this is an EM unit type.

func (TextUnit) IsSp

func (tu TextUnit) IsSp() bool

IsSp returns true if this is a SP unit type.

func (TextUnit) IsSpecified

func (tu TextUnit) IsSpecified() bool

2. `IsSpecified` – predicate (method on value receiver) IsSpecified returns true if this is a specified unit type.

func (TextUnit) IsUnspecified

func (tu TextUnit) IsUnspecified() bool

IsUnspecified returns true if this is an unspecified unit type.

func (TextUnit) Merge

func (tu TextUnit) Merge(other TextUnit) TextUnit

4. `Merge` – whole-value replacement for atomic packed types (method on value receiver) Merge returns other if specified, otherwise returns tu.

func (TextUnit) String

func (tu TextUnit) String() string

5. `String` – stringification (method on value receiver) String returns the string representation of the TextUnit.

func (TextUnit) TakeOrElse

func (tu TextUnit) TakeOrElse(def TextUnit) TextUnit

3. `TakeOrElse` – 2-param fallback (method on value receiver) TakeOrElse returns this TextUnit if specified, otherwise returns the default.

func (TextUnit) Times

func (tu TextUnit) Times(other float32) TextUnit

Times multiplies a TextUnit by a scalar.

func (TextUnit) Type

func (tu TextUnit) Type() TextUnitType

Type returns the TextUnitType of this TextUnit.

func (TextUnit) UnaryMinus

func (tu TextUnit) UnaryMinus() TextUnit

UnaryMinus returns the negation of this TextUnit.

func (TextUnit) Value

func (tu TextUnit) Value() float32

Value returns the float value of this TextUnit.

type TextUnitType

type TextUnitType int64
const (
	TextUnitTypeUnspecified TextUnitType = 0x00 << 32
	TextUnitTypeSp          TextUnitType = 0x01 << 32
	TextUnitTypeEm          TextUnitType = 0x02 << 32
)

func (TextUnitType) String

func (t TextUnitType) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL