Class: Tuile::Component::Layout::Percent

Inherits:
Object
  • Object
show all
Includes:
Constraint
Defined in:
lib/tuile/component/layout.rb,
sig/tuile.rbs

Overview

A percentage of the space available along a Box's axis — measured after Box#padding and Box#spacing have come off, so two Percent[50] children fit exactly rather than overflowing by the gap between them.

add(left, Percent[60])
add(right, Percent[40])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(percent:) ⇒ Percent

@param percent — percentage of the available extent, 0..100.

Parameters:

  • percent: (Numeric)


104
105
106
107
108
109
110
# File 'lib/tuile/component/layout.rb', line 104

def initialize(percent:)
  unless percent.is_a?(Numeric) && percent.between?(0, 100)
    raise ArgumentError, "Percent expects a Numeric in 0..100, got #{percent.inspect}"
  end

  super
end

Instance Attribute Details

#percent ⇒ Numeric (readonly)

@return — percentage of the available extent, 0..100.

Returns:

  • (Numeric)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/tuile/component/layout.rb', line 99

class Percent < Data.define(:percent)
  include Constraint

  # @param percent [Numeric] percentage of the available extent, `0..100`.
  # @raise [ArgumentError] unless `percent` is a Numeric in `0..100`.
  def initialize(percent:)
    unless percent.is_a?(Numeric) && percent.between?(0, 100)
      raise ArgumentError, "Percent expects a Numeric in 0..100, got #{percent.inspect}"
    end

    super
  end

  # Bounds the cells this share resolves to:
  #
  #   add(sidebar, Percent[33].clamp(..16))    # a third, never over 16
  #   add(list, Percent[33].clamp(20..40))     # a third, within 20..40
  #
  # The floor is best-effort: a {Box} still clamps the result to what is
  # unassigned, so an over-subscribed box starves a clamped child like any
  # other.
  # @param range [Range] inclusive, non-negative Integer endpoints, either
  #   one `nil` for unbounded.
  # @raise [ArgumentError] on an unusable `range`.
  # @return [Clamp]
  def clamp(range) = Clamp[self, range]

  # @api private
  # @param available [Integer]
  # @return [Integer] {#percent} of `available`, rounded.
  def resolve(available) = (available * percent / 100.0).round
end

Instance Method Details

#clamp(range) ⇒ Clamp

Bounds the cells this share resolves to:

add(sidebar, Percent[33].clamp(..16))    # a third, never over 16
add(list, Percent[33].clamp(20..40))     # a third, within 20..40

The floor is best-effort: a Box still clamps the result to what is unassigned, so an over-subscribed box starves a clamped child like any other.

@param range — inclusive, non-negative Integer endpoints, either one nil for unbounded.

Parameters:

  • range (::Range[untyped])

Returns:



124
# File 'lib/tuile/component/layout.rb', line 124

def clamp(range) = Clamp[self, range]

#resolve(available) ⇒ Integer

@param available

@return — #percent of available, rounded.

Parameters:

  • available (Integer)

Returns:

  • (Integer)


129
# File 'lib/tuile/component/layout.rb', line 129

def resolve(available) = (available * percent / 100.0).round