Raxol.UI.Layout.LayoutUtils (Raxol v2.0.1)
View SourceShared 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.
Clamps a value between minimum and maximum bounds.
Parses padding value into a normalized map.
Functions
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 keyspadding- 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}
Calculates available space after subtracting used space.
Parameters
total- Total available spaceused- Already used space
Returns
Remaining available space (minimum 0).
Clamps a value between minimum and maximum bounds.
Parameters
value- Value to clampmin- Minimum allowed valuemax- Maximum allowed value
Returns
Clamped value within the specified bounds.
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}