Class: MotionKit::ConstraintsTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/motion-kit-cocoa/constraints/constraints_target.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ ConstraintsTarget

Returns a new instance of ConstraintsTarget.



6
7
8
9
# File 'lib/motion-kit-cocoa/constraints/constraints_target.rb', line 6

def initialize(view)
  @view = view
  @constraints = []
end

Instance Attribute Details

#viewObject (readonly)

Returns the value of attribute view.



4
5
6
# File 'lib/motion-kit-cocoa/constraints/constraints_target.rb', line 4

def view
  @view
end

Instance Method Details

#==(value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/motion-kit-cocoa/constraints/constraints_target.rb', line 15

def ==(value)
  if value.is_a?(ConstraintsTarget)
    super
  else
    @view == value
  end
end

#add_constraints(constraints) ⇒ Object



11
12
13
# File 'lib/motion-kit-cocoa/constraints/constraints_target.rb', line 11

def add_constraints(constraints)
  @constraints.concat(constraints)
end

#apply_all_constraints(layout, target) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/motion-kit-cocoa/constraints/constraints_target.rb', line 23

def apply_all_constraints(layout, target)
  @constraints.map do |mk_constraint|
    mk_constraint.resolve_all(layout, target).map do |constraint|
      base_view_class = MotionKit.base_view_class

      if constraint.firstItem.is_a?(base_view_class) && constraint.secondItem.is_a?(base_view_class)
        common_ancestor = nil

        ancestors = [constraint.firstItem]
        parent_view = constraint.firstItem
        while parent_view = parent_view.superview
          ancestors << parent_view
        end

        current_view = constraint.secondItem
        while current_view
          if ancestors.include? current_view
            common_ancestor = current_view
            break
          end
          current_view = current_view.superview
        end

        unless common_ancestor
          raise NoCommonAncestorError.new("No common ancestors between #{constraint.firstItem} and #{constraint.secondItem}")
        end
      else
        common_ancestor = constraint.firstItem
      end

      common_ancestor.addConstraint(constraint)
      constraint
    end
  end.flatten
end