Class: MotionKit::ViewCalculator

Inherits:
Object
  • Object
show all
Includes:
FrameCalculator, OriginCalculator, SizeCalculator
Defined in:
lib/motion-kit/calculator/view_calculator.rb

Instance Method Summary collapse

Methods included from FrameCalculator

#calculate_frame, #calculate_frame_from_array, #calculate_frame_from_hash, #calculate_frame_from_symbol

Methods included from SizeCalculator

#calculate_scaled_width_height, #calculate_size, #calculate_size_from_multiple, #resolve_width_and_height

Methods included from OriginCalculator

#calculate_absolute_from_hash, #calculate_from_center, #calculate_from_hash, #calculate_from_hash_array, #calculate_origin, #calculate_relative_from_hash

Instance Method Details

#calculate(view, dimension, amount, full_view = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/motion-kit/calculator/view_calculator.rb', line 12

def calculate(view, dimension, amount, full_view=nil)
  if amount.is_a? Proc
    return view.instance_exec(&amount)
  elsif dimension == :origin || dimension == :center
    return calculate_origin(view, dimension, amount, nil, full_view)
  elsif dimension == :size
    return calculate_size(view, amount, full_view)
  elsif dimension == :frame
    return calculate_frame(view, amount, full_view)
  elsif amount == :full
    return calculate(view, dimension, '100%', full_view)
  elsif amount == :auto
    return intrinsic_size(view).send(dimension) # :left or :right
  elsif amount.is_a?(String) && amount.include?('%')
    full_view ||= view.superview || begin
      raise NoSuperviewError.new("Cannot calculate #{amount}% of #{dimension.inspect} because view #{view} has no superview.")
    end
    calc = Calculator.scan(amount)

    raise "Unknown dimension #{dimension}" unless [:width, :height].include?(dimension)
    return (full_view.frame.size.send(dimension) * calc.factor + calc.constant).round
  else
    return amount
  end
end

#intrinsic_size(view) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/motion-kit/calculator/view_calculator.rb', line 38

def intrinsic_size(view)
  size_that_fits = view.intrinsicContentSize
  if size_that_fits.width == MotionKit.no_intrinsic_metric
    size_that_fits.width = 0
  end
  if size_that_fits.height == MotionKit.no_intrinsic_metric
    size_that_fits.height = 0
  end
  return size_that_fits
end