class MotionKit::NSViewHelpers

Public Instance Methods

_calculate_frame(f, from: from_view, relative_to: point) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 146
def _calculate_frame(f, from: from_view, relative_to: point)
  if from_view.is_a?(Symbol)
    from_view = self.get_view(from_view)
  end

  from_view_size = from_view.frame.size
  o = from_view.convertPoint([0, 0], toView: target.superview)

  calculate_view = target

  if point[:x] == :reset || point[:y] == :reset
    calculate_view = NSView.alloc.initWithFrame([[0, 0], target.frame.size])
  end

  if f.is_a?(Hash)
    f = f.merge(relative: true, flipped: flipped?)
  end
  f = MotionKit.calculate(calculate_view, :frame, f, from_view)
  f.origin.x += o.x
  f.origin.y += o.y

  case point[:x]
  when :min, :reset
    # pass
  when :mid
    f.origin.x += (from_view_size.width - f.size.width) / 2.0
  when :max
    f.origin.x += from_view_size.width - f.size.width
  when :before
    f.origin.x -= f.size.width
  when :after
    f.origin.x += from_view_size.width
  else
    f.origin.x += point[:x]
  end

  case point[:y]
  when :reset
    # pass
  when :min
    unless flipped?
      f.origin.y += from_view_size.height - f.size.height
    end
  when :mid
    f.origin.y += (from_view_size.height - f.size.height) / 2.0
  when :max
    if flipped?
      f.origin.y += from_view_size.height - f.size.height
    end
  when :above
    if flipped?
      f.origin.y += from_view_size.height
    else
      f.origin.y -= from_view_size.height + f.size.height
    end
  when :below
    if flipped?
      f.origin.y -= f.size.height
    else
      # pass
    end
  else
    f.origin.y += point[:y]
  end

  return f
end
_fix_frame_value(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 5
def _fix_frame_value(value)
  if value.is_a?(Hash) && value[:relative]
    return value.merge(flipped: flipped?)
  end
  return value
end
above(from_view, f={}) click to toggle source

The first arg can be a view or a frame @example

# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 342
def above(from_view, f={})
  _calculate_frame(f, from: from_view, relative_to: { x: :reset, y: :above })
end
after(from_view, f={}) click to toggle source

The first arg can be a view or a frame @example

# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 361
def after(from_view, f={})
  _calculate_frame(f, from: from_view, relative_to: { x: :after, y: :reset })
end
Also aliased as: right_of
autoresizingMask(*values)
Alias for: autoresizing_mask
autoresizing_mask(*values) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_autoresizing_helpers.rb, line 5
def autoresizing_mask(*values)
  value = 0
  values.each do |mask|
    case mask
    when :flexible_left
      value |= NSViewMinXMargin
    when :flexible_width
      value |= NSViewWidthSizable
    when :flexible_right
      value |= NSViewMaxXMargin
    when :flexible_top
      value |= NSViewMaxYMargin
    when :flexible_height
      value |= NSViewHeightSizable
    when :flexible_bottom
      value |= NSViewMinYMargin

    when :rigid_left
      value ^= NSViewMinXMargin
    when :rigid_width
      value ^= NSViewWidthSizable
    when :rigid_right
      value ^= NSViewMaxXMargin
    when :rigid_top
      value ^= NSViewMaxYMargin
    when :rigid_height
      value ^= NSViewHeightSizable
    when :rigid_bottom
      value ^= NSViewMinYMargin

    when :fill
      value |= NSViewWidthSizable | NSViewHeightSizable
    when :fill_top
      value |= NSViewWidthSizable | NSViewMinYMargin
    when :fill_bottom
      value |= NSViewWidthSizable | NSViewMaxYMargin
    when :fill_width
      value |= NSViewMinYMargin | NSViewWidthSizable | NSViewMaxYMargin
    when :fill_left
      value |= NSViewHeightSizable | NSViewMaxXMargin
    when :fill_right
      value |= NSViewHeightSizable | NSViewMinXMargin
    when :fill_height
      value |= NSViewMinXMargin | NSViewHeightSizable | NSViewMaxXMargin

    when :pin_to_top_left
      value |= NSViewMaxXMargin | NSViewMinYMargin
    when :pin_to_top
      value |= NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin
    when :pin_to_top_right
      value |= NSViewMinXMargin | NSViewMinYMargin
    when :pin_to_left
      value |= NSViewMaxYMargin | NSViewMinYMargin | NSViewMaxXMargin
    when :pin_to_center, :pin_to_middle
      value |= NSViewMaxYMargin | NSViewMinYMargin | NSViewMinXMargin | NSViewMaxXMargin
    when :pin_to_right
      value |= NSViewMaxYMargin | NSViewMinYMargin | NSViewMinXMargin
    when :pin_to_bottom_left
      value |= NSViewMaxXMargin | NSViewMaxYMargin
    when :pin_to_bottom
      value |= NSViewMinXMargin | NSViewMaxXMargin | NSViewMaxYMargin
    when :pin_to_bottom_right
      value |= NSViewMinXMargin | NSViewMaxYMargin
    else
      value |= mask
    end
  end

  target.autoresizingMask = value
  value
end
Also aliased as: autoresizingMask
before(from_view, f={}) click to toggle source

The first arg can be a view or a frame @example

# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 354
def before(from_view, f={})
  _calculate_frame(f, from: from_view, relative_to: { x: :before, y: :reset })
end
Also aliased as: left_of
below(from_view, f={}) click to toggle source

The first arg can be a view or a frame @example

# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 348
def below(from_view, f={})
  _calculate_frame(f, from: from_view, relative_to: { x: :reset, y: :below })
end
bottom(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 51
def bottom(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.origin.y = MotionKit.calculate(target, :height, value)
  unless flipped?
    f.origin.y -= f.size.height
  end
  target.frame = f

  if flipped?
    return CGRectGetMinY(target.frame)
  else
    return CGRectGetMaxY(target.frame)
  end
end
center(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 119
def center(value)
  value = _fix_frame_value(value)
  f = target.frame
  center = MotionKit.calculate(target, :center, value)
  origin = CGPoint.new(center.x, center.y)
  origin.x -= f.size.width / 2
  origin.y -= f.size.height / 2
  f.origin = origin
  target.frame = f
  return center
end
Also aliased as: middle
center_x(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 33
def center_x(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.origin.x = MotionKit.calculate(target, :width, value)
  f.origin.x -= f.size.width / 2
  target.frame = f
  return CGRectGetMidX(target.frame)
end
Also aliased as: middle_x
center_y(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 83
def center_y(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.origin.y = MotionKit.calculate(target, :height, value)
  f.origin.y -= f.size.height / 2
  target.frame = f
  return CGRectGetMidY(target.frame)
end
Also aliased as: middle_y
compression_priority(value, for_orientation: orientation) click to toggle source
# File lib/motion-kit-osx/helpers/constraints_helpers.rb, line 10
def compression_priority(value, for_orientation: orientation)
  content_compression_resistance_priority(value, for_orientation: orientation)
end
constraints(add_to_view=nil, &block) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_constraints_helpers.rb, line 5
def constraints(add_to_view=nil, &block)
  add_to_view ||= target
  if add_to_view.is_a?(Symbol)
    add_to_view = self.get_view(add_to_view)
  end
  add_to_view.setTranslatesAutoresizingMaskIntoConstraints(false)

  constraints_target = ConstraintsTarget.new(add_to_view)
  deferred(constraints_target) do
    context(constraints_target, &block)
    constraints_target.apply_all_constraints(self, add_to_view)
  end
end
content_compression_resistance_priority(value, for_orientation: orientation) click to toggle source
# File lib/motion-kit-osx/helpers/constraints_helpers.rb, line 5
def content_compression_resistance_priority(value, for_orientation: orientation)
  orientation = Constraint.orientation_lookup(orientation)
  target.setContentCompressionResistancePriority(value, forOrientation: axis)
end
content_hugging_priority(value, for_orientation: orientation) click to toggle source
# File lib/motion-kit-osx/helpers/constraints_helpers.rb, line 14
def content_hugging_priority(value, for_orientation: orientation)
  orientation = Constraint.orientation_lookup(orientation)
  target.setContentHuggingPriority(value, forOrientation: axis)
end
flipped?() click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 12
def flipped?
  target.superview && !target.superview.flipped?
end
frame(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 140
def frame(value)
  value = _fix_frame_value(value)
  target.frame = MotionKit.calculate(target, :frame, value)
  return target.frame
end
from_bottom(from_view=nil, f=nil) click to toggle source

The first arg can be a view or a frame @example

frame from_bottom(width: 80, height: 22)
frame from_bottom(another_view, width: 80, height: 22)
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 316
def from_bottom(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :mid, y: :min })
end
from_bottom_left(from_view=nil, f=nil) click to toggle source

The first arg can be a view or a frame @example

frame from_bottom_left(width: 80, height: 22)
frame from_bottom_left(another_view, width: 80, height: 22)
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 302
def from_bottom_left(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :min, y: :min })
end
from_bottom_right(from_view=nil, f=nil) click to toggle source

The first arg can be a view or a frame @example

frame from_bottom_right(width: 80, height: 22)
frame from_bottom_right(another_view, width: 80, height: 22)
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 330
def from_bottom_right(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :max, y: :min })
end
from_center(from_view=nil, f=nil) click to toggle source

The first arg can be a view or a frame @example

frame from_center(width: 80, height: 22)
frame from_center(another_view, width: 80, height: 22)
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 274
def from_center(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :mid, y: :mid })
end
from_left(from_view=nil, f=nil) click to toggle source

The first arg can be a view or a frame @example

frame from_left(width: 80, height: 22)
frame from_left(another_view, width: 80, height: 22)
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 260
def from_left(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :min, y: :mid })
end
from_right(from_view=nil, f=nil) click to toggle source

The first arg can be a view or a frame @example

frame from_right(width: 80, height: 22)
frame from_right(another_view, width: 80, height: 22)
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 288
def from_right(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :max, y: :mid })
end
from_top(from_view=nil, f=nil) click to toggle source

The first arg can be a view or a frame @example

frame from_top(width: 80, height: 22)
frame from_top(another_view, width: 80, height: 22)
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 232
def from_top(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :mid, y: :max })
end
from_top_left(from_view=nil, f=nil) click to toggle source

The first arg can be a view or a frame @example

frame from_top_left(width: 80, height: 22)
frame from_top_left(another_view, width: 80, height: 22)
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 218
def from_top_left(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :min, y: :max })
end
from_top_right(from_view=nil, f=nil) click to toggle source

The first arg can be a view or a frame @example

frame from_top_right(width: 80, height: 22)
frame from_top_right(another_view, width: 80, height: 22)
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 246
def from_top_right(from_view=nil, f=nil)
  if from_view.is_a?(Hash)
    f = from_view
    from_view = nil
  end
  f ||= {}
  from_view ||= target.superview
  _calculate_frame(f, from: from_view, relative_to: { x: :max, y: :max })
end
h(value)
Alias for: height
height(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 102
def height(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.size.height = MotionKit.calculate(target, :height, value)
  target.frame = f
  return CGRectGetHeight(f)
end
Also aliased as: h
hugging_priority(value, for_orientation: orientation) click to toggle source
# File lib/motion-kit-osx/helpers/constraints_helpers.rb, line 19
def hugging_priority(value, for_orientation: orientation)
  content_hugging_priority(value, for_orientation: orientation)
end
left(value)
Alias for: x
left_of(from_view, f={})
Alias for: before
middle(value)
Alias for: center
middle_x(value)
Alias for: center_x
middle_y(value)
Alias for: center_y
origin(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 111
def origin(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.origin = MotionKit.calculate(target, :origin, value)
  target.frame = f
  return target.frame.origin
end
relative_to(from_view, f) click to toggle source

The first arg must be a view @example

# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 368
def relative_to(from_view, f)
  _calculate_frame(f, from: from_view, relative_to: { x: :reset, y: :reset })
end
right(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 25
def right(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.origin.x = MotionKit.calculate(target, :width, value) - f.size.width
  target.frame = f
  return CGRectGetMaxX(f)
end
right_of(from_view, f={})
Alias for: after
size(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 132
def size(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.size = MotionKit.calculate(target, :size, value)
  target.frame = f
  return target.frame.size
end
top(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 67
def top(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.origin.y = MotionKit.calculate(target, :height, value)
  if flipped?
    f.origin.y -= f.size.height
  end
  target.frame = f

  if flipped?
    return CGRectGetMaxY(f)
  else
    return CGRectGetMinY(f)
  end
end
w(value)
Alias for: width
width(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 93
def width(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.size.width = MotionKit.calculate(target, :width, value)
  target.frame = f
  return CGRectGetWidth(f)
end
Also aliased as: w
x(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 16
def x(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.origin.x = MotionKit.calculate(target, :width, value)
  target.frame = f
  return CGRectGetMinX(f)
end
Also aliased as: left
y(value) click to toggle source
# File lib/motion-kit-osx/helpers/nsview_frame_helpers.rb, line 43
def y(value)
  value = _fix_frame_value(value)
  f = target.frame
  f.origin.y = MotionKit.calculate(target, :height, value)
  target.frame = f
  return CGRectGetMinY(f)
end