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.



267
268
269
270
271
272
273
274
275
# File 'lib/tuile/component/layout.rb', line 267

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

#bottom ⇒ Integer (readonly)

@return — cells inset from the bottom edge.

Returns:

  • (Integer)


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/tuile/component/layout.rb', line 231

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

#left ⇒ Integer (readonly)

@return — cells inset from the left edge.

Returns:

  • (Integer)


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/tuile/component/layout.rb', line 231

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

#right ⇒ Integer (readonly)

@return — cells inset from the right edge.

Returns:

  • (Integer)


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/tuile/component/layout.rb', line 231

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

#top ⇒ Integer (readonly)

@return — cells inset from the top edge.

Returns:

  • (Integer)


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/tuile/component/layout.rb', line 231

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:



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

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

.coerce(value) ⇒ Insets

@param value — an Integer becomes a uniform inset.

Parameters:

Returns:



253
254
255
256
257
258
259
260
# File 'lib/tuile/component/layout.rb', line 253

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:



236
237
238
239
240
# File 'lib/tuile/component/layout.rb', line 236

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

  super(**kwargs)
end

Instance Method Details

#horizontal ⇒ Integer

@return — left + right.

Returns:

  • (Integer)


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

def horizontal = left + right

#vertical ⇒ Integer

@return — top + bottom.

Returns:

  • (Integer)


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

def vertical = top + bottom