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 #361 from adobe/rlim/handle-dropped-folder
Browse files Browse the repository at this point in the history
Allow folder drop on top of Brackets app.
  • Loading branch information
bchintx committed Oct 22, 2013
2 parents dfebf5e + d0a3036 commit 91310c3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
53 changes: 48 additions & 5 deletions appshell/cef_main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,57 @@ void cef_main_window::RestoreWindowPlacement(int showCmd)
BOOL cef_main_window::HandleCopyData(HWND, PCOPYDATASTRUCT lpCopyData)
{
if ((lpCopyData) && (lpCopyData->dwData == ID_WM_COPYDATA_SENDOPENFILECOMMAND) && (lpCopyData->cbData > 0)) {
// another Brackets instance requests that we open the given filename
// another Brackets instance requests that we open the given files/folders
std::wstring wstrFilename = (LPCWSTR)lpCopyData->lpData;
std::wstring wstrFileArray = L"[";
bool hasMultipleFiles = false;

if (wstrFilename.find('"') != std::wstring::npos) {
if (wstrFilename.find(L"\" ") != std::wstring::npos ||
(wstrFilename.front() != '"' || wstrFilename.back() != '"')) {
hasMultipleFiles = true;
}
} else {
hasMultipleFiles = (wstrFilename.find(L" ") != std::wstring::npos);
}

// Windows Explorer might enclose the filename in double-quotes. We need to strip these off.
if ((wstrFilename.front() == '\"') && wstrFilename.back() == '\"')
wstrFilename = wstrFilename.substr(1, wstrFilename.length() - 2);
if (hasMultipleFiles) {
std::size_t curFilePathEnd1 = wstrFilename.find(L" ");
std::size_t curFilePathEnd2 = wstrFilename.find(L"\" ");
std::size_t nextQuoteIndex = wstrFilename.find(L"\"");

while ((nextQuoteIndex == 0 && curFilePathEnd2 != std::wstring::npos) ||
(nextQuoteIndex != 0 && curFilePathEnd1 != std::wstring::npos)) {

if (nextQuoteIndex == 0 && curFilePathEnd2 != std::wstring::npos) {
// Appending a file path that is already wrapped in double-quotes.
wstrFileArray += (wstrFilename.substr(0, curFilePathEnd2 + 1) + L",");

// Strip the current file path and move index to next file path.
wstrFilename = wstrFilename.substr(curFilePathEnd2 + 2);
} else {
// Explicitly wrap a file path in double-quotes and append it to the file array.
wstrFileArray += (L"\"" + wstrFilename.substr(0, curFilePathEnd1) + L"\",");

// Strip the current file path and move index to next file path.
wstrFilename = wstrFilename.substr(curFilePathEnd1 + 1);
}

curFilePathEnd1 = wstrFilename.find(L" ");
curFilePathEnd2 = wstrFilename.find(L"\" ");
nextQuoteIndex = wstrFilename.find(L"\"");
}
}

// Add the last file or the only file into the file array.
if (wstrFilename.front() == '"' && wstrFilename.back() == '"') {
wstrFileArray += wstrFilename;
} else if (wstrFilename.length()) {
wstrFileArray += (L"\"" + wstrFilename + L"\"");
}
wstrFileArray += L"]";

g_handler->SendOpenFileCommand(g_handler->GetBrowser(), CefString(wstrFilename.c_str()));
g_handler->SendOpenFileCommand(g_handler->GetBrowser(), CefString(wstrFileArray.c_str()));
return TRUE;
}

Expand Down
13 changes: 11 additions & 2 deletions appshell/cefclient_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,17 @@ - (BOOL)application:(NSApplication *)theApplication openFiles:(NSArray *)filenam
NSWindow* targetWindow = [clientApp findTargetWindow];
if (targetWindow) {
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow(targetWindow);
for (NSUInteger i = 0; i < [filenames count]; i++) {
g_handler->SendOpenFileCommand(browser, CefString([[filenames objectAtIndex:i] UTF8String]));
NSUInteger count = [filenames count];
if (count) {
std::string files = "[";
for (NSUInteger i = 0; i < count; i++) {
if (i > 0) {
files += ", ";
}
files += ("\"" + std::string([[filenames objectAtIndex:i] UTF8String]) + "\"");
}
files += "]";
g_handler->SendOpenFileCommand(browser, CefString(files));
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions appshell/client_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,10 @@ bool ClientHandler::SendJSCommand(CefRefPtr<CefBrowser> browser, const CefString
return browser->SendProcessMessage(PID_RENDERER, message);
}

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

0 comments on commit 91310c3

Please sign in to comment.