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

Commit c4d8fb7

Browse files
committed
Merge pull request #361 from adobe/rlim/handle-dropped-folder
Allow folder drop on top of Brackets app.
2 parents cbb8613 + af4de6c commit c4d8fb7

3 files changed

Lines changed: 62 additions & 10 deletions

File tree

appshell/cef_main_window.cpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,57 @@ void cef_main_window::RestoreWindowPlacement(int showCmd)
366366
BOOL cef_main_window::HandleCopyData(HWND, PCOPYDATASTRUCT lpCopyData)
367367
{
368368
if ((lpCopyData) && (lpCopyData->dwData == ID_WM_COPYDATA_SENDOPENFILECOMMAND) && (lpCopyData->cbData > 0)) {
369-
// another Brackets instance requests that we open the given filename
369+
// another Brackets instance requests that we open the given files/folders
370370
std::wstring wstrFilename = (LPCWSTR)lpCopyData->lpData;
371+
std::wstring wstrFileArray = L"[";
372+
bool hasMultipleFiles = false;
373+
374+
if (wstrFilename.find('"') != std::wstring::npos) {
375+
if (wstrFilename.find(L"\" ") != std::wstring::npos ||
376+
(wstrFilename.front() != '"' || wstrFilename.back() != '"')) {
377+
hasMultipleFiles = true;
378+
}
379+
} else {
380+
hasMultipleFiles = (wstrFilename.find(L" ") != std::wstring::npos);
381+
}
371382

372-
// Windows Explorer might enclose the filename in double-quotes. We need to strip these off.
373-
if ((wstrFilename.front() == '\"') && wstrFilename.back() == '\"')
374-
wstrFilename = wstrFilename.substr(1, wstrFilename.length() - 2);
383+
if (hasMultipleFiles) {
384+
std::size_t curFilePathEnd1 = wstrFilename.find(L" ");
385+
std::size_t curFilePathEnd2 = wstrFilename.find(L"\" ");
386+
std::size_t nextQuoteIndex = wstrFilename.find(L"\"");
387+
388+
while ((nextQuoteIndex == 0 && curFilePathEnd2 != std::wstring::npos) ||
389+
(nextQuoteIndex != 0 && curFilePathEnd1 != std::wstring::npos)) {
390+
391+
if (nextQuoteIndex == 0 && curFilePathEnd2 != std::wstring::npos) {
392+
// Appending a file path that is already wrapped in double-quotes.
393+
wstrFileArray += (wstrFilename.substr(0, curFilePathEnd2 + 1) + L",");
394+
395+
// Strip the current file path and move index to next file path.
396+
wstrFilename = wstrFilename.substr(curFilePathEnd2 + 2);
397+
} else {
398+
// Explicitly wrap a file path in double-quotes and append it to the file array.
399+
wstrFileArray += (L"\"" + wstrFilename.substr(0, curFilePathEnd1) + L"\",");
400+
401+
// Strip the current file path and move index to next file path.
402+
wstrFilename = wstrFilename.substr(curFilePathEnd1 + 1);
403+
}
404+
405+
curFilePathEnd1 = wstrFilename.find(L" ");
406+
curFilePathEnd2 = wstrFilename.find(L"\" ");
407+
nextQuoteIndex = wstrFilename.find(L"\"");
408+
}
409+
}
410+
411+
// Add the last file or the only file into the file array.
412+
if (wstrFilename.front() == '"' && wstrFilename.back() == '"') {
413+
wstrFileArray += wstrFilename;
414+
} else if (wstrFilename.length()) {
415+
wstrFileArray += (L"\"" + wstrFilename + L"\"");
416+
}
417+
wstrFileArray += L"]";
375418

376-
g_handler->SendOpenFileCommand(g_handler->GetBrowser(), CefString(wstrFilename.c_str()));
419+
g_handler->SendOpenFileCommand(g_handler->GetBrowser(), CefString(wstrFileArray.c_str()));
377420
return TRUE;
378421
}
379422

appshell/cefclient_mac.mm

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,17 @@ - (BOOL)application:(NSApplication *)theApplication openFiles:(NSArray *)filenam
647647
NSWindow* targetWindow = [clientApp findTargetWindow];
648648
if (targetWindow) {
649649
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow(targetWindow);
650-
for (NSUInteger i = 0; i < [filenames count]; i++) {
651-
g_handler->SendOpenFileCommand(browser, CefString([[filenames objectAtIndex:i] UTF8String]));
650+
NSUInteger count = [filenames count];
651+
if (count) {
652+
std::string files = "[";
653+
for (NSUInteger i = 0; i < count; i++) {
654+
if (i > 0) {
655+
files += ", ";
656+
}
657+
files += ("\"" + std::string([[filenames objectAtIndex:i] UTF8String]) + "\"");
658+
}
659+
files += "]";
660+
g_handler->SendOpenFileCommand(browser, CefString(files));
652661
}
653662
}
654663
} else {

appshell/client_handler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,10 @@ bool ClientHandler::SendJSCommand(CefRefPtr<CefBrowser> browser, const CefString
349349
return browser->SendProcessMessage(PID_RENDERER, message);
350350
}
351351

352-
void ClientHandler::SendOpenFileCommand(CefRefPtr<CefBrowser> browser, const CefString &filename) {
353-
std::string filenameStr(filename);
352+
void ClientHandler::SendOpenFileCommand(CefRefPtr<CefBrowser> browser, const CefString &fileArray) {
353+
std::string fileArrayStr(fileArray);
354354
// FIXME: Use SendJSCommand once it supports parameters
355-
std::string cmd = "require('command/CommandManager').execute('file.addToWorkingSet',{fullPath:'" + filenameStr + "'})";
355+
std::string cmd = "require('command/CommandManager').execute('file.openDroppedFiles'," + fileArrayStr + ")";
356356
browser->GetMainFrame()->ExecuteJavaScript(CefString(cmd.c_str()),
357357
browser->GetMainFrame()->GetURL(), 0);
358358
}

0 commit comments

Comments
 (0)