motion-kit 0.10.5 → 0.10.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b22aaa500be2edac6395ebf69d34966726906a6f
4
- data.tar.gz: 73762f0b31935e2eafdf23eaf8ad397b262e31f9
3
+ metadata.gz: 20015e2ea39d1ee5ade966a4db342b0605fcfb6e
4
+ data.tar.gz: 74a853c1ddf2ecb4627d2ef159411f6b76c9c324
5
5
  SHA512:
6
- metadata.gz: 1a96bd95971b62699b7f5650c89d91b16dcc61220ec741c31b1d8ea79b25809a3b02840113d2f3bf42366044180e7f4afda70668abbeb7696a738e0565c8c2f0
7
- data.tar.gz: 16151eed8d35d0db5848665d561a6cba5d3f4d66d93eeb1aae1e3fa474c02c318e1970d7709b8d093a4dfffab60296ea77a4f033bbe737b04160ef7f2f989782
6
+ metadata.gz: a2c4fb5357b601dafe6455b4f0f344b90b9913a44a2a984085f139903f187992f3d4b855a26717b0f4fb4beaaee83474f636318ad48e4f060cfc2249cedd0094
7
+ data.tar.gz: 3e7e1c372e731a783919e6567ebce275b4c74e149a463dc78ec1175d7839076d0ccd9660384721ed9a0298a109568ff0470afbeb43925c0a08cbc9ce473c5c9a
data/README.md CHANGED
@@ -504,11 +504,18 @@ You can pass symbols like `autoresizing_mask :flexible_width`, or use
504
504
  symbols that have more intuitive meaning than the usual
505
505
  `UIViewAutoresizingFlexible*` constants. These work in iOS and OS X.
506
506
 
507
+ All of the `:pin_to_` shorthands have a fixed size, whereas the `:fill_`
508
+ shorthands have flexible size.
509
+
507
510
  ```ruby
511
+ # the :fill shorthands will get you a ton of mileage
512
+ autoresizing_mask :fill_top
513
+ # but if you want the size to stay constant, use :pin_to
514
+ autoresizing_mask :pin_to_bottom
515
+ # or, a list of flexible sides
508
516
  autoresizing_mask :flexible_right, :flexible_bottom, :flexible_width
517
+ # or, combine them in some crazy fancy way
509
518
  autoresizing_mask :pin_to_left, :rigid_top # 'rigid' undoes a 'flexible' setting
510
- autoresizing_mask :pin_to_bottom, :flexible_width
511
- autoresizing_mask :fill_top
512
519
 
513
520
  flexible_left: Sticks to the right side
514
521
  flexible_width: Width varies with parent
@@ -701,6 +708,16 @@ simplifying the controller code (and usually making it more testable). See the
701
708
  [motion-kit-events]: https://github.com/rubymotion/motion-kit-events
702
709
 
703
710
 
711
+ ### MotionKit::Templates
712
+
713
+ gem install motion-kit-templates
714
+
715
+ Adds project templates, for use with `motion create`.
716
+
717
+ motion create foo --template=mk-ios
718
+ motion create foo --template=mk-osx
719
+
720
+
704
721
  ## Some handy tricks and Features
705
722
 
706
723
  ### Orientation specific styles
@@ -18,485 +18,184 @@ module MotionKit
18
18
  end
19
19
 
20
20
  def x(value=nil)
21
- constraint = Constraint.new(target.view, :left, :equal)
22
-
23
- if value
24
- constraint.equals(value)
25
- end
26
-
27
- target.add_constraints([constraint])
28
- return constraint
21
+ target_constraint(:left, :equal, value)
29
22
  end
30
23
  alias left x
31
24
 
32
25
  def min_x(value=nil)
33
- constraint = Constraint.new(target.view, :left, :gte)
34
-
35
- if value
36
- constraint.equals(value)
37
- end
38
-
39
- target.add_constraints([constraint])
40
- return constraint
26
+ target_constraint(:left, :gte, value)
41
27
  end
42
28
  alias min_left min_x
43
29
 
44
30
  def max_x(value=nil)
45
- constraint = Constraint.new(target.view, :left, :lte)
46
-
47
- if value
48
- constraint.equals(value)
49
- end
50
-
51
- target.add_constraints([constraint])
52
- return constraint
31
+ target_constraint(:left, :lte, value)
53
32
  end
54
33
  alias max_left max_x
55
34
 
56
35
  def leading(value=nil)
57
- constraint = Constraint.new(target.view, :leading, :equal)
58
-
59
- if value
60
- constraint.equals(value)
61
- end
62
-
63
- target.add_constraints([constraint])
64
- return constraint
36
+ target_constraint(:leading, :equal, value)
65
37
  end
66
38
 
67
39
  def min_leading(value=nil)
68
- constraint = Constraint.new(target.view, :leading, :gte)
69
-
70
- if value
71
- constraint.equals(value)
72
- end
73
-
74
- target.add_constraints([constraint])
75
- return constraint
40
+ target_constraint(:leading, :gte, value)
76
41
  end
77
42
 
78
43
  def max_leading(value=nil)
79
- constraint = Constraint.new(target.view, :leading, :lte)
80
-
81
- if value
82
- constraint.equals(value)
83
- end
84
-
85
- target.add_constraints([constraint])
86
- return constraint
44
+ target_constraint(:leading, :lte, value)
87
45
  end
88
46
 
89
47
  def center_x(value=nil)
90
- constraint = Constraint.new(target.view, :center_x, :equal)
91
-
92
- if value
93
- constraint.equals(value)
94
- end
95
-
96
- target.add_constraints([constraint])
97
- return constraint
48
+ target_constraint(:center_x, :equal, value)
98
49
  end
99
50
 
100
51
  def min_center_x(value=nil)
101
- constraint = Constraint.new(target.view, :center_x, :gte)
102
-
103
- if value
104
- constraint.equals(value)
105
- end
106
-
107
- target.add_constraints([constraint])
108
- return constraint
52
+ target_constraint(:center_x, :gte, value)
109
53
  end
110
54
 
111
55
  def max_center_x(value=nil)
112
- constraint = Constraint.new(target.view, :center_x, :lte)
113
-
114
- if value
115
- constraint.equals(value)
116
- end
117
-
118
- target.add_constraints([constraint])
119
- return constraint
56
+ target_constraint(:center_x, :lte, value)
120
57
  end
121
58
 
122
59
  def right(value=nil)
123
- constraint = Constraint.new(target.view, :right, :equal)
124
-
125
- if value
126
- constraint.equals(value)
127
- end
128
-
129
- target.add_constraints([constraint])
130
- return constraint
60
+ target_constraint(:right, :equal, value)
131
61
  end
132
62
 
133
63
  def min_right(value=nil)
134
- constraint = Constraint.new(target.view, :right, :gte)
135
-
136
- if value
137
- constraint.equals(value)
138
- end
139
-
140
- target.add_constraints([constraint])
141
- return constraint
64
+ target_constraint(:right, :gte, value)
142
65
  end
143
66
 
144
67
  def max_right(value=nil)
145
- constraint = Constraint.new(target.view, :right, :lte)
146
-
147
- if value
148
- constraint.equals(value)
149
- end
150
-
151
- target.add_constraints([constraint])
152
- return constraint
68
+ target_constraint(:right, :lte, value)
153
69
  end
154
70
 
155
71
  def trailing(value=nil)
156
- constraint = Constraint.new(target.view, :trailing, :equal)
157
-
158
- if value
159
- constraint.equals(value)
160
- end
161
-
162
- target.add_constraints([constraint])
163
- return constraint
72
+ target_constraint(:trailing, :equal, value)
164
73
  end
165
74
 
166
75
  def min_trailing(value=nil)
167
- constraint = Constraint.new(target.view, :trailing, :gte)
168
-
169
- if value
170
- constraint.equals(value)
171
- end
172
-
173
- target.add_constraints([constraint])
174
- return constraint
76
+ target_constraint(:trailing, :gte, value)
175
77
  end
176
78
 
177
79
  def max_trailing(value=nil)
178
- constraint = Constraint.new(target.view, :trailing, :lte)
179
-
180
- if value
181
- constraint.equals(value)
182
- end
183
-
184
- target.add_constraints([constraint])
185
- return constraint
80
+ target_constraint(:trailing, :lte, value)
186
81
  end
187
82
 
188
83
  def y(value=nil)
189
- constraint = Constraint.new(target.view, :top, :equal)
190
-
191
- if value
192
- constraint.equals(value)
193
- end
194
-
195
- target.add_constraints([constraint])
196
- return constraint
84
+ target_constraint(:top, :equal, value)
197
85
  end
198
86
  alias top y
199
87
 
200
88
  def min_y(value=nil)
201
- constraint = Constraint.new(target.view, :top, :gte)
202
-
203
- if value
204
- constraint.equals(value)
205
- end
206
-
207
- target.add_constraints([constraint])
208
- return constraint
89
+ target_constraint(:top, :gte, value)
209
90
  end
210
91
  alias min_top min_y
211
92
 
212
93
  def max_y(value=nil)
213
- constraint = Constraint.new(target.view, :top, :lte)
214
-
215
- if value
216
- constraint.equals(value)
217
- end
218
-
219
- target.add_constraints([constraint])
220
- return constraint
94
+ target_constraint(:top, :lte, value)
221
95
  end
222
96
  alias max_top max_y
223
97
 
224
98
  def center_y(value=nil)
225
- constraint = Constraint.new(target.view, :center_y, :equal)
226
-
227
- if value
228
- constraint.equals(value)
229
- end
230
-
231
- target.add_constraints([constraint])
232
- return constraint
99
+ target_constraint(:center_y, :equal, value)
233
100
  end
234
101
 
235
102
  def min_center_y(value=nil)
236
- constraint = Constraint.new(target.view, :center_y, :gte)
237
-
238
- if value
239
- constraint.equals(value)
240
- end
241
-
242
- target.add_constraints([constraint])
243
- return constraint
103
+ target_constraint(:center_y, :gte, value)
244
104
  end
245
105
 
246
106
  def max_center_y(value=nil)
247
- constraint = Constraint.new(target.view, :center_y, :lte)
248
-
249
- if value
250
- constraint.equals(value)
251
- end
252
-
253
- target.add_constraints([constraint])
254
- return constraint
107
+ target_constraint(:center_y, :lte, value)
255
108
  end
256
109
 
257
110
  def bottom(value=nil)
258
- constraint = Constraint.new(target.view, :bottom, :equal)
259
-
260
- if value
261
- constraint.equals(value)
262
- end
263
-
264
- target.add_constraints([constraint])
265
- return constraint
111
+ target_constraint(:bottom, :equal, value)
266
112
  end
267
113
 
268
114
  def min_bottom(value=nil)
269
- constraint = Constraint.new(target.view, :bottom, :gte)
270
-
271
- if value
272
- constraint.equals(value)
273
- end
274
-
275
- target.add_constraints([constraint])
276
- return constraint
115
+ target_constraint(:bottom, :gte, value)
277
116
  end
278
117
 
279
118
  def max_bottom(value=nil)
280
- constraint = Constraint.new(target.view, :bottom, :lte)
281
-
282
- if value
283
- constraint.equals(value)
284
- end
285
-
286
- target.add_constraints([constraint])
287
- return constraint
119
+ target_constraint(:bottom, :lte, value)
288
120
  end
289
121
 
290
122
  def baseline(value=nil)
291
- constraint = Constraint.new(target.view, :baseline, :equal)
292
-
293
- if value
294
- constraint.equals(value)
295
- end
296
-
297
- target.add_constraints([constraint])
298
- return constraint
123
+ target_constraint(:baseline, :equal, value)
299
124
  end
300
125
 
301
126
  def min_baseline(value=nil)
302
- constraint = Constraint.new(target.view, :baseline, :gte)
303
-
304
- if value
305
- constraint.equals(value)
306
- end
307
-
308
- target.add_constraints([constraint])
309
- return constraint
127
+ target_constraint(:baseline, :gte, value)
310
128
  end
311
129
 
312
130
  def max_baseline(value=nil)
313
- constraint = Constraint.new(target.view, :baseline, :lte)
314
-
315
- if value
316
- constraint.equals(value)
317
- end
318
-
319
- target.add_constraints([constraint])
320
- return constraint
131
+ target_constraint(:baseline, :lte, value)
321
132
  end
322
133
 
323
134
  def width(value=nil)
324
- constraint = Constraint.new(target.view, :width, :equal)
325
-
326
- if value
327
- constraint.equals(value)
328
- end
329
-
330
- target.add_constraints([constraint])
331
- return constraint
135
+ target_constraint(:width, :equal, value)
332
136
  end
333
137
  alias w width
334
138
 
335
139
  def min_width(value=nil)
336
- constraint = Constraint.new(target.view, :width, :gte)
337
-
338
- if value
339
- constraint.equals(value)
340
- end
341
-
342
- target.add_constraints([constraint])
343
- return constraint
140
+ target_constraint(:width, :gte, value)
344
141
  end
345
142
 
346
143
  def max_width(value=nil)
347
- constraint = Constraint.new(target.view, :width, :lte)
348
-
349
- if value
350
- constraint.equals(value)
351
- end
352
-
353
- target.add_constraints([constraint])
354
- return constraint
144
+ target_constraint(:width, :lte, value)
355
145
  end
356
146
 
357
147
  def height(value=nil)
358
- constraint = Constraint.new(target.view, :height, :equal)
359
-
360
- if value
361
- constraint.equals(value)
362
- end
363
-
364
- target.add_constraints([constraint])
365
- return constraint
148
+ target_constraint(:height, :equal, value)
366
149
  end
367
150
  alias h height
368
151
 
369
152
  def min_height(value=nil)
370
- constraint = Constraint.new(target.view, :height, :gte)
371
-
372
- if value
373
- constraint.equals(value)
374
- end
375
-
376
- target.add_constraints([constraint])
377
- return constraint
153
+ target_constraint(:height, :gte, value)
378
154
  end
379
155
 
380
156
  def max_height(value=nil)
381
- constraint = Constraint.new(target.view, :height, :lte)
382
-
383
- if value
384
- constraint.equals(value)
385
- end
386
-
387
- target.add_constraints([constraint])
388
- return constraint
157
+ target_constraint(:height, :lte, value)
389
158
  end
390
159
 
391
160
  def size(value=nil)
392
- constraint = SizeConstraint.new(target.view, :size, :equal)
393
-
394
- if value
395
- constraint.equals(value)
396
- end
397
-
398
- target.add_constraints([constraint])
399
- return constraint
161
+ target_constraint(:size, :equal, value, SizeConstraint)
400
162
  end
401
163
 
402
164
  def min_size(value=nil)
403
- constraint = SizeConstraint.new(target.view, :size, :gte)
404
-
405
- if value
406
- constraint.equals(value)
407
- end
408
-
409
- target.add_constraints([constraint])
410
- return constraint
165
+ target_constraint(:size, :gte, value, SizeConstraint)
411
166
  end
412
167
 
413
168
  def max_size(value=nil)
414
- constraint = SizeConstraint.new(target.view, :size, :lte)
415
-
416
- if value
417
- constraint.equals(value)
418
- end
419
-
420
- target.add_constraints([constraint])
421
- return constraint
169
+ target_constraint(:size, :lte, value, SizeConstraint)
422
170
  end
423
171
 
424
172
  def center(value=nil)
425
- constraint = PointConstraint.new(target.view, [:center_x, :center_y], :equal)
426
-
427
- if value
428
- constraint.equals(value)
429
- end
430
-
431
- target.add_constraints([constraint])
432
- return constraint
173
+ target_constraint([:center_x, :center_y], :equal, value, PointConstraint)
433
174
  end
434
175
 
435
176
  def min_center(value=nil)
436
- constraint = PointConstraint.new(target.view, [:center_x, :center_y], :gte)
437
-
438
- if value
439
- constraint.equals(value)
440
- end
441
-
442
- target.add_constraints([constraint])
443
- return constraint
177
+ target_constraint([:center_x, :center_y], :gte, value, PointConstraint)
444
178
  end
445
179
 
446
180
  def max_center(value=nil)
447
- constraint = PointConstraint.new(target.view, [:center_x, :center_y], :lte)
448
-
449
- if value
450
- constraint.equals(value)
451
- end
452
-
453
- target.add_constraints([constraint])
454
- return constraint
181
+ target_constraint([:center_x, :center_y], :lte, value, PointConstraint)
455
182
  end
456
183
 
457
184
  def top_left(value=nil)
458
- constraint = PointConstraint.new(target.view, [:left, :top], :equal)
459
-
460
- if value
461
- constraint.equals(value)
462
- end
463
-
464
- target.add_constraints([constraint])
465
- return constraint
185
+ target_constraint([:left, :top], :equal, value, PointConstraint)
466
186
  end
467
187
  alias origin top_left
468
188
 
469
189
  def top_right(value=nil)
470
- constraint = PointConstraint.new(target.view, [:right, :top], :equal)
471
-
472
- if value
473
- constraint.equals(value)
474
- end
475
-
476
- target.add_constraints([constraint])
477
- return constraint
190
+ target_constraint([:right, :top], :equal, value, PointConstraint)
478
191
  end
479
192
 
480
193
  def bottom_left(value=nil)
481
- constraint = PointConstraint.new(target.view, [:left, :bottom], :equal)
482
-
483
- if value
484
- constraint.equals(value)
485
- end
486
-
487
- target.add_constraints([constraint])
488
- return constraint
194
+ target_constraint([:left, :bottom], :equal, value, PointConstraint)
489
195
  end
490
196
 
491
197
  def bottom_right(value=nil)
492
- constraint = PointConstraint.new(target.view, [:right, :bottom], :equal)
493
-
494
- if value
495
- constraint.equals(value)
496
- end
497
-
498
- target.add_constraints([constraint])
499
- return constraint
198
+ target_constraint([:right, :bottom], :equal, value, PointConstraint)
500
199
  end
501
200
 
502
201
  def above(view)
@@ -533,5 +232,15 @@ module MotionKit
533
232
  end
534
233
  alias right_of after
535
234
 
235
+ private
236
+
237
+ def target_constraint(attribute, relationship, value=nil, constraint_class=nil)
238
+ constraint_class ||= Constraint
239
+ constraint = constraint_class.new(target.view, attribute, relationship)
240
+ constraint.equals(value) if value
241
+ target.add_constraints([constraint])
242
+ constraint
243
+ end
244
+
536
245
  end
537
246
  end
@@ -4,7 +4,9 @@ module MotionKit
4
4
 
5
5
  # useful when writing menus
6
6
  def app_name
7
- NSBundle.mainBundle.infoDictionary['CFBundleName']
7
+ # this returns an ImmediateRef, whatever that is. It needs to be
8
+ # converted to a String.
9
+ "#{NSBundle.mainBundle.infoDictionary['CFBundleName']}"
8
10
  end
9
11
 
10
12
  def _menu_title_and_options(title, options, default_title=nil, default_options={})
@@ -0,0 +1,9 @@
1
+ # @requires MotionKit::ViewCalculator
2
+ module MotionKit
3
+ module_function
4
+
5
+ def calculate(view, dimension, amount, full_view=nil)
6
+ ViewCalculator.new.calculate(view, dimension, amount, full_view)
7
+ end
8
+
9
+ end
@@ -0,0 +1,28 @@
1
+ # @provides MotionKit::Calculator
2
+ module MotionKit
3
+ class Calculator
4
+ attr_accessor :factor, :constant
5
+
6
+ def self.memo
7
+ @memo ||= {}
8
+ end
9
+
10
+ def self.scan(amount)
11
+ amount = amount.gsub(' ', '')
12
+ Calculator.memo[amount] ||= Calculator.new(amount)
13
+ end
14
+
15
+ def initialize(amount)
16
+ location = amount.index '%'
17
+ if location
18
+ self.factor = amount.slice(0, location).to_f / 100.0
19
+ location += 1
20
+ else
21
+ self.factor = 0
22
+ location = 0
23
+ end
24
+ self.constant = amount.slice(location, amount.size).to_f
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,65 @@
1
+ # @provides module:MotionKit::FrameCalculator
2
+ module MotionKit
3
+
4
+ module FrameCalculator
5
+
6
+ def calculate_frame(view, amount, full_view)
7
+ if amount.is_a?(Symbol)
8
+ calculate_frame_from_symbol(view, amount, full_view)
9
+ elsif amount.is_a?(Array)
10
+ calculate_frame_from_array(view, amount, full_view)
11
+ elsif amount.is_a?(Hash)
12
+ calculate_frame_from_hash(view, amount, full_view)
13
+ else
14
+ return amount
15
+ end
16
+ end
17
+
18
+ def calculate_frame_from_symbol(view, amount, full_view)
19
+ case amount
20
+ when :full, :auto
21
+ size = calculate_size(view, amount, full_view)
22
+ origin = [0, 0]
23
+ when :center
24
+ size = view.frame.size
25
+ origin = calculate_origin(view, :center, ['50%', '50%'], size, full_view)
26
+ origin.x -= size.width / 2.0
27
+ origin.y -= size.height / 2.0
28
+ else
29
+ raise "Unrecognized amount symbol #{amount.inspect} in MotionKit#calculate_frame"
30
+ end
31
+
32
+ return CGRect.new(origin, size)
33
+ end
34
+
35
+ def calculate_frame_from_array(view, amount, full_view)
36
+ if amount.length == 2
37
+ size = calculate_size(view, amount[1], full_view)
38
+ origin = calculate_origin(view, :origin, amount[0], size, full_view)
39
+ elsif amount.length == 4
40
+ size = calculate_size(view, [amount[2], amount[3]], full_view)
41
+ origin = calculate_origin(view, :origin, [amount[0], amount[1]], size, full_view)
42
+ else
43
+ raise "Don't know what to do with frame value #{amount.inspect}"
44
+ end
45
+
46
+ return CGRect.new(origin, size)
47
+ end
48
+
49
+ def calculate_frame_from_hash(view, amount, full_view)
50
+ size = calculate_size(view, (amount[:size] || amount), full_view)
51
+
52
+ if amount.key?(:center)
53
+ origin = calculate_origin(view, :center, amount[:center], size, full_view)
54
+ origin.x -= size.width / 2
55
+ origin.y -= size.height / 2
56
+ else
57
+ origin = calculate_origin(view, :origin, (amount[:origin] || amount), size, full_view)
58
+ end
59
+
60
+ return CGRect.new(origin, size)
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,90 @@
1
+ # @provides module:MotionKit::OriginCalculator
2
+ module MotionKit
3
+
4
+ module OriginCalculator
5
+
6
+ def calculate_origin(view, dimension, amount, my_size=nil, full_view)
7
+ my_size ||= view.frame.size
8
+ if amount == :center
9
+ calculate_from_center(view, dimension, amount, my_size, full_view)
10
+ elsif amount.is_a?(Array) || amount.is_a?(Hash)
11
+ calculate_from_hash_array(view, dimension, amount, my_size, full_view)
12
+ else
13
+ return amount
14
+ end
15
+ end
16
+
17
+ def calculate_from_center(view, dimension, amount, my_size, full_view)
18
+ x = calculate(view, :width, '50%', full_view) - my_size.width / 2.0
19
+ y = calculate(view, :height, '50%', full_view) - my_size.height / 2.0
20
+ return CGPoint.new(x, y)
21
+ end
22
+
23
+ def calculate_from_hash_array(view, dimension, amount, my_size, full_view)
24
+ x, y, x_offset, y_offset = calculate_from_hash(view, dimension, amount, my_size)
25
+ x = calculate(view, :width, x, full_view) + (x_offset || 0)
26
+ y = calculate(view, :height, y, full_view) + (y_offset || 0)
27
+ return CGPoint.new(x, y)
28
+ end
29
+
30
+ def calculate_from_hash(view, dimension, amount, my_size)
31
+ return amount[0], amount[1], nil, nil if amount.is_a?(Array)
32
+ if amount[:relative]
33
+ calculate_relative_from_hash(view, dimension, amount, my_size)
34
+ else
35
+ calculate_absolute_from_hash(view, dimension, amount, my_size)
36
+ end
37
+ end
38
+
39
+ def calculate_relative_from_hash(view, dimension, amount, my_size)
40
+ x = amount[:x] || begin
41
+ x = view.frame.origin.x
42
+ x += my_size.width / 2.0 if dimension == :center
43
+ x
44
+ end
45
+
46
+ y = amount[:y] || begin
47
+ y = view.frame.origin.y
48
+ y += my_size.height / 2.0 if dimension == :center
49
+ y
50
+ end
51
+
52
+ x_offset = amount[:right] if amount.key?(:right)
53
+ x_offset = -amount[:left] if amount.key?(:left)
54
+
55
+ y_offset = amount[:down] if amount.key?(:down)
56
+ y_offset = -amount[:up] if amount.key?(:up)
57
+ y_offset = -y_offset if amount[:flipped]
58
+
59
+ return x, y, x_offset, y_offset
60
+ end
61
+
62
+ def calculate_absolute_from_hash(view, dimension, amount, my_size)
63
+ if amount.key?(:right)
64
+ x_offset = -my_size.width
65
+ x = amount[:right]
66
+ elsif amount.key?(:x) || amount.key?(:left)
67
+ x = amount[:x] || amount[:left]
68
+ elsif dimension == :center
69
+ x = view.frame.origin.x + (my_size.width / 2)
70
+ else
71
+ x = view.frame.origin.x
72
+ end
73
+
74
+ if amount.key?(:bottom)
75
+ y_offset = -my_size.height
76
+ y = amount[:bottom]
77
+ elsif amount.key?(:y) || amount.key?(:top)
78
+ y = amount[:y] || amount[:top]
79
+ elsif dimension == :center
80
+ y = view.frame.origin.y + (my_size.height / 2)
81
+ else
82
+ y = view.frame.origin.y
83
+ end
84
+
85
+ return x, y, x_offset, y_offset
86
+ end
87
+
88
+ end
89
+
90
+ end
@@ -0,0 +1,65 @@
1
+ # @provides module:MotionKit::SizeCalculator
2
+ module MotionKit
3
+
4
+ module SizeCalculator
5
+
6
+ def calculate_size(view, amount, full_view)
7
+ if amount.is_a?(Array) || amount.is_a?(Hash)
8
+ calculate_size_from_multiple(view, amount, full_view)
9
+ elsif amount == :full
10
+ w = calculate(view, :width, '100%', full_view)
11
+ h = calculate(view, :height, '100%', full_view)
12
+ return CGSize.new(w, h)
13
+ elsif amount == :auto
14
+ return intrinsic_size(view)
15
+ elsif amount.is_a?(Symbol)
16
+ raise "Unrecognized amount symbol #{amount.inspect} in MotionKit#calculate_size"
17
+ else
18
+ return amount
19
+ end
20
+ end
21
+
22
+ def calculate_size_from_multiple(view, amount, full_view)
23
+ w, h = resolve_width_and_height(view, amount)
24
+
25
+ if w == :scale || h == :scale
26
+ w, h = calculate_scaled_width_height(w, h, view, full_view)
27
+ else
28
+ w = calculate(view, :width, w, full_view)
29
+ h = calculate(view, :height, h, full_view)
30
+ end
31
+
32
+ return CGSize.new(w, h)
33
+ end
34
+
35
+ def calculate_scaled_width_height(w, h, view, full_view)
36
+ # scaling is handled a little differently, because it is dependent on
37
+ # the other amount and the intrinsic size.
38
+ if w == :scale && h == :scale
39
+ raise "Either width or height can be :scale, but not both"
40
+ elsif w == :scale
41
+ h = calculate(view, :height, h, full_view)
42
+ size = intrinsic_size(view)
43
+ w = h * size.width / size.height
44
+ elsif h == :scale
45
+ w = calculate(view, :width, w, full_view)
46
+ size = intrinsic_size(view)
47
+ h = w * size.height / size.width
48
+ end
49
+ return w, h
50
+ end
51
+
52
+ def resolve_width_and_height(view, amount)
53
+ if amount.is_a?(Hash)
54
+ w = amount[:w] || amount[:width] || view.frame.size.width
55
+ h = amount[:h] || amount[:height] || view.frame.size.height
56
+ else
57
+ w = amount[0]
58
+ h = amount[1]
59
+ end
60
+ return w, h
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,51 @@
1
+ # @requires MotionKit::Calculator
2
+ # @requires module:MotionKit::OriginCalculator
3
+ # @requires module:MotionKit::SizeCalculator
4
+ # @requires module:MotionKit::FrameCalculator
5
+ # @provides MotionKit::ViewCalculator
6
+ module MotionKit
7
+ class ViewCalculator
8
+ include MotionKit::OriginCalculator
9
+ include MotionKit::SizeCalculator
10
+ include MotionKit::FrameCalculator
11
+
12
+ def calculate(view, dimension, amount, full_view=nil)
13
+ if amount.is_a? Proc
14
+ return view.instance_exec(&amount)
15
+ elsif dimension == :origin || dimension == :center
16
+ return calculate_origin(view, dimension, amount, nil, full_view)
17
+ elsif dimension == :size
18
+ return calculate_size(view, amount, full_view)
19
+ elsif dimension == :frame
20
+ return calculate_frame(view, amount, full_view)
21
+ elsif amount == :full
22
+ return calculate(view, dimension, '100%', full_view)
23
+ elsif amount == :auto
24
+ return intrinsic_size(view).send(dimension) # :left or :right
25
+ elsif amount.is_a?(String) && amount.include?('%')
26
+ full_view ||= view.superview || begin
27
+ raise NoSuperviewError.new("Cannot calculate #{amount}% of #{dimension.inspect} because view #{view} has no superview.")
28
+ end
29
+ calc = Calculator.scan(amount)
30
+
31
+ raise "Unknown dimension #{dimension}" unless [:width, :height].include?(dimension)
32
+ return (full_view.frame.size.send(dimension) * calc.factor + calc.constant).round
33
+ else
34
+ return amount
35
+ end
36
+ end
37
+
38
+ def intrinsic_size(view)
39
+ size_that_fits = view.intrinsicContentSize
40
+ if size_that_fits.width == MotionKit.no_intrinsic_metric
41
+ size_that_fits.width = 0
42
+ end
43
+ if size_that_fits.height == MotionKit.no_intrinsic_metric
44
+ size_that_fits.height = 0
45
+ end
46
+ return size_that_fits
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -1,3 +1,3 @@
1
1
  module MotionKit
2
- VERSION = '0.10.5'
2
+ VERSION = '0.10.6'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.5
4
+ version: 0.10.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin T.A. Gray
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-13 00:00:00.000000000 Z
12
+ date: 2014-06-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dbt
@@ -34,7 +34,12 @@ executables: []
34
34
  extensions: []
35
35
  extra_rdoc_files: []
36
36
  files:
37
- - lib/motion-kit/calculate.rb
37
+ - lib/motion-kit/calculator/calculate.rb
38
+ - lib/motion-kit/calculator/calculator.rb
39
+ - lib/motion-kit/calculator/frame_calculator.rb
40
+ - lib/motion-kit/calculator/origin_calculator.rb
41
+ - lib/motion-kit/calculator/size_calculator.rb
42
+ - lib/motion-kit/calculator/view_calculator.rb
38
43
  - lib/motion-kit/layouts/base_layout.rb
39
44
  - lib/motion-kit/layouts/base_layout_class_methods.rb
40
45
  - lib/motion-kit/layouts/parent.rb
@@ -1,263 +0,0 @@
1
- # @provides MotionKit::Calculator
2
- module MotionKit
3
- module_function
4
-
5
- def calculate(view, dimension, amount, full_view=nil)
6
- if amount.is_a? Proc
7
- return view.instance_exec(&amount)
8
- elsif dimension == :origin || dimension == :center
9
- return calculate_origin(view, dimension, amount, nil, full_view)
10
- elsif dimension == :size
11
- return calculate_size(view, amount, full_view)
12
- elsif dimension == :frame
13
- return calculate_frame(view, amount, full_view)
14
- elsif amount == :full
15
- return calculate(view, dimension, '100%', full_view)
16
- elsif amount == :auto
17
- size_that_fits = intrinsic_size(view)
18
-
19
- return case dimension
20
- when :width
21
- return size_that_fits.width
22
- when :height
23
- return size_that_fits.height
24
- end
25
- elsif amount.is_a?(String) && amount.include?('%')
26
- full_view ||= view.superview
27
- unless full_view
28
- raise NoSuperviewError.new("Cannot calculate #{amount}% of #{dimension.inspect} because view #{view} has no superview.")
29
- end
30
- calc = Calculator.scan(amount)
31
-
32
- factor = calc.factor
33
- constant = calc.constant
34
-
35
- return case dimension
36
- when :width
37
- return (full_view.frame.size.width * factor + constant).round
38
- when :height
39
- return (full_view.frame.size.height * factor + constant).round
40
- else
41
- raise "Unknown dimension #{dimension}"
42
- end
43
- else
44
- return amount
45
- end
46
- end
47
-
48
- def calculate_origin(view, dimension, amount, my_size=nil, full_view)
49
- my_size ||= view.frame.size
50
-
51
- if amount == :center
52
- x = calculate(view, :width, '50%', full_view) - my_size.width / 2.0
53
- y = calculate(view, :height, '50%', full_view) - my_size.height / 2.0
54
- return CGPoint.new(x, y)
55
- elsif amount.is_a?(Array) || amount.is_a?(Hash)
56
- x_offset = 0
57
- y_offset = 0
58
-
59
- if amount.is_a?(Hash)
60
- if amount[:relative]
61
- if amount.key?(:x)
62
- x = amount[:x]
63
- else
64
- x = view.frame.origin.x
65
- if dimension == :center
66
- x += my_size.width / 2.0
67
- end
68
- end
69
-
70
- if amount.key?(:y)
71
- y = amount[:y]
72
- else
73
- y = view.frame.origin.y
74
- if dimension == :center
75
- y += my_size.height / 2.0
76
- end
77
- end
78
-
79
- if amount.key?(:right)
80
- x_offset = amount[:right]
81
- elsif amount.key?(:left)
82
- x_offset = -amount[:left]
83
- end
84
-
85
- if amount.key?(:down)
86
- y_offset = amount[:down]
87
- elsif amount.key?(:up)
88
- y_offset = -amount[:up]
89
- end
90
- y_offset = -y_offset if amount[:flipped]
91
- else
92
- if amount.key?(:right)
93
- x_offset = -my_size.width
94
- x = amount[:right]
95
- elsif amount.key?(:x) || amount.key?(:left)
96
- x = amount[:x] || amount[:left]
97
- elsif dimension == :center
98
- x = view.frame.origin.x
99
- x += my_size.width / 2
100
- else
101
- x = view.frame.origin.x
102
- end
103
-
104
- if amount.key?(:bottom)
105
- y_offset = -my_size.height
106
- y = amount[:bottom]
107
- elsif amount.key?(:y) || amount.key?(:top)
108
- y = amount[:y] || amount[:top]
109
- elsif dimension == :center
110
- y = view.frame.origin.y
111
- y += my_size.height / 2
112
- else
113
- y = view.frame.origin.y
114
- end
115
- end
116
- else
117
- x = amount[0]
118
- y = amount[1]
119
- end
120
-
121
- x = calculate(view, :width, x, full_view) + x_offset
122
- y = calculate(view, :height, y, full_view) + y_offset
123
- return CGPoint.new(x, y)
124
- else
125
- return amount
126
- end
127
- end
128
-
129
- def calculate_size(view, amount, full_view)
130
- if amount.is_a?(Array) || amount.is_a?(Hash)
131
- if amount.is_a?(Hash)
132
- w = amount.fetch(:w, amount.fetch(:width, view.frame.size.width))
133
- h = amount.fetch(:h, amount.fetch(:height, view.frame.size.height))
134
- else
135
- w = amount[0]
136
- h = amount[1]
137
- end
138
-
139
- # scaling is handled a little differently, because it is dependent on
140
- # the other amount and the intrinsic size.
141
- if w == :scale || h == :scale
142
- if w == :scale && h == :scale
143
- raise "Either width or height can be :scale, but not both"
144
- elsif w == :scale
145
- h = calculate(view, :height, h, full_view)
146
- size = intrinsic_size(view)
147
- w = h * size.width / size.height
148
- elsif h == :scale
149
- w = calculate(view, :width, w, full_view)
150
- size = intrinsic_size(view)
151
- h = w * size.height / size.width
152
- end
153
- else
154
- w = calculate(view, :width, w, full_view)
155
- h = calculate(view, :height, h, full_view)
156
- end
157
-
158
- return CGSize.new(w, h)
159
- elsif amount == :full
160
- w = calculate(view, :width, '100%', full_view)
161
- h = calculate(view, :height, '100%', full_view)
162
- return CGSize.new(w, h)
163
- elsif amount == :auto
164
- return intrinsic_size(view)
165
- elsif amount.is_a?(Symbol)
166
- raise "Unrecognized amount symbol #{amount.inspect} in MotionKit#calculate_size"
167
- else
168
- return amount
169
- end
170
- end
171
-
172
- def calculate_frame(view, amount, full_view)
173
- if amount.is_a?(Symbol)
174
- case amount
175
- when :full, :auto
176
- size = calculate_size(view, amount, full_view)
177
- origin = [0, 0]
178
- when :center
179
- size = view.frame.size
180
- origin = calculate_origin(view, :center, ['50%', '50%'], size, full_view)
181
- origin.x -= size.width / 2.0
182
- origin.y -= size.height / 2.0
183
- else
184
- raise "Unrecognized amount symbol #{amount.inspect} in MotionKit#calculate_frame"
185
- end
186
-
187
- return CGRect.new(origin, size)
188
- elsif amount.is_a?(Array)
189
- if amount.length == 2
190
- size = calculate_size(view, amount[1], full_view)
191
- origin = calculate_origin(view, :origin, amount[0], size, full_view)
192
- elsif amount.length == 4
193
- size = calculate_size(view, [amount[2], amount[3]], full_view)
194
- origin = calculate_origin(view, :origin, [amount[0], amount[1]], size, full_view)
195
- else
196
- raise "Don't know what to do with frame value #{amount.inspect}"
197
- end
198
-
199
- return CGRect.new(origin, size)
200
- elsif amount.is_a?(Hash)
201
- if amount.key?(:size)
202
- size = calculate_size(view, amount[:size], full_view)
203
- else
204
- size = calculate_size(view, amount, full_view)
205
- end
206
-
207
- if amount.key?(:center)
208
- origin = calculate_origin(view, :center, amount[:center], size, full_view)
209
- origin.x -= size.width / 2
210
- origin.y -= size.height / 2
211
- elsif amount.key?(:origin)
212
- origin = calculate_origin(view, :origin, amount[:origin], size, full_view)
213
- else
214
- origin = calculate_origin(view, :origin, amount, size, full_view)
215
- end
216
-
217
- return CGRect.new(origin, size)
218
- else
219
- return amount
220
- end
221
- end
222
-
223
- def intrinsic_size(view)
224
- size_that_fits = view.intrinsicContentSize
225
- if size_that_fits.width == MotionKit.no_intrinsic_metric
226
- size_that_fits.width = 0
227
- end
228
- if size_that_fits.height == MotionKit.no_intrinsic_metric
229
- size_that_fits.height = 0
230
- end
231
- return size_that_fits
232
- end
233
-
234
- class Calculator
235
- attr_accessor :factor, :constant
236
-
237
- def self.memo
238
- @memo ||= {}
239
- end
240
-
241
- def self.scan(amount)
242
- amount = amount.gsub(' ', '')
243
- return Calculator.memo[amount] if Calculator.memo[amount]
244
-
245
- calc = Calculator.new
246
-
247
- location = amount.index '%'
248
- if location
249
- calc.factor = amount.slice(0, location).to_f / 100.0
250
- location += 1
251
- else
252
- calc.factor = 0
253
- location = 0
254
- end
255
- calc.constant = amount.slice(location, amount.size).to_f
256
-
257
- Calculator.memo[amount] = calc
258
- return calc
259
- end
260
-
261
- end
262
-
263
- end