motion-kit 0.14.1 → 0.14.2

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: 18f1f9da189410e1683214e3227a873166251cd7
4
- data.tar.gz: 9acef866d767505e18fe4dc7defa0cc0cb41727d
3
+ metadata.gz: 4aef617ad6abab23df20a00836c0189ae5f7d962
4
+ data.tar.gz: 5795cf1d58ea7c33a713b0f964dd812bdccc57fc
5
5
  SHA512:
6
- metadata.gz: be182328f8a528833582295c4d189c6985755742165e3d708f7f9aca15532bfb7fc7c15c70285e018c0edf57c515a8637abb1658e06b1db25b70c71205a32edb
7
- data.tar.gz: 749585a9301994dea5f870876dd2fa9980eee29cfaf2d6e3a1a9f274d0f3ca2a8be820acfd82785b5fa878915d130003a71aad76ca3baafffda4d14be3f21196
6
+ metadata.gz: 99f22fcf82ab5a2dda61e570725cd0450455512ad99366595d4119cf39c94ed6b59981e5b7adc9579568116a3fc900b2feaf3dd72f3566a88ad857064ada680b
7
+ data.tar.gz: 349f157dac34e5b4aa2ef0ce58776a75b6b931d0668de50f1e597747051510af00f15cfe1b846fec04734c5350f4e46047890de6f59b89e327708f5acb525026
data/README.md CHANGED
@@ -598,7 +598,7 @@ pin_to_center: View stays centered, size does not change.
598
598
  pin_to_right: View stays centered on the right, size does not change.
599
599
  pin_to_bottom_left: View stays in bottom-left corner, size does not change.
600
600
  pin_to_bottom: View stays in bottom-center, size does not change.
601
- pin_to_bottom_right: View stays in bottom-left corner, size does not change.
601
+ pin_to_bottom_right: View stays in bottom-right corner, size does not change.
602
602
  ```
603
603
 
604
604
 
@@ -23,8 +23,16 @@ module MotionKit
23
23
  elsif amount == :auto
24
24
  return intrinsic_size(view).send(dimension) # :left or :right
25
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.")
26
+ if not full_view
27
+ if view.is_a?(MotionKit.base_view_class)
28
+ full_view = view.superview
29
+ elsif view.is_a?(CALayer)
30
+ full_view = view.superlayer
31
+ end
32
+
33
+ if not full_view
34
+ raise NoSuperviewError.new("Cannot calculate #{amount.inspect} of #{dimension.inspect} because view #{view} has no superview.")
35
+ end
28
36
  end
29
37
  calc = Calculator.scan(amount)
30
38
 
@@ -0,0 +1,329 @@
1
+ # @requires MotionKit::CALayerHelpers
2
+ module MotionKit
3
+ class CALayerHelpers
4
+
5
+ def x(value)
6
+ f = target.frame
7
+ superview = target.superlayer || parent_layout.parent
8
+ f.origin.x = MotionKit.calculate(target, :width, value, superview)
9
+ target.frame = f
10
+ return CGRectGetMinX(f)
11
+ end
12
+ alias left x
13
+
14
+ def right(value)
15
+ f = target.frame
16
+ superview = target.superlayer || parent_layout.parent
17
+ f.origin.x = MotionKit.calculate(target, :width, value, superview) - f.size.width
18
+ target.frame = f
19
+ return CGRectGetMaxX(f)
20
+ end
21
+
22
+ def center_x(value)
23
+ c = target.center
24
+ superview = target.superlayer || parent_layout.parent
25
+ c.x = MotionKit.calculate(target, :width, value, superview)
26
+ target.center = c
27
+ return CGRectGetMidX(target.frame)
28
+ end
29
+ alias middle_x center_x
30
+
31
+ def y(value)
32
+ f = target.frame
33
+ superview = target.superlayer || parent_layout.parent
34
+ f.origin.y = MotionKit.calculate(target, :height, value, superview)
35
+ target.frame = f
36
+ return CGRectGetMinY(f)
37
+ end
38
+ alias top y
39
+
40
+ def bottom(value)
41
+ f = target.frame
42
+ superview = target.superlayer || parent_layout.parent
43
+ f.origin.y = MotionKit.calculate(target, :height, value, superview) - f.size.height
44
+ target.frame = f
45
+ return CGRectGetMaxY(f)
46
+ end
47
+
48
+ def center_y(value)
49
+ c = target.center
50
+ superview = target.superlayer || parent_layout.parent
51
+ c.y = MotionKit.calculate(target, :height, value, superview)
52
+ target.center = c
53
+ return CGRectGetMidY(target.frame)
54
+ end
55
+ alias middle_y center_y
56
+
57
+ def width(value)
58
+ f = target.frame
59
+ superview = target.superlayer || parent_layout.parent
60
+ f.size.width = MotionKit.calculate(target, :width, value, superview)
61
+ target.frame = f
62
+ return CGRectGetWidth(f)
63
+ end
64
+ alias w width
65
+
66
+ def height(value)
67
+ f = target.frame
68
+ superview = target.superlayer || parent_layout.parent
69
+ f.size.height = MotionKit.calculate(target, :height, value, superview)
70
+ target.frame = f
71
+ return CGRectGetHeight(f)
72
+ end
73
+ alias h height
74
+
75
+ def origin(value)
76
+ f = target.frame
77
+ superview = target.superlayer || parent_layout.parent
78
+ f.origin = MotionKit.calculate(target, :origin, value, superview)
79
+ target.frame = f
80
+ return target.frame.origin
81
+ end
82
+
83
+ def center(value)
84
+ superview = target.superlayer || parent_layout.parent
85
+ target.center = MotionKit.calculate(target, :center, value, superview)
86
+ return target.center
87
+ end
88
+ alias middle center
89
+
90
+ def size(value)
91
+ f = target.frame
92
+ superview = target.superlayer || parent_layout.parent
93
+ f.size = MotionKit.calculate(target, :size, value, superview)
94
+ target.frame = f
95
+ return target.frame.size
96
+ end
97
+
98
+ def frame(value)
99
+ superview = target.superlayer || parent_layout.parent
100
+ target.frame = MotionKit.calculate(target, :frame, value, superview)
101
+ return target.frame
102
+ end
103
+
104
+ def _calculate_frame(f, from: from_layer, relative_to: point)
105
+ if from_layer.is_a?(Symbol)
106
+ from_layer = self.get_view(from_layer)
107
+ end
108
+
109
+ from_layer_size = from_layer.frame.size
110
+ o = from_layer.convertPoint([0, 0], toLayer: target.superlayer)
111
+
112
+ calculate_layer = CALayer.layer
113
+ calculate_layer.frame = [[0, 0], target.frame.size]
114
+
115
+ if f.is_a?(Hash)
116
+ f = f.merge(relative: true)
117
+ end
118
+ f = MotionKit.calculate(calculate_layer, :frame, f, target.superlayer || parent_layout.parent)
119
+ f.origin.x += o.x
120
+ f.origin.y += o.y
121
+
122
+ case point[:x]
123
+ when :min, :reset
124
+ # pass
125
+ when :mid
126
+ f.origin.x += (from_layer_size.width - f.size.width) / 2.0
127
+ when :max
128
+ f.origin.x += from_layer_size.width - f.size.width
129
+ when :before
130
+ f.origin.x -= f.size.width
131
+ when :after
132
+ f.origin.x += from_layer_size.width
133
+ else
134
+ f.origin.x += point[:x]
135
+ end
136
+
137
+ case point[:y]
138
+ when :min, :reset
139
+ # pass
140
+ when :mid
141
+ f.origin.y += (from_layer_size.height - f.size.height) / 2.0
142
+ when :max
143
+ f.origin.y += from_layer_size.height - f.size.height
144
+ when :above
145
+ f.origin.y -= f.size.height
146
+ when :below
147
+ f.origin.y += from_layer_size.height
148
+ else
149
+ f.origin.y += point[:y]
150
+ end
151
+
152
+ return f
153
+ end
154
+
155
+ # The first arg can be a layer or a frame
156
+ # @example
157
+ # frame from_top_left(width: 80, height: 22)
158
+ # frame from_top_left(another_layer, width: 80, height: 22)
159
+ def from_top_left(from_layer=nil, f=nil)
160
+ if from_layer.is_a?(Hash)
161
+ f = from_layer
162
+ from_layer = nil
163
+ end
164
+ f ||= {}
165
+ from_layer ||= target.superlayer
166
+ _calculate_frame(f, from: from_layer, relative_to: { x: :min, y: :min })
167
+ end
168
+
169
+ # The first arg can be a layer or a frame
170
+ # @example
171
+ # frame from_top(width: 80, height: 22)
172
+ # frame from_top(another_layer, width: 80, height: 22)
173
+ def from_top(from_layer=nil, f=nil)
174
+ if from_layer.is_a?(Hash)
175
+ f = from_layer
176
+ from_layer = nil
177
+ end
178
+ f ||= {}
179
+ from_layer ||= target.superlayer
180
+ _calculate_frame(f, from: from_layer, relative_to: { x: :mid, y: :min })
181
+ end
182
+
183
+ # The first arg can be a layer or a frame
184
+ # @example
185
+ # frame from_top_right(width: 80, height: 22)
186
+ # frame from_top_right(another_layer, width: 80, height: 22)
187
+ def from_top_right(from_layer=nil, f=nil)
188
+ if from_layer.is_a?(Hash)
189
+ f = from_layer
190
+ from_layer = nil
191
+ end
192
+ f ||= {}
193
+ from_layer ||= target.superlayer
194
+ _calculate_frame(f, from: from_layer, relative_to: { x: :max, y: :min })
195
+ end
196
+
197
+ # The first arg can be a layer or a frame
198
+ # @example
199
+ # frame from_left(width: 80, height: 22)
200
+ # frame from_left(another_layer, width: 80, height: 22)
201
+ def from_left(from_layer=nil, f=nil)
202
+ if from_layer.is_a?(Hash)
203
+ f = from_layer
204
+ from_layer = nil
205
+ end
206
+ f ||= {}
207
+ from_layer ||= target.superlayer
208
+ _calculate_frame(f, from: from_layer, relative_to: { x: :min, y: :mid })
209
+ end
210
+
211
+ # The first arg can be a layer or a frame
212
+ # @example
213
+ # frame from_center(width: 80, height: 22)
214
+ # frame from_center(another_layer, width: 80, height: 22)
215
+ def from_center(from_layer=nil, f=nil)
216
+ if from_layer.is_a?(Hash)
217
+ f = from_layer
218
+ from_layer = nil
219
+ end
220
+ f ||= {}
221
+ from_layer ||= target.superlayer
222
+ _calculate_frame(f, from: from_layer, relative_to: { x: :mid, y: :mid })
223
+ end
224
+
225
+ # The first arg can be a layer or a frame
226
+ # @example
227
+ # frame from_right(width: 80, height: 22)
228
+ # frame from_right(another_layer, width: 80, height: 22)
229
+ def from_right(from_layer=nil, f=nil)
230
+ if from_layer.is_a?(Hash)
231
+ f = from_layer
232
+ from_layer = nil
233
+ end
234
+ f ||= {}
235
+ from_layer ||= target.superlayer
236
+ _calculate_frame(f, from: from_layer, relative_to: { x: :max, y: :mid })
237
+ end
238
+
239
+ # The first arg can be a layer or a frame
240
+ # @example
241
+ # frame from_bottom_left(width: 80, height: 22)
242
+ # frame from_bottom_left(another_layer, width: 80, height: 22)
243
+ def from_bottom_left(from_layer=nil, f=nil)
244
+ if from_layer.is_a?(Hash)
245
+ f = from_layer
246
+ from_layer = nil
247
+ end
248
+ f ||= {}
249
+ from_layer ||= target.superlayer
250
+ _calculate_frame(f, from: from_layer, relative_to: { x: :min, y: :max })
251
+ end
252
+
253
+ # The first arg can be a layer or a frame
254
+ # @example
255
+ # frame from_bottom(width: 80, height: 22)
256
+ # frame from_bottom(another_layer, width: 80, height: 22)
257
+ def from_bottom(from_layer=nil, f=nil)
258
+ if from_layer.is_a?(Hash)
259
+ f = from_layer
260
+ from_layer = nil
261
+ end
262
+ f ||= {}
263
+ from_layer ||= target.superlayer
264
+ _calculate_frame(f, from: from_layer, relative_to: { x: :mid, y: :max })
265
+ end
266
+
267
+ # The first arg can be a layer or a frame
268
+ # @example
269
+ # frame from_bottom_right(width: 80, height: 22)
270
+ # frame from_bottom_right(another_layer, width: 80, height: 22)
271
+ def from_bottom_right(from_layer=nil, f=nil)
272
+ if from_layer.is_a?(Hash)
273
+ f = from_layer
274
+ from_layer = nil
275
+ end
276
+ f ||= {}
277
+ from_layer ||= target.superlayer
278
+ _calculate_frame(f, from: from_layer, relative_to: { x: :max, y: :max })
279
+ end
280
+
281
+ # The first arg can be a layer or a frame
282
+ # @example
283
+ # frame above(layer, [[0, 0], [100, 20]])
284
+ # frame above(:layer, x: 0, y: 0, width: 100, height: 20)
285
+ # frame above(:layer, down: 0, right: 0, width: 100, height: 20)
286
+ def above(from_layer, f={})
287
+ _calculate_frame(f, from: from_layer, relative_to: { x: :reset, y: :above })
288
+ end
289
+
290
+ # The first arg can be a layer or a frame
291
+ # @example
292
+ # frame below(layer, [[0, 0], [100, 20]])
293
+ # frame below(:layer, x: 0, y: 0, width: 100, height: 20)
294
+ # frame below(:layer, down: 0, right: 0, width: 100, height: 20)
295
+ def below(from_layer, f={})
296
+ _calculate_frame(f, from: from_layer, relative_to: { x: :reset, y: :below })
297
+ end
298
+
299
+ # The first arg can be a layer or a frame
300
+ # @example
301
+ # frame before(layer, [[0, 0], [100, 20]])
302
+ # frame before(:layer, x: 0, y: 0, width: 100, height: 20)
303
+ # frame before(:layer, down: 0, right: 0, width: 100, height: 20)
304
+ def before(from_layer, f={})
305
+ _calculate_frame(f, from: from_layer, relative_to: { x: :before, y: :reset })
306
+ end
307
+ alias left_of before
308
+
309
+ # The first arg can be a layer or a frame
310
+ # @example
311
+ # frame after(layer, [[0, 0], [100, 20]])
312
+ # frame after(:layer, x: 0, y: 0, width: 100, height: 20)
313
+ # frame after(:layer, down: 0, right: 0, width: 100, height: 20)
314
+ def after(from_layer, f={})
315
+ _calculate_frame(f, from: from_layer, relative_to: { x: :after, y: :reset })
316
+ end
317
+ alias right_of after
318
+
319
+ # The first arg must be a layer
320
+ # @example
321
+ # frame relative_to(layer, [[0, 0], [100, 20]])
322
+ # frame relative_to(:layer, x: 0, y: 0, width: 100, height: 20)
323
+ # frame relative_to(:layer, down: 0, right: 0, width: 100, height: 20)
324
+ def relative_to(from_layer, f)
325
+ _calculate_frame(f, from: from_layer, relative_to: { x: :reset, y: :reset })
326
+ end
327
+
328
+ end
329
+ end
@@ -10,6 +10,10 @@ module MotionKit
10
10
  @element = element
11
11
  end
12
12
 
13
+ def frame
14
+ try(:frame)
15
+ end
16
+
13
17
  def origin
14
18
  try(:frame, :origin)
15
19
  end
@@ -1,3 +1,3 @@
1
1
  module MotionKit
2
- VERSION = '0.14.1'
2
+ VERSION = '0.14.2'
3
3
  end
@@ -1,10 +1,23 @@
1
- describe CALayer do
1
+ describe 'CALayerHelpers' do
2
2
 
3
3
  before do
4
+ @subject = TestCALayerLayout.new.build
4
5
  end
5
6
 
6
- it 'should style a CALayer' do
7
- 1.should == 1
7
+ describe 'should style a CALayer' do
8
+ describe 'should style :gradient layer' do
9
+ it 'should set the frame' do
10
+ @subject.get(:gradient).frame.should == CGRectMake(0, 0, 20, 20)
11
+ end
12
+ it 'should set the colors' do
13
+ @subject.get(:gradient).colors.length.should == 2
14
+ end
15
+ end
16
+ describe 'should style :bottom layer' do
17
+ it 'should set the frame' do
18
+ @subject.get(:bottom).frame.should == CGRectMake(0, 20, 20, 10)
19
+ end
20
+ end
8
21
  end
9
22
 
10
23
  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.14.1
4
+ version: 0.14.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin T.A. Gray
@@ -54,6 +54,7 @@ files:
54
54
  - lib/motion-kit-cocoa/constraints/constraints_target.rb
55
55
  - lib/motion-kit-cocoa/helpers/accessibility_compat.rb
56
56
  - lib/motion-kit-cocoa/helpers/cagradientlayer_helpers.rb
57
+ - lib/motion-kit-cocoa/helpers/calayer_frame_helpers.rb
57
58
  - lib/motion-kit-cocoa/helpers/calayer_helpers.rb
58
59
  - lib/motion-kit-cocoa/helpers/sugarcube_compat.rb
59
60
  - lib/motion-kit-ios/deprecated.rb