Skip to content

Commit fdb5692

Browse files
committed
feat: drop file/files/folder ux
1 parent 5881194 commit fdb5692

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/drop-files.html

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>Project Name</title>
77
<script>
8-
let windowLabelOfListener;
8+
let windowLabelOfListener, dropMessage, dropProjectMessage, dropMessageOneFile;
99
window.__TAURI__.event.listen('tauri://file-drop', (event) => {
1010
__TAURI__.window.appWindow.hide();
1111
if(!event || !event.payload || !event.payload.length || !windowLabelOfListener){
@@ -16,9 +16,32 @@
1616
pathList: event.payload
1717
});
1818
});
19+
window.__TAURI__.event.listen('tauri://file-drop-cancelled', (event) => {
20+
__TAURI__.window.appWindow.hide();
21+
});
22+
window.__TAURI__.event.listen('tauri://file-drop-hover', (event) => {
23+
if(!event || !event.payload || !dropProjectMessage || !dropMessage){
24+
return;
25+
}
26+
if(event.payload.length === 1) {
27+
window.__TAURI__.fs.readDir(event.payload[0])
28+
.then(async ()=>{
29+
// if a single folder is present, we treat it as drop project
30+
document.getElementById("dropMessage").innerText = dropProjectMessage
31+
.replace("{0}", await window.__TAURI__.path.basename(event.payload[0]));
32+
}).catch(()=>{
33+
document.getElementById("dropMessage").innerText = dropMessageOneFile;
34+
})
35+
} else {
36+
document.getElementById("dropMessage").innerText = dropMessage;
37+
}
38+
});
1939
window.__TAURI__.event.listen("drop-attach-on-window", ({payload})=> {
2040
document.getElementById("projectName").innerText = payload.projectName;
21-
document.getElementById("dropMessage").innerText = payload.dropMessage;
41+
// dropMessage will be set on drag hover events depending on file/files/project
42+
dropMessage = payload.dropMessage;
43+
dropProjectMessage = payload.dropProjectMessage;
44+
dropMessageOneFile = payload.dropMessageOneFile;
2245
windowLabelOfListener = payload.windowLabelOfListener;
2346
});
2447
window.addEventListener('mouseout', function(event) {

src/nls/root/strings.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ define({
9797
"ERROR_CREATING_FILE_TITLE": "Error Creating {0}",
9898
"ERROR_CREATING_FILE": "An error occurred when trying to create the {0} <span class='dialog-filename'>{1}</span>. {2}",
9999
"ERROR_MIXED_DRAGDROP": "Cannot open a folder at the same time as opening other files.",
100+
"DROP_TO_OPEN_FILES": "Drop to open files",
101+
"DROP_TO_OPEN_FILE": "Drop to open file",
102+
"DROP_TO_OPEN_PROJECT": "Drop to open folder `{0}` as project",
100103

101104
// User key map error strings
102105
"ERROR_KEYMAP_TITLE": "Error Reading User Key Map",

src/utils/DragAndDrop.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,17 @@ define(function (require, exports, module) {
168168
});
169169
}
170170

171+
async function _focusAndOpenDroppedFiles(droppedPaths) {
172+
try{
173+
const currentWindow = window.__TAURI__.window.getCurrent();
174+
await currentWindow.setAlwaysOnTop(true);
175+
await currentWindow.setAlwaysOnTop(false);
176+
} catch (e) {
177+
console.error("Error focusing window");
178+
}
179+
openDroppedFiles(droppedPaths);
180+
}
181+
171182
if(Phoenix.isNativeApp){
172183
window.__TAURI__.event.listen('file-drop-event-phoenix', ({payload})=> {
173184
if(!payload || !payload.pathList || !payload.pathList.length || !payload.windowLabelOfListener
@@ -182,7 +193,7 @@ define(function (require, exports, module) {
182193
console.error("Error resolving dropped path: ", droppedPath);
183194
}
184195
}
185-
openDroppedFiles(droppedVirtualPaths);
196+
_focusAndOpenDroppedFiles(droppedVirtualPaths);
186197
});
187198
}
188199

@@ -223,7 +234,9 @@ define(function (require, exports, module) {
223234
const isSamePosition = currentPosition.x === newPosition.x && currentPosition.y === newPosition.y;
224235
window.__TAURI__.event.emit("drop-attach-on-window", {
225236
projectName: window.path.basename(ProjectManager.getProjectRoot().fullPath),
226-
dropMessage: "Drop files to open or drop a folder to open it as a project",
237+
dropMessage: Strings.DROP_TO_OPEN_FILES,
238+
dropMessageOneFile: Strings.DROP_TO_OPEN_FILE,
239+
dropProjectMessage: Strings.DROP_TO_OPEN_PROJECT,
227240
windowLabelOfListener: window.__TAURI__.window.appWindow.label
228241
});
229242
if (isSameSize && isSamePosition && (await fileDropWindow.isVisible())) {

0 commit comments

Comments
 (0)