Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit edc1b34

Browse files
committed
Merge pull request #553 from adobe/prashant/mac-file-dlg-fix
[MAC] Fix for open file dialog being jerky
2 parents 0894803 + d84f9e4 commit edc1b34

File tree

4 files changed

+111
-35
lines changed

4 files changed

+111
-35
lines changed

appshell/appshell_extensions.cpp

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
117117
argList->GetType(5) != VTYPE_STRING) {
118118
error = ERR_INVALID_PARAMS;
119119
}
120-
121-
CefRefPtr<CefListValue> selectedFiles = CefListValue::Create();
122120

123121
if (error == NO_ERROR) {
124122
bool allowMultipleSelection = argList->GetBool(1);
@@ -127,16 +125,33 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
127125
ExtensionString initialPath = argList->GetString(4);
128126
ExtensionString fileTypes = argList->GetString(5);
129127

128+
#ifdef OS_MACOSX
129+
ShowOpenDialog(allowMultipleSelection,
130+
chooseDirectory,
131+
title,
132+
initialPath,
133+
fileTypes,
134+
browser,
135+
response);
136+
137+
// Skip standard callback handling. ShowOpenDialog fires the
138+
// callback asynchronously.
139+
140+
return true;
141+
#else
142+
CefRefPtr<CefListValue> selectedFiles = CefListValue::Create();
130143
error = ShowOpenDialog(allowMultipleSelection,
131144
chooseDirectory,
132145
title,
133146
initialPath,
134147
fileTypes,
135148
selectedFiles);
149+
// Set response args for this function
150+
responseArgs->SetList(2, selectedFiles);
151+
#endif
152+
136153
}
137-
138-
// Set response args for this function
139-
responseArgs->SetList(2, selectedFiles);
154+
140155
} else if (message_name == "ShowSaveDialog") {
141156
// Parameters:
142157
// 0: int32 - callback id
@@ -150,21 +165,32 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
150165
error = ERR_INVALID_PARAMS;
151166
}
152167

153-
ExtensionString newFilePath;
154-
155168
if (error == NO_ERROR) {
156169
ExtensionString title = argList->GetString(1);
157170
ExtensionString initialPath = argList->GetString(2);
158171
ExtensionString proposedNewFilename = argList->GetString(3);
159-
172+
173+
#ifdef OS_MACOSX
174+
// Skip standard callback handling. ShowSaveDialog fires the
175+
// callback asynchronously.
176+
ShowSaveDialog(title,
177+
initialPath,
178+
proposedNewFilename,
179+
browser,
180+
response);
181+
return true;
182+
#else
183+
ExtensionString newFilePath;
160184
error = ShowSaveDialog(title,
161-
initialPath,
162-
proposedNewFilename,
163-
newFilePath);
185+
initialPath,
186+
proposedNewFilename,
187+
newFilePath);
188+
189+
// Set response args for this function
190+
responseArgs->SetString(2, newFilePath);
191+
#endif
164192
}
165193

166-
// Set response args for this function
167-
responseArgs->SetString(2, newFilePath);
168194
} else if (message_name == "IsNetworkDrive") {
169195
// Parameters:
170196
// 0: int32 - callback id

appshell/appshell_extensions_mac.mm

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,13 @@ int32 OpenURLInDefaultBrowser(ExtensionString url)
375375
return NO_ERROR;
376376
}
377377

378-
int32 ShowOpenDialog(bool allowMulitpleSelection,
378+
void ShowOpenDialog(bool allowMulitpleSelection,
379379
bool chooseDirectory,
380380
ExtensionString title,
381381
ExtensionString initialDirectory,
382382
ExtensionString fileTypes,
383-
CefRefPtr<CefListValue>& selectedFiles)
383+
CefRefPtr<CefBrowser> browser,
384+
CefRefPtr<CefProcessMessage> response)
384385
{
385386
NSArray* allowedFileTypes = nil;
386387
BOOL canChooseDirectories = chooseDirectory;
@@ -408,23 +409,36 @@ int32 ShowOpenDialog(bool allowMulitpleSelection,
408409

409410
[openPanel setAllowedFileTypes:allowedFileTypes];
410411

411-
[openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:nil];
412-
if ([openPanel runModal] == NSOKButton)
412+
// cache the browser and response variables, so that these
413+
// can be accessed from within the completionHandler block.
414+
CefRefPtr<CefBrowser> _browser = browser;
415+
CefRefPtr<CefProcessMessage> _response = response;
416+
417+
[openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler: ^(NSInteger returnCode)
413418
{
414-
NSArray* urls = [openPanel URLs];
415-
for (NSUInteger i = 0; i < [urls count]; i++) {
416-
selectedFiles->SetString(i, [[[urls objectAtIndex:i] path] UTF8String]);
419+
if(_browser && _response){
420+
421+
NSArray *urls = [openPanel URLs];
422+
CefRefPtr<CefListValue> selectedFiles = CefListValue::Create();
423+
if (returnCode == NSModalResponseOK){
424+
for (NSUInteger i = 0; i < [urls count]; i++) {
425+
selectedFiles->SetString(i, [[[urls objectAtIndex:i] path] UTF8String]);
426+
}
427+
}
428+
429+
// Set common response args (error and selectedfiles list)
430+
_response->GetArgumentList()->SetInt(1, NO_ERROR);
431+
_response->GetArgumentList()->SetList(2, selectedFiles);
432+
_browser->SendProcessMessage(PID_RENDERER, _response);
417433
}
418-
}
419-
[NSApp endSheet:openPanel];
420-
421-
return NO_ERROR;
434+
}];
422435
}
423436

424-
int32 ShowSaveDialog(ExtensionString title,
437+
void ShowSaveDialog(ExtensionString title,
425438
ExtensionString initialDirectory,
426439
ExtensionString proposedNewFilename,
427-
ExtensionString& absoluteFilepath)
440+
CefRefPtr<CefBrowser> browser,
441+
CefRefPtr<CefProcessMessage> response)
428442
{
429443
NSSavePanel* savePanel = [NSSavePanel savePanel];
430444
[savePanel setTitle: [NSString stringWithUTF8String:title.c_str()]];
@@ -436,17 +450,29 @@ int32 ShowSaveDialog(ExtensionString title,
436450
}
437451

438452
[savePanel setNameFieldStringValue:[NSString stringWithUTF8String:proposedNewFilename.c_str()]];
439-
[savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:nil];
440-
441-
if ([savePanel runModal] == NSFileHandlingPanelOKButton)
442-
{
443-
NSURL* theFile = [savePanel URL];
444-
absoluteFilepath = [[theFile path] UTF8String];
445453

446-
}
447-
[NSApp endSheet:savePanel];
454+
// cache the browser and response variables, so that these
455+
// can be accessed from within the completionHandler block.
456+
CefRefPtr<CefBrowser> _browser = browser;
457+
CefRefPtr<CefProcessMessage> _response = response;
448458

449-
return NO_ERROR;
459+
[savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler: ^(NSInteger returnCode)
460+
{
461+
if(_response && _browser){
462+
463+
CefString pathStr;
464+
if (returnCode == NSModalResponseOK){
465+
NSURL* selectedFile = [savePanel URL];
466+
if(selectedFile)
467+
pathStr = [[selectedFile path] UTF8String];
468+
}
469+
470+
// Set common response args (error and the new file name string)
471+
_response->GetArgumentList()->SetInt(1, NO_ERROR);
472+
_response->GetArgumentList()->SetString(2, pathStr);
473+
_browser->SendProcessMessage(PID_RENDERER, _response);
474+
}
475+
}];
450476
}
451477

452478
int32 IsNetworkDrive(ExtensionString path, bool& isRemote)

appshell/appshell_extensions_platform.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void CloseLiveBrowser(CefRefPtr<CefBrowser> browser, CefRefPtr<CefProcessMessage
8686

8787
int32 OpenURLInDefaultBrowser(ExtensionString url);
8888

89+
#ifndef OS_MACOSX
8990
int32 ShowOpenDialog(bool allowMulitpleSelection,
9091
bool chooseDirectory,
9192
ExtensionString title,
@@ -97,6 +98,21 @@ int32 ShowSaveDialog(ExtensionString title,
9798
ExtensionString initialDirectory,
9899
ExtensionString proposedNewFilename,
99100
ExtensionString& newFilePath);
101+
#else
102+
void ShowOpenDialog(bool allowMulitpleSelection,
103+
bool chooseDirectory,
104+
ExtensionString title,
105+
ExtensionString initialDirectory,
106+
ExtensionString fileTypes,
107+
CefRefPtr<CefBrowser> browser,
108+
CefRefPtr<CefProcessMessage> response);
109+
110+
void ShowSaveDialog(ExtensionString title,
111+
ExtensionString initialDirectory,
112+
ExtensionString proposedNewFilename,
113+
CefRefPtr<CefBrowser> browser,
114+
CefRefPtr<CefProcessMessage> response);
115+
#endif
100116

101117
int32 IsNetworkDrive(ExtensionString path, bool& isRemote);
102118

appshell/client_handler_mac.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
if ([window styleMask] & NSFullScreenWindowMask) {
6262
[window setTitle:str];
6363
}
64+
// This is required as we are now managing the window's title
65+
// on our own, using CustomTitlbeBarView. As we are nuking the
66+
// window's title, currently open windows like new brackets
67+
// windows/dev tools are not getting listed under the
68+
// 'Window' menu. So we are now explicitly telling OS to make
69+
// sure we have a menu entry under 'Window' menu. This is a
70+
// safe call and is officially supported.
71+
[NSApp changeWindowsItem:window title:str filename:NO];
6472

6573
[delegate performSelectorOnMainThread:@selector(windowTitleDidChange:) withObject:str waitUntilDone:NO];
6674
}

0 commit comments

Comments
 (0)