| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 <Cocoa/Cocoa.h> |
| 6 |
| 7 #include "base/logging.h" // for NOTREACHED() |
| 8 #include "base/mac/mac_util.h" |
| 9 #include "base/sys_string_conversions.h" |
| 10 #include "chrome/app/chrome_command_ids.h" |
| 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 13 #import "chrome/browser/ui/cocoa/animatable_view.h" |
| 14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 15 #include "chrome/browser/ui/cocoa/event_utils.h" |
| 16 #import "chrome/browser/ui/cocoa/fullscreen_exit_bubble_controller.h" |
| 17 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" |
| 18 #include "grit/generated_resources.h" |
| 19 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
| 20 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h" |
| 21 #include "ui/base/models/accelerator_cocoa.h" |
| 22 #include "ui/base/l10n/l10n_util_mac.h" |
| 23 |
| 24 const int kPaddingPx = 8; |
| 25 const int kInitialDelayMs = 3800; |
| 26 const int kSlideOutDurationMs = 700; |
| 27 |
| 28 @interface FullscreenExitBubbleController (PrivateMethods) |
| 29 // Sets |exitLabel_| based on |exitLabelPlaceholder_|, |
| 30 // sets |exitLabelPlaceholder_| to nil. |
| 31 - (void)initializeLabel; |
| 32 |
| 33 - (void)hideSoon; |
| 34 |
| 35 // Returns the Accelerator for the Toggle Fullscreen menu item. |
| 36 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen; |
| 37 |
| 38 // Returns a string representation fit for display of |
| 39 // +acceleratorForToggleFullscreen. |
| 40 + (NSString*)keyCommandString; |
| 41 |
| 42 + (NSString*)keyCombinationForAccelerator:(const ui::AcceleratorCocoa&)item; |
| 43 @end |
| 44 |
| 45 @implementation FullscreenExitBubbleController |
| 46 |
| 47 - (id)initWithOwner:(BrowserWindowController*)owner browser:(Browser*)browser { |
| 48 if ((self = [super initWithNibName:@"FullscreenExitBubble" |
| 49 bundle:base::mac::MainAppBundle()])) { |
| 50 browser_ = browser; |
| 51 owner_ = owner; |
| 52 } |
| 53 return self; |
| 54 } |
| 55 |
| 56 - (void)awakeFromNib { |
| 57 [self initializeLabel]; |
| 58 [self hideSoon]; |
| 59 } |
| 60 |
| 61 - (void)positionInWindowAtTop:(CGFloat)maxY width:(CGFloat)maxWidth { |
| 62 NSRect bubbleFrame = [[self view] frame]; |
| 63 bubbleFrame.origin.x = (int)(maxWidth/2 - NSWidth(bubbleFrame)/2); |
| 64 bubbleFrame.origin.y = maxY - NSHeight(bubbleFrame); |
| 65 [[self view] setFrame:bubbleFrame]; |
| 66 } |
| 67 |
| 68 // Called when someone clicks on the embedded link. |
| 69 - (BOOL) textView:(NSTextView*)textView |
| 70 clickedOnLink:(id)link |
| 71 atIndex:(NSUInteger)charIndex { |
| 72 browser_->ExecuteCommand(IDC_FULLSCREEN); |
| 73 return YES; |
| 74 } |
| 75 |
| 76 - (void)hideTimerFired:(NSTimer*)timer { |
| 77 NSRect endFrame = [[self view] frame]; |
| 78 endFrame.origin.y += endFrame.size.height; |
| 79 endFrame.size.height = 0; |
| 80 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: |
| 81 [self view], NSViewAnimationTargetKey, |
| 82 [NSValue valueWithRect:endFrame], NSViewAnimationEndFrameKey, nil]; |
| 83 |
| 84 NSViewAnimation* animation = |
| 85 [[NSViewAnimation alloc] |
| 86 initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]]; |
| 87 [animation gtm_setDuration:kSlideOutDurationMs/1000.0 |
| 88 eventMask:NSLeftMouseUpMask]; |
| 89 [animation setDelegate:self]; |
| 90 [animation startAnimation]; |
| 91 hideAnimation_.reset(animation); |
| 92 } |
| 93 |
| 94 - (void)animationDidEnd:(NSAnimation*)animation { |
| 95 if (animation == hideAnimation_.get()) { |
| 96 hideAnimation_.reset(); |
| 97 } |
| 98 } |
| 99 |
| 100 - (AnimatableView*)animatableView { |
| 101 return static_cast<AnimatableView*>([self view]); |
| 102 } |
| 103 |
| 104 - (void)dealloc { |
| 105 [hideAnimation_.get() stopAnimation]; |
| 106 [hideTimer_ invalidate]; |
| 107 [super dealloc]; |
| 108 } |
| 109 |
| 110 @end |
| 111 |
| 112 @implementation FullscreenExitBubbleController (PrivateMethods) |
| 113 |
| 114 - (void)initializeLabel { |
| 115 // Replace the label placeholder NSTextField with the real label NSTextView. |
| 116 // The former doesn't show links in a nice way, but the latter can't be added |
| 117 // in IB without a containing scroll view, so create the NSTextView |
| 118 // programmatically. |
| 119 exitLabel_.reset([[HyperlinkTextView alloc] |
| 120 initWithFrame:[exitLabelPlaceholder_ frame]]); |
| 121 [exitLabel_.get() setAutoresizingMask: |
| 122 [exitLabelPlaceholder_ autoresizingMask]]; |
| 123 [[exitLabelPlaceholder_ superview] |
| 124 replaceSubview:exitLabelPlaceholder_ with:exitLabel_.get()]; |
| 125 exitLabelPlaceholder_ = nil; // Now released. |
| 126 [exitLabel_.get() setDelegate:self]; |
| 127 |
| 128 NSString *message = l10n_util::GetNSStringF(IDS_EXIT_FULLSCREEN_MODE, |
| 129 base::SysNSStringToUTF16([[self class] keyCommandString])); |
| 130 |
| 131 [(HyperlinkTextView*)exitLabel_.get() |
| 132 setMessageAndLink:@"" |
| 133 withLink:message |
| 134 atOffset:0 |
| 135 font:[NSFont systemFontOfSize:18] |
| 136 messageColor:[NSColor whiteColor] |
| 137 linkColor:[NSColor whiteColor]]; |
| 138 |
| 139 [exitLabel_.get() sizeToFit]; |
| 140 NSLayoutManager* layoutManager = [exitLabel_.get() layoutManager]; |
| 141 NSTextContainer* textContainer = [exitLabel_.get() textContainer]; |
| 142 [layoutManager ensureLayoutForTextContainer:textContainer]; |
| 143 NSRect textFrame = [layoutManager usedRectForTextContainer:textContainer]; |
| 144 NSRect frame = [[self view] frame]; |
| 145 NSSize textSize = textFrame.size; |
| 146 frame.size.width = textSize.width + 2 * kPaddingPx; |
| 147 [[self view] setFrame:frame]; |
| 148 textFrame.origin.x = textFrame.origin.y = kPaddingPx; |
| 149 [exitLabel_.get() setFrame:textFrame]; |
| 150 } |
| 151 |
| 152 // This looks at the Main Menu and determines what the user has set as the |
| 153 // key combination for quit. It then gets the modifiers and builds an object |
| 154 // to hold the data. |
| 155 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen { |
| 156 NSMenu* mainMenu = [NSApp mainMenu]; |
| 157 // Get the application menu (i.e. Chromium). |
| 158 for (NSMenuItem* menu in [mainMenu itemArray]) { |
| 159 for (NSMenuItem* item in [[menu submenu] itemArray]) { |
| 160 // Find the toggle presentation mode item. |
| 161 if ([item tag] == IDC_PRESENTATION_MODE) { |
| 162 return ui::AcceleratorCocoa([item keyEquivalent], |
| 163 [item keyEquivalentModifierMask]); |
| 164 } |
| 165 } |
| 166 } |
| 167 // Default to Cmd+Shift+F. |
| 168 return ui::AcceleratorCocoa(@"f", NSCommandKeyMask|NSShiftKeyMask); |
| 169 } |
| 170 |
| 171 // This looks at the Main Menu and determines what the user has set as the |
| 172 // key combination for quit. It then gets the modifiers and builds a string |
| 173 // to display them. |
| 174 + (NSString*)keyCommandString { |
| 175 ui::AcceleratorCocoa accelerator = |
| 176 [[self class] acceleratorForToggleFullscreen]; |
| 177 return [[self class] keyCombinationForAccelerator:accelerator]; |
| 178 } |
| 179 |
| 180 + (NSString*)keyCombinationForAccelerator:(const ui::AcceleratorCocoa&)item { |
| 181 NSMutableString* string = [NSMutableString string]; |
| 182 NSUInteger modifiers = item.modifiers(); |
| 183 |
| 184 if (modifiers & NSCommandKeyMask) |
| 185 [string appendString:@"\u2318"]; |
| 186 if (modifiers & NSControlKeyMask) |
| 187 [string appendString:@"\u2303"]; |
| 188 if (modifiers & NSAlternateKeyMask) |
| 189 [string appendString:@"\u2325"]; |
| 190 BOOL isUpperCase = [[NSCharacterSet uppercaseLetterCharacterSet] |
| 191 characterIsMember:[item.characters() characterAtIndex:0]]; |
| 192 if (modifiers & NSShiftKeyMask || isUpperCase) |
| 193 [string appendString:@"\u21E7"]; |
| 194 |
| 195 [string appendString:[item.characters() uppercaseString]]; |
| 196 return string; |
| 197 } |
| 198 |
| 199 - (void)hideSoon { |
| 200 hideTimer_.reset( |
| 201 [[NSTimer scheduledTimerWithTimeInterval:kInitialDelayMs/1000.0 |
| 202 target:self |
| 203 selector:@selector(hideTimerFired:) |
| 204 userInfo:nil |
| 205 repeats:NO] retain]); |
| 206 } |
| 207 |
| 208 @end |
| OLD | NEW |