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 |
25 const int kInitialDelayMs = 3800; | 28 namespace { |
26 const int kSlideOutDurationMs = 700; | 29 const int kBubbleOffsetY = 10; |
| 30 const float kInitialDelay = 3.8; |
| 31 const float kHideDuration = 0.7; |
| 32 } // namespace |
| 33 |
| 34 @interface OneClickHyperlinkTextView : HyperlinkTextView |
| 35 @end |
| 36 @implementation OneClickHyperlinkTextView |
| 37 - (BOOL)acceptsFirstMouse:(NSEvent*)event { |
| 38 return YES; |
| 39 } |
| 40 @end |
27 | 41 |
28 @interface FullscreenExitBubbleController (PrivateMethods) | 42 @interface FullscreenExitBubbleController (PrivateMethods) |
29 // Sets |exitLabel_| based on |exitLabelPlaceholder_|, | 43 // Sets |exitLabel_| based on |exitLabelPlaceholder_|, |
30 // sets |exitLabelPlaceholder_| to nil. | 44 // sets |exitLabelPlaceholder_| to nil. |
31 - (void)initializeLabel; | 45 - (void)initializeLabel; |
32 | 46 |
33 - (void)hideSoon; | 47 - (void)hideSoon; |
34 | 48 |
35 // Returns the Accelerator for the Toggle Fullscreen menu item. | 49 // Returns the Accelerator for the Toggle Fullscreen menu item. |
36 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen; | 50 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen; |
37 | 51 |
38 // Returns a string representation fit for display of | 52 // Returns a string representation fit for display of |
39 // +acceleratorForToggleFullscreen. | 53 // +acceleratorForToggleFullscreen. |
40 + (NSString*)keyCommandString; | 54 + (NSString*)keyCommandString; |
41 | 55 |
42 + (NSString*)keyCombinationForAccelerator:(const ui::AcceleratorCocoa&)item; | 56 + (NSString*)keyCombinationForAccelerator:(const ui::AcceleratorCocoa&)item; |
43 @end | 57 @end |
44 | 58 |
45 @implementation FullscreenExitBubbleController | 59 @implementation FullscreenExitBubbleController |
46 | 60 |
47 - (id)initWithOwner:(BrowserWindowController*)owner browser:(Browser*)browser { | 61 - (id)initWithOwner:(BrowserWindowController*)owner |
48 if ((self = [super initWithNibName:@"FullscreenExitBubble" | 62 browser:(Browser*)browser |
49 bundle:base::mac::MainAppBundle()])) { | 63 url:(const GURL&)url |
| 64 askPermission:(BOOL)askPermission { |
| 65 NSString* nibPath = |
| 66 [base::mac::MainAppBundle() pathForResource:@"FullscreenExitBubble" |
| 67 ofType:@"nib"]; |
| 68 if ((self = [super initWithWindowNibPath:nibPath owner:self])) { |
50 browser_ = browser; | 69 browser_ = browser; |
51 owner_ = owner; | 70 owner_ = owner; |
| 71 url_ = url; |
| 72 showButtons_ = askPermission; |
52 } | 73 } |
53 return self; | 74 return self; |
54 } | 75 } |
55 | 76 |
56 - (void)awakeFromNib { | 77 - (void)allow:(id)sender { |
57 [self initializeLabel]; | 78 [self hideButtons]; |
| 79 browser_->OnAcceptFullscreenPermission(url_); |
58 [self hideSoon]; | 80 [self hideSoon]; |
59 } | 81 } |
60 | 82 |
| 83 - (void)deny:(id)sender { |
| 84 browser_->ToggleFullscreenMode(false); |
| 85 } |
| 86 |
| 87 - (void)hideButtons { |
| 88 [allowButton_ setHidden:YES]; |
| 89 [denyButton_ setHidden:YES]; |
| 90 [exitLabel_ setHidden:NO]; |
| 91 } |
| 92 |
| 93 // We want this to be a child of a browser window. addChildWindow: |
| 94 // (called from this function) will bring the window on-screen; |
| 95 // unfortunately, [NSWindowController showWindow:] will also bring it |
| 96 // on-screen (but will cause unexpected changes to the window's |
| 97 // position). We cannot have an addChildWindow: and a subsequent |
| 98 // showWindow:. Thus, we have our own version. |
| 99 - (void)showWindow { |
| 100 // Completes nib load. |
| 101 InfoBubbleWindow* info_bubble = static_cast<InfoBubbleWindow*>([self window]); |
| 102 [bubble_ setArrowLocation:info_bubble::kNoArrow]; |
| 103 [info_bubble setCanBecomeKeyWindow:NO]; |
| 104 if (!showButtons_) { |
| 105 [self hideButtons]; |
| 106 [self hideSoon]; |
| 107 } |
| 108 NSRect windowFrame = [owner_ window].frame; |
| 109 [tweaker_ tweakUI:info_bubble]; |
| 110 [self positionInWindowAtTop:NSHeight(windowFrame) width:NSWidth(windowFrame)]; |
| 111 [[owner_ window] addChildWindow:info_bubble ordered:NSWindowAbove]; |
| 112 |
| 113 [info_bubble orderFront:self]; |
| 114 } |
| 115 |
| 116 - (void)awakeFromNib { |
| 117 DCHECK([[self window] isKindOfClass:[InfoBubbleWindow class]]); |
| 118 NSString* title = |
| 119 l10n_util::GetNSStringF(IDS_FULLSCREEN_INFOBAR_REQUEST_PERMISSION, |
| 120 UTF8ToUTF16(url_.host())); |
| 121 [messageLabel_ setStringValue:title]; |
| 122 [self initializeLabel]; |
| 123 } |
| 124 |
61 - (void)positionInWindowAtTop:(CGFloat)maxY width:(CGFloat)maxWidth { | 125 - (void)positionInWindowAtTop:(CGFloat)maxY width:(CGFloat)maxWidth { |
62 NSRect bubbleFrame = [[self view] frame]; | 126 NSRect windowFrame = [self window].frame; |
63 bubbleFrame.origin.x = (int)(maxWidth/2 - NSWidth(bubbleFrame)/2); | 127 NSPoint origin; |
64 bubbleFrame.origin.y = maxY - NSHeight(bubbleFrame); | 128 origin.x = (int)(maxWidth/2 - NSWidth(windowFrame)/2); |
65 [[self view] setFrame:bubbleFrame]; | 129 origin.y = maxY - NSHeight(windowFrame); |
| 130 origin.y -= kBubbleOffsetY; |
| 131 [[self window] setFrameOrigin:origin]; |
66 } | 132 } |
67 | 133 |
68 // Called when someone clicks on the embedded link. | 134 // Called when someone clicks on the embedded link. |
69 - (BOOL) textView:(NSTextView*)textView | 135 - (BOOL) textView:(NSTextView*)textView |
70 clickedOnLink:(id)link | 136 clickedOnLink:(id)link |
71 atIndex:(NSUInteger)charIndex { | 137 atIndex:(NSUInteger)charIndex { |
72 browser_->ExecuteCommand(IDC_FULLSCREEN); | 138 browser_->ExecuteCommand(IDC_FULLSCREEN); |
73 return YES; | 139 return YES; |
74 } | 140 } |
75 | 141 |
76 - (void)hideTimerFired:(NSTimer*)timer { | 142 - (void)hideTimerFired:(NSTimer*)timer { |
77 NSRect endFrame = [[self view] frame]; | 143 [NSAnimationContext beginGrouping]; |
78 endFrame.origin.y += endFrame.size.height; | 144 [[NSAnimationContext currentContext] |
79 endFrame.size.height = 0; | 145 gtm_setDuration:kHideDuration |
80 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys: | 146 eventMask:NSLeftMouseUpMask|NSLeftMouseDownMask]; |
81 [self view], NSViewAnimationTargetKey, | 147 [[[self window] animator] setAlphaValue:0.0]; |
82 [NSValue valueWithRect:endFrame], NSViewAnimationEndFrameKey, nil]; | 148 [NSAnimationContext endGrouping]; |
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 } | 149 } |
93 | 150 |
94 - (void)animationDidEnd:(NSAnimation*)animation { | 151 - (void)animationDidEnd:(NSAnimation*)animation { |
95 if (animation == hideAnimation_.get()) { | 152 if (animation == hideAnimation_.get()) { |
96 hideAnimation_.reset(); | 153 hideAnimation_.reset(); |
97 } | 154 } |
98 } | 155 } |
99 | 156 |
100 - (AnimatableView*)animatableView { | |
101 return static_cast<AnimatableView*>([self view]); | |
102 } | |
103 | |
104 - (void)dealloc { | 157 - (void)dealloc { |
105 [hideAnimation_.get() stopAnimation]; | 158 [hideAnimation_.get() stopAnimation]; |
106 [hideTimer_ invalidate]; | 159 [hideTimer_ invalidate]; |
107 [super dealloc]; | 160 [super dealloc]; |
108 } | 161 } |
109 | 162 |
110 @end | 163 @end |
111 | 164 |
112 @implementation FullscreenExitBubbleController (PrivateMethods) | 165 @implementation FullscreenExitBubbleController (PrivateMethods) |
113 | 166 |
114 - (void)initializeLabel { | 167 - (void)initializeLabel { |
115 // Replace the label placeholder NSTextField with the real label NSTextView. | 168 // 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 | 169 // 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 | 170 // in IB without a containing scroll view, so create the NSTextView |
118 // programmatically. | 171 // programmatically. |
119 exitLabel_.reset([[HyperlinkTextView alloc] | 172 exitLabel_.reset([[OneClickHyperlinkTextView alloc] |
120 initWithFrame:[exitLabelPlaceholder_ frame]]); | 173 initWithFrame:[exitLabelPlaceholder_ frame]]); |
121 [exitLabel_.get() setAutoresizingMask: | 174 [exitLabel_.get() setAutoresizingMask: |
122 [exitLabelPlaceholder_ autoresizingMask]]; | 175 [exitLabelPlaceholder_ autoresizingMask]]; |
| 176 [exitLabel_.get() setHidden:[exitLabelPlaceholder_ isHidden]]; |
123 [[exitLabelPlaceholder_ superview] | 177 [[exitLabelPlaceholder_ superview] |
124 replaceSubview:exitLabelPlaceholder_ with:exitLabel_.get()]; | 178 replaceSubview:exitLabelPlaceholder_ with:exitLabel_.get()]; |
125 exitLabelPlaceholder_ = nil; // Now released. | 179 exitLabelPlaceholder_ = nil; // Now released. |
126 [exitLabel_.get() setDelegate:self]; | 180 [exitLabel_.get() setDelegate:self]; |
127 | 181 |
128 NSString *message = l10n_util::GetNSStringF(IDS_EXIT_FULLSCREEN_MODE, | 182 NSString *message = l10n_util::GetNSStringF(IDS_EXIT_FULLSCREEN_MODE, |
129 base::SysNSStringToUTF16([[self class] keyCommandString])); | 183 base::SysNSStringToUTF16([[self class] keyCommandString])); |
130 | 184 |
| 185 NSFont* font = [NSFont systemFontOfSize: |
| 186 [NSFont systemFontSizeForControlSize:NSRegularControlSize]]; |
131 [(HyperlinkTextView*)exitLabel_.get() | 187 [(HyperlinkTextView*)exitLabel_.get() |
132 setMessageAndLink:@"" | 188 setMessageAndLink:@"" |
133 withLink:message | 189 withLink:message |
134 atOffset:0 | 190 atOffset:0 |
135 font:[NSFont systemFontOfSize:18] | 191 font:font |
136 messageColor:[NSColor whiteColor] | 192 messageColor:[NSColor blackColor] |
137 linkColor:[NSColor whiteColor]]; | 193 linkColor:[NSColor blueColor]]; |
| 194 [exitLabel_.get() setAlignment:NSRightTextAlignment]; |
138 | 195 |
139 [exitLabel_.get() sizeToFit]; | 196 NSRect labelFrame = [exitLabel_ frame]; |
140 NSLayoutManager* layoutManager = [exitLabel_.get() layoutManager]; | 197 |
141 NSTextContainer* textContainer = [exitLabel_.get() textContainer]; | 198 // NSTextView's sizeToFit: method seems to enjoy wrapping lines. Temporarily |
| 199 // set the size large to force it not to. |
| 200 NSRect windowFrame = [[self window] frame]; |
| 201 [exitLabel_ setFrameSize:windowFrame.size]; |
| 202 NSLayoutManager* layoutManager = [exitLabel_ layoutManager]; |
| 203 NSTextContainer* textContainer = [exitLabel_ textContainer]; |
142 [layoutManager ensureLayoutForTextContainer:textContainer]; | 204 [layoutManager ensureLayoutForTextContainer:textContainer]; |
143 NSRect textFrame = [layoutManager usedRectForTextContainer:textContainer]; | 205 NSRect textFrame = [layoutManager usedRectForTextContainer:textContainer]; |
144 NSRect frame = [[self view] frame]; | 206 |
145 NSSize textSize = textFrame.size; | 207 labelFrame.origin.x += NSWidth(labelFrame) - NSWidth(textFrame); |
146 frame.size.width = textSize.width + 2 * kPaddingPx; | 208 labelFrame.size = textFrame.size; |
147 [[self view] setFrame:frame]; | 209 [exitLabel_ setFrame:labelFrame]; |
148 textFrame.origin.x = textFrame.origin.y = kPaddingPx; | |
149 [exitLabel_.get() setFrame:textFrame]; | |
150 } | 210 } |
151 | 211 |
152 // This looks at the Main Menu and determines what the user has set as the | 212 // 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 | 213 // key combination for quit. It then gets the modifiers and builds an object |
154 // to hold the data. | 214 // to hold the data. |
155 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen { | 215 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen { |
156 NSMenu* mainMenu = [NSApp mainMenu]; | 216 NSMenu* mainMenu = [NSApp mainMenu]; |
157 // Get the application menu (i.e. Chromium). | 217 // Get the application menu (i.e. Chromium). |
158 for (NSMenuItem* menu in [mainMenu itemArray]) { | 218 for (NSMenuItem* menu in [mainMenu itemArray]) { |
159 for (NSMenuItem* item in [[menu submenu] itemArray]) { | 219 for (NSMenuItem* item in [[menu submenu] itemArray]) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 characterIsMember:[item.characters() characterAtIndex:0]]; | 251 characterIsMember:[item.characters() characterAtIndex:0]]; |
192 if (modifiers & NSShiftKeyMask || isUpperCase) | 252 if (modifiers & NSShiftKeyMask || isUpperCase) |
193 [string appendString:@"\u21E7"]; | 253 [string appendString:@"\u21E7"]; |
194 | 254 |
195 [string appendString:[item.characters() uppercaseString]]; | 255 [string appendString:[item.characters() uppercaseString]]; |
196 return string; | 256 return string; |
197 } | 257 } |
198 | 258 |
199 - (void)hideSoon { | 259 - (void)hideSoon { |
200 hideTimer_.reset( | 260 hideTimer_.reset( |
201 [[NSTimer scheduledTimerWithTimeInterval:kInitialDelayMs/1000.0 | 261 [[NSTimer scheduledTimerWithTimeInterval:kInitialDelay |
202 target:self | 262 target:self |
203 selector:@selector(hideTimerFired:) | 263 selector:@selector(hideTimerFired:) |
204 userInfo:nil | 264 userInfo:nil |
205 repeats:NO] retain]); | 265 repeats:NO] retain]); |
206 } | 266 } |
207 | 267 |
208 @end | 268 @end |
OLD | NEW |