motion-kit 0.0.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +839 -0
  3. data/lib/motion-kit-cocoa/cocoa_util.rb +59 -0
  4. data/lib/motion-kit-cocoa/constraints/constraint.rb +765 -0
  5. data/lib/motion-kit-cocoa/constraints/constraint_placeholder.rb +22 -0
  6. data/lib/motion-kit-cocoa/constraints/constraints_layout.rb +536 -0
  7. data/lib/motion-kit-cocoa/constraints/constraints_target.rb +52 -0
  8. data/lib/motion-kit-cocoa/layouts/cagradientlayer_layout.rb +13 -0
  9. data/lib/motion-kit-cocoa/layouts/calayer_layout.rb +27 -0
  10. data/lib/motion-kit-cocoa/layouts/sugarcube_compat.rb +38 -0
  11. data/lib/motion-kit-ios/dummy.rb +93 -0
  12. data/lib/motion-kit-ios/ios_util.rb +20 -0
  13. data/lib/motion-kit-ios/layouts/constraints_layout.rb +22 -0
  14. data/lib/motion-kit-ios/layouts/layout_device.rb +32 -0
  15. data/lib/motion-kit-ios/layouts/layout_orientation.rb +47 -0
  16. data/lib/motion-kit-ios/layouts/uibutton_layout.rb +52 -0
  17. data/lib/motion-kit-ios/layouts/uiview_layout.rb +45 -0
  18. data/lib/motion-kit-ios/layouts/uiview_layout_autoresizing.rb +75 -0
  19. data/lib/motion-kit-ios/layouts/uiview_layout_constraints.rb +36 -0
  20. data/lib/motion-kit-ios/layouts/uiview_layout_frame.rb +307 -0
  21. data/lib/motion-kit-ios/layouts/uiview_layout_gradient.rb +20 -0
  22. data/lib/motion-kit-osx/dummy.rb +89 -0
  23. data/lib/motion-kit-osx/layouts/constraints_layout.rb +42 -0
  24. data/lib/motion-kit-osx/layouts/nsmenu_extensions.rb +186 -0
  25. data/lib/motion-kit-osx/layouts/nsmenu_layout.rb +109 -0
  26. data/lib/motion-kit-osx/layouts/nsmenuitem_extensions.rb +45 -0
  27. data/lib/motion-kit-osx/layouts/nstablecolumn_layout.rb +12 -0
  28. data/lib/motion-kit-osx/layouts/nstableview_layout.rb +20 -0
  29. data/lib/motion-kit-osx/layouts/nsview_layout.rb +47 -0
  30. data/lib/motion-kit-osx/layouts/nsview_layout_autoresizing.rb +75 -0
  31. data/lib/motion-kit-osx/layouts/nsview_layout_constraints.rb +34 -0
  32. data/lib/motion-kit-osx/layouts/nsview_layout_frame.rb +375 -0
  33. data/lib/motion-kit-osx/layouts/nswindow_frame.rb +14 -0
  34. data/lib/motion-kit-osx/layouts/nswindow_layout.rb +77 -0
  35. data/lib/motion-kit-osx/osx_util.rb +16 -0
  36. data/lib/motion-kit.rb +33 -0
  37. data/lib/motion-kit/calculate.rb +263 -0
  38. data/lib/motion-kit/layouts/base_layout.rb +299 -0
  39. data/lib/motion-kit/layouts/base_layout_class_methods.rb +43 -0
  40. data/lib/motion-kit/layouts/parent.rb +68 -0
  41. data/lib/motion-kit/layouts/view_layout.rb +327 -0
  42. data/lib/motion-kit/motion-kit.rb +18 -0
  43. data/lib/motion-kit/object.rb +16 -0
  44. data/lib/motion-kit/util.rb +20 -0
  45. data/lib/motion-kit/version.rb +1 -1
  46. data/spec/ios/apply_styles_spec.rb +21 -0
  47. data/spec/ios/autoresizing_helper_spec.rb +224 -0
  48. data/spec/ios/calculate_spec.rb +322 -0
  49. data/spec/ios/calculator_spec.rb +31 -0
  50. data/spec/ios/constraints_helpers/attribute_lookup_spec.rb +27 -0
  51. data/spec/ios/constraints_helpers/axis_lookup_spec.rb +13 -0
  52. data/spec/ios/constraints_helpers/center_constraints_spec.rb +419 -0
  53. data/spec/ios/constraints_helpers/constraint_placeholder_spec.rb +72 -0
  54. data/spec/ios/constraints_helpers/priority_lookup_spec.rb +19 -0
  55. data/spec/ios/constraints_helpers/relationship_lookup_spec.rb +27 -0
  56. data/spec/ios/constraints_helpers/relative_corners_spec.rb +274 -0
  57. data/spec/ios/constraints_helpers/relative_location_spec.rb +111 -0
  58. data/spec/ios/constraints_helpers/simple_constraints_spec.rb +2763 -0
  59. data/spec/ios/constraints_helpers/size_constraints_spec.rb +422 -0
  60. data/spec/ios/constraints_helpers/view_lookup_constraints_spec.rb +93 -0
  61. data/spec/ios/create_layout_spec.rb +40 -0
  62. data/spec/ios/custom_layout_spec.rb +13 -0
  63. data/spec/ios/deferred_spec.rb +89 -0
  64. data/spec/ios/device_helpers_spec.rb +51 -0
  65. data/spec/ios/frame_helper_spec.rb +1150 -0
  66. data/spec/ios/layer_layout_spec.rb +36 -0
  67. data/spec/ios/layout_extensions_spec.rb +70 -0
  68. data/spec/ios/layout_spec.rb +74 -0
  69. data/spec/ios/layout_state_spec.rb +27 -0
  70. data/spec/ios/motionkit_util_spec.rb +102 -0
  71. data/spec/ios/objc_selectors_spec.rb +10 -0
  72. data/spec/ios/orientation_helper_specs.rb +67 -0
  73. data/spec/ios/parent_layout_spec.rb +19 -0
  74. data/spec/ios/parent_spec.rb +45 -0
  75. data/spec/ios/remove_layout_spec.rb +25 -0
  76. data/spec/ios/root_layout_spec.rb +53 -0
  77. data/spec/ios/setters_spec.rb +63 -0
  78. data/spec/ios/uibutton_layout_spec.rb +24 -0
  79. data/spec/ios/uitextfield_spec.rb +14 -0
  80. data/spec/ios/view_attr_spec.rb +25 -0
  81. data/spec/osx/autoresizing_helper_spec.rb +224 -0
  82. data/spec/osx/constraints_helper_spec.rb +0 -0
  83. data/spec/osx/constraints_helpers/orientation_lookup_spec.rb +13 -0
  84. data/spec/osx/constraints_helpers/simple_constraints_spec.rb +2095 -0
  85. data/spec/osx/constraints_helpers/size_constraints_spec.rb +362 -0
  86. data/spec/osx/create_menu_spec.rb +14 -0
  87. data/spec/osx/create_via_extensions_spec.rb +63 -0
  88. data/spec/osx/deferred_spec.rb +85 -0
  89. data/spec/osx/frame_helper_spec.rb +1881 -0
  90. data/spec/osx/menu_extensions_spec.rb +376 -0
  91. data/spec/osx/menu_layout_spec.rb +157 -0
  92. data/spec/osx/menu_spec.rb +70 -0
  93. data/spec/osx/root_menu_spec.rb +15 -0
  94. metadata +166 -14
@@ -0,0 +1,75 @@
1
+ # @requires MotionKit::NSViewLayout
2
+ module MotionKit
3
+ class NSViewLayout
4
+
5
+ def autoresizing_mask(*values)
6
+ value = 0
7
+ values.each do |mask|
8
+ case mask
9
+ when :flexible_left
10
+ value |= NSViewMinXMargin
11
+ when :flexible_width
12
+ value |= NSViewWidthSizable
13
+ when :flexible_right
14
+ value |= NSViewMaxXMargin
15
+ when :flexible_top
16
+ value |= NSViewMaxYMargin
17
+ when :flexible_height
18
+ value |= NSViewHeightSizable
19
+ when :flexible_bottom
20
+ value |= NSViewMinYMargin
21
+
22
+ when :rigid_left
23
+ value ^= NSViewMinXMargin
24
+ when :rigid_width
25
+ value ^= NSViewWidthSizable
26
+ when :rigid_right
27
+ value ^= NSViewMaxXMargin
28
+ when :rigid_top
29
+ value ^= NSViewMaxYMargin
30
+ when :rigid_height
31
+ value ^= NSViewHeightSizable
32
+ when :rigid_bottom
33
+ value ^= NSViewMinYMargin
34
+
35
+ when :fill
36
+ value |= NSViewWidthSizable | NSViewHeightSizable
37
+ when :fill_top
38
+ value |= NSViewWidthSizable | NSViewMinYMargin
39
+ when :fill_bottom
40
+ value |= NSViewWidthSizable | NSViewMaxYMargin
41
+ when :fill_left
42
+ value |= NSViewHeightSizable | NSViewMaxXMargin
43
+ when :fill_right
44
+ value |= NSViewHeightSizable | NSViewMinXMargin
45
+
46
+ when :pin_to_top_left
47
+ value |= NSViewMaxXMargin | NSViewMinYMargin
48
+ when :pin_to_top
49
+ value |= NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin
50
+ when :pin_to_top_right
51
+ value |= NSViewMinXMargin | NSViewMinYMargin
52
+ when :pin_to_left
53
+ value |= NSViewMaxYMargin | NSViewMinYMargin | NSViewMaxXMargin
54
+ when :pin_to_center, :pin_to_middle
55
+ value |= NSViewMaxYMargin | NSViewMinYMargin | NSViewMinXMargin | NSViewMaxXMargin
56
+ when :pin_to_right
57
+ value |= NSViewMaxYMargin | NSViewMinYMargin | NSViewMinXMargin
58
+ when :pin_to_bottom_left
59
+ value |= NSViewMaxXMargin | NSViewMaxYMargin
60
+ when :pin_to_bottom
61
+ value |= NSViewMinXMargin | NSViewMaxXMargin | NSViewMaxYMargin
62
+ when :pin_to_bottom_right
63
+ value |= NSViewMinXMargin | NSViewMaxYMargin
64
+ else
65
+ value |= mask
66
+ end
67
+ end
68
+
69
+ target.autoresizingMask = value
70
+ value
71
+ end
72
+ alias autoresizingMask autoresizing_mask
73
+
74
+ end
75
+ end
@@ -0,0 +1,34 @@
1
+ # @requires MotionKit::NSViewLayout
2
+ module MotionKit
3
+ class NSViewLayout
4
+
5
+ def constraints(view=nil, &block)
6
+ view ||= target
7
+ if view.is_a?(Symbol)
8
+ view = self.get(view)
9
+ end
10
+ view.setTranslatesAutoresizingMaskIntoConstraints(false)
11
+
12
+ deferred do
13
+ constraints_target = ConstraintsTarget.new(view)
14
+ context(constraints_target, &block)
15
+ constraints_target.apply_all_constraints(self, view)
16
+ end
17
+ end
18
+
19
+ end
20
+
21
+ class Layout
22
+
23
+ def constraints(view=nil, &block)
24
+ if @context
25
+ apply(:constraints, view, &block)
26
+ else
27
+ context(self.view) do
28
+ constraints(view, &block)
29
+ end
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,375 @@
1
+ # @requires MotionKit::NSViewLayout
2
+ module MotionKit
3
+ class NSViewLayout
4
+ override_start
5
+
6
+ def _fix_frame_value(value)
7
+ if value.is_a?(Hash) && value[:relative]
8
+ return value.merge(flipped: flipped?)
9
+ end
10
+ return value
11
+ end
12
+
13
+ def flipped?
14
+ target.superview && !target.superview.flipped?
15
+ end
16
+
17
+ def x(value)
18
+ value = _fix_frame_value(value)
19
+ f = target.frame
20
+ f.origin.x = MotionKit.calculate(target, :width, value)
21
+ target.frame = f
22
+ return CGRectGetMinX(f)
23
+ end
24
+ alias left x
25
+
26
+ def right(value)
27
+ value = _fix_frame_value(value)
28
+ f = target.frame
29
+ f.origin.x = MotionKit.calculate(target, :width, value) - f.size.width
30
+ target.frame = f
31
+ return CGRectGetMaxX(f)
32
+ end
33
+
34
+ def center_x(value)
35
+ value = _fix_frame_value(value)
36
+ f = target.frame
37
+ f.origin.x = MotionKit.calculate(target, :width, value)
38
+ f.origin.x -= f.size.width / 2
39
+ target.frame = f
40
+ return CGRectGetMidX(target.frame)
41
+ end
42
+ alias middle_x center_x
43
+
44
+ def y(value)
45
+ value = _fix_frame_value(value)
46
+ f = target.frame
47
+ f.origin.y = MotionKit.calculate(target, :height, value)
48
+ target.frame = f
49
+ return CGRectGetMinY(f)
50
+ end
51
+
52
+ def bottom(value)
53
+ value = _fix_frame_value(value)
54
+ f = target.frame
55
+ f.origin.y = MotionKit.calculate(target, :height, value)
56
+ unless flipped?
57
+ f.origin.y -= f.size.height
58
+ end
59
+ target.frame = f
60
+
61
+ if flipped?
62
+ return CGRectGetMinY(target.frame)
63
+ else
64
+ return CGRectGetMaxY(target.frame)
65
+ end
66
+ end
67
+
68
+ def top(value)
69
+ value = _fix_frame_value(value)
70
+ f = target.frame
71
+ f.origin.y = MotionKit.calculate(target, :height, value)
72
+ if flipped?
73
+ f.origin.y -= f.size.height
74
+ end
75
+ target.frame = f
76
+
77
+ if flipped?
78
+ return CGRectGetMaxY(f)
79
+ else
80
+ return CGRectGetMinY(f)
81
+ end
82
+ end
83
+
84
+ def center_y(value)
85
+ value = _fix_frame_value(value)
86
+ f = target.frame
87
+ f.origin.y = MotionKit.calculate(target, :height, value)
88
+ f.origin.y -= f.size.height / 2
89
+ target.frame = f
90
+ return CGRectGetMidY(target.frame)
91
+ end
92
+ alias middle_y center_y
93
+
94
+ def width(value)
95
+ value = _fix_frame_value(value)
96
+ f = target.frame
97
+ f.size.width = MotionKit.calculate(target, :width, value)
98
+ target.frame = f
99
+ return CGRectGetWidth(f)
100
+ end
101
+ alias w width
102
+
103
+ def height(value)
104
+ value = _fix_frame_value(value)
105
+ f = target.frame
106
+ f.size.height = MotionKit.calculate(target, :height, value)
107
+ target.frame = f
108
+ return CGRectGetHeight(f)
109
+ end
110
+ alias h height
111
+
112
+ def origin(value)
113
+ value = _fix_frame_value(value)
114
+ f = target.frame
115
+ f.origin = MotionKit.calculate(target, :origin, value)
116
+ target.frame = f
117
+ return target.frame.origin
118
+ end
119
+
120
+ def center(value)
121
+ value = _fix_frame_value(value)
122
+ f = target.frame
123
+ center = MotionKit.calculate(target, :center, value)
124
+ origin = CGPoint.new(center.x, center.y)
125
+ origin.x -= f.size.width / 2
126
+ origin.y -= f.size.height / 2
127
+ f.origin = origin
128
+ target.frame = f
129
+ return center
130
+ end
131
+ alias middle center
132
+
133
+ def size(value)
134
+ value = _fix_frame_value(value)
135
+ f = target.frame
136
+ f.size = MotionKit.calculate(target, :size, value)
137
+ target.frame = f
138
+ return target.frame.size
139
+ end
140
+
141
+ def frame(value)
142
+ value = _fix_frame_value(value)
143
+ target.frame = MotionKit.calculate(target, :frame, value)
144
+ return target.frame
145
+ end
146
+
147
+ def _calculate_frame(f, from: from_view, relative_to: point)
148
+ if from_view.is_a?(Symbol)
149
+ from_view = self.get(from_view)
150
+ end
151
+
152
+ from_view_size = from_view.frame.size
153
+ o = from_view.convertPoint([0, 0], toView: target.superview)
154
+
155
+ calculate_view = target
156
+
157
+ if point[:x] == :reset || point[:y] == :reset
158
+ calculate_view = NSView.alloc.initWithFrame([[0, 0], target.frame.size])
159
+ end
160
+
161
+ if f.is_a?(Hash)
162
+ f = f.merge(relative: true, flipped: flipped?)
163
+ end
164
+ f = MotionKit.calculate(calculate_view, :frame, f, from_view)
165
+ f.origin.x += o.x
166
+ f.origin.y += o.y
167
+
168
+ case point[:x]
169
+ when :min, :reset
170
+ # pass
171
+ when :mid
172
+ f.origin.x += (from_view_size.width - f.size.width) / 2.0
173
+ when :max
174
+ f.origin.x += from_view_size.width - f.size.width
175
+ when :before
176
+ f.origin.x -= f.size.width
177
+ when :after
178
+ f.origin.x += from_view_size.width
179
+ else
180
+ f.origin.x += point[:x]
181
+ end
182
+
183
+ case point[:y]
184
+ when :reset
185
+ # pass
186
+ when :min
187
+ unless flipped?
188
+ f.origin.y += from_view_size.height - f.size.height
189
+ end
190
+ when :mid
191
+ f.origin.y += (from_view_size.height - f.size.height) / 2.0
192
+ when :max
193
+ if flipped?
194
+ f.origin.y += from_view_size.height - f.size.height
195
+ end
196
+ when :above
197
+ if flipped?
198
+ f.origin.y += from_view_size.height
199
+ else
200
+ f.origin.y -= from_view_size.height + f.size.height
201
+ end
202
+ when :below
203
+ if flipped?
204
+ f.origin.y -= f.size.height
205
+ else
206
+ # pass
207
+ end
208
+ else
209
+ f.origin.y += point[:y]
210
+ end
211
+
212
+ return f
213
+ end
214
+
215
+ # The first arg can be a view or a frame
216
+ # @example
217
+ # frame from_top_left(width: 80, height: 22)
218
+ # frame from_top_left(another_view, width: 80, height: 22)
219
+ def from_top_left(from_view=nil, f=nil)
220
+ if from_view.is_a?(Hash)
221
+ f = from_view
222
+ from_view = nil
223
+ end
224
+ f ||= {}
225
+ from_view ||= target.superview
226
+ _calculate_frame(f, from: from_view, relative_to: { x: :min, y: :max })
227
+ end
228
+
229
+ # The first arg can be a view or a frame
230
+ # @example
231
+ # frame from_top(width: 80, height: 22)
232
+ # frame from_top(another_view, width: 80, height: 22)
233
+ def from_top(from_view=nil, f=nil)
234
+ if from_view.is_a?(Hash)
235
+ f = from_view
236
+ from_view = nil
237
+ end
238
+ f ||= {}
239
+ from_view ||= target.superview
240
+ _calculate_frame(f, from: from_view, relative_to: { x: :mid, y: :max })
241
+ end
242
+
243
+ # The first arg can be a view or a frame
244
+ # @example
245
+ # frame from_top_right(width: 80, height: 22)
246
+ # frame from_top_right(another_view, width: 80, height: 22)
247
+ def from_top_right(from_view=nil, f=nil)
248
+ if from_view.is_a?(Hash)
249
+ f = from_view
250
+ from_view = nil
251
+ end
252
+ f ||= {}
253
+ from_view ||= target.superview
254
+ _calculate_frame(f, from: from_view, relative_to: { x: :max, y: :max })
255
+ end
256
+
257
+ # The first arg can be a view or a frame
258
+ # @example
259
+ # frame from_left(width: 80, height: 22)
260
+ # frame from_left(another_view, width: 80, height: 22)
261
+ def from_left(from_view=nil, f=nil)
262
+ if from_view.is_a?(Hash)
263
+ f = from_view
264
+ from_view = nil
265
+ end
266
+ f ||= {}
267
+ from_view ||= target.superview
268
+ _calculate_frame(f, from: from_view, relative_to: { x: :min, y: :mid })
269
+ end
270
+
271
+ # The first arg can be a view or a frame
272
+ # @example
273
+ # frame from_center(width: 80, height: 22)
274
+ # frame from_center(another_view, width: 80, height: 22)
275
+ def from_center(from_view=nil, f=nil)
276
+ if from_view.is_a?(Hash)
277
+ f = from_view
278
+ from_view = nil
279
+ end
280
+ f ||= {}
281
+ from_view ||= target.superview
282
+ _calculate_frame(f, from: from_view, relative_to: { x: :mid, y: :mid })
283
+ end
284
+
285
+ # The first arg can be a view or a frame
286
+ # @example
287
+ # frame from_right(width: 80, height: 22)
288
+ # frame from_right(another_view, width: 80, height: 22)
289
+ def from_right(from_view=nil, f=nil)
290
+ if from_view.is_a?(Hash)
291
+ f = from_view
292
+ from_view = nil
293
+ end
294
+ f ||= {}
295
+ from_view ||= target.superview
296
+ _calculate_frame(f, from: from_view, relative_to: { x: :max, y: :mid })
297
+ end
298
+
299
+ # The first arg can be a view or a frame
300
+ # @example
301
+ # frame from_bottom_left(width: 80, height: 22)
302
+ # frame from_bottom_left(another_view, width: 80, height: 22)
303
+ def from_bottom_left(from_view=nil, f=nil)
304
+ if from_view.is_a?(Hash)
305
+ f = from_view
306
+ from_view = nil
307
+ end
308
+ f ||= {}
309
+ from_view ||= target.superview
310
+ _calculate_frame(f, from: from_view, relative_to: { x: :min, y: :min })
311
+ end
312
+
313
+ # The first arg can be a view or a frame
314
+ # @example
315
+ # frame from_bottom(width: 80, height: 22)
316
+ # frame from_bottom(another_view, width: 80, height: 22)
317
+ def from_bottom(from_view=nil, f=nil)
318
+ if from_view.is_a?(Hash)
319
+ f = from_view
320
+ from_view = nil
321
+ end
322
+ f ||= {}
323
+ from_view ||= target.superview
324
+ _calculate_frame(f, from: from_view, relative_to: { x: :mid, y: :min })
325
+ end
326
+
327
+ # The first arg can be a view or a frame
328
+ # @example
329
+ # frame from_bottom_right(width: 80, height: 22)
330
+ # frame from_bottom_right(another_view, width: 80, height: 22)
331
+ def from_bottom_right(from_view=nil, f=nil)
332
+ if from_view.is_a?(Hash)
333
+ f = from_view
334
+ from_view = nil
335
+ end
336
+ f ||= {}
337
+ from_view ||= target.superview
338
+ _calculate_frame(f, from: from_view, relative_to: { x: :max, y: :min })
339
+ end
340
+
341
+ # The first arg can be a view or a frame
342
+ # @example
343
+ def above(from_view, f={})
344
+ _calculate_frame(f, from: from_view, relative_to: { x: :reset, y: :above })
345
+ end
346
+
347
+ # The first arg can be a view or a frame
348
+ # @example
349
+ def below(from_view, f={})
350
+ _calculate_frame(f, from: from_view, relative_to: { x: :reset, y: :below })
351
+ end
352
+
353
+ # The first arg can be a view or a frame
354
+ # @example
355
+ def before(from_view, f={})
356
+ _calculate_frame(f, from: from_view, relative_to: { x: :before, y: :reset })
357
+ end
358
+ alias left_of before
359
+
360
+ # The first arg can be a view or a frame
361
+ # @example
362
+ def after(from_view, f={})
363
+ _calculate_frame(f, from: from_view, relative_to: { x: :after, y: :reset })
364
+ end
365
+ alias right_of after
366
+
367
+ # The first arg must be a view
368
+ # @example
369
+ def relative_to(from_view, f)
370
+ _calculate_frame(f, from: from_view, relative_to: { x: :reset, y: :reset })
371
+ end
372
+
373
+ override_stop
374
+ end
375
+ end