class NSView

Public Class Methods

animate(options={}, more_options={}, &animations) click to toggle source
# File lib/osx/sugarcube-animations/nsview.rb, line 10
def animate(options={}, more_options={}, &animations)
  raise "animation block is required" unless animations

  if options.is_a? Numeric
    duration = options
    options = more_options
  else
    duration = options[:duration] || 0.3
  end

  delay = options[:delay] || 0

  after_animations = options[:after]

  animation_options = sugarcube_animation_options(options)
  if duration == 0 && delay == 0
    animations.call
    after_adjusted.call(true) if after_adjusted
  else
    NSAnimationContext.runAnimationGroup(-> (context) do
      context.duration = duration
      animations.call
    end, completionHandler: after_animations)
  end
end
attr_updates(*attrs) click to toggle source
# File lib/osx/sugarcube-ui/nsview.rb, line 5
def attr_updates(*attrs)
  attr_accessor(*attrs)
  attrs.each do |attr|
    define_method("#{attr}=") do |value|
      if instance_variable_get("@#{attr}") != value
        setNeedsDisplay
      end
      willChangeValueForKey(attr)
      instance_variable_set("@#{attr}", value)
      didChangeValueForKey(attr)
    end
  end
end
sugarcube_animation_options(options) click to toggle source

This is an internal helper method to determine the animation options.

# File lib/osx/sugarcube-animations/nsview.rb, line 6
def sugarcube_animation_options(options)
  {}
end

Public Instance Methods

<<(view) click to toggle source

superview << view

> superview.addSubview(view)

# File lib/osx/sugarcube-ui/nsview.rb, line 23
def <<(view)
  self.addSubview(view)
  return self
end
animate(options={}, more_options={}, &animations) click to toggle source
# File lib/osx/sugarcube-animations/nsview.rb, line 38
def animate(options={}, more_options={}, &animations)
  if options.is_a? Numeric
    options = more_options.merge(duration: options)
  end

  self.wantsLayer = true
  if self.layerContentsRedrawPolicy == NSViewLayerContentsRedrawDuringViewResize
    self.layerContentsRedrawPolicy = NSViewLayerContentsRedrawOnSetNeedsDisplay
  end

  NSView.animate(options, &animations)
  return self
end
convert_frame_from(source) click to toggle source
# File lib/osx/sugarcube-ui/nsview.rb, line 47
def convert_frame_from(source)
  return self.convert_rect(CGRectMake(0, 0, source.frame.size.width, source.frame.size.height), from: source)
end
convert_frame_to(destination) click to toggle source
# File lib/osx/sugarcube-ui/nsview.rb, line 43
def convert_frame_to(destination)
  return self.convert_rect(CGRectMake(0, 0, self.frame.size.width, self.frame.size.height), to: destination)
end
convert_origin_from(source) click to toggle source
# File lib/osx/sugarcube-ui/nsview.rb, line 63
def convert_origin_from(source)
  return self.convert_point([0, 0], from: source)
end
convert_origin_to(destination) click to toggle source
# File lib/osx/sugarcube-ui/nsview.rb, line 59
def convert_origin_to(destination)
  return self.convert_point([0, 0], to: destination)
end
convert_point(point, to: destination) click to toggle source
# File lib/osx/sugarcube-ui/nsview.rb, line 67
def convert_point(point, to: destination)
  return self.convertPoint(point, toView: destination)
end
convert_rect(rect, to: destination) click to toggle source
# File lib/osx/sugarcube-ui/nsview.rb, line 51
def convert_rect(rect, to: destination)
  return self.convertRect(rect, toView: destination)
end
fade(options={}, more_options={}, &after) click to toggle source

Changes the layer opacity.

# File lib/osx/sugarcube-animations/nsview.rb, line 63
def fade(options={}, more_options={}, &after)
  if options.is_a? Numeric
    options = { opacity: options }
  end

  options[:after] = after

  self.animate(options) do
    self.animator.alpha = options[:opacity]
  end
end
fade_in(options={}, more_options={}, &after) click to toggle source

Changes the layer opacity to 1. @see fade

# File lib/osx/sugarcube-animations/nsview.rb, line 89
def fade_in(options={}, more_options={}, &after)
  if options.is_a? Numeric
    options = more_options.merge(duration: options)
  end

  options[:opacity] ||= 1.0

  fade(options, &after)
end
fade_out(options={}, more_options={}, &after) click to toggle source

Changes the layer opacity to 0. @see fade

# File lib/osx/sugarcube-animations/nsview.rb, line 77
def fade_out(options={}, more_options={}, &after)
  if options.is_a? Numeric
    options = more_options.merge(duration: options)
  end

  options[:opacity] ||= 0.0

  fade(options, &after)
end
fade_out_and_remove(options={}, more_options={}, &after) click to toggle source

Changes the layer opacity to 0 and then removes the view from its superview @see fade_out

# File lib/osx/sugarcube-animations/nsview.rb, line 101
def fade_out_and_remove(options={}, more_options={}, &after)
  if options.is_a? Numeric
    options = more_options.merge(duration: options)
  end

  original_opacity = self.alpha

  after_remove = proc do
    self.alpha = original_opacity
    removeFromSuperview
    after.call if after
  end

  fade_out(options, &after_remove)
end
first_responder() click to toggle source

returns the first responder, or nil if it cannot be found

# File lib/osx/sugarcube-ui/nsview.rb, line 39
def first_responder
  self.window && self.window.firstResponder
end
hide() click to toggle source
# File lib/osx/sugarcube-animations/nsview.rb, line 57
def hide
  self.hidden = true
  return self
end
show() click to toggle source
# File lib/osx/sugarcube-animations/nsview.rb, line 52
def show
  self.hidden = false
  return self
end
sugarcube_to_s(options={}) click to toggle source
# File lib/osx/sugarcube-to_s/nsview.rb, line 3
def sugarcube_to_s(options={})
  if self.respond_to? :stylename and self.stylename
    suffix = ' stylename: ' + self.stylename.inspect
  else
    suffix = ''
  end
  if options[:inner].is_a? Hash
    inner = ''
    options[:inner].each do |key, value|
      inner += ', ' if inner.length > 0
      inner += "#{key}: #{value.inspect}"
    end
  else
    inner = options[:inner]
  end

  "#{self.class.to_s}(##{self.object_id.to_s(16)}, [[#{frame.origin.x}, #{frame.origin.y}], [#{frame.size.width}, #{frame.size.height}]]" +
                      (inner ? ', ' + inner : '') +
                      ')' +
                      (options.fetch(:superview, true) && self.superview ? ", child of #{self.superview.class.to_s}(##{self.superview.object_id.to_s(16)})" : '') +
                      suffix
end
to_s() click to toggle source
# File lib/osx/sugarcube-to_s/nsview.rb, line 26
def to_s
  sugarcube_to_s
end
unshift(view) click to toggle source
# File lib/osx/sugarcube-ui/nsview.rb, line 28
def unshift(view)
  first_view = self.subviews.first
  if first_view
    self.addSubview(view, positioned: NSWindowAbove, relativeTo: first_view)
  else
    self.addSubview(view)
  end
  return self
end