rm-extensions 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3b640dd2b89eae8bfcc443d4a5e5067abd283f3
4
- data.tar.gz: 26bb1864c3fc4f89cf09b39e858a92001df0ea1e
3
+ metadata.gz: bc5b5569839be0c1b41cfea9a8c319e8b34b1e74
4
+ data.tar.gz: 5ffb320a7f8e0bdc87c75b61238f4f2826d4a37c
5
5
  SHA512:
6
- metadata.gz: 17b76f012868bf23b795b02bda7b8ecb77907825e0a1b2684cbc101eb619c1c2ddaf68dda300eb21a140ab31299f596e2a3bf1dc93983641d9929b04d0672e49
7
- data.tar.gz: fd49633cff7b9fb77a9bd108c515ef3647054700b886e8a80cee317619be57109b91ff230428e48597a7bbc74098b84cf1750175e0205a25a81149adcc83836e
6
+ metadata.gz: f5c8699627b303137e762d883e77b6f9367ef192ff0a4795fbc7c1dbf202b2501a8490a274eb204aa57bbbb0cf8c3b410d731b5a1c45d3678b58b9a78d4f3c26
7
+ data.tar.gz: 3e1b1ea19d4a8e4846e6d0fba244658a9ebcff7ba426df55f051ed359a5d75a10f571b1f6bdd84ec6df4492d2f81a190baee95dac072c9e2a84aefbd4ed7e341
@@ -0,0 +1,228 @@
1
+ module RMExtensions
2
+ class Layout
3
+
4
+ ATTRIBUTE_LOOKUP = {
5
+ "left" => NSLayoutAttributeLeft,
6
+ "right" => NSLayoutAttributeRight,
7
+ "top" => NSLayoutAttributeTop,
8
+ "bottom" => NSLayoutAttributeBottom,
9
+ "leading" => NSLayoutAttributeLeading,
10
+ "trailing" => NSLayoutAttributeTrailing,
11
+ "width" => NSLayoutAttributeWidth,
12
+ "height" => NSLayoutAttributeHeight,
13
+ "centerX" => NSLayoutAttributeCenterX,
14
+ "centerY" => NSLayoutAttributeCenterY,
15
+ "baseline" => NSLayoutAttributeBaseline,
16
+ nil => NSLayoutAttributeNotAnAttribute
17
+ }
18
+
19
+ RELATED_BY_LOOKUP = {
20
+ "<=" => NSLayoutRelationLessThanOrEqual,
21
+ "==" => NSLayoutRelationEqual,
22
+ ">=" => NSLayoutRelationGreaterThanOrEqual
23
+ }
24
+
25
+ PRIORITY_LOOKUP = {
26
+ "required" => UILayoutPriorityRequired, # = 1000
27
+ "high" => UILayoutPriorityDefaultHigh, # = 750
28
+ "low" => UILayoutPriorityDefaultLow, # = 250
29
+ "fit" => UILayoutPriorityFittingSizeLevel # = 50
30
+ }
31
+
32
+ AXIS_LOOKUP = {
33
+ "h" => UILayoutConstraintAxisHorizontal,
34
+ "v" => UILayoutConstraintAxisVertical
35
+ }
36
+
37
+ def initialize
38
+ if block_given?
39
+ yield self
40
+ end
41
+ end
42
+
43
+ def view(view=nil)
44
+ if view
45
+ @view = view
46
+ end
47
+ @view
48
+ end
49
+
50
+ def subviews(subviews=nil)
51
+ if subviews
52
+ @subviews = subviews
53
+ @subviews.values.each do |subview|
54
+ subview.translatesAutoresizingMaskIntoConstraints = false
55
+ @view.addSubview(subview)
56
+ end
57
+ end
58
+ @subviews
59
+ end
60
+
61
+ def eqs(str, debug=false)
62
+ str.split("\n").map(&:strip).select { |x| !x.empty? }.map do |line|
63
+ eq(line, debug)
64
+ end
65
+ end
66
+
67
+ def eq?(str)
68
+ eq(str, true)
69
+ end
70
+
71
+ # Constraints are of the form "view1.attr1 <relation> view2.attr2 * multiplier + constant @ priority"
72
+ def eq(str, debug=false)
73
+ item = nil
74
+ item_attribute = nil
75
+ related_by = nil
76
+ to_item = nil
77
+ to_item_attribute = nil
78
+ multiplier = nil
79
+ constant = nil
80
+
81
+ parts = str.split(" ").select { |x| !x.empty? }
82
+
83
+ # first part should always be view1.attr1
84
+ part = parts.shift
85
+ item, item_attribute = part.split(".", 2)
86
+
87
+ # second part should always be relation
88
+ related_by = parts.shift
89
+
90
+ # now things get more complicated
91
+
92
+ # look for priority
93
+ if idx = parts.index("@")
94
+ priority = parts[idx + 1]
95
+ parts.delete_at(idx)
96
+ parts.delete_at(idx)
97
+ end
98
+
99
+ # look for negative or positive constant
100
+ if idx = parts.index("-")
101
+ constant = "-#{parts[idx + 1]}"
102
+ parts.delete_at(idx)
103
+ parts.delete_at(idx)
104
+ elsif idx = parts.index("+")
105
+ constant = parts[idx + 1]
106
+ parts.delete_at(idx)
107
+ parts.delete_at(idx)
108
+ end
109
+
110
+ # look for multipler
111
+ if idx = parts.index("*")
112
+ multipler = parts[idx + 1]
113
+ parts.delete_at(idx)
114
+ parts.delete_at(idx)
115
+ end
116
+
117
+ # now we need to_item, to_item_attribute
118
+
119
+ if part = parts.shift
120
+ # if part includes a . it could be either view2.attr2 or a float like 10.5
121
+ l, r = part.split(".", 2)
122
+ if !r || (r && r =~ /\d/)
123
+ # assume a solo constant was on the right side
124
+ constant = part
125
+ else
126
+ # assume view2.attr2
127
+ to_item, to_item_attribute = l, r
128
+ end
129
+ end
130
+
131
+ # if we dont have to_item and the item_attribute is something that requires a to_item, then
132
+ # assume superview
133
+ if !to_item
134
+ unless item_attribute == "height" || item_attribute == "width"
135
+ to_item = "view"
136
+ to_item_attribute = item_attribute
137
+ end
138
+ end
139
+
140
+ debug_hash = nil
141
+
142
+ if debug
143
+ debug_hash = {
144
+ :item => item,
145
+ :item_attribute => item_attribute,
146
+ :related_by => related_by,
147
+ :to_item => to_item,
148
+ :to_item_attribute => to_item_attribute,
149
+ :multiplier => multiplier,
150
+ :constant => constant,
151
+ :priority => priority
152
+ }
153
+ end
154
+
155
+ # normalize
156
+
157
+ res_item = item == "view" ? @view : @subviews[item]
158
+ res_item_attribute = ATTRIBUTE_LOOKUP[item_attribute]
159
+ res_related_by = RELATED_BY_LOOKUP[related_by]
160
+ res_to_item = if to_item
161
+ to_item == "view" ? @view : @subviews[to_item]
162
+ end
163
+ res_to_item_attribute = ATTRIBUTE_LOOKUP[to_item_attribute]
164
+ res_multiplier = multiplier ? Float(multiplier) : 1.0
165
+ res_constant = constant ? Float(PRIORITY_LOOKUP[constant] || constant) : 0.0
166
+ res_priority = priority ? Integer(PRIORITY_LOOKUP[priority] || priority) : nil
167
+
168
+ if res_item
169
+ case item_attribute
170
+ when "resistH"
171
+ return res_item.setContentCompressionResistancePriority(res_constant, forAxis:UILayoutConstraintAxisHorizontal)
172
+ when "resistV"
173
+ return res_item.setContentCompressionResistancePriority(res_constant, forAxis:UILayoutConstraintAxisVertical)
174
+ when "hugH"
175
+ return res_item.setContentHuggingPriority(res_constant, forAxis:UILayoutConstraintAxisHorizontal)
176
+ when "hugV"
177
+ return res_item.setContentHuggingPriority(res_constant, forAxis:UILayoutConstraintAxisVertical)
178
+ end
179
+ end
180
+
181
+ errors = []
182
+ errors.push("Invalid view1: #{item}") unless res_item
183
+ errors.push("Invalid attr1: #{item_attribute}") unless res_item_attribute
184
+ errors.push("Invalid relatedBy: #{related_by}") unless res_related_by
185
+ errors.push("Invalid view2: #{to_item}") if to_item && !res_to_item
186
+ errors.push("Invalid attr2: #{to_item_attribute}") unless res_to_item_attribute
187
+
188
+ if errors.size > 0 || debug
189
+ puts "======================== constraint debug ========================"
190
+ puts "given:"
191
+ puts " #{str}"
192
+ puts "interpreted:"
193
+ puts " item: #{item}"
194
+ puts " item_attribute: #{item_attribute}"
195
+ puts " related_by: #{related_by}"
196
+ puts " to_item: #{to_item}"
197
+ puts " to_item_attribute: #{to_item_attribute}"
198
+ puts " multiplier: #{multiplier}"
199
+ puts " constant: #{constant}"
200
+ puts " priority: #{priority}"
201
+ end
202
+
203
+ if errors.size > 0
204
+ raise(errors.join(", "))
205
+ end
206
+
207
+ constraint = NSLayoutConstraint.constraintWithItem(res_item,
208
+ attribute:res_item_attribute,
209
+ relatedBy:res_related_by,
210
+ toItem:res_to_item,
211
+ attribute:res_to_item_attribute,
212
+ multiplier:res_multiplier,
213
+ constant:res_constant)
214
+ if res_priority
215
+ constraint.priority = res_priority
216
+ end
217
+
218
+ if debug
219
+ puts "implemented:"
220
+ puts " #{constraint.description}"
221
+ end
222
+
223
+ @view.addConstraint(constraint)
224
+ constraint
225
+ end
226
+
227
+ end
228
+ end
@@ -1,3 +1,3 @@
1
1
  module RMExtensions
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/rm-extensions.rb CHANGED
@@ -6,6 +6,7 @@ end
6
6
 
7
7
  Motion::Project::App.setup do |app|
8
8
  %w(
9
+ layout
9
10
  util
10
11
  accessors
11
12
  observation
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rm-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Noon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-12 00:00:00.000000000 Z
11
+ date: 2013-08-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Extensions and helpers for dealing with various areas of rubymotion
14
14
  email:
@@ -23,6 +23,7 @@ files:
23
23
  - README.md
24
24
  - Rakefile
25
25
  - lib/motion/accessors.rb
26
+ - lib/motion/layout.rb
26
27
  - lib/motion/observation.rb
27
28
  - lib/motion/queues.rb
28
29
  - lib/motion/util.rb