Class: MotionKit::ViewCalculator

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

Class Method Summary collapse

Methods included from OriginCalculator

calculate_absolute_from_hash, calculate_from_center, calculate_from_hash, calculate_from_hash_array, calculate_origin, calculate_relative_from_hash

Methods included from SizeCalculator

calculate_scaled_width_height, calculate_size, calculate_size_from_multiple, resolve_width_and_height

Methods included from FrameCalculator

calculate_frame, calculate_frame_from_array, calculate_frame_from_hash, calculate_frame_from_symbol

Class 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
37
38
39
40
41
42
43
44
# File 'lib/motion-kit-cocoa/calculator/view_calculator.rb', line 12

def self.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?('%')
    if not full_view
      if view.is_a?(CALayer)
        full_view = view.superlayer
      elsif view.respond_to?(:superview)
        full_view = view.superview
      end

      if not full_view
        raise NoSuperviewError.new("Cannot calculate #{amount.inspect} of #{dimension.inspect} because view #{view} has no superview.")
      end
    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



46
47
48
49
50
51
52
53
54
55
# File 'lib/motion-kit-cocoa/calculator/view_calculator.rb', line 46

def self.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