OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/cocoa/disclosure_view_controller.h" |
| 6 #include "base/logging.h" |
| 7 #include "base/scoped_nsobject.h" |
| 8 |
| 9 const NSCellStateValue kInitialDisclosureState = NSOffState; |
| 10 const NSInteger kClosedBoxHeight = 20; |
| 11 NSString* const kKVODisclosedKey = @"disclosed"; |
| 12 |
| 13 // This class externalizes the state of the disclosure control. When the |
| 14 // disclosure control is pressed it changes the state of this object. In turn |
| 15 // the KVO machinery detects the change to |disclosed| and signals the |
| 16 // |observeValueForKeyPath| call in the |DisclosureViewController|. |
| 17 @interface DisclosureViewState : NSObject { |
| 18 @private |
| 19 NSCellStateValue disclosed; |
| 20 } |
| 21 @end |
| 22 |
| 23 @implementation DisclosureViewState |
| 24 @end |
| 25 |
| 26 @interface DisclosureViewController(PrivateMethods) |
| 27 |
| 28 - (void)initDisclosureState:(NSCellStateValue)state; |
| 29 - (NSRect)openStateFrameSize:(NSRect)startFrame; |
| 30 - (NSRect)closedStateFrameSize:(NSRect)startFrame; |
| 31 |
| 32 - (void)startAnimations:(NSView*)view |
| 33 start:(NSRect)startFrame |
| 34 end:(NSRect)endFrame; |
| 35 |
| 36 - (void)discloseDetails:(NSCellStateValue)state; |
| 37 |
| 38 - (void)observeValueForKeyPath:(NSString*)keyPath |
| 39 ofObject:(id)object |
| 40 change:(NSDictionary*)change |
| 41 context:(void*)context; |
| 42 |
| 43 @end |
| 44 |
| 45 @implementation DisclosureViewController |
| 46 |
| 47 @synthesize disclosureState = disclosureState_; |
| 48 |
| 49 - (void)awakeFromNib { |
| 50 // Create the disclosure state. |
| 51 [self setDisclosureState:[[[DisclosureViewState alloc] init] autorelease]]; |
| 52 |
| 53 // Set up the initial disclosure state before we install the observer. |
| 54 // We don't want our animations firing before we're done initializing. |
| 55 [disclosureState_ setValue:[NSNumber numberWithInt:kInitialDisclosureState] |
| 56 forKey:kKVODisclosedKey]; |
| 57 |
| 58 // Pick up "open" height from the initial state of the view in the nib. |
| 59 openHeight_ = [[self view] frame].size.height; |
| 60 |
| 61 // Set frame size according to initial disclosure state. |
| 62 [self initDisclosureState:kInitialDisclosureState]; |
| 63 |
| 64 // Setup observers so that when disclosure state changes we resize frame |
| 65 // accordingly. |
| 66 [disclosureState_ addObserver:self forKeyPath:kKVODisclosedKey |
| 67 options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld |
| 68 context:nil]; |
| 69 } |
| 70 |
| 71 - (void)dealloc { |
| 72 [disclosureState_ removeObserver:self forKeyPath:kKVODisclosedKey]; |
| 73 [disclosureState_ release]; |
| 74 [super dealloc]; |
| 75 } |
| 76 |
| 77 @end |
| 78 |
| 79 @implementation DisclosureViewController(PrivateMethods) |
| 80 |
| 81 // Initializes the view's frame geometry based on the input |state|. |
| 82 // If the |state| is NSOnState then the frame size corresponds to "open". |
| 83 // If the |state| is NSOffState then the frame size corresponds to "closed". |
| 84 // The |origin.x| and |size.width| remain unchanged, but the |origin.y| and |
| 85 // |size.height| may vary. |
| 86 - (void)initDisclosureState:(NSCellStateValue)state { |
| 87 if (state == NSOnState) { |
| 88 [[self view] setFrame:[self openStateFrameSize:[[self view] frame]]]; |
| 89 } |
| 90 else if (state == NSOffState) { |
| 91 [[self view] setFrame:[self closedStateFrameSize:[[self view] frame]]]; |
| 92 } |
| 93 else { |
| 94 NOTREACHED(); |
| 95 } |
| 96 } |
| 97 |
| 98 // Computes the frame geometry during the "open" state of the disclosure view. |
| 99 - (NSRect)openStateFrameSize:(NSRect)startFrame { |
| 100 return NSMakeRect(startFrame.origin.x, |
| 101 startFrame.size.height - openHeight_ + |
| 102 startFrame.origin.y, |
| 103 startFrame.size.width, |
| 104 openHeight_); |
| 105 } |
| 106 |
| 107 // Computes the frame geometry during the "closed" state of the disclosure view. |
| 108 - (NSRect)closedStateFrameSize:(NSRect)startFrame { |
| 109 return NSMakeRect(startFrame.origin.x, |
| 110 startFrame.size.height - kClosedBoxHeight + |
| 111 startFrame.origin.y, |
| 112 startFrame.size.width, |
| 113 kClosedBoxHeight); |
| 114 } |
| 115 |
| 116 // Animates the opening or closing of the disclosure view. The |startFrame| |
| 117 // specifies the frame geometry at the beginning of the animation and the |
| 118 // |endFrame| specifies the geometry at the end of the animation. The input |
| 119 // |view| is view managed by this controller. |
| 120 - (void)startAnimations:(NSView*)view |
| 121 start:(NSRect)startFrame |
| 122 end:(NSRect)endFrame |
| 123 { |
| 124 // Setup dictionary describing animation. |
| 125 // Create the attributes dictionary for the first view. |
| 126 NSMutableDictionary* dictionary; |
| 127 dictionary = [NSDictionary dictionaryWithObjectsAndKeys: |
| 128 // Specify which view to modify. |
| 129 view, NSViewAnimationTargetKey, |
| 130 // Specify the starting position of the view. |
| 131 [NSValue valueWithRect:startFrame], NSViewAnimationStartFrameKey, |
| 132 // Change the ending position of the view. |
| 133 [NSValue valueWithRect:endFrame], NSViewAnimationEndFrameKey, |
| 134 nil]; |
| 135 |
| 136 // Create the view animation object. |
| 137 scoped_nsobject<NSViewAnimation> animation; |
| 138 animation.reset([[NSViewAnimation alloc] initWithViewAnimations: |
| 139 [NSArray arrayWithObject:dictionary]]); |
| 140 |
| 141 // Set some additional attributes for the animation. |
| 142 [animation.get() setDuration:.2]; |
| 143 [animation.get() setAnimationCurve:NSAnimationEaseIn]; |
| 144 |
| 145 // Run the animation. |
| 146 [animation.get() startAnimation]; |
| 147 } |
| 148 |
| 149 // This method is invoked when the disclosure state changes. It computes |
| 150 // the appropriate view frame geometry and then initiates the animation to |
| 151 // change that geometry. |
| 152 - (void)discloseDetails:(NSCellStateValue)state { |
| 153 NSRect startFrame = [[self view] frame]; |
| 154 NSRect endFrame = startFrame; |
| 155 |
| 156 if (state == NSOnState) { |
| 157 endFrame = [self openStateFrameSize:startFrame]; |
| 158 } else if (state == NSOffState) { |
| 159 endFrame = [self closedStateFrameSize:startFrame]; |
| 160 } else { |
| 161 NOTREACHED(); |
| 162 return; |
| 163 } |
| 164 |
| 165 [self startAnimations:[self view] start:startFrame end:endFrame]; |
| 166 } |
| 167 |
| 168 // The |DisclosureViewController| is an observer of an instance of a |
| 169 // |DisclosureViewState| object. This object lives within the controller's |
| 170 // nib file. When the KVO machinery detects a change to the state |
| 171 // it triggers this call and we initiate the change in frame geometry of the |
| 172 // view. |
| 173 - (void)observeValueForKeyPath:(NSString*)keyPath |
| 174 ofObject:(id)object |
| 175 change:(NSDictionary*)change |
| 176 context:(void*)context { |
| 177 if ([keyPath isEqualToString:kKVODisclosedKey]) { |
| 178 NSCellStateValue newValue = |
| 179 [[change objectForKey:NSKeyValueChangeNewKey] intValue]; |
| 180 NSCellStateValue oldValue = |
| 181 [[change objectForKey:NSKeyValueChangeOldKey] intValue]; |
| 182 |
| 183 if (newValue != oldValue) { |
| 184 [self discloseDetails:newValue]; |
| 185 } |
| 186 } |
| 187 } |
| 188 |
| 189 @end |
OLD | NEW |