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

Commit

Permalink
Merge pull request #553 from adobe/prashant/mac-file-dlg-fix
Browse files Browse the repository at this point in the history
[MAC] Fix for open file dialog being jerky
  • Loading branch information
swmitra committed May 18, 2016
2 parents 0894803 + d84f9e4 commit edc1b34
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 35 deletions.
52 changes: 39 additions & 13 deletions appshell/appshell_extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
argList->GetType(5) != VTYPE_STRING) {
error = ERR_INVALID_PARAMS;
}

CefRefPtr<CefListValue> selectedFiles = CefListValue::Create();

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

#ifdef OS_MACOSX
ShowOpenDialog(allowMultipleSelection,
chooseDirectory,
title,
initialPath,
fileTypes,
browser,
response);

// Skip standard callback handling. ShowOpenDialog fires the
// callback asynchronously.

return true;
#else
CefRefPtr<CefListValue> selectedFiles = CefListValue::Create();
error = ShowOpenDialog(allowMultipleSelection,
chooseDirectory,
title,
initialPath,
fileTypes,
selectedFiles);
// Set response args for this function
responseArgs->SetList(2, selectedFiles);
#endif

}

// Set response args for this function
responseArgs->SetList(2, selectedFiles);

} else if (message_name == "ShowSaveDialog") {
// Parameters:
// 0: int32 - callback id
Expand All @@ -150,21 +165,32 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
error = ERR_INVALID_PARAMS;
}

ExtensionString newFilePath;

if (error == NO_ERROR) {
ExtensionString title = argList->GetString(1);
ExtensionString initialPath = argList->GetString(2);
ExtensionString proposedNewFilename = argList->GetString(3);


#ifdef OS_MACOSX
// Skip standard callback handling. ShowSaveDialog fires the
// callback asynchronously.
ShowSaveDialog(title,
initialPath,
proposedNewFilename,
browser,
response);
return true;
#else
ExtensionString newFilePath;
error = ShowSaveDialog(title,
initialPath,
proposedNewFilename,
newFilePath);
initialPath,
proposedNewFilename,
newFilePath);

// Set response args for this function
responseArgs->SetString(2, newFilePath);
#endif
}

// Set response args for this function
responseArgs->SetString(2, newFilePath);
} else if (message_name == "IsNetworkDrive") {
// Parameters:
// 0: int32 - callback id
Expand Down
70 changes: 48 additions & 22 deletions appshell/appshell_extensions_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,13 @@ int32 OpenURLInDefaultBrowser(ExtensionString url)
return NO_ERROR;
}

int32 ShowOpenDialog(bool allowMulitpleSelection,
void ShowOpenDialog(bool allowMulitpleSelection,
bool chooseDirectory,
ExtensionString title,
ExtensionString initialDirectory,
ExtensionString fileTypes,
CefRefPtr<CefListValue>& selectedFiles)
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefProcessMessage> response)
{
NSArray* allowedFileTypes = nil;
BOOL canChooseDirectories = chooseDirectory;
Expand Down Expand Up @@ -408,23 +409,36 @@ int32 ShowOpenDialog(bool allowMulitpleSelection,

[openPanel setAllowedFileTypes:allowedFileTypes];

[openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:nil];
if ([openPanel runModal] == NSOKButton)
// cache the browser and response variables, so that these
// can be accessed from within the completionHandler block.
CefRefPtr<CefBrowser> _browser = browser;
CefRefPtr<CefProcessMessage> _response = response;

[openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler: ^(NSInteger returnCode)
{
NSArray* urls = [openPanel URLs];
for (NSUInteger i = 0; i < [urls count]; i++) {
selectedFiles->SetString(i, [[[urls objectAtIndex:i] path] UTF8String]);
if(_browser && _response){

NSArray *urls = [openPanel URLs];
CefRefPtr<CefListValue> selectedFiles = CefListValue::Create();
if (returnCode == NSModalResponseOK){
for (NSUInteger i = 0; i < [urls count]; i++) {
selectedFiles->SetString(i, [[[urls objectAtIndex:i] path] UTF8String]);
}
}

// Set common response args (error and selectedfiles list)
_response->GetArgumentList()->SetInt(1, NO_ERROR);
_response->GetArgumentList()->SetList(2, selectedFiles);
_browser->SendProcessMessage(PID_RENDERER, _response);
}
}
[NSApp endSheet:openPanel];

return NO_ERROR;
}];
}

int32 ShowSaveDialog(ExtensionString title,
void ShowSaveDialog(ExtensionString title,
ExtensionString initialDirectory,
ExtensionString proposedNewFilename,
ExtensionString& absoluteFilepath)
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefProcessMessage> response)
{
NSSavePanel* savePanel = [NSSavePanel savePanel];
[savePanel setTitle: [NSString stringWithUTF8String:title.c_str()]];
Expand All @@ -436,17 +450,29 @@ int32 ShowSaveDialog(ExtensionString title,
}

[savePanel setNameFieldStringValue:[NSString stringWithUTF8String:proposedNewFilename.c_str()]];
[savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:nil];

if ([savePanel runModal] == NSFileHandlingPanelOKButton)
{
NSURL* theFile = [savePanel URL];
absoluteFilepath = [[theFile path] UTF8String];

}
[NSApp endSheet:savePanel];
// cache the browser and response variables, so that these
// can be accessed from within the completionHandler block.
CefRefPtr<CefBrowser> _browser = browser;
CefRefPtr<CefProcessMessage> _response = response;

return NO_ERROR;
[savePanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler: ^(NSInteger returnCode)
{
if(_response && _browser){

CefString pathStr;
if (returnCode == NSModalResponseOK){
NSURL* selectedFile = [savePanel URL];
if(selectedFile)
pathStr = [[selectedFile path] UTF8String];
}

// Set common response args (error and the new file name string)
_response->GetArgumentList()->SetInt(1, NO_ERROR);
_response->GetArgumentList()->SetString(2, pathStr);
_browser->SendProcessMessage(PID_RENDERER, _response);
}
}];
}

int32 IsNetworkDrive(ExtensionString path, bool& isRemote)
Expand Down
16 changes: 16 additions & 0 deletions appshell/appshell_extensions_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void CloseLiveBrowser(CefRefPtr<CefBrowser> browser, CefRefPtr<CefProcessMessage

int32 OpenURLInDefaultBrowser(ExtensionString url);

#ifndef OS_MACOSX
int32 ShowOpenDialog(bool allowMulitpleSelection,
bool chooseDirectory,
ExtensionString title,
Expand All @@ -97,6 +98,21 @@ int32 ShowSaveDialog(ExtensionString title,
ExtensionString initialDirectory,
ExtensionString proposedNewFilename,
ExtensionString& newFilePath);
#else
void ShowOpenDialog(bool allowMulitpleSelection,
bool chooseDirectory,
ExtensionString title,
ExtensionString initialDirectory,
ExtensionString fileTypes,
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefProcessMessage> response);

void ShowSaveDialog(ExtensionString title,
ExtensionString initialDirectory,
ExtensionString proposedNewFilename,
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefProcessMessage> response);
#endif

int32 IsNetworkDrive(ExtensionString path, bool& isRemote);

Expand Down
8 changes: 8 additions & 0 deletions appshell/client_handler_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
if ([window styleMask] & NSFullScreenWindowMask) {
[window setTitle:str];
}
// This is required as we are now managing the window's title
// on our own, using CustomTitlbeBarView. As we are nuking the
// window's title, currently open windows like new brackets
// windows/dev tools are not getting listed under the
// 'Window' menu. So we are now explicitly telling OS to make
// sure we have a menu entry under 'Window' menu. This is a
// safe call and is officially supported.
[NSApp changeWindowsItem:window title:str filename:NO];

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

0 comments on commit edc1b34

Please sign in to comment.