Class: NSView

Inherits:
Object show all
Includes:
SugarCube::Frameable
Defined in:
lib/osx/sugarcube-ui/nsview.rb,
lib/osx/sugarcube-to_s/nsview.rb,
lib/osx/sugarcube-ui/frameable.rb,
lib/osx/sugarcube-animations/nsview.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SugarCube::Frameable

#height, #setHeight, #setWidth, #setX, #setY, #width, #x, #y

Class Method Details

.animate(options = {}, more_options = {}, &animations) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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) ⇒ Object

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



6
7
8
# File 'lib/osx/sugarcube-animations/nsview.rb', line 6

def sugarcube_animation_options(options)
  {}
end

Instance Method Details

#<<(view) ⇒ Object

superview << view

> superview.addSubview(view)



23
24
25
26
# File 'lib/osx/sugarcube-ui/nsview.rb', line 23

def <<(view)
  self.addSubview(view)
  return self
end

#animate(options = {}, more_options = {}, &animations) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Object



47
48
49
# 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) ⇒ Object



43
44
45
# 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) ⇒ Object



63
64
65
# 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) ⇒ Object



59
60
61
# 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, from: source) ⇒ Object



67
68
69
# 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, from: source) ⇒ Object



51
52
53
# 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) ⇒ Object

Changes the layer opacity.



63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object

Changes the layer opacity to 1.

See Also:



89
90
91
92
93
94
95
96
97
# 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) ⇒ Object

Changes the layer opacity to 0.

See Also:



77
78
79
80
81
82
83
84
85
# 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) ⇒ Object

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

See Also:



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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_responderObject

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



39
40
41
# File 'lib/osx/sugarcube-ui/nsview.rb', line 39

def first_responder
  self.window && self.window.firstResponder
end

#hideObject



57
58
59
60
# File 'lib/osx/sugarcube-animations/nsview.rb', line 57

def hide
  self.hidden = true
  return self
end

#showObject



52
53
54
55
# File 'lib/osx/sugarcube-animations/nsview.rb', line 52

def show
  self.hidden = false
  return self
end

#sugarcube_to_s(options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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_sObject



26
27
28
# File 'lib/osx/sugarcube-to_s/nsview.rb', line 26

def to_s
  sugarcube_to_s
end

#unshift(view) ⇒ Object



28
29
30
31
32
33
34
35
36
# 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