Report abuse

1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#
#  PreferencesController.rb
#  PreferencesExample
#
#  Created by Matthew McCray on 1/18/08.
#  Copyright (c) 2008 Elucidata (www.elucidata.net). All rights reserved.
#

require 'osx/cocoa'

class PreferencesController < OSX::NSWindowController
  include OSX
  
  ib_outlets :generalPrefsView,
             :advancedPrefsView
  
  # This is the target action for the toolbar items
  ib_action :selectPrefPanel do |sender|
    tag =  sender.tag
    view, title = self.viewForTag(tag)
    previousView, prevTitle = self.viewForTag(@currentViewTag)
    @currentViewTag = tag
    newFrame = self.newFrameForNewContentView(view)
    window.title = "#{title} Preferences"
    # Using an animation grouping because we may be changing the duration
    NSAnimationContext.beginGrouping
      # Call the animator instead of the view / window directly
      window.contentView.animator.replaceSubview_with(previousView, view)
      window.animator.setFrame_display newFrame, true
    NSAnimationContext.endGrouping
  end
  
  def init
    self if self.initWithWindowNibName_owner('Preferences', self)
  end
  
  # Delegate method that returns the itemIdentifiers for the selectable items 
  # (in our case, all of 'em)
  def toolbarSelectableItemIdentifiers(toolbar)
    @toolbaritemidents ||= begin
      window.toolbar.items.map {|item| item.itemIdentifier }
    end
  end
  
  def awakeFromNib
    # Select the first toolbar item when the window loads
    window.toolbar.selectedItemIdentifier = window.toolbar.items[0].itemIdentifier
    # Show the initial preference pane
    window.setContentSize @generalPrefsView.frame.size 
    window.contentView.addSubview @generalPrefsView
    window.title = "General Preferences"
    @currentViewTag = 0
    # Will use CoreAnimation for the panel changes:
    window.contentView.wantsLayer = true
  end
  
  def viewForTag(tag)
    case tag
      when 0: [@generalPrefsView,  "General"]
      when 1: [@advancedPrefsView, "Advanced"]
    end
  end
  
  def newFrameForNewContentView(view)
    newFrameRect = window.frameRectForContentRect(view.frame)
    oldFrameRect = window.frame
    newSize = newFrameRect.size
    oldSize = oldFrameRect.size
    frame = window.frame
    frame.size = newSize
    frame.origin.y = frame.origin.y - (newSize.height - oldSize.height)
    frame
  end
end