[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / fullscreen_exit_bubble_controller.mm
blob051bb49bd877e038a03ce4f4fb4c5ff760e000bd
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.
5 #import <Cocoa/Cocoa.h>
7 #include "base/logging.h"  // for NOTREACHED()
8 #include "base/mac/bundle_locations.h"
9 #include "base/mac/mac_util.h"
10 #include "base/strings/sys_string_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/app/chrome_command_ids.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
17 #import "chrome/browser/ui/cocoa/fullscreen_exit_bubble_controller.h"
18 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
19 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
20 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
21 #include "chrome/browser/ui/fullscreen/fullscreen_controller.h"
22 #include "chrome/browser/ui/fullscreen/fullscreen_exit_bubble_type.h"
23 #include "grit/generated_resources.h"
24 #include "grit/ui_strings.h"
25 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMNSAnimation+Duration.h"
26 #include "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
27 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
28 #include "ui/base/accelerators/platform_accelerator_cocoa.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/l10n/l10n_util_mac.h"
33 namespace {
34 const float kInitialDelay = 3.8;
35 const float kHideDuration = 0.7;
36 } // namespace
38 @interface OneClickHyperlinkTextView : HyperlinkTextView
39 @end
40 @implementation OneClickHyperlinkTextView
41 - (BOOL)acceptsFirstMouse:(NSEvent*)event {
42   return YES;
44 @end
46 @interface FullscreenExitBubbleController (PrivateMethods)
47 // Sets |exitLabel_| based on |exitLabelPlaceholder_|,
48 // sets |exitLabelPlaceholder_| to nil.
49 - (void)initializeLabel;
51 - (NSString*)getLabelText;
53 - (void)hideSoon;
55 // Returns the Accelerator for the Toggle Fullscreen menu item.
56 + (scoped_ptr<ui::PlatformAcceleratorCocoa>)acceleratorForToggleFullscreen;
58 // Returns a string representation fit for display of
59 // +acceleratorForToggleFullscreen.
60 + (NSString*)keyCommandString;
62 + (NSString*)keyCombinationForAccelerator:
63     (const ui::PlatformAcceleratorCocoa&)item;
64 @end
66 @implementation FullscreenExitBubbleController
68 - (id)initWithOwner:(BrowserWindowController*)owner
69             browser:(Browser*)browser
70                 url:(const GURL&)url
71          bubbleType:(FullscreenExitBubbleType)bubbleType {
72   NSString* nibPath =
73       [base::mac::FrameworkBundle() pathForResource:@"FullscreenExitBubble"
74                                              ofType:@"nib"];
75   if ((self = [super initWithWindowNibPath:nibPath owner:self])) {
76     browser_ = browser;
77     owner_ = owner;
78     url_ = url;
79     bubbleType_ = bubbleType;
80     // Mouse lock expects mouse events to reach the main window immediately.
81     // Make the bubble transparent for mouse events if mouse lock is enabled.
82     if (bubbleType_ == FEB_TYPE_FULLSCREEN_MOUSELOCK_EXIT_INSTRUCTION ||
83         bubbleType_ == FEB_TYPE_MOUSELOCK_EXIT_INSTRUCTION)
84       [[self window] setIgnoresMouseEvents:YES];
85   }
86   return self;
89 - (void)allow:(id)sender {
90   // The mouselock code expects that mouse events reach the main window
91   // immediately, but the cursor is still over the bubble, which eats the
92   // mouse events. Make the bubble transparent for mouse events.
93   if (bubbleType_ == FEB_TYPE_FULLSCREEN_MOUSELOCK_BUTTONS ||
94       bubbleType_ == FEB_TYPE_MOUSELOCK_BUTTONS)
95     [[self window] setIgnoresMouseEvents:YES];
97   DCHECK(fullscreen_bubble::ShowButtonsForType(bubbleType_));
98   browser_->fullscreen_controller()->OnAcceptFullscreenPermission();
101 - (void)deny:(id)sender {
102   DCHECK(fullscreen_bubble::ShowButtonsForType(bubbleType_));
103   browser_->fullscreen_controller()->OnDenyFullscreenPermission();
106 - (void)showButtons:(BOOL)show {
107   [allowButton_ setHidden:!show];
108   [denyButton_ setHidden:!show];
109   [exitLabel_ setHidden:show];
112 // We want this to be a child of a browser window.  addChildWindow:
113 // (called from this function) will bring the window on-screen;
114 // unfortunately, [NSWindowController showWindow:] will also bring it
115 // on-screen (but will cause unexpected changes to the window's
116 // position).  We cannot have an addChildWindow: and a subsequent
117 // showWindow:. Thus, we have our own version.
118 - (void)showWindow {
119   // Completes nib load.
120   InfoBubbleWindow* info_bubble = static_cast<InfoBubbleWindow*>([self window]);
121   [info_bubble setCanBecomeKeyWindow:NO];
122   if (!fullscreen_bubble::ShowButtonsForType(bubbleType_)) {
123     [self showButtons:NO];
124     [self hideSoon];
125   }
126   [tweaker_ tweakUI:info_bubble];
127   [[owner_ window] addChildWindow:info_bubble ordered:NSWindowAbove];
128   [owner_ layoutSubviews];
130   [info_bubble orderFront:self];
133 - (void)awakeFromNib {
134   DCHECK([[self window] isKindOfClass:[InfoBubbleWindow class]]);
135   [messageLabel_ setStringValue:[self getLabelText]];
136   [self initializeLabel];
139 - (void)positionInWindowAtTop:(CGFloat)maxY width:(CGFloat)maxWidth {
140   NSRect windowFrame = [self window].frame;
141   NSRect ownerWindowFrame = [owner_ window].frame;
142   NSPoint origin;
143   origin.x = ownerWindowFrame.origin.x +
144       (int)(NSWidth(ownerWindowFrame)/2 - NSWidth(windowFrame)/2);
145   origin.y = ownerWindowFrame.origin.y + maxY - NSHeight(windowFrame);
146   [[self window] setFrameOrigin:origin];
149 // Called when someone clicks on the embedded link.
150 - (BOOL) textView:(NSTextView*)textView
151     clickedOnLink:(id)link
152           atIndex:(NSUInteger)charIndex {
153   browser_->fullscreen_controller()->
154       ExitTabOrBrowserFullscreenToPreviousState();
155   return YES;
158 - (void)hideTimerFired:(NSTimer*)timer {
159   // This might fire racily for buttoned bubbles, even though the timer is
160   // cancelled for them. Explicitly check for this case.
161   if (fullscreen_bubble::ShowButtonsForType(bubbleType_))
162     return;
164   [NSAnimationContext beginGrouping];
165   [[NSAnimationContext currentContext]
166       gtm_setDuration:kHideDuration
167             eventMask:NSLeftMouseUpMask|NSLeftMouseDownMask];
168   [[[self window] animator] setAlphaValue:0.0];
169   [NSAnimationContext endGrouping];
172 - (void)animationDidEnd:(NSAnimation*)animation {
173   if (animation == hideAnimation_.get()) {
174     hideAnimation_.reset();
175   }
178 - (void)closeImmediately {
179   // Without this, quitting fullscreen with esc will let the bubble reappear
180   // once the "exit fullscreen" animation is done on lion.
181   InfoBubbleWindow* infoBubble = static_cast<InfoBubbleWindow*>([self window]);
182   [[infoBubble parentWindow] removeChildWindow:infoBubble];
183   [hideAnimation_.get() stopAnimation];
184   [hideTimer_ invalidate];
185   [infoBubble setAllowedAnimations:info_bubble::kAnimateNone];
186   [self close];
189 - (void)dealloc {
190   [hideAnimation_.get() stopAnimation];
191   [hideTimer_ invalidate];
192   [super dealloc];
195 @end
197 @implementation FullscreenExitBubbleController (PrivateMethods)
199 - (void)initializeLabel {
200   // Replace the label placeholder NSTextField with the real label NSTextView.
201   // The former doesn't show links in a nice way, but the latter can't be added
202   // in IB without a containing scroll view, so create the NSTextView
203   // programmatically.
204   exitLabel_.reset([[OneClickHyperlinkTextView alloc]
205       initWithFrame:[exitLabelPlaceholder_ frame]]);
206   [exitLabel_.get() setAutoresizingMask:
207       [exitLabelPlaceholder_ autoresizingMask]];
208   [exitLabel_.get() setHidden:[exitLabelPlaceholder_ isHidden]];
209   [[exitLabelPlaceholder_ superview]
210       replaceSubview:exitLabelPlaceholder_ with:exitLabel_.get()];
211   exitLabelPlaceholder_ = nil;  // Now released.
212   [exitLabel_.get() setDelegate:self];
214   NSString* exitLinkText;
215   NSString* exitUnlinkedText;
216   if (bubbleType_ == FEB_TYPE_FULLSCREEN_MOUSELOCK_EXIT_INSTRUCTION ||
217       bubbleType_ == FEB_TYPE_MOUSELOCK_EXIT_INSTRUCTION) {
218     exitLinkText = @"";
219     exitUnlinkedText = [@" " stringByAppendingString:
220         l10n_util::GetNSStringF(IDS_FULLSCREEN_PRESS_ESC_TO_EXIT,
221                                 l10n_util::GetStringUTF16(IDS_APP_ESC_KEY))];
222   } else {
223     exitLinkText = l10n_util::GetNSString(IDS_EXIT_FULLSCREEN_MODE);
224     exitUnlinkedText = [@" " stringByAppendingString:
225         l10n_util::GetNSStringF(IDS_EXIT_FULLSCREEN_MODE_ACCELERATOR,
226                                 l10n_util::GetStringUTF16(IDS_APP_ESC_KEY))];
227   }
229   NSFont* font = [NSFont systemFontOfSize:
230       [NSFont systemFontSizeForControlSize:NSRegularControlSize]];
231   [(HyperlinkTextView*)exitLabel_.get()
232         setMessageAndLink:exitUnlinkedText
233                  withLink:exitLinkText
234                  atOffset:0
235                      font:font
236              messageColor:[NSColor blackColor]
237                 linkColor:[NSColor blueColor]];
238   [exitLabel_.get() setAlignment:NSRightTextAlignment];
240   NSRect labelFrame = [exitLabel_ frame];
242   // NSTextView's sizeToFit: method seems to enjoy wrapping lines. Temporarily
243   // set the size large to force it not to.
244   NSRect windowFrame = [[self window] frame];
245   [exitLabel_ setFrameSize:windowFrame.size];
246   NSLayoutManager* layoutManager = [exitLabel_ layoutManager];
247   NSTextContainer* textContainer = [exitLabel_ textContainer];
248   [layoutManager ensureLayoutForTextContainer:textContainer];
249   NSRect textFrame = [layoutManager usedRectForTextContainer:textContainer];
251   textFrame.size.width = ceil(NSWidth(textFrame));
252   labelFrame.origin.x += NSWidth(labelFrame) - NSWidth(textFrame);
253   labelFrame.size = textFrame.size;
254   [exitLabel_ setFrame:labelFrame];
257 - (NSString*)getLabelText {
258   if (bubbleType_ == FEB_TYPE_NONE)
259     return @"";
260   return SysUTF16ToNSString(fullscreen_bubble::GetLabelTextForType(
261           bubbleType_, url_, browser_->profile()->GetExtensionService()));
264 // This looks at the Main Menu and determines what the user has set as the
265 // key combination for quit. It then gets the modifiers and builds an object
266 // to hold the data.
267 + (scoped_ptr<ui::PlatformAcceleratorCocoa>)acceleratorForToggleFullscreen {
268   NSMenu* mainMenu = [NSApp mainMenu];
269   // Get the application menu (i.e. Chromium).
270   for (NSMenuItem* menu in [mainMenu itemArray]) {
271     for (NSMenuItem* item in [[menu submenu] itemArray]) {
272       // Find the toggle presentation mode item.
273       if ([item tag] == IDC_PRESENTATION_MODE) {
274         return scoped_ptr<ui::PlatformAcceleratorCocoa>(
275           new ui::PlatformAcceleratorCocoa([item keyEquivalent],
276                                            [item keyEquivalentModifierMask]));
277       }
278     }
279   }
280   // Default to Cmd+Shift+F.
281   return scoped_ptr<ui::PlatformAcceleratorCocoa>(
282       new ui::PlatformAcceleratorCocoa(@"f", NSCommandKeyMask|NSShiftKeyMask));
285 // This looks at the Main Menu and determines what the user has set as the
286 // key combination for quit. It then gets the modifiers and builds a string
287 // to display them.
288 + (NSString*)keyCommandString {
289   scoped_ptr<ui::PlatformAcceleratorCocoa> accelerator(
290       [[self class] acceleratorForToggleFullscreen]);
291   return [[self class] keyCombinationForAccelerator:*accelerator];
294 + (NSString*)keyCombinationForAccelerator:
295     (const ui::PlatformAcceleratorCocoa&)item {
296   NSMutableString* string = [NSMutableString string];
297   NSUInteger modifiers = item.modifier_mask();
299   if (modifiers & NSCommandKeyMask)
300     [string appendString:@"\u2318"];
301   if (modifiers & NSControlKeyMask)
302     [string appendString:@"\u2303"];
303   if (modifiers & NSAlternateKeyMask)
304     [string appendString:@"\u2325"];
305   BOOL isUpperCase = [[NSCharacterSet uppercaseLetterCharacterSet]
306       characterIsMember:[item.characters() characterAtIndex:0]];
307   if (modifiers & NSShiftKeyMask || isUpperCase)
308     [string appendString:@"\u21E7"];
310   [string appendString:[item.characters() uppercaseString]];
311   return string;
314 - (void)hideSoon {
315   hideTimer_.reset(
316       [[NSTimer scheduledTimerWithTimeInterval:kInitialDelay
317                                         target:self
318                                       selector:@selector(hideTimerFired:)
319                                       userInfo:nil
320                                        repeats:NO] retain]);
323 @end