lib/motion/layout.rb in rm-extensions-0.1.4 vs lib/motion/layout.rb in rm-extensions-0.1.5
- old
+ new
@@ -14,23 +14,29 @@
"centerY" => NSLayoutAttributeCenterY,
"baseline" => NSLayoutAttributeBaseline,
nil => NSLayoutAttributeNotAnAttribute
}
+ ATTRIBUTE_LOOKUP_INVERSE = ATTRIBUTE_LOOKUP.invert
+
RELATED_BY_LOOKUP = {
"<=" => NSLayoutRelationLessThanOrEqual,
"==" => NSLayoutRelationEqual,
">=" => NSLayoutRelationGreaterThanOrEqual
}
+ RELATED_BY_LOOKUP_INVERSE = RELATED_BY_LOOKUP.invert
+
PRIORITY_LOOKUP = {
"required" => UILayoutPriorityRequired, # = 1000
"high" => UILayoutPriorityDefaultHigh, # = 750
"low" => UILayoutPriorityDefaultLow, # = 250
"fit" => UILayoutPriorityFittingSizeLevel # = 50
}
+ PRIORITY_LOOKUP_INVERSE = PRIORITY_LOOKUP.invert
+
AXIS_LOOKUP = {
"h" => UILayoutConstraintAxisHorizontal,
"v" => UILayoutConstraintAxisVertical
}
@@ -56,32 +62,30 @@
end
end
@subviews
end
- def eqs(str, debug=false)
+ def eqs(str)
str.split("\n").map(&:strip).select { |x| !x.empty? }.map do |line|
- eq(line, debug)
+ eq(line)
end
end
- def eq?(str)
- eq(str, true)
- end
-
# Constraints are of the form "view1.attr1 <relation> view2.attr2 * multiplier + constant @ priority"
- def eq(str, debug=false)
+ def eq(str)
item = nil
item_attribute = nil
related_by = nil
to_item = nil
to_item_attribute = nil
multiplier = nil
constant = nil
- parts = str.split(" ").select { |x| !x.empty? }
+ parts = str.split("#", 2).first.split(" ").select { |x| !x.empty? }
+ debug = parts.delete("?")
+
# first part should always be view1.attr1
part = parts.shift
item, item_attribute = part.split(".", 2)
# second part should always be relation
@@ -105,13 +109,13 @@
constant = parts[idx + 1]
parts.delete_at(idx)
parts.delete_at(idx)
end
- # look for multipler
+ # look for multiplier
if idx = parts.index("*")
- multipler = parts[idx + 1]
+ multiplier = parts[idx + 1]
parts.delete_at(idx)
parts.delete_at(idx)
end
# now we need to_item, to_item_attribute
@@ -135,25 +139,10 @@
to_item = "view"
to_item_attribute = item_attribute
end
end
- debug_hash = nil
-
- if debug
- debug_hash = {
- :item => item,
- :item_attribute => item_attribute,
- :related_by => related_by,
- :to_item => to_item,
- :to_item_attribute => to_item_attribute,
- :multiplier => multiplier,
- :constant => constant,
- :priority => priority
- }
- end
-
# normalize
res_item = item == "view" ? @view : @subviews[item]
res_item_attribute = ATTRIBUTE_LOOKUP[item_attribute]
res_related_by = RELATED_BY_LOOKUP[related_by]
@@ -184,22 +173,22 @@
errors.push("Invalid relatedBy: #{related_by}") unless res_related_by
errors.push("Invalid view2: #{to_item}") if to_item && !res_to_item
errors.push("Invalid attr2: #{to_item_attribute}") unless res_to_item_attribute
if errors.size > 0 || debug
- puts "======================== constraint debug ========================"
- puts "given:"
- puts " #{str}"
- puts "interpreted:"
- puts " item: #{item}"
- puts " item_attribute: #{item_attribute}"
- puts " related_by: #{related_by}"
- puts " to_item: #{to_item}"
- puts " to_item_attribute: #{to_item_attribute}"
- puts " multiplier: #{multiplier}"
- puts " constant: #{constant}"
- puts " priority: #{priority}"
+ p "======================== constraint debug ========================"
+ p "given:"
+ p " #{str}"
+ p "interpreted:"
+ p " item: #{item}"
+ p " item_attribute: #{item_attribute}"
+ p " related_by: #{related_by}"
+ p " to_item: #{to_item}"
+ p " to_item_attribute: #{to_item_attribute}"
+ p " multiplier: #{multiplier}"
+ p " constant: #{constant}"
+ p " priority: #{priority}"
end
if errors.size > 0
raise(errors.join(", "))
end
@@ -214,15 +203,28 @@
if res_priority
constraint.priority = res_priority
end
if debug
- puts "implemented:"
- puts " #{constraint.description}"
+ p "implemented:"
+ p " #{constraint.description}"
end
@view.addConstraint(constraint)
constraint
+ end
+
+ def describe(constraint)
+ subviews_inverse = subviews.invert
+ item = subviews_inverse[constraint.firstItem]
+ item_attribute = ATTRIBUTE_LOOKUP_INVERSE[constraint.firstAttribute]
+ related_by = RELATED_BY_LOOKUP_INVERSE[constraint.relation]
+ to_item = subviews_inverse[constraint.secondItem]
+ to_item_attribute = ATTRIBUTE_LOOKUP_INVERSE[constraint.secondAttribute]
+ multiplier = constraint.multiplier
+ constant = constraint.constant
+ priority = PRIORITY_LOOKUP_INVERSE[constraint.priority] || constraint.priority
+ "#{item}.#{item_attribute} #{related_by} #{to_item}.#{to_item_attribute} * #{multiplier} + #{constant} @ #{priority}"
end
end
end