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 setscompare_flagto 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
232 233 234 235 236 237 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 232 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
225 226 227 228 229 230 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 225 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
312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 312 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
348 349 350 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 348 def attribute_reverse(attribute) Attributes.key(attribute) || :none end |
.axis_lookup(axis) ⇒ Object
276 277 278 279 280 281 282 283 284 285 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 276 def axis_lookup(axis) case axis when :horizontal UILayoutConstraintAxisHorizontal when :vertical UILayoutConstraintAxisVertical else axis end end |
.calculate?(value) ⇒ Boolean
356 357 358 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 356 def calculate?(value) value.is_a?(String) end |
.constant?(value) ⇒ Boolean
360 361 362 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 360 def constant?(value) value.is_a?(Numeric) || value.is_a?(Hash) || value.is_a?(Array) end |
.orientation_lookup(orientation) ⇒ Object
287 288 289 290 291 292 293 294 295 296 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 287 def orientation_lookup(orientation) case orientation when :horizontal NSLayoutConstraintOrientationHorizontal when :vertical NSLayoutConstraintOrientationVertical else orientation end end |
.priority_lookup(priority) ⇒ Object
324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 324 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
336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 336 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
352 353 354 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 352 def relationship_reverse(relationship) Relationships.key(relationship) end |
.view_lookup(layout, view, target) ⇒ Object
298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 298 def view_lookup(layout, view, target) if ! target || target.is_a?(MotionKit.base_view_class) target elsif target.is_a?(ConstraintPlaceholder) target.resolve(layout) elsif target == :self view elsif target == :superview view.superview else layout.get_view(target) end end |
Instance Method Details
#<=(compare) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 98 def <=(compare) if @compare_flag if @relationship && Constraint.relationship_lookup(@relationship) != NSLayoutRelationEqual raise InvalidRelationshipError.new("You cannot use `.is <=` on a constraint that is already defined as #{@relationship}") end lte(compare) self else super end end |
#==(compare) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 74 def ==(compare) if @compare_flag equals(compare) self else super end end |
#>=(compare) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 84 def >=(compare) if @compare_flag if @relationship && Constraint.relationship_lookup(@relationship) != NSLayoutRelationEqual raise InvalidRelationshipError.new("You cannot use `.is >=` on a constraint that is already defined as #{@relationship}") end gte(compare) self else super end end |
#divided_by(multiplier) ⇒ Object
202 203 204 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 202 def divided_by(multiplier) times 1.0/multiplier end |
#equals(target, attribute2 = nil) ⇒ Object Also known as: is_equal_to, equal_to
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 112 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
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 152 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 >=
66 67 68 69 70 71 72 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 66 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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 132 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
221 222 223 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 221 def minus(constant) plus -constant end |
#of(target, attribute2 = nil) ⇒ Object
width.is('100%').of(:view, :width)
173 174 175 176 177 178 179 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 173 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)
213 214 215 216 217 218 219 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 213 def plus(constant) unless self.relationship constant = -constant end self.constant += constant self end |
#resolve_all(layout, view) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 248 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) nsconstraint = NSLayoutConstraint.constraintWithItem(item, attribute: Constraint.attribute_lookup(self.attribute), relatedBy: Constraint.relationship_lookup(self.relationship), toItem: rel_item, attribute: Constraint.attribute_lookup(self.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
197 198 199 200 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 197 def times(multiplier) self.multiplier *= multiplier self end |
#update_constraint ⇒ Object
240 241 242 243 244 245 246 |
# File 'lib/motion-kit-cocoa/constraints/constraint.rb', line 240 def update_constraint if @resolved constraint = @resolved[0] constraint.constant = self.constant end end |