blob: 79803a32557e6265f2025ec105ae7c0d55bcfa19 [file] [log] [blame]
[email protected]3361e1f2012-03-20 20:31:441// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]38a4d772011-06-16 21:25:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Jinho Bang138fde32018-01-18 23:13:425#include <memory>
6
dcheng0765c492016-04-06 22:41:537#include "remoting/host/continue_window.h"
8
[email protected]729189552011-06-30 00:19:349#import <Cocoa/Cocoa.h>
[email protected]38a4d772011-06-16 21:25:3410
Hans Wennborgda08b7362020-05-14 17:30:5011#include "base/check_op.h"
[email protected]38a4d772011-06-16 21:25:3412#include "base/compiler_specific.h"
[email protected]a8522032013-06-24 22:51:4613#include "base/mac/scoped_nsobject.h"
avic5960f32015-12-22 22:49:4814#include "base/macros.h"
[email protected]13ac53532013-03-30 00:27:0015#include "base/strings/sys_string_conversions.h"
[email protected]fc877cf2013-07-31 23:08:3916#include "remoting/base/string_resources.h"
[email protected]fc877cf2013-07-31 23:08:3917#include "ui/base/l10n/l10n_util_mac.h"
[email protected]1bafb3c2011-11-23 23:39:5018
[email protected]78b1ef62011-11-01 22:20:2519// Handles the ContinueWindow.
20@interface ContinueWindowMacController : NSObject {
21 @private
Robert Liao61b89292019-12-13 18:15:1422 base::scoped_nsobject<NSMutableArray> _shades;
23 base::scoped_nsobject<NSAlert> _continue_alert;
24 remoting::ContinueWindow* _continue_window;
[email protected]78b1ef62011-11-01 22:20:2525}
[email protected]729189552011-06-30 00:19:3426
[email protected]fc877cf2013-07-31 23:08:3927- (id)initWithWindow:(remoting::ContinueWindow*)continue_window;
[email protected]78b1ef62011-11-01 22:20:2528- (void)show;
29- (void)hide;
30- (void)onCancel:(id)sender;
31- (void)onContinue:(id)sender;
[email protected]729189552011-06-30 00:19:3432@end
[email protected]38a4d772011-06-16 21:25:3433
34namespace remoting {
35
[email protected]78b1ef62011-11-01 22:20:2536// A bridge between C++ and ObjC implementations of ContinueWindow.
37// Everything important occurs in ContinueWindowMacController.
[email protected]ac115ce2013-04-24 20:44:3838class ContinueWindowMac : public ContinueWindow {
[email protected]38a4d772011-06-16 21:25:3439 public:
[email protected]fc877cf2013-07-31 23:08:3940 ContinueWindowMac();
dcheng562aba52014-10-21 12:30:1441 ~ContinueWindowMac() override;
[email protected]38a4d772011-06-16 21:25:3442
[email protected]ac115ce2013-04-24 20:44:3843 protected:
44 // ContinueWindow overrides.
dcheng562aba52014-10-21 12:30:1445 void ShowUi() override;
46 void HideUi() override;
[email protected]38a4d772011-06-16 21:25:3447
48 private:
[email protected]a8522032013-06-24 22:51:4649 base::scoped_nsobject<ContinueWindowMacController> controller_;
[email protected]b47049a2013-01-16 20:30:0750
[email protected]38a4d772011-06-16 21:25:3451 DISALLOW_COPY_AND_ASSIGN(ContinueWindowMac);
52};
53
[email protected]fc877cf2013-07-31 23:08:3954ContinueWindowMac::ContinueWindowMac() {
[email protected]b47049a2013-01-16 20:30:0755}
56
[email protected]ac115ce2013-04-24 20:44:3857ContinueWindowMac::~ContinueWindowMac() {
gabbf77513a2017-06-01 14:35:3458 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Erik Jensen61acedf32020-04-02 03:34:5559 if (controller_) {
60 HideUi();
61 }
[email protected]ac115ce2013-04-24 20:44:3862}
[email protected]b47049a2013-01-16 20:30:0763
[email protected]ac115ce2013-04-24 20:44:3864void ContinueWindowMac::ShowUi() {
gabbf77513a2017-06-01 14:35:3465 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]ac115ce2013-04-24 20:44:3866
Avi Drissman91160c632019-09-06 19:24:5867 @autoreleasepool {
68 controller_.reset(
69 [[ContinueWindowMacController alloc] initWithWindow:this]);
70 [controller_ show];
71 }
[email protected]38a4d772011-06-16 21:25:3472}
73
[email protected]ac115ce2013-04-24 20:44:3874void ContinueWindowMac::HideUi() {
gabbf77513a2017-06-01 14:35:3475 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]ac115ce2013-04-24 20:44:3876
Avi Drissman91160c632019-09-06 19:24:5877 @autoreleasepool {
78 [controller_ hide];
Erik Jensen61acedf32020-04-02 03:34:5579 controller_.reset();
Avi Drissman91160c632019-09-06 19:24:5880 }
[email protected]38a4d772011-06-16 21:25:3481}
82
[email protected]ac115ce2013-04-24 20:44:3883// static
dcheng0765c492016-04-06 22:41:5384std::unique_ptr<HostWindow> HostWindow::CreateContinueWindow() {
Jinho Bang138fde32018-01-18 23:13:4285 return std::make_unique<ContinueWindowMac>();
[email protected]38a4d772011-06-16 21:25:3486}
87
88} // namespace remoting
[email protected]78b1ef62011-11-01 22:20:2589
90@implementation ContinueWindowMacController
91
[email protected]fc877cf2013-07-31 23:08:3992- (id)initWithWindow:(remoting::ContinueWindow*)continue_window {
[email protected]78b1ef62011-11-01 22:20:2593 if ((self = [super init])) {
Robert Liao61b89292019-12-13 18:15:1494 _continue_window = continue_window;
[email protected]78b1ef62011-11-01 22:20:2595 }
96 return self;
97}
98
99- (void)show {
100 // Generate window shade
101 NSArray* screens = [NSScreen screens];
Robert Liao61b89292019-12-13 18:15:14102 _shades.reset([[NSMutableArray alloc] initWithCapacity:[screens count]]);
[email protected]78b1ef62011-11-01 22:20:25103 for (NSScreen *screen in screens) {
104 NSWindow* shade =
105 [[[NSWindow alloc] initWithContentRect:[screen frame]
106 styleMask:NSBorderlessWindowMask
107 backing:NSBackingStoreBuffered
108 defer:NO
109 screen:screen] autorelease];
110 [shade setReleasedWhenClosed:NO];
111 [shade setAlphaValue:0.8];
112 [shade setOpaque:NO];
113 [shade setBackgroundColor:[NSColor blackColor]];
114 // Raise the window shade above just about everything else.
115 // Leave the dock and menu bar exposed so the user has some basic level
116 // of control (like they can quit Chromium).
117 [shade setLevel:NSModalPanelWindowLevel - 1];
118 [shade orderFront:nil];
Robert Liao61b89292019-12-13 18:15:14119 [_shades addObject:shade];
[email protected]78b1ef62011-11-01 22:20:25120 }
121
122 // Create alert.
Robert Liao61b89292019-12-13 18:15:14123 _continue_alert.reset([[NSAlert alloc] init]);
124 [_continue_alert setMessageText:l10n_util::GetNSString(IDS_CONTINUE_PROMPT)];
[email protected]78b1ef62011-11-01 22:20:25125
126 NSButton* continue_button =
Robert Liao61b89292019-12-13 18:15:14127 [_continue_alert addButtonWithTitle:l10n_util::GetNSString(
[email protected]8bf71782014-01-27 21:44:11128 IDS_CONTINUE_BUTTON)];
[email protected]78b1ef62011-11-01 22:20:25129 [continue_button setAction:@selector(onContinue:)];
130 [continue_button setTarget:self];
131
132 NSButton* cancel_button =
Robert Liao61b89292019-12-13 18:15:14133 [_continue_alert addButtonWithTitle:l10n_util::GetNSString(
[email protected]8bf71782014-01-27 21:44:11134 IDS_STOP_SHARING_BUTTON)];
[email protected]78b1ef62011-11-01 22:20:25135 [cancel_button setAction:@selector(onCancel:)];
136 [cancel_button setTarget:self];
137
138 NSBundle *bundle = [NSBundle bundleForClass:[self class]];
139 NSString *imagePath = [bundle pathForResource:@"chromoting128" ofType:@"png"];
[email protected]a8522032013-06-24 22:51:46140 base::scoped_nsobject<NSImage> image(
[email protected]78b1ef62011-11-01 22:20:25141 [[NSImage alloc] initByReferencingFile:imagePath]);
Robert Liao61b89292019-12-13 18:15:14142 [_continue_alert setIcon:image];
143 [_continue_alert layout];
[email protected]78b1ef62011-11-01 22:20:25144
145 // Force alert to be at the proper level and location.
Robert Liao61b89292019-12-13 18:15:14146 NSWindow* continue_window = [_continue_alert window];
[email protected]78b1ef62011-11-01 22:20:25147 [continue_window center];
148 [continue_window setLevel:NSModalPanelWindowLevel];
149 [continue_window orderWindow:NSWindowAbove
Robert Liao61b89292019-12-13 18:15:14150 relativeTo:[[_shades lastObject] windowNumber]];
[email protected]78b1ef62011-11-01 22:20:25151 [continue_window makeKeyWindow];
152}
153
154- (void)hide {
155 // Remove window shade.
Robert Liao61b89292019-12-13 18:15:14156 for (NSWindow* window in _shades.get()) {
[email protected]78b1ef62011-11-01 22:20:25157 [window close];
158 }
Robert Liao61b89292019-12-13 18:15:14159 _shades.reset();
160 if (_continue_alert) {
161 [[_continue_alert window] close];
162 _continue_alert.reset();
Lambros Lambrou7eabfd92019-09-04 22:46:50163 }
[email protected]78b1ef62011-11-01 22:20:25164}
165
166- (void)onCancel:(id)sender {
167 [self hide];
Robert Liao61b89292019-12-13 18:15:14168 _continue_window->DisconnectSession();
[email protected]78b1ef62011-11-01 22:20:25169}
170
171- (void)onContinue:(id)sender {
172 [self hide];
Robert Liao61b89292019-12-13 18:15:14173 _continue_window->ContinueSession();
[email protected]78b1ef62011-11-01 22:20:25174}
175
176@end