Class: Shoes::Dimensions

Inherits:
Object
  • Object
show all
Includes:
Common::Inspect
Defined in:
shoes-core/lib/shoes/dimensions.rb

Overview

Dimensions is a central class that most Shoes classes use to represent their dimensions, e.g. where they are and how much space they are taking up there. All the different position types might be confusing. So here is a little list:

Position (left, top, right, bottom)

plain (left, top, right, bottom): An offset relative to the parent (parents mostly are slots e.g. flows/stacks), e.g it isn’t fully positioned/doesn’t flow anymore when set

absolute_* (absolute_left, absolute_top, absolute_right, absolute_bottom): The absolute position an element begins positioning from in the app (in slot.rb). Margins are not included in absolute position values–that’s the main difference between absolute and element values.

element_* (element_left, element_top, element_right, element_bottom): Derived from absolute_* but shows the real position of the object, e.g. it adds the margins to absolute_* (mostly used by backend drawing code).

Space Taken Up (width/height)

plain (width, height): The whole space taken up by this element with margins and everything. Used for positioning/by the user.

element_* (element_width, element_height): Just the space taken up by the element itself without margins. Used by drawing.

Note that this is NOT how margins work in the CSS box model. We diverge for reasons mentioned here

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common::Inspect

#inspect, #to_s

Constructor Details

#initialize(parent, left_or_hash = nil, top = nil, width = nil, height = nil, opts = {}) ⇒ Dimensions

Returns a new instance of Dimensions.



55
56
57
58
59
60
61
62
63
# File 'shoes-core/lib/shoes/dimensions.rb', line 55

def initialize(parent, left_or_hash = nil, top = nil, width = nil,
               height = nil, opts = {})
  @parent = parent
  if hash_as_argument?(left_or_hash)
    init_with_hash(left_or_hash)
  else
    init_with_arguments(left_or_hash, top, width, height, opts)
  end
end

Instance Attribute Details

#x_dimensionObject (readonly)

Returns the value of attribute x_dimension.



39
40
41
# File 'shoes-core/lib/shoes/dimensions.rb', line 39

def x_dimension
  @x_dimension
end

#y_dimensionObject (readonly)

Returns the value of attribute y_dimension.



39
40
41
# File 'shoes-core/lib/shoes/dimensions.rb', line 39

def y_dimension
  @y_dimension
end

Instance Method Details

#absolute_bottomObject



296
297
298
# File 'shoes-core/lib/shoes/dimensions.rb', line 296

def absolute_bottom
  @y_dimension.absolute_end
end

#absolute_bottom_position?Boolean

Is this element’s bottom positioned absolutely

Returns:

  • (Boolean)


96
97
98
# File 'shoes-core/lib/shoes/dimensions.rb', line 96

def absolute_bottom_position?
  @y_dimension.absolute_end_position?
end

#absolute_leftObject



276
277
278
# File 'shoes-core/lib/shoes/dimensions.rb', line 276

def absolute_left
  @x_dimension.absolute_start
end

#absolute_left=(value) ⇒ Object



280
281
282
# File 'shoes-core/lib/shoes/dimensions.rb', line 280

def absolute_left=(value)
  @x_dimension.absolute_start = value
end

#absolute_left_position?Boolean

Is this element’s left positioned absolutely

Returns:

  • (Boolean)


81
82
83
# File 'shoes-core/lib/shoes/dimensions.rb', line 81

def absolute_left_position?
  @x_dimension.absolute_start_position?
end

#absolute_rightObject



284
285
286
# File 'shoes-core/lib/shoes/dimensions.rb', line 284

def absolute_right
  @x_dimension.absolute_end
end

#absolute_right_position?Boolean

Is this element’s right positioned absolutely

Returns:

  • (Boolean)


86
87
88
# File 'shoes-core/lib/shoes/dimensions.rb', line 86

def absolute_right_position?
  @x_dimension.absolute_end_position?
end

#absolute_topObject



288
289
290
# File 'shoes-core/lib/shoes/dimensions.rb', line 288

def absolute_top
  @y_dimension.absolute_start
end

#absolute_top=(value) ⇒ Object



292
293
294
# File 'shoes-core/lib/shoes/dimensions.rb', line 292

def absolute_top=(value)
  @y_dimension.absolute_start = value
end

#absolute_top_position?Boolean

Is this element’s top positioned absolutely

Returns:

  • (Boolean)


91
92
93
# File 'shoes-core/lib/shoes/dimensions.rb', line 91

def absolute_top_position?
  @y_dimension.absolute_start_position?
end

#absolute_x_position?Boolean

Is this element absolutely positioned in the horizontal dimension

Returns:

  • (Boolean)


66
67
68
# File 'shoes-core/lib/shoes/dimensions.rb', line 66

def absolute_x_position?
  x_dimension.absolute_position?
end

#absolute_y_position?Boolean

Is this element absolutely positioned in the vertical dimension

Returns:

  • (Boolean)


71
72
73
# File 'shoes-core/lib/shoes/dimensions.rb', line 71

def absolute_y_position?
  y_dimension.absolute_position?
end

#absolutely_positioned?Boolean

Is this element absolutely positioned in any dimension

Returns:

  • (Boolean)


76
77
78
# File 'shoes-core/lib/shoes/dimensions.rb', line 76

def absolutely_positioned?
  absolute_x_position? || absolute_y_position?
end

#bottomObject



204
205
206
# File 'shoes-core/lib/shoes/dimensions.rb', line 204

def bottom
  @y_dimension.end
end

#bottom=(value) ⇒ Object



208
209
210
# File 'shoes-core/lib/shoes/dimensions.rb', line 208

def bottom=(value)
  @y_dimension.end = value
end

#contains?(other) ⇒ Boolean

Is another element fully contained within this element?

Parameters:

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
# File 'shoes-core/lib/shoes/dimensions.rb', line 117

def contains?(other)
  return false unless other.element_left && other.element_top &&
                      other.element_right && other.element_bottom

  element_left <= other.element_left &&
    element_right >= other.element_right &&
    element_top <= other.element_top &&
    element_bottom >= other.element_bottom
end

#displace_leftObject



300
301
302
# File 'shoes-core/lib/shoes/dimensions.rb', line 300

def displace_left
  @x_dimension.displace_start
end

#displace_left=(value) ⇒ Object



304
305
306
# File 'shoes-core/lib/shoes/dimensions.rb', line 304

def displace_left=(value)
  @x_dimension.displace_start = value
end

#displace_topObject



308
309
310
# File 'shoes-core/lib/shoes/dimensions.rb', line 308

def displace_top
  @y_dimension.displace_start
end

#displace_top=(value) ⇒ Object



312
313
314
# File 'shoes-core/lib/shoes/dimensions.rb', line 312

def displace_top=(value)
  @y_dimension.displace_start = value
end

#element_bottomObject



240
241
242
# File 'shoes-core/lib/shoes/dimensions.rb', line 240

def element_bottom
  @y_dimension.element_end
end

#element_heightObject



212
213
214
# File 'shoes-core/lib/shoes/dimensions.rb', line 212

def element_height
  @y_dimension.element_extent
end

#element_height=(value) ⇒ Object



216
217
218
# File 'shoes-core/lib/shoes/dimensions.rb', line 216

def element_height=(value)
  @y_dimension.element_extent = value
end

#element_leftObject



228
229
230
# File 'shoes-core/lib/shoes/dimensions.rb', line 228

def element_left
  @x_dimension.element_start
end

#element_rightObject



232
233
234
# File 'shoes-core/lib/shoes/dimensions.rb', line 232

def element_right
  @x_dimension.element_end
end

#element_topObject



236
237
238
# File 'shoes-core/lib/shoes/dimensions.rb', line 236

def element_top
  @y_dimension.element_start
end

#element_widthObject



220
221
222
# File 'shoes-core/lib/shoes/dimensions.rb', line 220

def element_width
  @x_dimension.element_extent
end

#element_width=(value) ⇒ Object



224
225
226
# File 'shoes-core/lib/shoes/dimensions.rb', line 224

def element_width=(value)
  @x_dimension.element_extent = value
end

#heightObject



172
173
174
# File 'shoes-core/lib/shoes/dimensions.rb', line 172

def height
  @y_dimension.extent
end

#height=(value) ⇒ Object



176
177
178
# File 'shoes-core/lib/shoes/dimensions.rb', line 176

def height=(value)
  @y_dimension.extent = value
end

#in_bounds?(x, y) ⇒ Boolean

Is the given point within the bounds of this element

Parameters:

  • x (Fixnum)

    Location the x dimension to check

  • y (Fiynum)

    Location the y dimension to check

Returns:

  • (Boolean)

    true if point is within this element, false otherwise



110
111
112
# File 'shoes-core/lib/shoes/dimensions.rb', line 110

def in_bounds?(x, y)
  x_dimension.in_bounds?(x) && y_dimension.in_bounds?(y)
end

#leftObject



180
181
182
# File 'shoes-core/lib/shoes/dimensions.rb', line 180

def left
  @x_dimension.start
end

#left=(value) ⇒ Object



184
185
186
# File 'shoes-core/lib/shoes/dimensions.rb', line 184

def left=(value)
  @x_dimension.start = value
end

#marginArray<Fixnum>

Margins for the element

Returns:

  • (Array<Fixnum>)

    left, top, right and bottom margin as array



130
131
132
# File 'shoes-core/lib/shoes/dimensions.rb', line 130

def margin
  [margin_left, margin_top, margin_right, margin_bottom]
end

#margin=(margin) ⇒ Object

Set margin value for element

If a single value is passed, all margins are set to that value.

If an array is passed, values from array are spread to left, top, right and bottom in that order.

margin to

Parameters:

  • margin (Fixnum, Array<Fixnum>)

    Value or array of values to set



143
144
145
146
147
# File 'shoes-core/lib/shoes/dimensions.rb', line 143

def margin=(margin)
  margin = [margin, margin, margin, margin] unless margin.is_a? Array
  self.margin_left, self.margin_top,
  self.margin_right, self.margin_bottom = margin
end

#margin_bottomObject



268
269
270
# File 'shoes-core/lib/shoes/dimensions.rb', line 268

def margin_bottom
  @y_dimension.margin_end
end

#margin_bottom=(value) ⇒ Object



272
273
274
# File 'shoes-core/lib/shoes/dimensions.rb', line 272

def margin_bottom=(value)
  @y_dimension.margin_end = value
end

#margin_leftObject



244
245
246
# File 'shoes-core/lib/shoes/dimensions.rb', line 244

def margin_left
  @x_dimension.margin_start
end

#margin_left=(value) ⇒ Object



248
249
250
# File 'shoes-core/lib/shoes/dimensions.rb', line 248

def margin_left=(value)
  @x_dimension.margin_start = value
end

#margin_rightObject



252
253
254
# File 'shoes-core/lib/shoes/dimensions.rb', line 252

def margin_right
  @x_dimension.margin_end
end

#margin_right=(value) ⇒ Object



256
257
258
# File 'shoes-core/lib/shoes/dimensions.rb', line 256

def margin_right=(value)
  @x_dimension.margin_end = value
end

#margin_topObject



260
261
262
# File 'shoes-core/lib/shoes/dimensions.rb', line 260

def margin_top
  @y_dimension.margin_start
end

#margin_top=(value) ⇒ Object



264
265
266
# File 'shoes-core/lib/shoes/dimensions.rb', line 264

def margin_top=(value)
  @y_dimension.margin_start = value
end

#needs_positioning?Boolean

Determines if this element participate in slot positioning

Returns:

  • (Boolean)

    Whether to position element during slot layout



152
153
154
# File 'shoes-core/lib/shoes/dimensions.rb', line 152

def needs_positioning?
  true
end

#positioned?Boolean

Has this element been positioned by layout

Returns:

  • (Boolean)


101
102
103
# File 'shoes-core/lib/shoes/dimensions.rb', line 101

def positioned?
  x_dimension.positioned? && y_dimension.positioned?
end

#rightObject



188
189
190
# File 'shoes-core/lib/shoes/dimensions.rb', line 188

def right
  @x_dimension.end
end

#right=(value) ⇒ Object



192
193
194
# File 'shoes-core/lib/shoes/dimensions.rb', line 192

def right=(value)
  @x_dimension.end = value
end

#takes_up_space?Boolean

Determines whether to advance current position during slot layout

element

Returns:

  • (Boolean)

    Whether to advance position after laying out this



160
161
162
# File 'shoes-core/lib/shoes/dimensions.rb', line 160

def takes_up_space?
  true
end

#topObject



196
197
198
# File 'shoes-core/lib/shoes/dimensions.rb', line 196

def top
  @y_dimension.start
end

#top=(value) ⇒ Object



200
201
202
# File 'shoes-core/lib/shoes/dimensions.rb', line 200

def top=(value)
  @y_dimension.start = value
end

#widthObject



164
165
166
# File 'shoes-core/lib/shoes/dimensions.rb', line 164

def width
  @x_dimension.extent
end

#width=(value) ⇒ Object



168
169
170
# File 'shoes-core/lib/shoes/dimensions.rb', line 168

def width=(value)
  @x_dimension.extent = value
end