Class: Tuile::Component::Layout::Insets

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

Overview

Per-edge padding for a Box, in cells:

Insets[top: 1]                     # one blank row above the children
Insets[top: 1, left: 2, right: 2]  # unnamed edges default to 0
Insets.coerce(1)                   # uniform on all four edges

Keyword-only: AWT and JavaFX order these same four numbers differently, so a positional form would be a coin flip.

Constant Summary collapse

ZERO =

No padding on any edge.

Returns:

new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(top: 0, right: 0, bottom: 0, left: 0) ⇒ Object

@param top — cells inset from the top edge; >= 0.

@param right — cells inset from the right edge; >= 0.

@param bottom — cells inset from the bottom edge; >= 0.

@param left — cells inset from the left edge; >= 0.



141
142
143
144
145
146
147
148
149
# File 'lib/tuile/component/layout.rb', line 141

def initialize(top: 0, right: 0, bottom: 0, left: 0)
  { top:, right:, bottom:, left: }.each do |edge, cells|
    unless cells.is_a?(Integer) && !cells.negative?
      raise ArgumentError, "Insets #{edge}: expected a non-negative Integer, got #{cells.inspect}"
    end
  end

  super
end

Instance Attribute Details

#bottomInteger (readonly)

@return — cells inset from the bottom edge.

Returns:

  • (Integer)


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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/tuile/component/layout.rb', line 105

class Insets < Data.define(:top, :right, :bottom, :left)
  # @param positional [Array] must be empty — see the class doc.
  # @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
  # @raise [ArgumentError] if any positional argument is given.
  # @return [Insets]
  def self.new(*positional, **kwargs)
    raise ArgumentError, "Insets is keyword-only, got #{positional.inspect}" unless positional.empty?

    super(**kwargs)
  end

  # Needed because `Data`'s inherited `[]` never dispatches through a `new`
  # override, so the guard above alone would miss `Insets[1, 2, 3, 4]`.
  # @param positional [Array] must be empty.
  # @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
  # @raise [ArgumentError] if any positional argument is given.
  # @return [Insets]
  def self.[](*positional, **kwargs) = new(*positional, **kwargs)

  # @param value [Insets, Integer] an Integer becomes a uniform inset.
  # @raise [ArgumentError] on anything else, or a negative Integer.
  # @return [Insets]
  def self.coerce(value)
    return value if value.is_a?(Insets)
    unless value.is_a?(Integer) && !value.negative?
      raise ArgumentError, "expected Insets or a non-negative Integer, got #{value.inspect}"
    end

    new(top: value, right: value, bottom: value, left: value)
  end

  # @param top [Integer] cells inset from the top edge; `>= 0`.
  # @param right [Integer] cells inset from the right edge; `>= 0`.
  # @param bottom [Integer] cells inset from the bottom edge; `>= 0`.
  # @param left [Integer] cells inset from the left edge; `>= 0`.
  # @raise [ArgumentError] unless every edge is a non-negative Integer.
  def initialize(top: 0, right: 0, bottom: 0, left: 0)
    { top:, right:, bottom:, left: }.each do |edge, cells|
      unless cells.is_a?(Integer) && !cells.negative?
        raise ArgumentError, "Insets #{edge}: expected a non-negative Integer, got #{cells.inspect}"
      end
    end

    super
  end

  # @return [Integer] `left` + `right`.
  def horizontal = left + right

  # @return [Integer] `top` + `bottom`.
  def vertical = top + bottom

  # No padding on any edge.
  # @return [Insets]
  ZERO = new
end

#leftInteger (readonly)

@return — cells inset from the left edge.

Returns:

  • (Integer)


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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/tuile/component/layout.rb', line 105

class Insets < Data.define(:top, :right, :bottom, :left)
  # @param positional [Array] must be empty — see the class doc.
  # @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
  # @raise [ArgumentError] if any positional argument is given.
  # @return [Insets]
  def self.new(*positional, **kwargs)
    raise ArgumentError, "Insets is keyword-only, got #{positional.inspect}" unless positional.empty?

    super(**kwargs)
  end

  # Needed because `Data`'s inherited `[]` never dispatches through a `new`
  # override, so the guard above alone would miss `Insets[1, 2, 3, 4]`.
  # @param positional [Array] must be empty.
  # @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
  # @raise [ArgumentError] if any positional argument is given.
  # @return [Insets]
  def self.[](*positional, **kwargs) = new(*positional, **kwargs)

  # @param value [Insets, Integer] an Integer becomes a uniform inset.
  # @raise [ArgumentError] on anything else, or a negative Integer.
  # @return [Insets]
  def self.coerce(value)
    return value if value.is_a?(Insets)
    unless value.is_a?(Integer) && !value.negative?
      raise ArgumentError, "expected Insets or a non-negative Integer, got #{value.inspect}"
    end

    new(top: value, right: value, bottom: value, left: value)
  end

  # @param top [Integer] cells inset from the top edge; `>= 0`.
  # @param right [Integer] cells inset from the right edge; `>= 0`.
  # @param bottom [Integer] cells inset from the bottom edge; `>= 0`.
  # @param left [Integer] cells inset from the left edge; `>= 0`.
  # @raise [ArgumentError] unless every edge is a non-negative Integer.
  def initialize(top: 0, right: 0, bottom: 0, left: 0)
    { top:, right:, bottom:, left: }.each do |edge, cells|
      unless cells.is_a?(Integer) && !cells.negative?
        raise ArgumentError, "Insets #{edge}: expected a non-negative Integer, got #{cells.inspect}"
      end
    end

    super
  end

  # @return [Integer] `left` + `right`.
  def horizontal = left + right

  # @return [Integer] `top` + `bottom`.
  def vertical = top + bottom

  # No padding on any edge.
  # @return [Insets]
  ZERO = new
end

#rightInteger (readonly)

@return — cells inset from the right edge.

Returns:

  • (Integer)


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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/tuile/component/layout.rb', line 105

class Insets < Data.define(:top, :right, :bottom, :left)
  # @param positional [Array] must be empty — see the class doc.
  # @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
  # @raise [ArgumentError] if any positional argument is given.
  # @return [Insets]
  def self.new(*positional, **kwargs)
    raise ArgumentError, "Insets is keyword-only, got #{positional.inspect}" unless positional.empty?

    super(**kwargs)
  end

  # Needed because `Data`'s inherited `[]` never dispatches through a `new`
  # override, so the guard above alone would miss `Insets[1, 2, 3, 4]`.
  # @param positional [Array] must be empty.
  # @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
  # @raise [ArgumentError] if any positional argument is given.
  # @return [Insets]
  def self.[](*positional, **kwargs) = new(*positional, **kwargs)

  # @param value [Insets, Integer] an Integer becomes a uniform inset.
  # @raise [ArgumentError] on anything else, or a negative Integer.
  # @return [Insets]
  def self.coerce(value)
    return value if value.is_a?(Insets)
    unless value.is_a?(Integer) && !value.negative?
      raise ArgumentError, "expected Insets or a non-negative Integer, got #{value.inspect}"
    end

    new(top: value, right: value, bottom: value, left: value)
  end

  # @param top [Integer] cells inset from the top edge; `>= 0`.
  # @param right [Integer] cells inset from the right edge; `>= 0`.
  # @param bottom [Integer] cells inset from the bottom edge; `>= 0`.
  # @param left [Integer] cells inset from the left edge; `>= 0`.
  # @raise [ArgumentError] unless every edge is a non-negative Integer.
  def initialize(top: 0, right: 0, bottom: 0, left: 0)
    { top:, right:, bottom:, left: }.each do |edge, cells|
      unless cells.is_a?(Integer) && !cells.negative?
        raise ArgumentError, "Insets #{edge}: expected a non-negative Integer, got #{cells.inspect}"
      end
    end

    super
  end

  # @return [Integer] `left` + `right`.
  def horizontal = left + right

  # @return [Integer] `top` + `bottom`.
  def vertical = top + bottom

  # No padding on any edge.
  # @return [Insets]
  ZERO = new
end

#topInteger (readonly)

@return — cells inset from the top edge.

Returns:

  • (Integer)


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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/tuile/component/layout.rb', line 105

class Insets < Data.define(:top, :right, :bottom, :left)
  # @param positional [Array] must be empty — see the class doc.
  # @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
  # @raise [ArgumentError] if any positional argument is given.
  # @return [Insets]
  def self.new(*positional, **kwargs)
    raise ArgumentError, "Insets is keyword-only, got #{positional.inspect}" unless positional.empty?

    super(**kwargs)
  end

  # Needed because `Data`'s inherited `[]` never dispatches through a `new`
  # override, so the guard above alone would miss `Insets[1, 2, 3, 4]`.
  # @param positional [Array] must be empty.
  # @param kwargs [Hash{Symbol => Integer}] any of `top:`/`right:`/`bottom:`/`left:`.
  # @raise [ArgumentError] if any positional argument is given.
  # @return [Insets]
  def self.[](*positional, **kwargs) = new(*positional, **kwargs)

  # @param value [Insets, Integer] an Integer becomes a uniform inset.
  # @raise [ArgumentError] on anything else, or a negative Integer.
  # @return [Insets]
  def self.coerce(value)
    return value if value.is_a?(Insets)
    unless value.is_a?(Integer) && !value.negative?
      raise ArgumentError, "expected Insets or a non-negative Integer, got #{value.inspect}"
    end

    new(top: value, right: value, bottom: value, left: value)
  end

  # @param top [Integer] cells inset from the top edge; `>= 0`.
  # @param right [Integer] cells inset from the right edge; `>= 0`.
  # @param bottom [Integer] cells inset from the bottom edge; `>= 0`.
  # @param left [Integer] cells inset from the left edge; `>= 0`.
  # @raise [ArgumentError] unless every edge is a non-negative Integer.
  def initialize(top: 0, right: 0, bottom: 0, left: 0)
    { top:, right:, bottom:, left: }.each do |edge, cells|
      unless cells.is_a?(Integer) && !cells.negative?
        raise ArgumentError, "Insets #{edge}: expected a non-negative Integer, got #{cells.inspect}"
      end
    end

    super
  end

  # @return [Integer] `left` + `right`.
  def horizontal = left + right

  # @return [Integer] `top` + `bottom`.
  def vertical = top + bottom

  # No padding on any edge.
  # @return [Insets]
  ZERO = new
end

Class Method Details

.[](*positional, **kwargs) ⇒ Insets

Needed because Data's inherited [] never dispatches through a new override, so the guard above alone would miss Insets[1, 2, 3, 4].

@param positional — must be empty.

@param kwargs — any of top:/right:/bottom:/left:.

Parameters:

  • positional (::Array[untyped])
  • kwargs (::Hash[Symbol, Integer])

Returns:



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

def self.[](*positional, **kwargs) = new(*positional, **kwargs)

.coerce(value) ⇒ Insets

@param value — an Integer becomes a uniform inset.

Parameters:

Returns:



127
128
129
130
131
132
133
134
# File 'lib/tuile/component/layout.rb', line 127

def self.coerce(value)
  return value if value.is_a?(Insets)
  unless value.is_a?(Integer) && !value.negative?
    raise ArgumentError, "expected Insets or a non-negative Integer, got #{value.inspect}"
  end

  new(top: value, right: value, bottom: value, left: value)
end

.new(*positional, **kwargs) ⇒ Insets

@param positional — must be empty — see the class doc.

@param kwargs — any of top:/right:/bottom:/left:.

Parameters:

  • positional (::Array[untyped])
  • kwargs (::Hash[Symbol, Integer])

Returns:



110
111
112
113
114
# File 'lib/tuile/component/layout.rb', line 110

def self.new(*positional, **kwargs)
  raise ArgumentError, "Insets is keyword-only, got #{positional.inspect}" unless positional.empty?

  super(**kwargs)
end

Instance Method Details

#horizontalInteger

@returnleft + right.

Returns:

  • (Integer)


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

def horizontal = left + right

#verticalInteger

@returntop + bottom.

Returns:

  • (Integer)


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

def vertical = top + bottom