[email protected] | 3361e1f | 2012-03-20 20:31:44 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 5 | #include <memory> |
6 | |||||
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 7 | #include "remoting/host/continue_window.h" |
8 | |||||
[email protected] | 72918955 | 2011-06-30 00:19:34 | [diff] [blame] | 9 | #import <Cocoa/Cocoa.h> |
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 10 | |
Hans Wennborg | da08b736 | 2020-05-14 17:30:50 | [diff] [blame] | 11 | #include "base/check_op.h" |
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 13 | #include "base/mac/scoped_nsobject.h" |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 13ac5353 | 2013-03-30 00:27:00 | [diff] [blame] | 15 | #include "base/strings/sys_string_conversions.h" |
[email protected] | fc877cf | 2013-07-31 23:08:39 | [diff] [blame] | 16 | #include "remoting/base/string_resources.h" |
[email protected] | fc877cf | 2013-07-31 23:08:39 | [diff] [blame] | 17 | #include "ui/base/l10n/l10n_util_mac.h" |
[email protected] | 1bafb3c | 2011-11-23 23:39:50 | [diff] [blame] | 18 | |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 19 | // Handles the ContinueWindow. |
20 | @interface ContinueWindowMacController : NSObject { | ||||
21 | @private | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 22 | base::scoped_nsobject<NSMutableArray> _shades; |
23 | base::scoped_nsobject<NSAlert> _continue_alert; | ||||
24 | remoting::ContinueWindow* _continue_window; | ||||
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 25 | } |
[email protected] | 72918955 | 2011-06-30 00:19:34 | [diff] [blame] | 26 | |
[email protected] | fc877cf | 2013-07-31 23:08:39 | [diff] [blame] | 27 | - (id)initWithWindow:(remoting::ContinueWindow*)continue_window; |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 28 | - (void)show; |
29 | - (void)hide; | ||||
30 | - (void)onCancel:(id)sender; | ||||
31 | - (void)onContinue:(id)sender; | ||||
[email protected] | 72918955 | 2011-06-30 00:19:34 | [diff] [blame] | 32 | @end |
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 33 | |
34 | namespace remoting { | ||||
35 | |||||
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 36 | // A bridge between C++ and ObjC implementations of ContinueWindow. |
37 | // Everything important occurs in ContinueWindowMacController. | ||||
[email protected] | ac115ce | 2013-04-24 20:44:38 | [diff] [blame] | 38 | class ContinueWindowMac : public ContinueWindow { |
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 39 | public: |
[email protected] | fc877cf | 2013-07-31 23:08:39 | [diff] [blame] | 40 | ContinueWindowMac(); |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 41 | ~ContinueWindowMac() override; |
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 42 | |
[email protected] | ac115ce | 2013-04-24 20:44:38 | [diff] [blame] | 43 | protected: |
44 | // ContinueWindow overrides. | ||||
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 45 | void ShowUi() override; |
46 | void HideUi() override; | ||||
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 47 | |
48 | private: | ||||
[email protected] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 49 | base::scoped_nsobject<ContinueWindowMacController> controller_; |
[email protected] | b47049a | 2013-01-16 20:30:07 | [diff] [blame] | 50 | |
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 51 | DISALLOW_COPY_AND_ASSIGN(ContinueWindowMac); |
52 | }; | ||||
53 | |||||
[email protected] | fc877cf | 2013-07-31 23:08:39 | [diff] [blame] | 54 | ContinueWindowMac::ContinueWindowMac() { |
[email protected] | b47049a | 2013-01-16 20:30:07 | [diff] [blame] | 55 | } |
56 | |||||
[email protected] | ac115ce | 2013-04-24 20:44:38 | [diff] [blame] | 57 | ContinueWindowMac::~ContinueWindowMac() { |
gab | bf77513a | 2017-06-01 14:35:34 | [diff] [blame] | 58 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Erik Jensen | 61acedf3 | 2020-04-02 03:34:55 | [diff] [blame] | 59 | if (controller_) { |
60 | HideUi(); | ||||
61 | } | ||||
[email protected] | ac115ce | 2013-04-24 20:44:38 | [diff] [blame] | 62 | } |
[email protected] | b47049a | 2013-01-16 20:30:07 | [diff] [blame] | 63 | |
[email protected] | ac115ce | 2013-04-24 20:44:38 | [diff] [blame] | 64 | void ContinueWindowMac::ShowUi() { |
gab | bf77513a | 2017-06-01 14:35:34 | [diff] [blame] | 65 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | ac115ce | 2013-04-24 20:44:38 | [diff] [blame] | 66 | |
Avi Drissman | 91160c63 | 2019-09-06 19:24:58 | [diff] [blame] | 67 | @autoreleasepool { |
68 | controller_.reset( | ||||
69 | [[ContinueWindowMacController alloc] initWithWindow:this]); | ||||
70 | [controller_ show]; | ||||
71 | } | ||||
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 72 | } |
73 | |||||
[email protected] | ac115ce | 2013-04-24 20:44:38 | [diff] [blame] | 74 | void ContinueWindowMac::HideUi() { |
gab | bf77513a | 2017-06-01 14:35:34 | [diff] [blame] | 75 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | ac115ce | 2013-04-24 20:44:38 | [diff] [blame] | 76 | |
Avi Drissman | 91160c63 | 2019-09-06 19:24:58 | [diff] [blame] | 77 | @autoreleasepool { |
78 | [controller_ hide]; | ||||
Erik Jensen | 61acedf3 | 2020-04-02 03:34:55 | [diff] [blame] | 79 | controller_.reset(); |
Avi Drissman | 91160c63 | 2019-09-06 19:24:58 | [diff] [blame] | 80 | } |
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 81 | } |
82 | |||||
[email protected] | ac115ce | 2013-04-24 20:44:38 | [diff] [blame] | 83 | // static |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 84 | std::unique_ptr<HostWindow> HostWindow::CreateContinueWindow() { |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 85 | return std::make_unique<ContinueWindowMac>(); |
[email protected] | 38a4d77 | 2011-06-16 21:25:34 | [diff] [blame] | 86 | } |
87 | |||||
88 | } // namespace remoting | ||||
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 89 | |
90 | @implementation ContinueWindowMacController | ||||
91 | |||||
[email protected] | fc877cf | 2013-07-31 23:08:39 | [diff] [blame] | 92 | - (id)initWithWindow:(remoting::ContinueWindow*)continue_window { |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 93 | if ((self = [super init])) { |
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 94 | _continue_window = continue_window; |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 95 | } |
96 | return self; | ||||
97 | } | ||||
98 | |||||
99 | - (void)show { | ||||
100 | // Generate window shade | ||||
101 | NSArray* screens = [NSScreen screens]; | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 102 | _shades.reset([[NSMutableArray alloc] initWithCapacity:[screens count]]); |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 103 | 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 Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 119 | [_shades addObject:shade]; |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 120 | } |
121 | |||||
122 | // Create alert. | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 123 | _continue_alert.reset([[NSAlert alloc] init]); |
124 | [_continue_alert setMessageText:l10n_util::GetNSString(IDS_CONTINUE_PROMPT)]; | ||||
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 125 | |
126 | NSButton* continue_button = | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 127 | [_continue_alert addButtonWithTitle:l10n_util::GetNSString( |
[email protected] | 8bf7178 | 2014-01-27 21:44:11 | [diff] [blame] | 128 | IDS_CONTINUE_BUTTON)]; |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 129 | [continue_button setAction:@selector(onContinue:)]; |
130 | [continue_button setTarget:self]; | ||||
131 | |||||
132 | NSButton* cancel_button = | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 133 | [_continue_alert addButtonWithTitle:l10n_util::GetNSString( |
[email protected] | 8bf7178 | 2014-01-27 21:44:11 | [diff] [blame] | 134 | IDS_STOP_SHARING_BUTTON)]; |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 135 | [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] | a852203 | 2013-06-24 22:51:46 | [diff] [blame] | 140 | base::scoped_nsobject<NSImage> image( |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 141 | [[NSImage alloc] initByReferencingFile:imagePath]); |
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 142 | [_continue_alert setIcon:image]; |
143 | [_continue_alert layout]; | ||||
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 144 | |
145 | // Force alert to be at the proper level and location. | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 146 | NSWindow* continue_window = [_continue_alert window]; |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 147 | [continue_window center]; |
148 | [continue_window setLevel:NSModalPanelWindowLevel]; | ||||
149 | [continue_window orderWindow:NSWindowAbove | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 150 | relativeTo:[[_shades lastObject] windowNumber]]; |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 151 | [continue_window makeKeyWindow]; |
152 | } | ||||
153 | |||||
154 | - (void)hide { | ||||
155 | // Remove window shade. | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 156 | for (NSWindow* window in _shades.get()) { |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 157 | [window close]; |
158 | } | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 159 | _shades.reset(); |
160 | if (_continue_alert) { | ||||
161 | [[_continue_alert window] close]; | ||||
162 | _continue_alert.reset(); | ||||
Lambros Lambrou | 7eabfd9 | 2019-09-04 22:46:50 | [diff] [blame] | 163 | } |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 164 | } |
165 | |||||
166 | - (void)onCancel:(id)sender { | ||||
167 | [self hide]; | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 168 | _continue_window->DisconnectSession(); |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 169 | } |
170 | |||||
171 | - (void)onContinue:(id)sender { | ||||
172 | [self hide]; | ||||
Robert Liao | 61b8929 | 2019-12-13 18:15:14 | [diff] [blame] | 173 | _continue_window->ContinueSession(); |
[email protected] | 78b1ef6 | 2011-11-01 22:20:25 | [diff] [blame] | 174 | } |
175 | |||||
176 | @end |