| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/bundle_locations.h" | |
| 9 #include "base/strings/sys_string_conversions.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "chrome/app/chrome_command_ids.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/ui/browser_commands.h" | |
| 14 #import "chrome/browser/ui/cocoa/exclusive_access_bubble_window_controller.h" | |
| 15 #import "chrome/browser/ui/cocoa/info_bubble_view.h" | |
| 16 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | |
| 17 #import "chrome/browser/ui/cocoa/tabs/tab_window_controller.h" | |
| 18 #include "chrome/browser/ui/exclusive_access/exclusive_access_bubble_type.h" | |
| 19 #include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h" | |
| 20 #include "chrome/browser/ui/exclusive_access/fullscreen_controller.h" | |
| 21 #include "chrome/grit/generated_resources.h" | |
| 22 #include "extensions/browser/extension_registry.h" | |
| 23 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h
" | |
| 24 #include "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutT
weaker.h" | |
| 25 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTw
eaker.h" | |
| 26 #include "ui/base/accelerators/platform_accelerator_cocoa.h" | |
| 27 #import "ui/base/cocoa/controls/hyperlink_text_view.h" | |
| 28 #include "ui/base/l10n/l10n_util.h" | |
| 29 #include "ui/base/l10n/l10n_util_mac.h" | |
| 30 #include "ui/strings/grit/ui_strings.h" | |
| 31 | |
| 32 namespace { | |
| 33 const float kInitialDelay = 3.8; | |
| 34 const float kHideDuration = 0.7; | |
| 35 } // namespace | |
| 36 | |
| 37 @interface OneClickHyperlinkTextView : HyperlinkTextView | |
| 38 @end | |
| 39 @implementation OneClickHyperlinkTextView | |
| 40 - (BOOL)acceptsFirstMouse:(NSEvent*)event { | |
| 41 return YES; | |
| 42 } | |
| 43 @end | |
| 44 | |
| 45 @interface ExclusiveAccessBubbleWindowController (PrivateMethods) | |
| 46 // Sets |exitLabel_| based on |exitLabelPlaceholder_|, | |
| 47 // sets |exitLabelPlaceholder_| to nil, | |
| 48 // sets |denyButton_| text based on |bubbleType_|. | |
| 49 - (void)initializeLabelAndButton; | |
| 50 | |
| 51 - (NSString*)getLabelText; | |
| 52 | |
| 53 - (void)hideSoon; | |
| 54 | |
| 55 + (NSString*)keyCombinationForAccelerator: | |
| 56 (const ui::PlatformAcceleratorCocoa&)item; | |
| 57 @end | |
| 58 | |
| 59 @implementation ExclusiveAccessBubbleWindowController | |
| 60 | |
| 61 - (id)initWithOwner:(NSWindowController*)owner | |
| 62 exclusive_access_manager:(ExclusiveAccessManager*)exclusive_access_manager | |
| 63 profile:(Profile*)profile | |
| 64 url:(const GURL&)url | |
| 65 bubbleType:(ExclusiveAccessBubbleType)bubbleType { | |
| 66 NSString* nibPath = | |
| 67 [base::mac::FrameworkBundle() pathForResource:@"ExclusiveAccessBubble" | |
| 68 ofType:@"nib"]; | |
| 69 if ((self = [super initWithWindowNibPath:nibPath owner:self])) { | |
| 70 exclusive_access_manager_ = exclusive_access_manager; | |
| 71 profile_ = profile; | |
| 72 owner_ = owner; | |
| 73 url_ = url; | |
| 74 bubbleType_ = bubbleType; | |
| 75 // Mouse lock expects mouse events to reach the main window immediately. | |
| 76 // Make the bubble transparent for mouse events if mouse lock is enabled. | |
| 77 if (bubbleType_ == | |
| 78 EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_MOUSELOCK_EXIT_INSTRUCTION |
| | |
| 79 bubbleType_ == EXCLUSIVE_ACCESS_BUBBLE_TYPE_MOUSELOCK_EXIT_INSTRUCTION) | |
| 80 [[self window] setIgnoresMouseEvents:YES]; | |
| 81 } | |
| 82 return self; | |
| 83 } | |
| 84 | |
| 85 - (void)allow:(id)sender { | |
| 86 // The mouselock code expects that mouse events reach the main window | |
| 87 // immediately, but the cursor is still over the bubble, which eats the | |
| 88 // mouse events. Make the bubble transparent for mouse events. | |
| 89 if (bubbleType_ == | |
| 90 EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_MOUSELOCK_BUTTONS || | |
| 91 bubbleType_ == EXCLUSIVE_ACCESS_BUBBLE_TYPE_MOUSELOCK_BUTTONS) | |
| 92 [[self window] setIgnoresMouseEvents:YES]; | |
| 93 | |
| 94 DCHECK(exclusive_access_bubble::ShowButtonsForType(bubbleType_)); | |
| 95 exclusive_access_manager_->OnAcceptExclusiveAccessPermission(); | |
| 96 } | |
| 97 | |
| 98 - (void)deny:(id)sender { | |
| 99 DCHECK(exclusive_access_bubble::ShowButtonsForType(bubbleType_)); | |
| 100 exclusive_access_manager_->OnDenyExclusiveAccessPermission(); | |
| 101 } | |
| 102 | |
| 103 - (void)showButtons:(BOOL)show { | |
| 104 [allowButton_ setHidden:!show]; | |
| 105 [denyButton_ setHidden:!show]; | |
| 106 [exitLabel_ setHidden:show]; | |
| 107 } | |
| 108 | |
| 109 // We want this to be a child of a browser window. addChildWindow: | |
| 110 // (called from this function) will bring the window on-screen; | |
| 111 // unfortunately, [NSWindowController showWindow:] will also bring it | |
| 112 // on-screen (but will cause unexpected changes to the window's | |
| 113 // position). We cannot have an addChildWindow: and a subsequent | |
| 114 // showWindow:. Thus, we have our own version. | |
| 115 - (void)showWindow { | |
| 116 // Completes nib load. | |
| 117 InfoBubbleWindow* info_bubble = static_cast<InfoBubbleWindow*>([self window]); | |
| 118 [info_bubble setInfoBubbleCanBecomeKeyWindow:NO]; | |
| 119 if (!exclusive_access_bubble::ShowButtonsForType(bubbleType_)) { | |
| 120 [self hideSoon]; | |
| 121 } | |
| 122 [tweaker_ tweakUI:info_bubble]; | |
| 123 [[owner_ window] addChildWindow:info_bubble ordered:NSWindowAbove]; | |
| 124 | |
| 125 if ([owner_ respondsToSelector:@selector(layoutSubviews)]) | |
| 126 [(id)owner_ layoutSubviews]; | |
| 127 | |
| 128 [info_bubble orderFront:self]; | |
| 129 } | |
| 130 | |
| 131 - (void)awakeFromNib { | |
| 132 DCHECK([[self window] isKindOfClass:[InfoBubbleWindow class]]); | |
| 133 [messageLabel_ setStringValue:[self getLabelText]]; | |
| 134 [self initializeLabelAndButton]; | |
| 135 } | |
| 136 | |
| 137 - (void)positionInWindowAtTop:(CGFloat)maxY { | |
| 138 NSRect windowFrame = [self window].frame; | |
| 139 NSRect ownerWindowFrame = [owner_ window].frame; | |
| 140 NSPoint origin; | |
| 141 origin.x = ownerWindowFrame.origin.x + | |
| 142 (int)(NSWidth(ownerWindowFrame) / 2 - NSWidth(windowFrame) / 2); | |
| 143 origin.y = ownerWindowFrame.origin.y + maxY - NSHeight(windowFrame); | |
| 144 [[self window] setFrameOrigin:origin]; | |
| 145 } | |
| 146 | |
| 147 // Called when someone clicks on the embedded link. | |
| 148 - (BOOL)textView:(NSTextView*)textView | |
| 149 clickedOnLink:(id)link | |
| 150 atIndex:(NSUInteger)charIndex { | |
| 151 exclusive_access_manager_->fullscreen_controller() | |
| 152 ->ExitExclusiveAccessToPreviousState(); | |
| 153 return YES; | |
| 154 } | |
| 155 | |
| 156 - (void)hideTimerFired:(NSTimer*)timer { | |
| 157 // This might fire racily for buttoned bubbles, even though the timer is | |
| 158 // cancelled for them. Explicitly check for this case. | |
| 159 if (exclusive_access_bubble::ShowButtonsForType(bubbleType_)) | |
| 160 return; | |
| 161 | |
| 162 [NSAnimationContext beginGrouping]; | |
| 163 [[NSAnimationContext currentContext] | |
| 164 gtm_setDuration:kHideDuration | |
| 165 eventMask:NSLeftMouseUpMask | NSLeftMouseDownMask]; | |
| 166 [[[self window] animator] setAlphaValue:0.0]; | |
| 167 [NSAnimationContext endGrouping]; | |
| 168 } | |
| 169 | |
| 170 - (void)animationDidEnd:(NSAnimation*)animation { | |
| 171 if (animation == hideAnimation_.get()) { | |
| 172 hideAnimation_.reset(); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 - (void)closeImmediately { | |
| 177 // Without this, quitting fullscreen with esc will let the bubble reappear | |
| 178 // once the "exit fullscreen" animation is done on lion. | |
| 179 InfoBubbleWindow* infoBubble = static_cast<InfoBubbleWindow*>([self window]); | |
| 180 [[infoBubble parentWindow] removeChildWindow:infoBubble]; | |
| 181 [hideAnimation_.get() stopAnimation]; | |
| 182 [hideTimer_ invalidate]; | |
| 183 [infoBubble setAllowedAnimations:info_bubble::kAnimateNone]; | |
| 184 [self close]; | |
| 185 } | |
| 186 | |
| 187 - (void)dealloc { | |
| 188 [hideAnimation_.get() stopAnimation]; | |
| 189 [hideTimer_ invalidate]; | |
| 190 [super dealloc]; | |
| 191 } | |
| 192 | |
| 193 @end | |
| 194 | |
| 195 @implementation ExclusiveAccessBubbleWindowController (PrivateMethods) | |
| 196 | |
| 197 - (void)initializeLabelAndButton { | |
| 198 // Replace the label placeholder NSTextField with the real label NSTextView. | |
| 199 // The former doesn't show links in a nice way, but the latter can't be added | |
| 200 // in IB without a containing scroll view, so create the NSTextView | |
| 201 // programmatically. | |
| 202 exitLabel_.reset([[OneClickHyperlinkTextView alloc] | |
| 203 initWithFrame:[exitLabelPlaceholder_ frame]]); | |
| 204 [exitLabel_.get() | |
| 205 setAutoresizingMask:[exitLabelPlaceholder_ autoresizingMask]]; | |
| 206 [exitLabel_.get() setHidden:[exitLabelPlaceholder_ isHidden]]; | |
| 207 [[exitLabelPlaceholder_ superview] replaceSubview:exitLabelPlaceholder_ | |
| 208 with:exitLabel_.get()]; | |
| 209 exitLabelPlaceholder_ = nil; // Now released. | |
| 210 [exitLabel_.get() setDelegate:self]; | |
| 211 | |
| 212 NSString* exitLinkText; | |
| 213 NSString* exitLinkedText; | |
| 214 if (bubbleType_ == | |
| 215 EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_MOUSELOCK_EXIT_INSTRUCTION || | |
| 216 bubbleType_ == EXCLUSIVE_ACCESS_BUBBLE_TYPE_MOUSELOCK_EXIT_INSTRUCTION) { | |
| 217 exitLinkText = @""; | |
| 218 exitLinkedText = | |
| 219 [@" " stringByAppendingString:l10n_util::GetNSStringF( | |
| 220 IDS_FULLSCREEN_PRESS_ESC_TO_EXIT_SENTENCE, | |
| 221 l10n_util::GetStringUTF16( | |
| 222 IDS_APP_ESC_KEY))]; | |
| 223 } else { | |
| 224 exitLinkText = l10n_util::GetNSString(IDS_EXIT_FULLSCREEN_MODE); | |
| 225 NSString* messageText = l10n_util::GetNSStringF( | |
| 226 IDS_EXIT_FULLSCREEN_MODE_ACCELERATOR, | |
| 227 l10n_util::GetStringUTF16(IDS_APP_ESC_KEY)); | |
| 228 exitLinkedText = | |
| 229 [NSString stringWithFormat:@"%@ %@", exitLinkText, messageText]; | |
| 230 } | |
| 231 | |
| 232 NSFont* font = [NSFont | |
| 233 systemFontOfSize:[NSFont | |
| 234 systemFontSizeForControlSize:NSRegularControlSize]]; | |
| 235 [exitLabel_.get() setMessage:exitLinkedText | |
| 236 withFont:font | |
| 237 messageColor:[NSColor blackColor]]; | |
| 238 if ([exitLinkText length] != 0) { | |
| 239 [exitLabel_.get() addLinkRange:NSMakeRange(0, [exitLinkText length]) | |
| 240 withURL:nil | |
| 241 linkColor:[NSColor blueColor]]; | |
| 242 } | |
| 243 [exitLabel_.get() setAlignment:NSRightTextAlignment]; | |
| 244 | |
| 245 NSRect labelFrame = [exitLabel_ frame]; | |
| 246 | |
| 247 // NSTextView's sizeToFit: method seems to enjoy wrapping lines. Temporarily | |
| 248 // set the size large to force it not to. | |
| 249 NSRect windowFrame = [[self window] frame]; | |
| 250 [exitLabel_ setFrameSize:windowFrame.size]; | |
| 251 NSLayoutManager* layoutManager = [exitLabel_ layoutManager]; | |
| 252 NSTextContainer* textContainer = [exitLabel_ textContainer]; | |
| 253 [layoutManager ensureLayoutForTextContainer:textContainer]; | |
| 254 NSRect textFrame = [layoutManager usedRectForTextContainer:textContainer]; | |
| 255 | |
| 256 textFrame.size.width = ceil(NSWidth(textFrame)); | |
| 257 labelFrame.origin.x += NSWidth(labelFrame) - NSWidth(textFrame); | |
| 258 labelFrame.size = textFrame.size; | |
| 259 [exitLabel_ setFrame:labelFrame]; | |
| 260 | |
| 261 // Update the title of |allowButton_| and |denyButton_| according to the | |
| 262 // current |bubbleType_|, or show no button at all. | |
| 263 if (exclusive_access_bubble::ShowButtonsForType(bubbleType_)) { | |
| 264 NSString* denyButtonText = | |
| 265 SysUTF16ToNSString( | |
| 266 exclusive_access_bubble::GetDenyButtonTextForType(bubbleType_)); | |
| 267 [denyButton_ setTitle:denyButtonText]; | |
| 268 NSString* allowButtonText = SysUTF16ToNSString( | |
| 269 exclusive_access_bubble::GetAllowButtonTextForType(bubbleType_, url_)); | |
| 270 [allowButton_ setTitle:allowButtonText]; | |
| 271 } else { | |
| 272 [self showButtons:NO]; | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 - (NSString*)getLabelText { | |
| 277 if (bubbleType_ == EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE) | |
| 278 return @""; | |
| 279 extensions::ExtensionRegistry* registry = | |
| 280 extensions::ExtensionRegistry::Get(profile_); | |
| 281 return SysUTF16ToNSString(exclusive_access_bubble::GetLabelTextForType( | |
| 282 bubbleType_, url_, registry)); | |
| 283 } | |
| 284 | |
| 285 + (NSString*)keyCombinationForAccelerator: | |
| 286 (const ui::PlatformAcceleratorCocoa&)item { | |
| 287 NSMutableString* string = [NSMutableString string]; | |
| 288 NSUInteger modifiers = item.modifier_mask(); | |
| 289 | |
| 290 if (modifiers & NSCommandKeyMask) | |
| 291 [string appendString:@"\u2318"]; | |
| 292 if (modifiers & NSControlKeyMask) | |
| 293 [string appendString:@"\u2303"]; | |
| 294 if (modifiers & NSAlternateKeyMask) | |
| 295 [string appendString:@"\u2325"]; | |
| 296 BOOL isUpperCase = [[NSCharacterSet uppercaseLetterCharacterSet] | |
| 297 characterIsMember:[item.characters() characterAtIndex:0]]; | |
| 298 if (modifiers & NSShiftKeyMask || isUpperCase) | |
| 299 [string appendString:@"\u21E7"]; | |
| 300 | |
| 301 [string appendString:[item.characters() uppercaseString]]; | |
| 302 return string; | |
| 303 } | |
| 304 | |
| 305 - (void)hideSoon { | |
| 306 hideTimer_.reset( | |
| 307 [[NSTimer scheduledTimerWithTimeInterval:kInitialDelay | |
| 308 target:self | |
| 309 selector:@selector(hideTimerFired:) | |
| 310 userInfo:nil | |
| 311 repeats:NO] retain]); | |
| 312 } | |
| 313 | |
| 314 @end | |
| OLD | NEW |