teacup 1.0.1 → 1.0.2

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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- teacup (1.0.0)
4
+ teacup (1.0.1)
5
5
  rake
6
6
 
7
7
  GEM
@@ -50,20 +50,24 @@ module Teacup
50
50
 
51
51
  ##|
52
52
  def constrain_below(relative_to, margin=0)
53
+ margin = 8 if margin == :auto
53
54
  Teacup::Constraint.new(:self, :top).equals(relative_to, :bottom).plus(margin)
54
55
  end
55
56
 
56
57
  def constrain_above(relative_to, margin=0)
58
+ margin = 8 if margin == :auto
57
59
  Teacup::Constraint.new(:self, :bottom).equals(relative_to, :top).plus(margin)
58
60
  end
59
61
 
60
- def constrain_to_left(relative_to, margin=0)
61
- Teacup::Constraint.new(:self, :right).equals(relative_to, :left).plus(margin)
62
- end
63
-
64
62
  def constrain_to_right(relative_to, margin=0)
63
+ margin = 20 if margin == :auto
65
64
  Teacup::Constraint.new(:self, :left).equals(relative_to, :right).plus(margin)
66
65
  end
67
66
 
67
+ def constrain_to_left(relative_to, margin=0)
68
+ margin = 20 if margin == :auto
69
+ Teacup::Constraint.new(:self, :right).equals(relative_to, :left).minus(margin)
70
+ end
71
+
68
72
  end
69
73
  end
@@ -1,5 +1,5 @@
1
1
  module Teacup
2
2
 
3
- VERSION = '1.0.1'
3
+ VERSION = '1.0.2'
4
4
 
5
5
  end
@@ -51,8 +51,6 @@ class UIView
51
51
 
52
52
  def restyle!(orientation=nil)
53
53
  if Teacup.should_restyle?
54
- resetTeacupConstraints
55
-
56
54
  if stylesheet && stylesheet.is_a?(Teacup::Stylesheet)
57
55
  style(stylesheet.query(stylename, self, orientation))
58
56
  end
@@ -60,6 +58,89 @@ class UIView
60
58
  end
61
59
  end
62
60
 
61
+ def get_ns_constraints
62
+ # gets the array of Teacup::Constraint objects
63
+ my_constraints = (@teacup_constraints || []).map { |constraint, relative_to|
64
+ if constraint.is_a?(Teacup::Constraint)
65
+ constraint
66
+ else
67
+ if relative_to == true
68
+ Teacup::Constraint.from_sym(constraint)
69
+ else
70
+ Teacup::Constraint.from_sym(constraint, relative_to)
71
+ end
72
+ end
73
+ }.flatten.tap{ |my_constraints|
74
+ unless my_constraints.empty?
75
+ self.setTranslatesAutoresizingMaskIntoConstraints(false)
76
+ end
77
+ }.map do |original_constraint|
78
+ constraint = original_constraint.copy
79
+
80
+ case original_constraint.target
81
+ when UIView
82
+ constraint.target = original_constraint.target
83
+ when :self
84
+ constraint.target = self
85
+ when :superview
86
+ constraint.target = self.superview
87
+ when Symbol, String
88
+ container = self
89
+ constraint.target = nil
90
+ while container.superview && constraint.target == nil
91
+ constraint.target = container.viewWithStylename(original_constraint.target)
92
+ container = container.superview
93
+ end
94
+ end
95
+
96
+ case original_constraint.relative_to
97
+ when UIView
98
+ constraint.relative_to = original_constraint.relative_to
99
+ when :self
100
+ constraint.relative_to = self
101
+ when :superview
102
+ constraint.relative_to = self.superview
103
+ when Symbol, String
104
+ # TODO: this re-checks lots of views - everytime it goes up to the
105
+ # superview, it checks all the leaves again.
106
+ container = self
107
+ constraint.relative_to = nil
108
+ while container.superview && constraint.relative_to == nil
109
+ constraint.relative_to = container.viewWithStylename(original_constraint.relative_to)
110
+ container = container.superview
111
+ end
112
+ end
113
+
114
+ # the return value, for the map
115
+ constraint.nslayoutconstraint
116
+ end
117
+
118
+ # now add all che child constraints
119
+ subviews.each do |subview|
120
+ my_constraints.concat(subview.get_ns_constraints)
121
+ end
122
+
123
+ my_constraints
124
+ end
125
+
126
+ def apply_constraints
127
+ if @teacup_added_constraints
128
+ @teacup_added_constraints.each do |constraint|
129
+ self.removeConstraint(constraint)
130
+ end
131
+ end
132
+ @teacup_added_constraints = nil
133
+ all_constraints = get_ns_constraints
134
+
135
+ return if all_constraints.empty?
136
+
137
+ @teacup_added_constraints = []
138
+ all_constraints.each do |ns_constraint|
139
+ @teacup_added_constraints << ns_constraint
140
+ self.addConstraint(ns_constraint)
141
+ end
142
+ end
143
+
63
144
  # Animate a change to a new stylename.
64
145
  #
65
146
  # This is equivalent to wrapping a call to .stylename= inside
@@ -110,72 +191,7 @@ class UIView
110
191
  # @param Hash the properties to set.
111
192
  def style(properties, orientation=nil)
112
193
  if properties.key?(:constraints)
113
- new_constraints = add_uniq_constraints(properties.delete(:constraints))
114
-
115
- self.setTranslatesAutoresizingMaskIntoConstraints(false) unless new_constraints.empty?
116
-
117
- @teacup_added_constraints ||= []
118
- new_constraints.each do |original_constraint|
119
- constraint = original_constraint.copy
120
-
121
- case original_constraint.target
122
- when :self
123
- constraint.target = self
124
- when :superview
125
- constraint.target = self.superview
126
- when Symbol, String
127
- container = self
128
- constraint.target = nil
129
- while container.superview && constraint.target == nil
130
- constraint.target = container.viewWithStylename(original_constraint.target)
131
- container = container.superview
132
- end
133
- end
134
-
135
- case original_constraint.relative_to
136
- when :self
137
- constraint.relative_to = self
138
- when :superview
139
- constraint.relative_to = self.superview
140
- when Symbol, String
141
- # TODO: this re-checks lots of views - everytime it goes up to the
142
- # superview, it checks all the leaves again.
143
- container = self
144
- constraint.relative_to = nil
145
- while container.superview && constraint.relative_to == nil
146
- constraint.relative_to = container.viewWithStylename(original_constraint.relative_to)
147
- container = container.superview
148
- end
149
- end
150
-
151
- add_constraint_to = nil
152
- if constraint.target == constraint.relative_to || constraint.relative_to.nil?
153
- add_constraint_to = constraint.target.superview
154
- elsif constraint.target.isDescendantOfView(constraint.relative_to)
155
- add_constraint_to = constraint.relative_to
156
- elsif constraint.relative_to.isDescendantOfView(constraint.target)
157
- add_constraint_to = constraint.target
158
- else
159
- parent = constraint.relative_to.superview
160
- while parent
161
- if constraint.target.isDescendantOfView(parent)
162
- add_constraint_to = parent
163
- parent = nil
164
- elsif parent.superview
165
- parent = parent.superview
166
- end
167
- end
168
- end
169
-
170
- if add_constraint_to
171
- ns_constraint = constraint.nslayoutconstraint
172
-
173
- @teacup_added_constraints << { target: add_constraint_to, constraint: ns_constraint }
174
- add_constraint_to.addConstraint(ns_constraint)
175
- else
176
- raise "The two views #{original_constraint.target} and #{original_constraint.relative_to} do not have a common ancestor"
177
- end
178
- end
194
+ add_uniq_constraints(properties.delete(:constraints))
179
195
  end
180
196
 
181
197
  Teacup.apply_hash self, properties
@@ -188,46 +204,22 @@ class UIView
188
204
  return self
189
205
  end
190
206
 
191
- def teacup_added_constraints ; @teacup_added_constraints ; end
192
-
193
- def resetTeacupConstraints
194
- if @teacup_added_constraints
195
- @teacup_added_constraints.each do |added_constraint|
196
- target = added_constraint[:target]
197
- constraint = added_constraint[:constraint]
198
- target.removeConstraint(constraint)
199
- end
200
- end
201
- @teacup_added_constraints = nil
202
- @teacup_constrain_just_once = nil
203
- end
204
-
205
207
  def add_uniq_constraints(constraint)
206
- @teacup_constrain_just_once ||= {}
208
+ @teacup_constraints ||= {}
207
209
 
208
210
  if constraint.is_a? Array
209
- new_consraints = constraint.map{|constraint| add_uniq_constraints(constraint) }.flatten
211
+ constraint.each { |constraint|
212
+ add_uniq_constraints(constraint)
213
+ }
210
214
  elsif constraint.is_a? Hash
211
- new_consraints = constraint.select{|sym, relative_to|
212
- @teacup_constrain_just_once[sym].nil?
213
- }.map{|sym, relative_to|
214
- @teacup_constrain_just_once[sym] = true
215
- Teacup::Constraint.from_sym(sym, relative_to)
215
+ constraint.each { |sym, relative_to|
216
+ @teacup_constraints[sym] = relative_to
216
217
  }
218
+ elsif constraint.is_a? Teacup::Constraint or constraint.is_a? Symbol
219
+ @teacup_constraints[constraint] = true
217
220
  else
218
- if @teacup_constrain_just_once[constraint]
219
- new_consraints = []
220
- else
221
- @teacup_constrain_just_once[constraint] = true
222
- if constraint.is_a? Symbol
223
- new_consraints = [Teacup::Constraint.from_sym(constraint)]
224
- else
225
- new_consraints = [constraint]
226
- end
227
- end
221
+ raise "Unsupported constraint: #{constraint.inspect}"
228
222
  end
229
-
230
- return new_consraints
231
223
  end
232
224
 
233
225
  end
@@ -118,6 +118,9 @@ class UIViewController
118
118
  def viewWillAppear(animated)
119
119
  old_viewWillAppear(animated)
120
120
  self.view.restyle! unless @teacup_view_appeared
121
+ if defined? NSLayoutConstraint
122
+ self.view.apply_constraints
123
+ end
121
124
  @teacup_view_appeared = true
122
125
  end
123
126
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: teacup
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-29 00:00:00.000000000 Z
12
+ date: 2012-11-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake