Class: MotionKit::Constraint
- Defined in:
- lib/motion-kit-cocoa/constraints/constraint.rb
Direct Known Subclasses
Constant Summary collapse
- Priorities =
{ required: 1000, # NSLayoutPriorityRequired high: 750, # NSLayoutPriorityDefaultHigh medium: 500, low: 250, # NSLayoutPriorityDefaultLow }
- Relationships =
NSLayoutPriorityDefaultLow
{ equal: NSLayoutRelationEqual, lte: NSLayoutRelationLessThanOrEqual, gte: NSLayoutRelationGreaterThanOrEqual, }
- Attributes =
{ none: NSLayoutAttributeNotAnAttribute, left: NSLayoutAttributeLeft, right: NSLayoutAttributeRight, top: NSLayoutAttributeTop, bottom: NSLayoutAttributeBottom, leading: NSLayoutAttributeLeading, trailing: NSLayoutAttributeTrailing, width: NSLayoutAttributeWidth, height: NSLayoutAttributeHeight, center_x: NSLayoutAttributeCenterX, center_y: NSLayoutAttributeCenterY, baseline: NSLayoutAttributeBaseline, }
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#attribute2 ⇒ Object
Returns the value of attribute attribute2.
-
#constant ⇒ Object
Returns the value of attribute constant.
- #identifier(identifier = nil) ⇒ Object (also: #identified_by)
-
#multiplier ⇒ Object
Returns the value of attribute multiplier.
- #priority(priority = nil) ⇒ Object
-
#relationship ⇒ Object
Returns the value of attribute relationship.
-
#relative_to ⇒ Object
Returns the value of attribute relative_to.
-
#target ⇒ Object
Returns the value of attribute target.
Class Method Summary collapse
- .attribute_lookup(attribute) ⇒ Object
- .attribute_reverse(attribute) ⇒ Object
- .axis_lookup(axis) ⇒ Object
- .calculate?(value) ⇒ Boolean
- .constant?(value) ⇒ Boolean
- .orientation_lookup(orientation) ⇒ Object
- .priority_lookup(priority) ⇒ Object
- .relationship_lookup(relationship) ⇒ Object
- .relationship_reverse(relationship) ⇒ Object
- .view_lookup(layout, view, target) ⇒ Object
Instance Method Summary collapse
- #<=(compare) ⇒ Object
- #==(compare) ⇒ Object
- #>=(compare) ⇒ Object
- #divided_by(multiplier) ⇒ Object
- #equals(target, attribute2 = nil) ⇒ Object (also: #is_equal_to, #equal_to)
- #gte(target, attribute2 = nil) ⇒ Object (also: #is_at_least, #at_least)
-
#initialize(target, attribute = nil, relationship) ⇒ Constraint
constructor
A new instance of Constraint.
-
#is(value = nil) ⇒ Object
like ‘equals`, but also sets `compare_flag` to true, so you can use ==, <=, and >=.
- #lte(target, attribute2 = nil) ⇒ Object (also: #is_at_most, #at_most)
- #minus(constant) ⇒ Object
-
#of(target, attribute2 = nil) ⇒ Object
width.is(‘100%’).of(:view, :width).
-
#plus(constant) ⇒ Object
If no relationship has been set, the “use case” here is:.
- #resolve_all(layout, view) ⇒ Object
- #times(multiplier) ⇒ Object
- #update_constraint ⇒ Object
Constructor Details
#initialize(target, attribute = nil, relationship) ⇒ Constraint
Returns a new instance of Constraint.
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 46 def initialize(target, attribute=nil, relationship) @target = target @attribute = attribute @attribute2 = attribute @relative_to = nil @relationship = relationship @multiplier = 1 @constant = 0 @priority = nil @compare_flag = false end |
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
11 12 13 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 11 def attribute @attribute end |
#attribute2 ⇒ Object
Returns the value of attribute attribute2.
14 15 16 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 14 def attribute2 @attribute2 end |
#constant ⇒ Object
Returns the value of attribute constant.
16 17 18 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 16 def constant @constant end |
#identifier(identifier = nil) ⇒ Object Also known as: identified_by
226 227 228 229 230 231 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 226 def identifier(identifier=nil) return @identifier if identifier.nil? @identifier = identifier self end |
#multiplier ⇒ Object
Returns the value of attribute multiplier.
15 16 17 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 15 def multiplier @multiplier end |
#priority(priority = nil) ⇒ Object
219 220 221 222 223 224 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 219 def priority(priority=nil) return @priority if priority.nil? @priority = priority self end |
#relationship ⇒ Object
Returns the value of attribute relationship.
12 13 14 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 12 def relationship @relationship end |
#relative_to ⇒ Object
Returns the value of attribute relative_to.
13 14 15 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 13 def relative_to @relative_to end |
#target ⇒ Object
Returns the value of attribute target.
10 11 12 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 10 def target @target end |
Class Method Details
.attribute_lookup(attribute) ⇒ Object
307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 307 def attribute_lookup(attribute) if attribute.is_a? Fixnum return attribute end unless Attributes.key? attribute raise InvalidAttributeError.new("Unsupported attribute #{attribute.inspect}") end Attributes[attribute] end |
.attribute_reverse(attribute) ⇒ Object
343 344 345 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 343 def attribute_reverse(attribute) Attributes.key(attribute) || :none end |
.axis_lookup(axis) ⇒ Object
273 274 275 276 277 278 279 280 281 282 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 273 def axis_lookup(axis) case axis when :horizontal UILayoutConstraintAxisHorizontal when :vertical UILayoutConstraintAxisVertical else axis end end |
.calculate?(value) ⇒ Boolean
351 352 353 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 351 def calculate?(value) value.is_a?(String) end |
.constant?(value) ⇒ Boolean
355 356 357 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 355 def constant?(value) value.is_a?(Numeric) || value.is_a?(Hash) || value.is_a?(Array) end |
.orientation_lookup(orientation) ⇒ Object
284 285 286 287 288 289 290 291 292 293 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 284 def orientation_lookup(orientation) case orientation when :horizontal NSLayoutConstraintOrientationHorizontal when :vertical NSLayoutConstraintOrientationVertical else orientation end end |
.priority_lookup(priority) ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 319 def priority_lookup(priority) if priority.is_a? Fixnum return priority end unless Priorities.key? priority raise InvalidPriorityError.new("Unsupported priority #{priority.inspect}") end Priorities[priority] end |
.relationship_lookup(relationship) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 331 def relationship_lookup(relationship) if relationship.is_a? Fixnum return relationship end unless Relationships.key? relationship raise InvalidRelationshipError.new("Unsupported relationship #{relationship.inspect}") end Relationships[relationship] end |
.relationship_reverse(relationship) ⇒ Object
347 348 349 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 347 def relationship_reverse(relationship) Relationships.key(relationship) end |
.view_lookup(layout, view, target) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 295 def view_lookup(layout, view, target) if ! target || target.is_a?(MotionKit.base_view_class) target elsif target == :self view elsif target == :superview view.superview else layout.get_view(target) end end |
Instance Method Details
#<=(compare) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 96 def <=(compare) if @compare_flag lte(compare) self else super end end |
#==(compare) ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 76 def ==(compare) if @compare_flag equals(compare) self else super end end |
#>=(compare) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 86 def >=(compare) if @compare_flag gte(compare) self else super end end |
#divided_by(multiplier) ⇒ Object
196 197 198 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 196 def divided_by(multiplier) times 1.0/multiplier end |
#equals(target, attribute2 = nil) ⇒ Object Also known as: is_equal_to, equal_to
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 106 def equals(target, attribute2=nil) self.relationship ||= :equal if Constraint.constant?(target) self.constant = target elsif Constraint.calculate?(target) calc = Calculator.scan(target) self.multiplier = calc.factor self.constant = calc.constant || 0 else self.relative_to = target if attribute2 self.attribute2 = attribute2 end end @compare_flag = false self end |
#gte(target, attribute2 = nil) ⇒ Object Also known as: is_at_least, at_least
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 146 def gte(target, attribute2=nil) self.relationship = :gte if Constraint.constant?(target) self.constant = target elsif Constraint.calculate?(target) calc = Calculator.scan(target) self.multiplier = calc.factor self.constant = calc.constant || 0 else self.relative_to = target if attribute2 self.attribute2 = attribute2 end end @compare_flag = false self end |
#is(value = nil) ⇒ Object
like ‘equals`, but also sets `compare_flag` to true, so you can use ==, <=, and >=
68 69 70 71 72 73 74 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 68 def is(value=nil) if value self.equals(value) end @compare_flag = true self end |
#lte(target, attribute2 = nil) ⇒ Object Also known as: is_at_most, at_most
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 126 def lte(target, attribute2=nil) self.relationship = :lte if Constraint.constant?(target) self.constant = target elsif Constraint.calculate?(target) calc = Calculator.scan(target) self.multiplier = calc.factor self.constant = calc.constant || 0 else self.relative_to = target if attribute2 self.attribute2 = attribute2 end end @compare_flag = false self end |
#minus(constant) ⇒ Object
215 216 217 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 215 def minus(constant) plus -constant end |
#of(target, attribute2 = nil) ⇒ Object
width.is(‘100%’).of(:view, :width)
167 168 169 170 171 172 173 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 167 def of(target, attribute2=nil) self.relative_to = target if attribute2 self.attribute2 = attribute2 end self end |
#plus(constant) ⇒ Object
If no relationship has been set, the “use case” here is:
c.plus(10).equals(:view, :x)
Which is the same as
c.equals(:view, :x).minus(10)
207 208 209 210 211 212 213 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 207 def plus(constant) unless self.relationship constant = -constant end self.constant += constant self end |
#resolve_all(layout, view) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 242 def resolve_all(layout, view) @resolved ||= begin item = Constraint.view_lookup(layout, view, self.target) rel_item = Constraint.view_lookup(layout, view, self.relative_to) relationship = Constraint.relationship_lookup(self.relationship) attribute1 = Constraint.attribute_lookup(self.attribute) attribute2 = Constraint.attribute_lookup(self.attribute2) nsconstraint = NSLayoutConstraint.constraintWithItem(item, attribute: attribute1, relatedBy: relationship, toItem: rel_item, attribute: attribute2, multiplier: self.multiplier, constant: self.constant ) if self.priority nsconstraint.priority = Constraint.priority_lookup(self.priority) end if self.identifier nsconstraint.setIdentifier(self.identifier) end [nsconstraint] end end |
#times(multiplier) ⇒ Object
191 192 193 194 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 191 def times(multiplier) self.multiplier *= multiplier self end |
#update_constraint ⇒ Object
234 235 236 237 238 239 240 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 234 def update_constraint if @resolved constraint = @resolved[0] constraint.constant = self.constant end end |