Raxol.UI.Layout.LayoutUtils (Raxol v2.6.0)

View Source

Shared utilities for layout calculations.

This module provides common functions used across different layout engines to avoid code duplication and ensure consistent behavior.

Summary

Functions

Applies padding to a space/container.

Calculates available space after subtracting used space.

Centers text within a given width by prepending spaces.

Clamps a value between minimum and maximum bounds.

Parses padding value into a normalized map.

Functions

apply_padding(space, padding)

Applies padding to a space/container.

Takes a space with x, y, width, height and applies padding on all sides, returning the adjusted inner space.

Parameters

  • space - Map with :x, :y, :width, :height keys
  • padding - Map with :top, :right, :bottom, :left keys

Returns

Map with adjusted dimensions accounting for padding.

Examples

iex> space = %{x: 10, y: 10, width: 100, height: 50}
iex> padding = %{top: 5, right: 10, bottom: 5, left: 10}
iex> LayoutUtils.apply_padding(space, padding)
%{x: 20, y: 15, width: 80, height: 40}

available_space(total, used)

Calculates available space after subtracting used space.

Parameters

  • total - Total available space
  • used - Already used space

Returns

Remaining available space (minimum 0).

center_text(text, width)

@spec center_text(String.t(), non_neg_integer()) :: String.t()

Centers text within a given width by prepending spaces.

clamp(value, lo, hi)

Clamps a value between minimum and maximum bounds.

Parameters

  • value - Value to clamp
  • min - Minimum allowed value
  • max - Maximum allowed value

Returns

Clamped value within the specified bounds.

parse_padding(padding)

Parses padding value into a normalized map.

Supports various padding formats:

  • Single number: applies to all sides
  • Two numbers: vertical, horizontal
  • Four numbers: top, right, bottom, left

Parameters

  • padding - Number, tuple, or string representation

Returns

Map with :top, :right, :bottom, :left keys.

Examples

iex> LayoutUtils.parse_padding(10)
%{top: 10, right: 10, bottom: 10, left: 10}

iex> LayoutUtils.parse_padding({5, 10})
%{top: 5, right: 10, bottom: 5, left: 10}

iex> LayoutUtils.parse_padding({1, 2, 3, 4})
%{top: 1, right: 2, bottom: 3, left: 4}