forked from adobe/brackets-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_handler_mac.mm
More file actions
288 lines (238 loc) · 10.4 KB
/
Copy pathclient_handler_mac.mm
File metadata and controls
288 lines (238 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#import <Cocoa/Cocoa.h>
#include "client_handler.h"
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "cefclient.h"
#include "native_menu_model.h"
extern CefRefPtr<ClientHandler> g_handler;
// ClientHandler::ClientLifeSpanHandler implementation
void ClientHandler::OnAddressChange(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& url) {
REQUIRE_UI_THREAD();
if (m_BrowserId == browser->GetIdentifier() && frame->IsMain()) {
// Set the edit window text
NSTextField* textField = (NSTextField*)m_EditHwnd;
std::string urlStr(url);
NSString* str = [NSString stringWithUTF8String:urlStr.c_str()];
[textField setStringValue:str];
}
}
void ClientHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) {
REQUIRE_UI_THREAD();
// Set the frame window title bar
NSView* view = (NSView*)browser->GetHost()->GetWindowHandle();
NSWindow* window = [view window];
std::string titleStr(title);
NSString* str = [NSString stringWithUTF8String:titleStr.c_str()];
[window setTitle:str];
}
void ClientHandler::SendNotification(NotificationType type) {
SEL sel = nil;
switch(type) {
case NOTIFY_CONSOLE_MESSAGE:
sel = @selector(notifyConsoleMessage:);
break;
case NOTIFY_DOWNLOAD_COMPLETE:
sel = @selector(notifyDownloadComplete:);
break;
case NOTIFY_DOWNLOAD_ERROR:
sel = @selector(notifyDownloadError:);
break;
}
if (sel == nil)
return;
NSWindow* window = [AppGetMainHwnd() window];
NSObject* delegate = [window delegate];
[delegate performSelectorOnMainThread:sel withObject:nil waitUntilDone:NO];
}
void ClientHandler::SetLoading(bool isLoading) {
// TODO(port): Change button status.
}
void ClientHandler::SetNavState(bool canGoBack, bool canGoForward) {
// TODO(port): Change button status.
}
void ClientHandler::CloseMainWindow() {
// TODO(port): Close window
}
// Receives notifications from controls and the browser window. Will delete
// itself when done.
@interface PopupClientWindowDelegate : NSObject <NSWindowDelegate> {
CefRefPtr<ClientHandler> clientHandler;
NSWindow* window;
BOOL isReallyClosing;
}
- (IBAction)quit:(id)sender;
- (IBAction)handleMenuAction:(id)sender;
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem;
- (BOOL)windowShouldClose:(id)window;
- (void)setClientHandler:(CefRefPtr<ClientHandler>)handler;
- (void)setWindow:(NSWindow*)window;
@end
@implementation PopupClientWindowDelegate
- (id) init {
[super init];
isReallyClosing = false;
return self;
}
- (IBAction)quit:(id)sender {
/*
CefRefPtr<CefBrowser> browser;
// If the main browser exists, send the command to that browser
if (clientHandler->GetBrowserId())
browser = clientHandler->GetBrowser();
if (!browser)
browser = ClientHandler::GetBrowserForNativeWindow(window);
// TODO: we should have a "get frontmost brackets window" command for this
if (clientHandler && browser) {
clientHandler->SendJSCommand(browser, FILE_QUIT);
} else {
[NSApp terminate:nil];
}
*/
clientHandler->DispatchCloseToNextBrowser();
}
- (IBAction)handleMenuAction:(id)sender {
if (clientHandler.get() && clientHandler->GetBrowserId()) {
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow(window);
NSMenuItem* senderItem = sender;
NSUInteger tag = [senderItem tag];
ExtensionString commandId = NativeMenuModel::getInstance(getMenuParent(browser)).getCommandId(tag);
CefRefPtr<CommandCallback> callback = new EditCommandCallback(browser, commandId);
clientHandler->SendJSCommand(browser, commandId, callback);
}
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow(window);
NSInteger menuState = NSOffState;
NSUInteger tag = [menuItem tag];
NativeMenuModel menus = NativeMenuModel::getInstance(getMenuParent(browser));
if (menus.isMenuItemChecked(tag)) {
menuState = NSOnState;
}
[menuItem setState:menuState];
return menus.isMenuItemEnabled(tag);
}
- (void)setClientHandler:(CefRefPtr<ClientHandler>) handler {
clientHandler = handler;
}
- (void)setWindow:(NSWindow*)win {
window = win;
}
- (void)setIsReallyClosing {
isReallyClosing = true;
}
// Called when the window is about to close. Perform the self-destruction
// sequence by getting rid of the window. By returning YES, we allow the window
// to be removed from the screen.
- (BOOL)windowShouldClose:(id)aWindow {
// This function can be called as many as THREE times for each window.
// The first time will dispatch a FILE_CLOSE_WINDOW command and return NO.
// This gives the JavaScript the first crack at handling the command, which may result
// is a "Save Changes" dialog being shown.
// If the user chose to save changes (or ignore changes), the JavaScript calls window.close(),
// which results in a second call to this function. This time we dispatch another FILE_CLOSE_WINDOW
// command and return NO again.
// The second time the command is called it returns false. In that case, the CloseWindowCommandCallback
// will call browser->GetHost()->CloseBrowser(). However, before calling CloseBrowser(), the "isReallyClosing"
// flag is set. The CloseBrowser() call results in a THIRD call to this function, but this time we check
// the isReallyClosing flag and if true, return YES.
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow(aWindow);
if (!isReallyClosing && browser) {
CefRefPtr<CommandCallback> callback = new CloseWindowCommandCallback(browser);
clientHandler->SendJSCommand(browser, FILE_CLOSE_WINDOW, callback);
return NO;
}
// Clean ourselves up after clearing the stack of anything that might have the
// window on it.
[(NSObject*)[window delegate] performSelectorOnMainThread:@selector(cleanup:)
withObject:aWindow waitUntilDone:NO];
return YES;
}
// Deletes itself.
- (void)cleanup:(id)window {
[self release];
}
- (void)windowDidBecomeKey:(NSNotification*)notification {
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow([notification object]);
if(browser) {
// Give focus to the browser window.
browser->GetHost()->SetFocus(true);
}
}
- (void)windowDidResignKey:(NSNotification *)notification {
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow([notification object]);
if(browser) {
// Remove focus from the browser window.
browser->GetHost()->SetFocus(false);
}
[[notification object] makeFirstResponder:nil];
}
@end
void ClientHandler::PopupCreated(CefRefPtr<CefBrowser> browser) {
NSWindow* window = [browser->GetHost()->GetWindowHandle() window];
[window setCollectionBehavior: (1 << 7) /* NSWindowCollectionBehaviorFullScreenPrimary */];
// CEF3 is now using a window delegate with this revision http://code.google.com/p/chromiumembedded/source/detail?r=1149
// And the declaration of the window delegate (CefWindowDelegate class) is in libcef/browser/browser_host_impl_mac.mm and
// thus it is not available for us to extend it. So for now, we have to override it with our delegate class
// (PopupClientWindowDelegate) since we're not using its implementation of JavaScript 'onbeforeunload' handling yet.
// Besides, we need to keep using our window delegate class for native menus and commands to function correctly in all
// popup windows. Ideally, we should extend CefWindowDelegate class when its declaration is available and start using its
// windowShouldClose method instead of our own that is also handling similar events of JavaScript 'onbeforeunload' events.
if ([window delegate]) {
[[window delegate] release]; // Release the delegate created by CEF
[window setDelegate: nil];
}
if (![window delegate]) {
PopupClientWindowDelegate* delegate = [[PopupClientWindowDelegate alloc] init];
[delegate setClientHandler:this];
[delegate setWindow:window];
[window setDelegate:delegate];
}
}
bool ClientHandler::OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
const CefKeyEvent& event,
CefEventHandle os_event,
bool* is_keyboard_shortcut) {
//This is not fun. Here's the facts as I know them:
//+ OnPreKeyEvent is before the browser get the keydown event
//+ OnKeyEvent is after the browser has done it's default processing
//+ On win the native keyboard accelerators fire in this function
//+ On Mac if nothing else stops the event, the acceleators fire last
//+ In JS if eventPreventDefault() is called, the shortcut won't fire on mac
//+ On Win TranslateAccerator will return true even if the command is disabled
//+ On Mac performKeyEquivalent will return true only of the command is enabled
//+ On Mac if JS handles the command then the menu bar won't blink
//+ In this fuction, the browser passed won't return a V8 Context so you can't get any access there
//+ Communicating between the shell and JS is async, so there's no easy way for the JS to decided what to do
// in the middle of the key event, unless we introduce promises there, but that is a lot of work now
if([[NSApp mainMenu] performKeyEquivalent: os_event]) {
return true;
}
return false;
}
bool ClientHandler::OnKeyEvent(CefRefPtr<CefBrowser> browser,
const CefKeyEvent& event,
CefEventHandle os_event) {
return false;
}
CefRefPtr<CefBrowser> ClientHandler::GetBrowserForNativeWindow(void* window) {
CefRefPtr<CefBrowser> browser = NULL;
if (window) {
//go through all the browsers looking for a browser within this window
for (BrowserWindowMap::const_iterator i = browser_window_map_.begin() ; i != browser_window_map_.end() && browser == NULL ; i++ ) {
NSView* browserView = i->first;
if (browserView && [browserView window] == window) {
browser = i->second;
}
}
}
return browser;
}
bool ClientHandler::CanCloseBrowser(CefRefPtr<CefBrowser> browser) {
return true;
}