Class: Tuile::Component::Layout::Percent
- Inherits:
-
Object
- Object
- Tuile::Component::Layout::Percent
- 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
-
#percent ⇒ Numeric
readonly
@return — percentage of the available extent,
0..100.
Instance Method Summary collapse
-
#clamp(range) ⇒ Clamp
Bounds the cells this share resolves to:.
-
#initialize(percent:) ⇒ Percent
constructor
@param
percent— percentage of the available extent,0..100. -
#resolve(available) ⇒ Integer
@param
available.
Constructor Details
#initialize(percent:) ⇒ Percent
@param percent — percentage of the available extent, 0..100.
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.
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.
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.
129 |
# File 'lib/tuile/component/layout.rb', line 129 def resolve(available) = (available * percent / 100.0).round |