Class: HotCocoa::LayoutOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/hotcocoa/layout_view.rb

Overview

Stores the layout configuration for an object (e.g. a button).

Constant Summary

VALID_EXPANSIONS =

List of valid values for the #expand parameter.

[nil, :height, :width, [:height, :width], [:width, :height]]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, options = {}) ⇒ LayoutOptions

Returns a new instance of LayoutOptions

Parameters:

  • view (NSView)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :start (Boolean) — default: true

    Whether the view is packed at the start or the end of the packing view

  • :expand (Symbol) — default: nil

    Whether the view's first dimension (width for horizontal and height for vertical) should be expanded to the maximum possible size, and should be variable according to the packing view frame. Values can be :height, :width, or [:height, :width]

  • :padding (Float) — default: 0.0

    Controls the padding area around the view. :padding controls all the areas, while options like :left_padding only control the left side--if :padding is set, other padding flags are ignored

  • :left_padding (Fload) — default: 0.0
  • :right_padding (Float) — default: 0.0
  • :top_padding (Float) — default: 0.0
  • :bottom_padding (Float) — default: 0.0
  • :align (Symbol)

    Controls the view's alignment if its not expanded in the other dimension; modes can be:

    • :left For horizontal layouts, align left

    • :center Align center for horizontal or vertical layouts

    • :right For horizontal layouts, align right

    • :top For vertical layouts, align top

    • :bottom For vertical layouts, align bottom

  • :defaults_view (NSView)

    The layout to which this configuration belongs



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hotcocoa/layout_view.rb', line 61

def initialize view, options = {}
  @view           = view
  @start          = options[:start]
  @expand         = options[:expand]
  @padding        = options[:padding] || 0
  @left_padding   = @padding || options[:left_padding]
  @right_padding  = @padding || options[:right_padding]
  @top_padding    = @padding || options[:top_padding]
  @bottom_padding = @padding || options[:bottom_padding]
  @align          = options[:align]
  @defaults_view  = options[:defaults_view]
end

Instance Attribute Details

#defaults_viewHotCocoa::LayoutView

The layout view to which this configuration belongs.



15
16
17
# File 'lib/hotcocoa/layout_view.rb', line 15

def defaults_view
  @defaults_view
end

#paddingNumber

Number of pixels of padding to add to each side of the view, unless otherwies overridden by setting left_padding, right_padding, etc.

Returns:

  • (Number)


285
286
287
# File 'lib/hotcocoa/layout_view.rb', line 285

def padding
  @padding
end

#viewNSView (readonly)

Returns:

  • (NSView)


18
19
20
# File 'lib/hotcocoa/layout_view.rb', line 18

def view
  @view
end

Instance Method Details

#alignSymbol

Returns which way that a view should be aligned.

Possible values are documented in #initialize.

Returns:

  • (Symbol)


260
261
262
263
264
265
266
267
# File 'lib/hotcocoa/layout_view.rb', line 260

def align
  return @align unless @align.nil?
  if in_layout_view?
    @view.superview.default_layout.align
  else
    :left
  end
end

#align=(value) ⇒ Object

Set which way the view should be aligned in the packing view.

Possible values are documented in #initialize.

Parameters:

  • (Symbol)


248
249
250
251
252
# File 'lib/hotcocoa/layout_view.rb', line 248

def align= value
  return if value == @align
  @align = value
  update_layout_views!
end

#bottom_paddingNumber

Return how much padding should be added to the buttom of the view.

Returns:

  • (Number)


233
234
235
236
237
238
239
240
# File 'lib/hotcocoa/layout_view.rb', line 233

def bottom_padding
  return @bottom_padding unless @bottom_padding.nil?
  if in_layout_view?
    @view.superview.default_layout.bottom_padding
  else
    padding
  end
end

#bottom_padding=(value) ⇒ Object

Set how much padding, in pixels, should be added to the bottom of the view.

Parameters:

  • (Number)


222
223
224
225
226
227
# File 'lib/hotcocoa/layout_view.rb', line 222

def bottom_padding= value
  return if value == @bottom_padding
  @bottom_padding = value
  @padding = nil
  update_layout_views!
end

#expandSymbol, ...

Returns how a view should expand in its packing view.

If there is nothing set for the expansion, then this method will return false.

Returns:

  • (Symbol, Array(Symbol,Symbol), Boolean)


120
121
122
123
124
125
126
127
# File 'lib/hotcocoa/layout_view.rb', line 120

def expand
  return @expand unless @expand.nil?
  if in_layout_view?
    @view.superview.default_layout.expand
  else
    false
  end
end

#expand=(value) ⇒ Object

Set whether or not the view should expand in either width or height to fill in unused space in the view.

The available values are those from VALID_EXPANSIONS.

Parameters:

  • (Symbol, Array(Symbol,Symbol))


104
105
106
107
108
109
110
111
# File 'lib/hotcocoa/layout_view.rb', line 104

def expand= value
  return if value == @expand
  unless VALID_EXPANSIONS.include?(value)
    raise ArgumentError, "Expand must be nil, :height, :width or [:width, :height] not #{value.inspect}"
  end
  @expand = value
  update_layout_views!
end

#expand_height?Boolean

Whether or not to expand the view height-wise.

Returns:

  • (Boolean)


138
139
140
141
# File 'lib/hotcocoa/layout_view.rb', line 138

def expand_height?
  e = self.expand
  e == :height || (e.respond_to?(:include?) && e.include?(:height))
end

#expand_width?Boolean

Whether or not to expand the view width-wise.

Returns:

  • (Boolean)


131
132
133
134
# File 'lib/hotcocoa/layout_view.rb', line 131

def expand_width?
  e = self.expand
  e == :width || (e.respond_to?(:include?) && e.include?(:width))
end

#inspectString

"Pretty" string version of the configuration.

Returns:

  • (String)


291
292
293
294
295
296
297
298
# File 'lib/hotcocoa/layout_view.rb', line 291

def inspect
  "#<#{self.class} " +
    "start=#{start?}, " +
    "expand=#{expand.inspect}, " +
    "padding=[l:#{left_padding}, r:#{right_padding}, t:#{top_padding}, b:#{bottom_padding}], " +
    "align=#{align.inspect}, " +
    "view=#{view.inspect}>"
end

#left_paddingNumber

Return how much padding should be added to the left of the view.

Returns:

  • (Number)


159
160
161
162
163
164
165
166
# File 'lib/hotcocoa/layout_view.rb', line 159

def left_padding
  return @left_padding unless @left_padding.nil?
  if in_layout_view?
    @view.superview.default_layout.left_padding
  else
    padding
  end
end

#left_padding=(value) ⇒ Object

Set the amount of padding, in pixels, to add to the left of the view

Parameters:

  • (Number)


148
149
150
151
152
153
# File 'lib/hotcocoa/layout_view.rb', line 148

def left_padding= value
  return if value == @left_padding
  @left_padding = value
  @padding = nil
  update_layout_views!
end

#right_paddingNumber

Return how much padding should be added to the right of the view.

Returns:

  • (Number)


183
184
185
186
187
188
189
190
# File 'lib/hotcocoa/layout_view.rb', line 183

def right_padding
  return @right_padding unless @right_padding.nil?
  if in_layout_view?
    @view.superview.default_layout.right_padding
  else
    padding
  end
end

#right_padding=(value) ⇒ Object

Return how much padding, in pixels, should be added to the right of the view.



172
173
174
175
176
177
# File 'lib/hotcocoa/layout_view.rb', line 172

def right_padding= value
  return if value == @right_padding
  @right_padding = value
  @padding = nil
  update_layout_views!
end

#start=(value) ⇒ Object

Set if this view should be relative to the start or end of the packing view.

Parameters:

  • (Boolean)


79
80
81
82
83
# File 'lib/hotcocoa/layout_view.rb', line 79

def start= value
  return if value == @start
  @start = value
  update_layout_views!
end

#start?Boolean

Whether or not to layout the view relative to the top of the packing view.

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/hotcocoa/layout_view.rb', line 88

def start?
  return @start unless @start.nil?
  if in_layout_view?
    @view.superview.default_layout.start?
  else
    true
  end
end

#top_paddingNumber

Return how much padding should be added to the top of the view.

Returns:

  • (Number)


208
209
210
211
212
213
214
215
# File 'lib/hotcocoa/layout_view.rb', line 208

def top_padding
  return @top_padding unless @top_padding.nil?
  if in_layout_view?
    @view.superview.default_layout.top_padding
  else
    padding
  end
end

#top_padding=(value) ⇒ Object

Set how much padding, in pixels, should be added to the top of the view.

Parameters:

  • (Number)


197
198
199
200
201
202
# File 'lib/hotcocoa/layout_view.rb', line 197

def top_padding= value
  return if value == @top_padding
  @top_padding = value
  @padding = nil
  update_layout_views!
end

#update_layout_views!Object

Tell the object using the configuration that it needs to relayout.



302
303
304
305
# File 'lib/hotcocoa/layout_view.rb', line 302

def update_layout_views!
  @view.superview.relayout! if in_layout_view?
  @defaults_view.relayout!  if @defaults_view
end