Module: MotionKit

Defined in:
lib/motion-kit/calculate.rb,
lib/motion-kit/util.rb,
lib/motion-kit/version.rb,
lib/motion-kit/motion-kit.rb,
lib/motion-kit-ios/ios_util.rb,
lib/motion-kit-osx/osx_util.rb,
lib/motion-kit/layouts/parent.rb,
lib/motion-kit-cocoa/cocoa_util.rb,
lib/motion-kit/layouts/base_layout.rb,
lib/motion-kit/layouts/tree_layout.rb,
lib/motion-kit-ios/layouts/layout_device.rb,
lib/motion-kit-ios/layouts/uiview_layout.rb,
lib/motion-kit-osx/layouts/nsmenu_layout.rb,
lib/motion-kit-osx/layouts/nsview_layout.rb,
lib/motion-kit-osx/layouts/nswindow_frame.rb,
lib/motion-kit-ios/layouts/uibutton_layout.rb,
lib/motion-kit-osx/layouts/nswindow_layout.rb,
lib/motion-kit-cocoa/constraints/constraint.rb,
lib/motion-kit-cocoa/layouts/calayer_layout.rb,
lib/motion-kit-osx/layouts/nsmenu_extensions.rb,
lib/motion-kit-cocoa/layouts/sugarcube_compat.rb,
lib/motion-kit-ios/layouts/constraints_layout.rb,
lib/motion-kit-ios/layouts/layout_orientation.rb,
lib/motion-kit-osx/layouts/constraints_layout.rb,
lib/motion-kit-osx/layouts/nstableview_layout.rb,
lib/motion-kit-ios/layouts/uiview_layout_frame.rb,
lib/motion-kit-osx/layouts/nsview_layout_frame.rb,
lib/motion-kit-osx/layouts/nstablecolumn_layout.rb,
lib/motion-kit-osx/layouts/nsmenuitem_extensions.rb,
lib/motion-kit/layouts/base_layout_class_methods.rb,
lib/motion-kit-ios/layouts/uiview_layout_gradient.rb,
lib/motion-kit-cocoa/constraints/constraints_layout.rb,
lib/motion-kit-cocoa/constraints/constraints_target.rb,
lib/motion-kit-cocoa/layouts/cagradientlayer_layout.rb,
lib/motion-kit-ios/layouts/uiview_layout_constraints.rb,
lib/motion-kit-osx/layouts/nsview_layout_constraints.rb,
lib/motion-kit-ios/layouts/uiview_layout_autoresizing.rb,
lib/motion-kit-osx/layouts/nsview_layout_autoresizing.rb,
lib/motion-kit-cocoa/constraints/constraint_placeholder.rb

Defined Under Namespace

Modules: BaseLayoutClassMethods Classes: ApplyError, BaseLayout, CAGradientLayerLayout, CALayerLayout, Calculator, CompoundConstraint, Constraint, ConstraintPlaceholder, ConstraintsLayout, ConstraintsTarget, ContextConflictError, InvalidAttributeError, InvalidDeferredError, InvalidPriorityError, InvalidRelationshipError, InvalidRootError, Layout, MenuLayout, NSMenuLayout, NSTableColumnLayout, NSTableViewLayout, NSViewLayout, NSWindowLayout, NoCommonAncestorError, NoContextError, NoSuperviewError, Parent, PointConstraint, SizeConstraint, TreeLayout, UIButtonLayout, UIViewLayout, WindowLayout

Constant Summary collapse

VERSION =
'0.10.1'

Class Method Summary collapse

Class Method Details

.appearance_classObject



16
17
18
# File 'lib/motion-kit/util.rb', line 16

def appearance_class
  @appearance_class ||= Class.new
end

.base_view_classObject



8
9
10
# File 'lib/motion-kit-ios/ios_util.rb', line 8

def base_view_class
  UIView
end

.calculate(view, dimension, amount, full_view = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/motion-kit/calculate.rb', line 5

def calculate(view, dimension, amount, full_view=nil)
  if amount.is_a? Proc
    return view.instance_exec(&amount)
  elsif dimension == :origin || dimension == :center
    return calculate_origin(view, dimension, amount, nil, full_view)
  elsif dimension == :size
    return calculate_size(view, amount, full_view)
  elsif dimension == :frame
    return calculate_frame(view, amount, full_view)
  elsif amount == :full
    return calculate(view, dimension, '100%', full_view)
  elsif amount == :auto
    size_that_fits = intrinsic_size(view)

    return case dimension
    when :width
      return size_that_fits.width
    when :height
      return size_that_fits.height
    end
  elsif amount.is_a?(String) && amount.include?('%')
    full_view ||= view.superview
    unless full_view
      raise NoSuperviewError.new("Cannot calculate #{amount}% of #{dimension.inspect} because view #{view} has no superview.")
    end
    calc = Calculator.scan(amount)

    factor = calc.factor
    constant = calc.constant

    return case dimension
    when :width
      return (full_view.frame.size.width * factor + constant).round
    when :height
      return (full_view.frame.size.height * factor + constant).round
    else
      raise "Unknown dimension #{dimension}"
    end
  else
    return amount
  end
end

.calculate_frame(view, amount, full_view) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/motion-kit/calculate.rb', line 172

def calculate_frame(view, amount, full_view)
  if amount.is_a?(Symbol)
    case amount
    when :full, :auto
      size = calculate_size(view, amount, full_view)
      origin = [0, 0]
    when :center
      size = view.frame.size
      origin = calculate_origin(view, :center, ['50%', '50%'], size, full_view)
      origin.x -= size.width / 2.0
      origin.y -= size.height / 2.0
    else
      raise "Unrecognized amount symbol #{amount.inspect} in MotionKit#calculate_frame"
    end

    return CGRect.new(origin, size)
  elsif amount.is_a?(Array)
    if amount.length == 2
      size = calculate_size(view, amount[1], full_view)
      origin = calculate_origin(view, :origin, amount[0], size, full_view)
    elsif amount.length == 4
      size = calculate_size(view, [amount[2], amount[3]], full_view)
      origin = calculate_origin(view, :origin, [amount[0], amount[1]], size, full_view)
    else
      raise "Don't know what to do with frame value #{amount.inspect}"
    end

    return CGRect.new(origin, size)
  elsif amount.is_a?(Hash)
    if amount.key?(:size)
      size = calculate_size(view, amount[:size], full_view)
    else
      size = calculate_size(view, amount, full_view)
    end

    if amount.key?(:center)
      origin = calculate_origin(view, :center, amount[:center], size, full_view)
      origin.x -= size.width / 2
      origin.y -= size.height / 2
    elsif amount.key?(:origin)
      origin = calculate_origin(view, :origin, amount[:origin], size, full_view)
    else
      origin = calculate_origin(view, :origin, amount, size, full_view)
    end

    return CGRect.new(origin, size)
  else
    return amount
  end
end

.calculate_origin(view, dimension, amount, my_size = nil, full_view) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
# File 'lib/motion-kit/calculate.rb', line 48

def calculate_origin(view, dimension, amount, my_size=nil, full_view)
  my_size ||= view.frame.size

  if amount == :center
    x = calculate(view, :width, '50%', full_view) - my_size.width / 2.0
    y = calculate(view, :height, '50%', full_view) - my_size.height / 2.0
    return CGPoint.new(x, y)
  elsif amount.is_a?(Array) || amount.is_a?(Hash)
    x_offset = 0
    y_offset = 0

    if amount.is_a?(Hash)
      if amount[:relative]
        if amount.key?(:x)
          x = amount[:x]
        else
          x = view.frame.origin.x
          if dimension == :center
            x += my_size.width / 2.0
          end
        end

        if amount.key?(:y)
          y = amount[:y]
        else
          y = view.frame.origin.y
          if dimension == :center
            y += my_size.height / 2.0
          end
        end

        if amount.key?(:right)
          x_offset = amount[:right]
        elsif amount.key?(:left)
          x_offset = -amount[:left]
        end

        if amount.key?(:down)
          y_offset = amount[:down]
        elsif amount.key?(:up)
          y_offset = -amount[:up]
        end
        y_offset = -y_offset if amount[:flipped]
      else
        if amount.key?(:right)
          x_offset = -my_size.width
          x = amount[:right]
        elsif amount.key?(:x) || amount.key?(:left)
          x = amount[:x] || amount[:left]
        elsif dimension == :center
          x = view.frame.origin.x
          x += my_size.width / 2
        else
          x = view.frame.origin.x
        end

        if amount.key?(:bottom)
          y_offset = -my_size.height
          y = amount[:bottom]
        elsif amount.key?(:y) || amount.key?(:top)
          y = amount[:y] || amount[:top]
        elsif dimension == :center
          y = view.frame.origin.y
          y += my_size.height / 2
        else
          y = view.frame.origin.y
        end
      end
    else
      x = amount[0]
      y = amount[1]
    end

    x = calculate(view, :width, x, full_view) + x_offset
    y = calculate(view, :height, y, full_view) + y_offset
    return CGPoint.new(x, y)
  else
    return amount
  end
end

.calculate_size(view, amount, full_view) ⇒ Object



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
161
162
163
164
165
166
167
168
169
170
# File 'lib/motion-kit/calculate.rb', line 129

def calculate_size(view, amount, full_view)
  if amount.is_a?(Array) || amount.is_a?(Hash)
    if amount.is_a?(Hash)
      w = amount.fetch(:w, amount.fetch(:width, view.frame.size.width))
      h = amount.fetch(:h, amount.fetch(:height, view.frame.size.height))
    else
      w = amount[0]
      h = amount[1]
    end

    # scaling is handled a little differently, because it is dependent on
    # the other amount and the intrinsic size.
    if w == :scale || h == :scale
      if w == :scale && h == :scale
        raise "Either width or height can be :scale, but not both"
      elsif w == :scale
        h = calculate(view, :height, h, full_view)
        size = intrinsic_size(view)
        w = h * size.width / size.height
      elsif h == :scale
        w = calculate(view, :width, w, full_view)
        size = intrinsic_size(view)
        h = w * size.height / size.width
      end
    else
      w = calculate(view, :width, w, full_view)
      h = calculate(view, :height, h, full_view)
    end

    return CGSize.new(w, h)
  elsif amount == :full
    w = calculate(view, :width, '100%', full_view)
    h = calculate(view, :height, '100%', full_view)
    return CGSize.new(w, h)
  elsif amount == :auto
    return intrinsic_size(view)
  elsif amount.is_a?(Symbol)
    raise "Unrecognized amount symbol #{amount.inspect} in MotionKit#calculate_size"
  else
    return amount
  end
end

.camel_case(str) ⇒ Object



8
9
10
# File 'lib/motion-kit/util.rb', line 8

def camel_case(str)
  str.split('_').map(&:capitalize).join
end

.color_classObject



16
17
18
# File 'lib/motion-kit-ios/ios_util.rb', line 16

def color_class
  UIColor
end

.default_view_classObject



12
13
14
# File 'lib/motion-kit-ios/ios_util.rb', line 12

def default_view_class
  UIView
end

.find_all_layers(root, &condition) ⇒ Object

similar to find_all_views, but for CALayer



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/motion-kit-cocoa/cocoa_util.rb', line 46

def find_all_layers(root, &condition)
  found = []
  if condition.call(root)
    found << root
  end

  (root.sublayers || []).each do |sublayer|
    found.concat(find_all_layers(sublayer, &condition))
  end

  return found
end

.find_all_views(root, &condition) ⇒ Object

performs a breadth-first search, but returns all matches



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/motion-kit-cocoa/cocoa_util.rb', line 32

def find_all_views(root, &condition)
  found = []
  if condition.call(root)
    found << root
  end

  (root.subviews || []).each do |subview|
    found.concat(find_all_views(subview, &condition))
  end

  return found
end

.find_first_view(root, &condition) ⇒ Object

performs a breadth-first search



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/motion-kit-cocoa/cocoa_util.rb', line 5

def find_first_view(root, &condition)
  if condition.call(root)
    root
  else
    found = nil
    root.subviews.find do |subview|
      found = find_first_view(subview, &condition)
    end
    return found
  end
end

.find_last_view(root, &condition) ⇒ Object

performs a depth-first and reversed search



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/motion-kit-cocoa/cocoa_util.rb', line 18

def find_last_view(root, &condition)
  found = nil
  root.subviews.reverse.find do |subview|
    found = find_last_view(subview, &condition)
  end

  if ! found && condition.call(root)
    found = root
  end

  return found
end

.intrinsic_size(view) ⇒ Object



223
224
225
226
227
228
229
230
231
232
# File 'lib/motion-kit/calculate.rb', line 223

def intrinsic_size(view)
  size_that_fits = view.intrinsicContentSize
  if size_that_fits.width == MotionKit.no_intrinsic_metric
    size_that_fits.width = 0
  end
  if size_that_fits.height == MotionKit.no_intrinsic_metric
    size_that_fits.height = 0
  end
  return size_that_fits
end

.no_intrinsic_metricObject



20
21
22
# File 'lib/motion-kit-ios/ios_util.rb', line 20

def no_intrinsic_metric
  UIViewNoIntrinsicMetric
end

.objective_c_method_name(str) ⇒ Object



4
5
6
# File 'lib/motion-kit/util.rb', line 4

def objective_c_method_name(str)
  str.split('_').inject([]) { |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
end

.setter(method_name) ⇒ Object



12
13
14
# File 'lib/motion-kit/util.rb', line 12

def setter(method_name)
  "set#{method_name[0].capitalize}#{method_name[1..-1]}:"
end