Skip to content

Commit

Permalink
[fix] Use FilesDropped to handle file drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
hbl917070 committed Nov 26, 2023
1 parent 501443e commit 44bcf58
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 43 deletions.
46 changes: 42 additions & 4 deletions Tiefsee/WebWindow.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using Newtonsoft.Json;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;


namespace Tiefsee {

[ComVisible(true)]
Expand Down Expand Up @@ -357,16 +359,52 @@ public async void Init() {
// 開啟時視窗時
wv2.CoreWebView2.NewWindowRequested += (sender2, e2) => {
String _fileurl = e2.Uri.ToString();
//if (_fileurl.IndexOf("http") != 0) {
e2.Handled = true;
//}
//System.Console.WriteLine(_fileurl);

RunJs($@"
var temp_dropPath = ""{_fileurl}"";
if(window.baseWindow !== undefined) baseWindow.onNewWindowRequested(""{_fileurl}"");
");
};

// js 將檔案傳遞進來時,取得檔案路徑,並將其回寫到 js 的全域變數裡面
wv2.CoreWebView2.WebMessageReceived += (sender, args) => {

// 用於組合 json
StringBuilder sb = new();
sb.Append("[");
var funcAdd = (string path) => {
sb.Append("\"" + Path.GetFullPath(path).Replace("\\", "\\\\") + "\",");
};

// 取得所有檔案的路徑
var files = args.AdditionalObjects
.Where(x => x is CoreWebView2File)
.Select(x => ((CoreWebView2File)x).Path)
.ToList();

if (files.Count() == 1) { // 項目只有一個時直接回傳,不論是檔案或資料夾
string path = files[0];
funcAdd(path);
} else { // 項目為多個時,將資料夾內的檔案一並回傳
foreach (var path in files) {
if (Directory.Exists(path)) { // 如果是資料夾
// 取得資料夾內所有檔案
foreach (var item in Directory.GetFiles(path, "*.*")) {
funcAdd(item);
}
} else { // 如果是檔案
funcAdd(path);
}
}
}

sb.Append("]");

RunJs($@"
var temp_dropPath = {sb.ToString()};
");
};


this.SizeChanged += (sender, e) => {
string s = this.WindowState.ToString();
Expand Down
17 changes: 8 additions & 9 deletions Www/ts/BaseWindow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ts-ignore
const WV2 = window.chrome.webview.hostObjects;
declare var chrome: any;
const WV2 = chrome.webview.hostObjects;
const WV_Window: WV_Window = WV2.WV_Window;
const WV_Directory: WV_Directory = WV2.WV_Directory;
const WV_File: WV_File = WV2.WV_File;
Expand All @@ -9,7 +9,7 @@ const WV_RunApp: WV_RunApp = WV2.WV_RunApp;
const WV_Image: WV_Image = WV2.WV_Image;

const APIURL = "http://127.0.0.1:" + location.hash.replace("#", ""); // api 網址
var temp_dropPath = ""; // 暫存。取得拖曳進視窗的檔案路徑
var temp_dropPath: string[] | undefined = undefined; // 暫存。取得拖曳進視窗的檔案路徑

class BaseWindow {

Expand Down Expand Up @@ -142,19 +142,18 @@ class BaseWindow {
/**
* 取得拖曳進來的檔案路徑
*/
public async getDropPath(): Promise<string> {
public async getDropPath() {

// 觸發拖曳檔案後,C#會修改全域變數temp_dropPath
let _dropPath: string = "";
let _dropPath: string[] | undefined = undefined;
for (let i = 0; i < 100; i++) {
if (temp_dropPath !== "") {
_dropPath = Lib.URLToPath(temp_dropPath);
if (temp_dropPath !== undefined) {
_dropPath = temp_dropPath;
break;
}
await Lib.sleep(10);
}
temp_dropPath = "";
_dropPath = _dropPath.replace(/[/]/g, "\\");
temp_dropPath = undefined;
return _dropPath;
}

Expand Down
22 changes: 6 additions & 16 deletions Www/ts/MainWindow/FileLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,22 +339,14 @@ class FileLoad {
*/
async function loadDropFile(files: string[]) {

// 取得拖曳進來的檔案路徑
let _dropPath = await baseWindow.getDropPath();
if (_dropPath === "") { return; }

M.msgbox.closeAll(); // 關閉所有訊息視窗
M.menu.close();
M.textEditor.close();

if (files.length > 1) {
let dirPath = Lib.GetDirectoryName(_dropPath);
if (dirPath === null) { return; }
if (dirPath !== null) {
await loadFiles(dirPath, files);
}
await loadFiles(files);
} else {
await loadFile(_dropPath);
await loadFile(files[0]);
}
}

Expand All @@ -363,18 +355,16 @@ class FileLoad {
* @param dirPath
* @param arName
*/
async function loadFiles(dirPath: string, arName: string[] = []) {
async function loadFiles(ar: string[] = []) {

await WV_System.NewFileWatcher("fileList", ""); // 取消偵測檔案變化

dirPath = await WV_Path.GetFullPath(dirPath); // 避免長路經被轉換成虛擬路徑
arFile = ar;
let dirPath = Lib.GetDirectoryName(arFile[0]);
if (dirPath === null) { return }

dirPathNow = dirPath;
fileLoadType = FileLoadType.userDefined; // 名單類型,自訂義

// 改用C#處理,增加執行效率
arFile = await WebAPI.Directory.getFiles2(dirPath, arName);

let path = arFile[0]; // 以拖曳進來的第一個檔案為開啟對象

let fileInfo2 = await WebAPI.getFileInfo2(path);
Expand Down
22 changes: 8 additions & 14 deletions Www/ts/MainWindow/MainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,18 +364,7 @@ class MainWindow {
}
}

if ((text === "" || text.indexOf("file://") === 0) && files.length > 0) { // 本機的檔案

let arFile = [];
for (let i = 0; i < files.length; i++) {
const item = files[i];
arFile.push(item.name);
}
await fileLoad.loadDropFile(arFile);

e.preventDefault(); // 避免影響 baseWindow.getDropPath()

} else if (text.search(/^http:\/\/127\.0\.0\.1:\d+\/file=/) === 0) { // 如果是 Stable Diffusion webui 的圖片,則直接開啟檔案
if (text.search(/^http:\/\/127\.0\.0\.1:\d+\/file=/) === 0) { // 如果是 Stable Diffusion webui 的圖片,則直接開啟檔案

// ex: http://127.0.0.1:7860/file=D:/ai/a.png

Expand Down Expand Up @@ -465,8 +454,14 @@ class MainWindow {
let path = Lib.URLToPath(textUrl);
await fileLoad.loadFile(path);

} else {
} else if ((text === "" || text.indexOf("file://") === 0) && files.length > 0) { // 本機的檔案

// 取得拖曳進來的檔案路徑
chrome.webview.postMessageWithAdditionalObjects("FilesDropped", files);
let arFile = await baseWindow.getDropPath();

if (arFile === undefined) { return; }
await fileLoad.loadDropFile(arFile);
e.preventDefault();

}
Expand All @@ -478,7 +473,6 @@ class MainWindow {
baseWindow.onNewWindowRequested = (url: string) => {
if (url.indexOf("https://maps.google.com/") === 0) {
WV_RunApp.OpenUrl(url);
temp_dropPath = "";
}
}

Expand Down

0 comments on commit 44bcf58

Please sign in to comment.