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

Commit b99e9c6

Browse files
committed
Merge pull request #272 from adobe/glenn/drag-and-drop
Add brackets.app.getDroppedFiles() function
2 parents bce3514 + f45699e commit b99e9c6

6 files changed

Lines changed: 75 additions & 31 deletions

File tree

appshell/appshell_extensions.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#include "native_menu_model.h"
2727
#include "appshell_node_process.h"
2828

29+
#include <algorithm>
30+
31+
extern std::vector<CefString> gDroppedFiles;
32+
2933
namespace appshell_extensions {
3034

3135
class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
@@ -429,6 +433,34 @@ class ProcessMessageDelegate : public ClientHandler::ProcessMessageDelegate {
429433
error = GetPendingFilesToOpen(files);
430434
responseArgs->SetString(2, files.c_str());
431435
}
436+
} else if (message_name == "GetDroppedFiles") {
437+
// Parameters:
438+
// 0: int32 - callback id
439+
if (argList->GetSize() != 1) {
440+
error = ERR_INVALID_PARAMS;
441+
}
442+
443+
if (error == NO_ERROR) {
444+
std::wstring files;
445+
446+
files = L"[";
447+
for (unsigned int i = 0; i < gDroppedFiles.size(); i++) {
448+
std::wstring file(gDroppedFiles[i]);
449+
// Convert windows paths to unix paths
450+
replace(file.begin(), file.end(), '\\', '/');
451+
files += L"\"";
452+
files += file;
453+
files += L"\"";
454+
if (i < gDroppedFiles.size() - 1) {
455+
files += L", ";
456+
}
457+
}
458+
files += L"]";
459+
gDroppedFiles.clear();
460+
461+
responseArgs->SetString(2, files.c_str());
462+
}
463+
432464
} else if (message_name == "AddMenu") {
433465
// Parameters:
434466
// 0: int32 - callback id

appshell/appshell_extensions.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,27 @@ if (!appshell.app) {
494494
appshell.app.getPendingFilesToOpen = function (callback) {
495495
GetPendingFilesToOpen(function (err, files) {
496496
// "files" is a string, convert to Array
497-
callback(err, err ? [] : (files ? JSON.parse(files) : []));
497+
callback(err, (err || !files) ? [] : JSON.parse(files));
498498
});
499499
};
500500

501+
/**
502+
* Get files and folders dropped onto the application.
503+
*
504+
* @param {function(err, files)} callback Asynchronous callback function with two arguments:
505+
* err - error code
506+
* files - Array of file paths
507+
*
508+
* @return None. This is an asynchronous call that sends all return information to the callback.
509+
*/
510+
native function GetDroppedFiles();
511+
appshell.app.getDroppedFiles = function (callback) {
512+
GetDroppedFiles(function (err, files) {
513+
// "files" is a string, convert to Array
514+
callback(err, (err || !files) ? [] : JSON.parse(files));
515+
});
516+
};
517+
501518
/**
502519
* Get the remote debugging port used by the appshell.
503520
*

appshell/cefclient_win.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -585,25 +585,12 @@ BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) {
585585
if (!hWndMain)
586586
return FALSE;
587587

588-
DragAcceptFiles(hWndMain, TRUE);
589588
RestoreWindowPlacement(hWndMain, showCmd);
590589
UpdateWindow(hWndMain);
591590

592591
return TRUE;
593592
}
594593

595-
LRESULT HandleDropFiles(HDROP hDrop, CefRefPtr<ClientHandler> handler, CefRefPtr<CefBrowser> browser) {
596-
UINT fileCount = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
597-
for (UINT i = 0; i < fileCount; i++) {
598-
wchar_t filename[MAX_PATH];
599-
DragQueryFile(hDrop, i, filename, MAX_PATH);
600-
std::wstring pathStr(filename);
601-
replace(pathStr.begin(), pathStr.end(), '\\', '/');
602-
handler->SendOpenFileCommand(browser, CefString(pathStr));
603-
}
604-
return 0;
605-
}
606-
607594
//
608595
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
609596
//
@@ -880,12 +867,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
880867
PostQuitMessage(0);
881868
return 0;
882869

883-
case WM_DROPFILES:
884-
if (g_handler.get()) {
885-
return HandleDropFiles((HDROP)wParam, g_handler, g_handler->GetBrowser());
886-
}
887-
return 0;
888-
889870
case WM_INITMENUPOPUP:
890871
// Notify before popping up
891872
g_handler->SendJSCommand(g_handler->GetBrowser(), APP_BEFORE_MENUPOPUP);

appshell/client_handler.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
120120
}
121121
}
122122

123+
std::vector<CefString> gDroppedFiles;
124+
125+
bool ClientHandler::OnDragEnter(CefRefPtr<CefBrowser> browser,
126+
CefRefPtr<CefDragData> dragData,
127+
DragOperationsMask mask) {
128+
REQUIRE_UI_THREAD();
129+
130+
if (dragData->IsFile()) {
131+
gDroppedFiles.clear();
132+
// Store the dragged files in a vector for later use
133+
dragData->GetFileNames(gDroppedFiles);
134+
}
135+
return false;
136+
}
137+
123138
void ClientHandler::OnLoadStart(CefRefPtr<CefBrowser> browser,
124139
CefRefPtr<CefFrame> frame) {
125140
REQUIRE_UI_THREAD();

appshell/client_handler.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@
2020
// ClientHandler implementation.
2121
class ClientHandler : public CefClient,
2222
public CefLifeSpanHandler,
23+
public CefDragHandler,
2324
public CefLoadHandler,
2425
public CefRequestHandler,
2526
public CefDisplayHandler,
2627
public CefKeyboardHandler,
2728
public CefGeolocationHandler,
2829
public CefContextMenuHandler {
29-
public:
30+
public:
3031
// Interface for process message delegates. Do not perform work in the
3132
// RenderDelegate constructor.
3233
class ProcessMessageDelegate : public virtual CefBase {
@@ -70,6 +71,9 @@ class ClientHandler : public CefClient,
7071
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE {
7172
return this;
7273
}
74+
virtual CefRefPtr<CefDragHandler> GetDragHandler() OVERRIDE {
75+
return this;
76+
}
7377
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE {
7478
return this;
7579
}
@@ -99,6 +103,11 @@ class ClientHandler : public CefClient,
99103
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) OVERRIDE;
100104
virtual bool DoClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
101105
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) OVERRIDE;
106+
107+
// CefDragHandler methods
108+
virtual bool OnDragEnter(CefRefPtr<CefBrowser> browser,
109+
CefRefPtr<CefDragData> dragData,
110+
DragOperationsMask mask) OVERRIDE;
102111

103112
// CefLoadHandler methods
104113
virtual void OnLoadStart(CefRefPtr<CefBrowser> browser,

appshell/client_handler_win.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@
1616

1717
extern CefRefPtr<ClientHandler> g_handler;
1818

19-
// WM_DROPFILES handler, defined in cefclient_win.cpp
20-
extern LRESULT HandleDropFiles(HDROP hDrop, CefRefPtr<ClientHandler> handler, CefRefPtr<CefBrowser> browser);
21-
2219
// Additional globals
2320
extern HACCEL hAccelTable;
2421

25-
2622
void ClientHandler::OnAddressChange(CefRefPtr<CefBrowser> browser,
2723
CefRefPtr<CefFrame> frame,
2824
const CefString& url) {
@@ -147,12 +143,6 @@ LRESULT CALLBACK PopupWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPa
147143
}
148144
break;
149145

150-
case WM_DROPFILES:
151-
if (g_handler.get() && browser.get()) {
152-
return HandleDropFiles((HDROP)wParam, g_handler, browser);
153-
}
154-
break;
155-
156146
case WM_INITMENUPOPUP:
157147
HMENU menu = (HMENU)wParam;
158148
int count = GetMenuItemCount(menu);

0 commit comments

Comments
 (0)