Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(751)

Side by Side Diff: chrome/browser/ui/cocoa/fullscreen_exit_bubble_controller.mm

Issue 7890056: FullscreenExitBubble temp UI for Mac. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: add test Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #import <Cocoa/Cocoa.h>
6
7 #include "base/logging.h" // for NOTREACHED()
8 #include "base/mac/mac_util.h"
9 #include "base/sys_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/ui/browser.h"
12 #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"
15 #include "chrome/browser/ui/cocoa/event_utils.h"
16 #import "chrome/browser/ui/cocoa/fullscreen_exit_bubble_controller.h"
17 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
18 #include "grit/generated_resources.h"
19 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
20 #import "third_party/GTM/AppKit/GTMNSAnimation+Duration.h"
21 #include "ui/base/models/accelerator_cocoa.h"
22 #include "ui/base/l10n/l10n_util_mac.h"
23
24 const int kPaddingPx = 8;
25 const int kInitialDelayMs = 3800;
26 const int kSlideOutDurationMs = 700;
27
28 @interface FullscreenExitBubbleController (PrivateMethods)
29 // Sets |exitLabel_| based on |exitLabelPlaceholder_|,
30 // sets |exitLabelPlaceholder_| to nil.
31 - (void)initializeLabel;
32
33 - (void)hideSoon;
34
35 // Returns the Accelerator for the Toggle Fullscreen menu item.
36 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen;
37
38 // Returns a string representation fit for display of
39 // +acceleratorForToggleFullscreen.
40 + (NSString*)keyCommandString;
41
42 + (NSString*)keyCombinationForAccelerator:(const ui::AcceleratorCocoa&)item;
43 @end
44
45 @implementation FullscreenExitBubbleController
46
47 - (id)initWithOwner:(BrowserWindowController*)owner browser:(Browser*)browser {
48 if ((self = [super initWithNibName:@"FullscreenExitBubble"
49 bundle:base::mac::MainAppBundle()])) {
50 browser_ = browser;
51 owner_ = owner;
52 }
53 return self;
54 }
55
56 - (void)awakeFromNib {
57 [self initializeLabel];
58 [self hideSoon];
59 }
60
61 - (void)positionInWindowAtTop:(CGFloat)maxY width:(CGFloat)maxWidth {
62 NSRect bubbleFrame = [[self view] frame];
63 bubbleFrame.origin.x = (int)(maxWidth/2 - NSWidth(bubbleFrame)/2);
64 bubbleFrame.origin.y = maxY - NSHeight(bubbleFrame);
65 [[self view] setFrame:bubbleFrame];
66 }
67
68 // Called when someone clicks on the embedded link.
69 - (BOOL) textView:(NSTextView*)textView
70 clickedOnLink:(id)link
71 atIndex:(NSUInteger)charIndex {
72 browser_->ExecuteCommand(IDC_FULLSCREEN);
73 return YES;
74 }
75
76 - (void)hideTimerFired:(NSTimer*)timer {
77 NSRect endFrame = [[self view] frame];
78 endFrame.origin.y += endFrame.size.height;
79 endFrame.size.height = 0;
80 NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
81 [self view], NSViewAnimationTargetKey,
82 [NSValue valueWithRect:endFrame], NSViewAnimationEndFrameKey, nil];
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 }
93
94 - (void)animationDidEnd:(NSAnimation*)animation {
95 if (animation == hideAnimation_.get()) {
96 hideAnimation_.reset(nil);
97 }
98
99 [owner_ setShowFloatingChrome:YES];
100 }
101
102 - (AnimatableView*)animatableView {
103 return static_cast<AnimatableView*>([self view]);
104 }
105
106 - (void)dealloc {
107 [hideAnimation_.get() stopAnimation];
108 [hideTimer_ invalidate];
109 [super dealloc];
110 }
111
112 @end
113
114 @implementation FullscreenExitBubbleController (PrivateMethods)
115
116 - (void)initializeLabel {
117 // Replace the label placeholder NSTextField with the real label NSTextView.
118 // The former doesn't show links in a nice way, but the latter can't be added
119 // in IB without a containing scroll view, so create the NSTextView
120 // programmatically.
121 exitLabel_.reset([[HyperlinkTextView alloc]
122 initWithFrame:[exitLabelPlaceholder_ frame]]);
123 [exitLabel_.get() setAutoresizingMask:
124 [exitLabelPlaceholder_ autoresizingMask]];
125 [[exitLabelPlaceholder_ superview]
126 replaceSubview:exitLabelPlaceholder_ with:exitLabel_.get()];
127 exitLabelPlaceholder_ = nil; // Now released.
128 [exitLabel_.get() setDelegate:self];
129
130 NSString *message = l10n_util::GetNSStringF(IDS_EXIT_FULLSCREEN_MODE,
131 base::SysNSStringToUTF16([[self class] keyCommandString]));
132
133 [(HyperlinkTextView*)exitLabel_.get()
134 setMessageAndLink:@""
135 withLink:message
136 atOffset:0
137 font:[NSFont systemFontOfSize:18]
138 messageColor:[NSColor whiteColor]
139 linkColor:[NSColor whiteColor]];
140
141 [exitLabel_.get() sizeToFit];
142 NSLayoutManager* layoutManager = [exitLabel_.get() layoutManager];
143 NSTextContainer* textContainer = [exitLabel_.get() textContainer];
144 [layoutManager ensureLayoutForTextContainer:textContainer];
145 NSRect textFrame = [layoutManager usedRectForTextContainer:textContainer];
146 NSRect frame = [[self view] frame];
147 NSSize textSize = textFrame.size;
148 frame.size.width = textSize.width + 2 * kPaddingPx;
149 [[self view] setFrame:frame];
150 textFrame.origin.x = textFrame.origin.y = kPaddingPx;
151 [exitLabel_.get() setFrame:textFrame];
152 }
153
154 // This looks at the Main Menu and determines what the user has set as the
155 // key combination for quit. It then gets the modifiers and builds an object
156 // to hold the data.
157 + (ui::AcceleratorCocoa)acceleratorForToggleFullscreen {
158 NSMenu* mainMenu = [NSApp mainMenu];
159 // Get the application menu (i.e. Chromium).
160 for (NSMenuItem* menu in [mainMenu itemArray]) {
161 for (NSMenuItem* item in [[menu submenu] itemArray]) {
162 // Find the toggle presentation mode item.
163 if ([item tag] == IDC_PRESENTATION_MODE) {
164 return ui::AcceleratorCocoa([item keyEquivalent],
165 [item keyEquivalentModifierMask]);
166 }
167 }
168 }
169 // Default to Cmd+Shift+F.
170 return ui::AcceleratorCocoa(@"f", NSCommandKeyMask|NSShiftKeyMask);
171 }
172
173 // This looks at the Main Menu and determines what the user has set as the
174 // key combination for quit. It then gets the modifiers and builds a string
175 // to display them.
176 + (NSString*)keyCommandString {
177 ui::AcceleratorCocoa accelerator =
178 [[self class] acceleratorForToggleFullscreen];
179 return [[self class] keyCombinationForAccelerator:accelerator];
180 }
181
182 + (NSString*)keyCombinationForAccelerator:(const ui::AcceleratorCocoa&)item {
183 NSMutableString* string = [NSMutableString string];
184 NSUInteger modifiers = item.modifiers();
185
186 if (modifiers & NSCommandKeyMask)
187 [string appendString:@"\u2318"];
188 if (modifiers & NSControlKeyMask)
189 [string appendString:@"\u2303"];
190 if (modifiers & NSAlternateKeyMask)
191 [string appendString:@"\u2325"];
192 BOOL isUpperCase = [[NSCharacterSet uppercaseLetterCharacterSet]
193 characterIsMember:[item.characters() characterAtIndex:0]];
194 if (modifiers & NSShiftKeyMask || isUpperCase)
195 [string appendString:@"\u21E7"];
196
197 [string appendString:[item.characters() uppercaseString]];
198 return string;
199 }
200
201 - (void)hideSoon {
202 hideTimer_.reset(
203 [[NSTimer scheduledTimerWithTimeInterval:kInitialDelayMs/1000.0
204 target:self
205 selector:@selector(hideTimerFired:)
206 userInfo:nil
207 repeats:NO] retain]);
208 }
209
210 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698