| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
| 6 | 6 |
| 7 #include "base/logging.h" // for NOTREACHED() | 7 #include "base/logging.h" // for NOTREACHED() |
| 8 #include "base/mac/mac_util.h" | 8 #include "base/mac/mac_util.h" |
| 9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 10 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
| 11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 13 #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" | 14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 15 #include "chrome/browser/ui/cocoa/event_utils.h" | 15 #include "chrome/browser/ui/cocoa/event_utils.h" |
| 16 #import "chrome/browser/ui/cocoa/fullscreen_exit_bubble_controller.h" | 16 #import "chrome/browser/ui/cocoa/fullscreen_exit_bubble_controller.h" |
| 17 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" | 17 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" |
| 18 #import "chrome/browser/ui/cocoa/info_bubble_view.h" |
| 19 #import "chrome/browser/ui/cocoa/info_bubble_window.h" |
| 18 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 19 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | 21 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
| 20 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h" | 22 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h" |
| 23 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
| 21 #include "ui/base/models/accelerator_cocoa.h" | 24 #include "ui/base/models/accelerator_cocoa.h" |
| 22 #include "ui/base/l10n/l10n_util_mac.h" | 25 #include "ui/base/l10n/l10n_util_mac.h" |
| 23 | 26 |
| 24 const int kPaddingPx = 8; | 27 const int kPaddingPx = 8; |
| 25 const int kInitialDelayMs = 3800; | 28 const int kInitialDelayMs = 3800; |
| 26 const int kSlideOutDurationMs = 700; | 29 const int kSlideOutDurationMs = 700; |
| 27 | 30 |
| 31 @interface OneClickHyperlinkTextView : HyperlinkTextView |
| 32 @end |
| 33 @implementation OneClickHyperlinkTextView |
| 34 - (BOOL)acceptsFirstMouse:(NSEvent*)event { |
| 35 return YES; |
| 36 } |
| 37 @end |
| 38 |
| 28 @interface FullscreenExitBubbleController (PrivateMethods) | 39 @interface FullscreenExitBubbleController (PrivateMethods) |
| 29 // Sets |exitLabel_| based on |exitLabelPlaceholder_|, | 40 // Sets |exitLabel_| based on |exitLabelPlaceholder_|, |
| 30 // sets |exitLabelPlaceholder_| to nil. | 41 // sets |exitLabelPlaceholder_| to nil. |
| 31 - (void)initializeLabel; | 42 - (void)initializeLabel; |
| 32 | 43 |
| 33 - (void)hideSoon; | 44 - (void)hideSoon; |
| 34 | 45 |
| 35 // Returns the Accelerator for the Toggle Fullscreen menu item. | 46 // Returns the Accelerator for the Toggle Fullscreen menu item. |
| 36 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen; | 47 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen; |
| 37 | 48 |
| 38 // Returns a string representation fit for display of | 49 // Returns a string representation fit for display of |
| 39 // +acceleratorForToggleFullscreen. | 50 // +acceleratorForToggleFullscreen. |
| 40 + (NSString*)keyCommandString; | 51 + (NSString*)keyCommandString; |
| 41 | 52 |
| 42 + (NSString*)keyCombinationForAccelerator:(const ui::AcceleratorCocoa&)item; | 53 + (NSString*)keyCombinationForAccelerator:(const ui::AcceleratorCocoa&)item; |
| 43 @end | 54 @end |
| 44 | 55 |
| 45 @implementation FullscreenExitBubbleController | 56 @implementation FullscreenExitBubbleController |
| 46 | 57 |
| 47 - (id)initWithOwner:(BrowserWindowController*)owner browser:(Browser*)browser { | 58 - (id)initWithOwner:(BrowserWindowController*)owner |
| 48 if ((self = [super initWithNibName:@"FullscreenExitBubble" | 59 browser:(Browser*)browser |
| 49 bundle:base::mac::MainAppBundle()])) { | 60 forURL:(const GURL&)url |
| 61 askPermission:(BOOL)ask_permission { |
| 62 NSString* nibPath = |
| 63 [base::mac::MainAppBundle() pathForResource:@"FullscreenExitBubble" |
| 64 ofType:@"nib"]; |
| 65 if ((self = [super initWithWindowNibPath:nibPath owner:self])) { |
| 50 browser_ = browser; | 66 browser_ = browser; |
| 51 owner_ = owner; | 67 owner_ = owner; |
| 68 url_ = url; |
| 69 show_buttons_ = ask_permission; |
| 52 } | 70 } |
| 53 return self; | 71 return self; |
| 54 } | 72 } |
| 55 | 73 |
| 56 - (void)awakeFromNib { | 74 - (void)allow:(id)sender { |
| 57 [self initializeLabel]; | 75 [self hideButtons]; |
| 76 browser_->OnAcceptFullscreenPermission(url_); |
| 58 [self hideSoon]; | 77 [self hideSoon]; |
| 59 } | 78 } |
| 60 | 79 |
| 80 - (void)deny:(id)sender { |
| 81 browser_->ToggleFullscreenMode(false); |
| 82 } |
| 83 |
| 84 - (void)hideButtons { |
| 85 [allowButton_ setHidden:YES]; |
| 86 [denyButton_ setHidden:YES]; |
| 87 [exitLabel_ setHidden:NO]; |
| 88 } |
| 89 |
| 90 // We want this to be a child of a browser window. addChildWindow: |
| 91 // (called from this function) will bring the window on-screen; |
| 92 // unfortunately, [NSWindowController showWindow:] will also bring it |
| 93 // on-screen (but will cause unexpected changes to the window's |
| 94 // position). We cannot have an addChildWindow: and a subsequent |
| 95 // showWindow:. Thus, we have our own version. |
| 96 - (void)showWindow { |
| 97 NSWindow* window = [self window]; // completes nib load |
| 98 [bubble_ setArrowLocation:info_bubble::kNoArrow]; |
| 99 [(InfoBubbleWindow*)[self window] setCanBecomeKey:NO]; |
| 100 if (!show_buttons_) { |
| 101 [self hideButtons]; |
| 102 [self hideSoon]; |
| 103 } |
| 104 NSRect windowFrame = [owner_ window].frame; |
| 105 [tweaker_ tweakUI:[self window]]; |
| 106 [self positionInWindowAtTop:NSHeight(windowFrame) width:NSWidth(windowFrame)]; |
| 107 [[owner_ window] addChildWindow:window ordered:NSWindowAbove]; |
| 108 |
| 109 [window orderFront:self]; |
| 110 } |
| 111 |
| 112 - (void)awakeFromNib { |
| 113 NSString* title = |
| 114 l10n_util::GetNSStringF(IDS_FULLSCREEN_INFOBAR_REQUEST_PERMISSION, |
| 115 UTF8ToUTF16(url_.host())); |
| 116 [messageLabel_ setStringValue:title]; |
| 117 [super awakeFromNib]; |
| 118 [self initializeLabel]; |
| 119 } |
| 120 |
| 61 - (void)positionInWindowAtTop:(CGFloat)maxY width:(CGFloat)maxWidth { | 121 - (void)positionInWindowAtTop:(CGFloat)maxY width:(CGFloat)maxWidth { |
| 62 NSRect bubbleFrame = [[self view] frame]; | 122 NSRect windowFrame = [self window].frame; |
| 63 bubbleFrame.origin.x = (int)(maxWidth/2 - NSWidth(bubbleFrame)/2); | 123 NSPoint origin = windowFrame.origin; |
| 64 bubbleFrame.origin.y = maxY - NSHeight(bubbleFrame); | 124 origin.x = (int)(maxWidth/2 - NSWidth(windowFrame)/2); |
| 65 [[self view] setFrame:bubbleFrame]; | 125 origin.y = maxY - NSHeight(windowFrame); |
| 126 origin.y -= fullscreen_exit_bubble::kBubbleOffsetY; |
| 127 [[self window] setFrameOrigin:origin]; |
| 66 } | 128 } |
| 67 | 129 |
| 68 // Called when someone clicks on the embedded link. | 130 // Called when someone clicks on the embedded link. |
| 69 - (BOOL) textView:(NSTextView*)textView | 131 - (BOOL) textView:(NSTextView*)textView |
| 70 clickedOnLink:(id)link | 132 clickedOnLink:(id)link |
| 71 atIndex:(NSUInteger)charIndex { | 133 atIndex:(NSUInteger)charIndex { |
| 72 browser_->ExecuteCommand(IDC_FULLSCREEN); | 134 browser_->ExecuteCommand(IDC_FULLSCREEN); |
| 73 return YES; | 135 return YES; |
| 74 } | 136 } |
| 75 | 137 |
| 76 - (void)hideTimerFired:(NSTimer*)timer { | 138 - (void)hideTimerFired:(NSTimer*)timer { |
| 77 NSRect endFrame = [[self view] frame]; | 139 [NSAnimationContext beginGrouping]; |
| 140 // The star currently triggers on mouse down, not mouse up. |
| 141 [[NSAnimationContext currentContext] |
| 142 gtm_setDuration:0.2 // TODO: Constantify. |
| 143 eventMask:NSLeftMouseUpMask|NSLeftMouseDownMask]; |
| 144 [[[self window] animator] setAlphaValue:0.0]; |
| 145 [NSAnimationContext endGrouping]; |
| 146 return; |
| 147 NSRect endFrame = [[self window] frame]; |
| 78 endFrame.origin.y += endFrame.size.height; | 148 endFrame.origin.y += endFrame.size.height; |
| 79 endFrame.size.height = 0; | |
| 80 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: | 149 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: |
| 81 [self view], NSViewAnimationTargetKey, | 150 [self window], NSViewAnimationTargetKey, |
| 82 [NSValue valueWithRect:endFrame], NSViewAnimationEndFrameKey, nil]; | 151 [NSValue valueWithRect:endFrame], NSViewAnimationEndFrameKey, nil]; |
| 83 | 152 |
| 84 NSViewAnimation* animation = | 153 NSViewAnimation* animation = |
| 85 [[NSViewAnimation alloc] | 154 [[NSViewAnimation alloc] |
| 86 initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]]; | 155 initWithViewAnimations:[NSArray arrayWithObjects:dict, nil]]; |
| 87 [animation gtm_setDuration:kSlideOutDurationMs/1000.0 | 156 [animation gtm_setDuration:kSlideOutDurationMs/1000.0 |
| 88 eventMask:NSLeftMouseUpMask]; | 157 eventMask:NSLeftMouseUpMask]; |
| 89 [animation setDelegate:self]; | 158 [animation setDelegate:self]; |
| 90 [animation startAnimation]; | 159 [animation startAnimation]; |
| 91 hideAnimation_.reset(animation); | 160 hideAnimation_.reset(animation); |
| 92 } | 161 } |
| 93 | 162 |
| 94 - (void)animationDidEnd:(NSAnimation*)animation { | 163 - (void)animationDidEnd:(NSAnimation*)animation { |
| 95 if (animation == hideAnimation_.get()) { | 164 if (animation == hideAnimation_.get()) { |
| 96 hideAnimation_.reset(); | 165 hideAnimation_.reset(); |
| 97 } | 166 } |
| 98 } | 167 } |
| 99 | 168 |
| 100 - (AnimatableView*)animatableView { | |
| 101 return static_cast<AnimatableView*>([self view]); | |
| 102 } | |
| 103 | |
| 104 - (void)dealloc { | 169 - (void)dealloc { |
| 105 [hideAnimation_.get() stopAnimation]; | 170 [hideAnimation_.get() stopAnimation]; |
| 106 [hideTimer_ invalidate]; | 171 [hideTimer_ invalidate]; |
| 107 [super dealloc]; | 172 [super dealloc]; |
| 108 } | 173 } |
| 109 | 174 |
| 110 @end | 175 @end |
| 111 | 176 |
| 112 @implementation FullscreenExitBubbleController (PrivateMethods) | 177 @implementation FullscreenExitBubbleController (PrivateMethods) |
| 113 | 178 |
| 114 - (void)initializeLabel { | 179 - (void)initializeLabel { |
| 115 // Replace the label placeholder NSTextField with the real label NSTextView. | 180 // 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 | 181 // 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 | 182 // in IB without a containing scroll view, so create the NSTextView |
| 118 // programmatically. | 183 // programmatically. |
| 119 exitLabel_.reset([[HyperlinkTextView alloc] | 184 exitLabel_.reset([[OneClickHyperlinkTextView alloc] |
| 120 initWithFrame:[exitLabelPlaceholder_ frame]]); | 185 initWithFrame:[exitLabelPlaceholder_ frame]]); |
| 121 [exitLabel_.get() setAutoresizingMask: | 186 [exitLabel_.get() setAutoresizingMask: |
| 122 [exitLabelPlaceholder_ autoresizingMask]]; | 187 [exitLabelPlaceholder_ autoresizingMask]]; |
| 188 [exitLabel_.get() setHidden:[exitLabelPlaceholder_ isHidden]]; |
| 123 [[exitLabelPlaceholder_ superview] | 189 [[exitLabelPlaceholder_ superview] |
| 124 replaceSubview:exitLabelPlaceholder_ with:exitLabel_.get()]; | 190 replaceSubview:exitLabelPlaceholder_ with:exitLabel_.get()]; |
| 125 exitLabelPlaceholder_ = nil; // Now released. | 191 exitLabelPlaceholder_ = nil; // Now released. |
| 126 [exitLabel_.get() setDelegate:self]; | 192 [exitLabel_.get() setDelegate:self]; |
| 127 | 193 |
| 128 NSString *message = l10n_util::GetNSStringF(IDS_EXIT_FULLSCREEN_MODE, | 194 NSString *message = l10n_util::GetNSStringF(IDS_EXIT_FULLSCREEN_MODE, |
| 129 base::SysNSStringToUTF16([[self class] keyCommandString])); | 195 base::SysNSStringToUTF16([[self class] keyCommandString])); |
| 130 | 196 |
| 197 NSFont* font = [NSFont systemFontOfSize: |
| 198 [NSFont systemFontSizeForControlSize:NSRegularControlSize]]; |
| 131 [(HyperlinkTextView*)exitLabel_.get() | 199 [(HyperlinkTextView*)exitLabel_.get() |
| 132 setMessageAndLink:@"" | 200 setMessageAndLink:@"" |
| 133 withLink:message | 201 withLink:message |
| 134 atOffset:0 | 202 atOffset:0 |
| 135 font:[NSFont systemFontOfSize:18] | 203 font:font |
| 136 messageColor:[NSColor whiteColor] | 204 messageColor:[NSColor blackColor] |
| 137 linkColor:[NSColor whiteColor]]; | 205 linkColor:[NSColor blueColor]]; |
| 206 [exitLabel_.get() setAlignment:NSRightTextAlignment]; |
| 138 | 207 |
| 139 [exitLabel_.get() sizeToFit]; | 208 NSRect labelFrame = [exitLabel_ frame]; |
| 140 NSLayoutManager* layoutManager = [exitLabel_.get() layoutManager]; | 209 |
| 141 NSTextContainer* textContainer = [exitLabel_.get() textContainer]; | 210 // NSTextView's sizeToFit: method seems to enjoy wrapping lines. Temporarily |
| 211 // set the size very large to force it not to. |
| 212 [exitLabel_ setFrameSize:NSMakeSize(1.0e7, 1.0e7)]; |
| 213 NSLayoutManager* layoutManager = [exitLabel_ layoutManager]; |
| 214 NSTextContainer* textContainer = [exitLabel_ textContainer]; |
| 142 [layoutManager ensureLayoutForTextContainer:textContainer]; | 215 [layoutManager ensureLayoutForTextContainer:textContainer]; |
| 143 NSRect textFrame = [layoutManager usedRectForTextContainer:textContainer]; | 216 NSRect textFrame = [layoutManager usedRectForTextContainer:textContainer]; |
| 144 NSRect frame = [[self view] frame]; | 217 |
| 145 NSSize textSize = textFrame.size; | 218 labelFrame.origin.x += labelFrame.size.width - textFrame.size.width; |
| 146 frame.size.width = textSize.width + 2 * kPaddingPx; | 219 labelFrame.size = textFrame.size; |
| 147 [[self view] setFrame:frame]; | 220 [exitLabel_ setFrame:labelFrame]; |
| 148 textFrame.origin.x = textFrame.origin.y = kPaddingPx; | |
| 149 [exitLabel_.get() setFrame:textFrame]; | |
| 150 } | 221 } |
| 151 | 222 |
| 152 // This looks at the Main Menu and determines what the user has set as the | 223 // 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 | 224 // key combination for quit. It then gets the modifiers and builds an object |
| 154 // to hold the data. | 225 // to hold the data. |
| 155 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen { | 226 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen { |
| 156 NSMenu* mainMenu = [NSApp mainMenu]; | 227 NSMenu* mainMenu = [NSApp mainMenu]; |
| 157 // Get the application menu (i.e. Chromium). | 228 // Get the application menu (i.e. Chromium). |
| 158 for (NSMenuItem* menu in [mainMenu itemArray]) { | 229 for (NSMenuItem* menu in [mainMenu itemArray]) { |
| 159 for (NSMenuItem* item in [[menu submenu] itemArray]) { | 230 for (NSMenuItem* item in [[menu submenu] itemArray]) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 - (void)hideSoon { | 270 - (void)hideSoon { |
| 200 hideTimer_.reset( | 271 hideTimer_.reset( |
| 201 [[NSTimer scheduledTimerWithTimeInterval:kInitialDelayMs/1000.0 | 272 [[NSTimer scheduledTimerWithTimeInterval:kInitialDelayMs/1000.0 |
| 202 target:self | 273 target:self |
| 203 selector:@selector(hideTimerFired:) | 274 selector:@selector(hideTimerFired:) |
| 204 userInfo:nil | 275 userInfo:nil |
| 205 repeats:NO] retain]); | 276 repeats:NO] retain]); |
| 206 } | 277 } |
| 207 | 278 |
| 208 @end | 279 @end |
| OLD | NEW |