1 // Copyright 2013 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 #include "chrome/browser/ui/cocoa/screen_capture_notification_ui_cocoa.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/compiler_specific.h"
10 #include "base/i18n/rtl.h"
11 #include "base/mac/mac_util.h"
12 #include "base/mac/scoped_nsobject.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/sys_string_conversions.h"
15 #include "grit/generated_resources.h"
16 #include "grit/theme_resources.h"
17 #include "skia/ext/skia_utils_mac.h"
18 #import "ui/base/cocoa/controls/blue_label_button.h"
19 #import "ui/base/cocoa/controls/hyperlink_button_cell.h"
20 #include "ui/base/cocoa/window_size_constants.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/resource/resource_bundle.h"
23 #include "ui/gfx/font_list.h"
24 #include "ui/gfx/image/image_skia.h"
25 #include "ui/gfx/image/image_skia_util_mac.h"
26 #include "ui/gfx/text_elider.h"
27 #include "ui/native_theme/native_theme.h"
29 const CGFloat kMinimumWidth = 460;
30 const CGFloat kMaximumWidth = 1000;
31 const CGFloat kHorizontalMargin = 10;
32 const CGFloat kPaddingVertical = 5;
33 const CGFloat kPaddingHorizontal = 10;
34 const CGFloat kWindowCornerRadius = 2;
35 const CGFloat kWindowAlphaValue = 0.85;
37 @interface ScreenCaptureNotificationController()
39 - (void)populateWithText:(const base::string16&)text;
42 @interface ScreenCaptureNotificationView : NSView
45 @interface WindowGripView : NSImageView
46 - (WindowGripView*)init;
50 ScreenCaptureNotificationUICocoa::ScreenCaptureNotificationUICocoa(
51 const base::string16& text)
55 ScreenCaptureNotificationUICocoa::~ScreenCaptureNotificationUICocoa() {}
57 gfx::NativeViewId ScreenCaptureNotificationUICocoa::OnStarted(
58 const base::Closure& stop_callback) {
59 DCHECK(!stop_callback.is_null());
60 DCHECK(!windowController_);
62 windowController_.reset([[ScreenCaptureNotificationController alloc]
63 initWithCallback:stop_callback
65 [windowController_ showWindow:nil];
66 return [[windowController_ window] windowNumber];
69 scoped_ptr<ScreenCaptureNotificationUI> ScreenCaptureNotificationUI::Create(
70 const base::string16& text) {
71 return scoped_ptr<ScreenCaptureNotificationUI>(
72 new ScreenCaptureNotificationUICocoa(text));
75 @implementation ScreenCaptureNotificationController
76 - (id)initWithCallback:(const base::Closure&)stop_callback
77 text:(const base::string16&)text {
78 base::scoped_nsobject<NSWindow> window(
79 [[NSWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater
80 styleMask:NSBorderlessWindowMask
81 backing:NSBackingStoreBuffered
83 [window setReleasedWhenClosed:NO];
84 [window setAlphaValue:kWindowAlphaValue];
85 [window setBackgroundColor:[NSColor clearColor]];
86 [window setOpaque:NO];
87 [window setHasShadow:YES];
88 [window setLevel:NSStatusWindowLevel];
89 [window setMovableByWindowBackground:YES];
90 [window setDelegate:self];
92 self = [super initWithWindow:window];
94 stop_callback_ = stop_callback;
95 [self populateWithText:text];
97 // Center the window at the bottom of the screen, above the dock (if
99 NSRect desktopRect = [[NSScreen mainScreen] visibleFrame];
100 NSRect contentRect = [[window contentView] frame];
102 NSMakeRect((NSWidth(desktopRect) - NSWidth(contentRect)) / 2,
104 NSWidth(contentRect),
105 NSHeight(contentRect));
106 [window setFrame:windowRect display:YES];
111 - (void)stopSharing:(id)sender {
112 if (!stop_callback_.is_null()) {
113 base::Closure callback = stop_callback_;
114 stop_callback_.Reset();
115 callback.Run(); // Deletes |self|.
119 - (void)minimize:(id)sender {
120 [[self window] miniaturize:sender];
124 stop_callback_.Reset();
128 - (void)populateWithText:(const base::string16&)text {
129 base::scoped_nsobject<ScreenCaptureNotificationView> content(
130 [[ScreenCaptureNotificationView alloc]
131 initWithFrame:ui::kWindowSizeDeterminedLater]);
132 [[self window] setContentView:content];
135 stopButton_.reset([[BlueLabelButton alloc] initWithFrame:NSZeroRect]);
136 [stopButton_ setTitle:l10n_util::GetNSString(
137 IDS_MEDIA_SCREEN_CAPTURE_NOTIFICATION_STOP)];
138 [stopButton_ setTarget:self];
139 [stopButton_ setAction:@selector(stopSharing:)];
140 [stopButton_ sizeToFit];
141 [content addSubview:stopButton_];
143 base::scoped_nsobject<HyperlinkButtonCell> cell(
144 [[HyperlinkButtonCell alloc]
145 initTextCell:l10n_util::GetNSString(
146 IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON)]);
147 [cell setShouldUnderline:NO];
149 minimizeButton_.reset([[NSButton alloc] initWithFrame:NSZeroRect]);
150 [minimizeButton_ setCell:cell.get()];
151 [minimizeButton_ sizeToFit];
152 [minimizeButton_ setTarget:self];
153 [minimizeButton_ setAction:@selector(minimize:)];
154 [content addSubview:minimizeButton_];
156 CGFloat buttonsWidth = NSWidth([stopButton_ frame]) + kHorizontalMargin +
157 NSWidth([minimizeButton_ frame]);
158 CGFloat totalHeight =
159 kPaddingVertical + NSHeight([stopButton_ frame]) + kPaddingVertical;
162 base::scoped_nsobject<WindowGripView> gripView([[WindowGripView alloc] init]);
163 [content addSubview:gripView];
164 CGFloat gripWidth = NSWidth([gripView frame]);
165 CGFloat gripHeight = NSHeight([gripView frame]);
166 [gripView setFrameOrigin:NSMakePoint(kPaddingHorizontal,
167 (totalHeight - gripHeight) / 2)];
169 // Create text label.
171 std::min(kMaximumWidth, NSWidth([[NSScreen mainScreen] visibleFrame]));
172 int maxLabelWidth = maximumWidth - kPaddingHorizontal * 2 -
173 kHorizontalMargin * 2 - gripWidth - buttonsWidth;
174 gfx::FontList font_list;
175 base::string16 elidedText =
176 ElideText(text, font_list, maxLabelWidth, gfx::ELIDE_IN_MIDDLE);
177 NSString* statusText = base::SysUTF16ToNSString(elidedText);
178 base::scoped_nsobject<NSTextField> statusTextField(
179 [[NSTextField alloc] initWithFrame:ui::kWindowSizeDeterminedLater]);
180 [statusTextField setEditable:NO];
181 [statusTextField setSelectable:NO];
182 [statusTextField setDrawsBackground:NO];
183 [statusTextField setBezeled:NO];
184 [statusTextField setStringValue:statusText];
185 [statusTextField setFont:font_list.GetPrimaryFont().GetNativeFont()];
186 [statusTextField sizeToFit];
187 [statusTextField setFrameOrigin:NSMakePoint(
188 kPaddingHorizontal + kHorizontalMargin + gripWidth,
189 (totalHeight - NSHeight([statusTextField frame])) / 2)];
190 [content addSubview:statusTextField];
192 // Resize content view to fit controls.
193 CGFloat minimumLableWidth = kMinimumWidth - kPaddingHorizontal * 2 -
194 kHorizontalMargin * 2 - gripWidth - buttonsWidth;
196 std::max(NSWidth([statusTextField frame]), minimumLableWidth);
197 CGFloat totalWidth = kPaddingHorizontal * 2 + kHorizontalMargin * 2 +
198 gripWidth + lableWidth + buttonsWidth;
199 [content setFrame:NSMakeRect(0, 0, totalWidth, totalHeight)];
201 // Move the buttons to the right place.
202 NSPoint buttonOrigin = NSMakePoint(
203 totalWidth - kPaddingHorizontal - buttonsWidth, kPaddingVertical);
204 [stopButton_ setFrameOrigin:buttonOrigin];
206 [minimizeButton_ setFrameOrigin:NSMakePoint(
207 totalWidth - kPaddingHorizontal - NSWidth([minimizeButton_ frame]),
208 (totalHeight - NSHeight([minimizeButton_ frame])) / 2)];
210 if (base::i18n::IsRTL()) {
212 setFrameOrigin:NSMakePoint(totalWidth - NSMaxX([stopButton_ frame]),
213 NSMinY([stopButton_ frame]))];
215 setFrameOrigin:NSMakePoint(totalWidth - NSMaxX([minimizeButton_ frame]),
216 NSMinY([minimizeButton_ frame]))];
218 setFrameOrigin:NSMakePoint(totalWidth - NSMaxX([statusTextField frame]),
219 NSMinY([statusTextField frame]))];
220 [gripView setFrameOrigin:NSMakePoint(totalWidth - NSMaxX([gripView frame]),
221 NSMinY([gripView frame]))];
225 - (void)windowWillClose:(NSNotification*)notification {
226 [self stopSharing:nil];
231 @implementation ScreenCaptureNotificationView
233 - (void)drawRect:(NSRect)dirtyRect {
234 [gfx::SkColorToSRGBNSColor(ui::NativeTheme::instance()->GetSystemColor(
235 ui::NativeTheme::kColorId_DialogBackground)) set];
236 [[NSBezierPath bezierPathWithRoundedRect:[self bounds]
237 xRadius:kWindowCornerRadius
238 yRadius:kWindowCornerRadius] fill];
243 @implementation WindowGripView
244 - (WindowGripView*)init {
245 gfx::Image gripImage =
246 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed(
247 IDR_SCREEN_CAPTURE_NOTIFICATION_GRIP,
248 ui::ResourceBundle::RTL_DISABLED);
250 initWithFrame:NSMakeRect(0, 0, gripImage.Width(), gripImage.Height())];
251 [self setImage:gripImage.ToNSImage()];
255 - (BOOL)mouseDownCanMoveWindow {