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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Cocoa/Cocoa.h>
#include "base/logging.h" // for NOTREACHED()
#include "base/mac/bundle_locations.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#import "chrome/browser/ui/cocoa/exclusive_access_bubble_window_controller.h"
#import "chrome/browser/ui/cocoa/info_bubble_view.h"
#import "chrome/browser/ui/cocoa/info_bubble_window.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_bubble_type.h"
#include "chrome/browser/ui/exclusive_access/fullscreen_controller.h"
#include "chrome/grit/generated_resources.h"
#include "extensions/browser/extension_registry.h"
#import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h"
#include "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
#import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
#include "ui/base/accelerators/platform_accelerator_cocoa.h"
#import "ui/base/cocoa/controls/hyperlink_text_view.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h"
#include "ui/strings/grit/ui_strings.h"
namespace {
const float kInitialDelay = 3.8;
const float kHideDuration = 0.7;
} // namespace
@interface OneClickHyperlinkTextView : HyperlinkTextView
@end
@implementation OneClickHyperlinkTextView
- (BOOL)acceptsFirstMouse:(NSEvent*)event {
return YES;
}
@end
@interface ExclusiveAccessBubbleWindowController (PrivateMethods)
// Sets |exitLabel_| based on |exitLabelPlaceholder_|,
// sets |exitLabelPlaceholder_| to nil.
- (void)initializeLabel;
- (NSString*)getLabelText;
- (void)hideSoon;
// Returns the Accelerator for the Toggle Fullscreen menu item.
+ (scoped_ptr<ui::PlatformAcceleratorCocoa>)acceleratorForToggleFullscreen;
// Returns a string representation fit for display of
// +acceleratorForToggleFullscreen.
+ (NSString*)keyCommandString;
+ (NSString*)keyCombinationForAccelerator:
(const ui::PlatformAcceleratorCocoa&)item;
@end
@implementation ExclusiveAccessBubbleWindowController
- (id)initWithOwner:(BrowserWindowController*)owner
browser:(Browser*)browser
url:(const GURL&)url
bubbleType:(ExclusiveAccessBubbleType)bubbleType {
NSString* nibPath =
[base::mac::FrameworkBundle() pathForResource:@"ExclusiveAccessBubble"
ofType:@"nib"];
if ((self = [super initWithWindowNibPath:nibPath owner:self])) {
browser_ = browser;
owner_ = owner;
url_ = url;
bubbleType_ = bubbleType;
// Mouse lock expects mouse events to reach the main window immediately.
// Make the bubble transparent for mouse events if mouse lock is enabled.
if (bubbleType_ ==
EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_MOUSELOCK_EXIT_INSTRUCTION ||
bubbleType_ == EXCLUSIVE_ACCESS_BUBBLE_TYPE_MOUSELOCK_EXIT_INSTRUCTION)
[[self window] setIgnoresMouseEvents:YES];
}
return self;
}
- (void)allow:(id)sender {
// The mouselock code expects that mouse events reach the main window
// immediately, but the cursor is still over the bubble, which eats the
// mouse events. Make the bubble transparent for mouse events.
if (bubbleType_ ==
EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_MOUSELOCK_BUTTONS ||
bubbleType_ == EXCLUSIVE_ACCESS_BUBBLE_TYPE_MOUSELOCK_BUTTONS)
[[self window] setIgnoresMouseEvents:YES];
DCHECK(exclusive_access_bubble::ShowButtonsForType(bubbleType_));
browser_->fullscreen_controller()->OnAcceptFullscreenPermission();
}
- (void)deny:(id)sender {
DCHECK(exclusive_access_bubble::ShowButtonsForType(bubbleType_));
browser_->fullscreen_controller()->OnDenyFullscreenPermission();
}
- (void)showButtons:(BOOL)show {
[allowButton_ setHidden:!show];
[denyButton_ setHidden:!show];
[exitLabel_ setHidden:show];
}
// We want this to be a child of a browser window. addChildWindow:
// (called from this function) will bring the window on-screen;
// unfortunately, [NSWindowController showWindow:] will also bring it
// on-screen (but will cause unexpected changes to the window's
// position). We cannot have an addChildWindow: and a subsequent
// showWindow:. Thus, we have our own version.
- (void)showWindow {
// Completes nib load.
InfoBubbleWindow* info_bubble = static_cast<InfoBubbleWindow*>([self window]);
[info_bubble setCanBecomeKeyWindow:NO];
if (!exclusive_access_bubble::ShowButtonsForType(bubbleType_)) {
[self showButtons:NO];
[self hideSoon];
}
[tweaker_ tweakUI:info_bubble];
[[owner_ window] addChildWindow:info_bubble ordered:NSWindowAbove];
[owner_ layoutSubviews];
[info_bubble orderFront:self];
}
- (void)awakeFromNib {
DCHECK([[self window] isKindOfClass:[InfoBubbleWindow class]]);
[messageLabel_ setStringValue:[self getLabelText]];
[self initializeLabel];
}
- (void)positionInWindowAtTop:(CGFloat)maxY width:(CGFloat)maxWidth {
NSRect windowFrame = [self window].frame;
NSRect ownerWindowFrame = [owner_ window].frame;
NSPoint origin;
origin.x = ownerWindowFrame.origin.x +
(int)(NSWidth(ownerWindowFrame) / 2 - NSWidth(windowFrame) / 2);
origin.y = ownerWindowFrame.origin.y + maxY - NSHeight(windowFrame);
[[self window] setFrameOrigin:origin];
}
// Called when someone clicks on the embedded link.
- (BOOL)textView:(NSTextView*)textView
clickedOnLink:(id)link
atIndex:(NSUInteger)charIndex {
browser_->fullscreen_controller()
->ExitTabOrBrowserFullscreenToPreviousState();
return YES;
}
- (void)hideTimerFired:(NSTimer*)timer {
// This might fire racily for buttoned bubbles, even though the timer is
// cancelled for them. Explicitly check for this case.
if (exclusive_access_bubble::ShowButtonsForType(bubbleType_))
return;
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext]
gtm_setDuration:kHideDuration
eventMask:NSLeftMouseUpMask | NSLeftMouseDownMask];
[[[self window] animator] setAlphaValue:0.0];
[NSAnimationContext endGrouping];
}
- (void)animationDidEnd:(NSAnimation*)animation {
if (animation == hideAnimation_.get()) {
hideAnimation_.reset();
}
}
- (void)closeImmediately {
// Without this, quitting fullscreen with esc will let the bubble reappear
// once the "exit fullscreen" animation is done on lion.
InfoBubbleWindow* infoBubble = static_cast<InfoBubbleWindow*>([self window]);
[[infoBubble parentWindow] removeChildWindow:infoBubble];
[hideAnimation_.get() stopAnimation];
[hideTimer_ invalidate];
[infoBubble setAllowedAnimations:info_bubble::kAnimateNone];
[self close];
}
- (void)dealloc {
[hideAnimation_.get() stopAnimation];
[hideTimer_ invalidate];
[super dealloc];
}
@end
@implementation ExclusiveAccessBubbleWindowController (PrivateMethods)
- (void)initializeLabel {
// Replace the label placeholder NSTextField with the real label NSTextView.
// The former doesn't show links in a nice way, but the latter can't be added
// in IB without a containing scroll view, so create the NSTextView
// programmatically.
exitLabel_.reset([[OneClickHyperlinkTextView alloc]
initWithFrame:[exitLabelPlaceholder_ frame]]);
[exitLabel_.get()
setAutoresizingMask:[exitLabelPlaceholder_ autoresizingMask]];
[exitLabel_.get() setHidden:[exitLabelPlaceholder_ isHidden]];
[[exitLabelPlaceholder_ superview] replaceSubview:exitLabelPlaceholder_
with:exitLabel_.get()];
exitLabelPlaceholder_ = nil; // Now released.
[exitLabel_.get() setDelegate:self];
NSString* exitLinkText;
NSString* exitUnlinkedText;
if (bubbleType_ ==
EXCLUSIVE_ACCESS_BUBBLE_TYPE_FULLSCREEN_MOUSELOCK_EXIT_INSTRUCTION ||
bubbleType_ == EXCLUSIVE_ACCESS_BUBBLE_TYPE_MOUSELOCK_EXIT_INSTRUCTION) {
exitLinkText = @"";
exitUnlinkedText =
[@" " stringByAppendingString:l10n_util::GetNSStringF(
IDS_FULLSCREEN_PRESS_ESC_TO_EXIT,
l10n_util::GetStringUTF16(
IDS_APP_ESC_KEY))];
} else {
exitLinkText = l10n_util::GetNSString(IDS_EXIT_FULLSCREEN_MODE);
exitUnlinkedText =
[@" " stringByAppendingString:l10n_util::GetNSStringF(
IDS_EXIT_FULLSCREEN_MODE_ACCELERATOR,
l10n_util::GetStringUTF16(
IDS_APP_ESC_KEY))];
}
NSFont* font = [NSFont
systemFontOfSize:[NSFont
systemFontSizeForControlSize:NSRegularControlSize]];
[(HyperlinkTextView*)exitLabel_.get() setMessageAndLink:exitUnlinkedText
withLink:exitLinkText
atOffset:0
font:font
messageColor:[NSColor blackColor]
linkColor:[NSColor blueColor]];
[exitLabel_.get() setAlignment:NSRightTextAlignment];
NSRect labelFrame = [exitLabel_ frame];
// NSTextView's sizeToFit: method seems to enjoy wrapping lines. Temporarily
// set the size large to force it not to.
NSRect windowFrame = [[self window] frame];
[exitLabel_ setFrameSize:windowFrame.size];
NSLayoutManager* layoutManager = [exitLabel_ layoutManager];
NSTextContainer* textContainer = [exitLabel_ textContainer];
[layoutManager ensureLayoutForTextContainer:textContainer];
NSRect textFrame = [layoutManager usedRectForTextContainer:textContainer];
textFrame.size.width = ceil(NSWidth(textFrame));
labelFrame.origin.x += NSWidth(labelFrame) - NSWidth(textFrame);
labelFrame.size = textFrame.size;
[exitLabel_ setFrame:labelFrame];
}
- (NSString*)getLabelText {
if (bubbleType_ == EXCLUSIVE_ACCESS_BUBBLE_TYPE_NONE)
return @"";
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(browser_->profile());
return SysUTF16ToNSString(exclusive_access_bubble::GetLabelTextForType(
bubbleType_, url_, registry));
}
// This looks at the Main Menu and determines what the user has set as the
// key combination for quit. It then gets the modifiers and builds an object
// to hold the data.
+ (scoped_ptr<ui::PlatformAcceleratorCocoa>)acceleratorForToggleFullscreen {
NSMenu* mainMenu = [NSApp mainMenu];
// Get the application menu (i.e. Chromium).
for (NSMenuItem* menu in [mainMenu itemArray]) {
for (NSMenuItem* item in [[menu submenu] itemArray]) {
// Find the toggle presentation mode item.
if ([item tag] == IDC_PRESENTATION_MODE) {
return scoped_ptr<ui::PlatformAcceleratorCocoa>(
new ui::PlatformAcceleratorCocoa([item keyEquivalent],
[item keyEquivalentModifierMask]));
}
}
}
// Default to Cmd+Shift+F.
return scoped_ptr<ui::PlatformAcceleratorCocoa>(
new ui::PlatformAcceleratorCocoa(@"f",
NSCommandKeyMask | NSShiftKeyMask));
}
// This looks at the Main Menu and determines what the user has set as the
// key combination for quit. It then gets the modifiers and builds a string
// to display them.
+ (NSString*)keyCommandString {
scoped_ptr<ui::PlatformAcceleratorCocoa> accelerator(
[[self class] acceleratorForToggleFullscreen]);
return [[self class] keyCombinationForAccelerator:*accelerator];
}
+ (NSString*)keyCombinationForAccelerator:
(const ui::PlatformAcceleratorCocoa&)item {
NSMutableString* string = [NSMutableString string];
NSUInteger modifiers = item.modifier_mask();
if (modifiers & NSCommandKeyMask)
[string appendString:@"\u2318"];
if (modifiers & NSControlKeyMask)
[string appendString:@"\u2303"];
if (modifiers & NSAlternateKeyMask)
[string appendString:@"\u2325"];
BOOL isUpperCase = [[NSCharacterSet uppercaseLetterCharacterSet]
characterIsMember:[item.characters() characterAtIndex:0]];
if (modifiers & NSShiftKeyMask || isUpperCase)
[string appendString:@"\u21E7"];
[string appendString:[item.characters() uppercaseString]];
return string;
}
- (void)hideSoon {
hideTimer_.reset(
[[NSTimer scheduledTimerWithTimeInterval:kInitialDelay
target:self
selector:@selector(hideTimerFired:)
userInfo:nil
repeats:NO] retain]);
}
@end
|