Skip to content

Commit 5b99cf4

Browse files
committed
✨ support drag file load
1 parent 36c3c38 commit 5b99cf4

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

src-tauri/src/main.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
mod preview;
55
mod unity;
66

7-
use tauri::{AppHandle, Manager, WindowEvent::CloseRequested};
7+
use tauri::{AppHandle, Manager, WindowEvent::{CloseRequested, self}, async_runtime};
88

99
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
1010
#[tauri::command]
1111
fn greet(name: &str) -> String {
1212
format!("Hello, {}! You've been greeted from Rust!", name)
1313
}
1414
use preview::{preview_image, preview_text};
15-
use unity::{load_unity_asset, preview_object};
15+
use unity::{load_unity_asset, preview_object, dropped_unity_asset};
1616

1717
use crate::unity::{export_bundle, export_file_type, export_object, sync_loaded_asset};
1818

@@ -33,9 +33,15 @@ fn main() {
3333
let windows = app.get_window("main").expect("Main windows not found");
3434
let app_handle = app.app_handle();
3535
windows.on_window_event(move |event| {
36-
if let CloseRequested { api, .. } = event {
37-
api.prevent_close();
38-
app_handle.exit(0)
36+
match event {
37+
CloseRequested { api, .. } => {
38+
api.prevent_close();
39+
app_handle.exit(0)
40+
}
41+
WindowEvent::FileDrop(tauri::FileDropEvent::Dropped(paths)) => {
42+
async_runtime::spawn(dropped_unity_asset(app_handle.clone(), paths.clone()));
43+
},
44+
_ => (),
3945
}
4046
});
4147
#[cfg(debug_assertions)]

src-tauri/src/unity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ fn get_opened_unity_asset() -> &'static DashMap<Uuid, StoreUnityBundle> {
2121

2222
pub use commands::{
2323
export_bundle, export_file_type, export_object, load_unity_asset, preview_object,
24-
sync_loaded_asset,
24+
sync_loaded_asset,dropped_unity_asset
2525
};

src-tauri/src/unity/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ mod import;
44
mod preview;
55

66
pub use export::{export_bundle, export_file_type, export_object};
7-
pub use import::{load_unity_asset, sync_loaded_asset};
7+
pub use import::{load_unity_asset, sync_loaded_asset,dropped_unity_asset};
88
pub use preview::preview_object;

src-tauri/src/unity/commands/import.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::PathBuf;
22

3-
use tauri::{command, AppHandle};
3+
use tauri::{command, AppHandle, Manager};
44
use uuid::Uuid;
55

66
use crate::{
@@ -12,6 +12,8 @@ use crate::{
1212
},
1313
};
1414

15+
const LOAD_ASSET_DONE: &str = "unity-bundle-load-done";
16+
1517
#[command(async)]
1618
pub async fn load_unity_asset(app: AppHandle, path: String) -> UnityResult<UnityAsset> {
1719
let key = Uuid::new_v5(&Uuid::NAMESPACE_OID, path.as_bytes());
@@ -33,6 +35,22 @@ pub async fn load_unity_asset(app: AppHandle, path: String) -> UnityResult<Unity
3335
loading_done(app)?;
3436
Ok(asset)
3537
}
38+
39+
pub async fn dropped_unity_asset(app: AppHandle, paths: Vec<PathBuf>) -> UnityResult<()> {
40+
app.emit_all("loading", true)?;
41+
let mut result = Vec::new();
42+
for path in paths.into_iter().filter(|path| path.is_file()) {
43+
let Some(path) = path.as_os_str().to_str() else{
44+
continue;
45+
};
46+
if let Ok(ua) = load_unity_asset(app.clone(), path.to_string()).await {
47+
result.push(ua)
48+
}
49+
}
50+
app.emit_to("main", LOAD_ASSET_DONE, result)?;
51+
Ok(())
52+
}
53+
3654
#[command]
3755
pub fn sync_loaded_asset() -> Vec<UnityAsset> {
3856
get_opened_unity_asset()

src/components/MainPage.vue

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { ref } from "vue";
1414
import { BtnDefine } from "../tauri_pack/pack.ts";
1515
import TopBtnBar from "./TopBtnBar.vue";
1616
import ExpandableList from "./ExpandableList.vue";
17-
import { openOneFile } from "../tauri_pack/pack";
17+
import { loadDone, openOneFile } from "../tauri_pack/pack";
1818
import {
1919
loadUnityAsset,
2020
syncLoadedAsset,
@@ -76,19 +76,38 @@ const buttons: BtnDefine[] = [
7676
];
7777
7878
const unliten = ref<UnlistenFn | null>();
79+
const unLitenAsset = ref<UnlistenFn | null>();
7980
onMounted(async () => {
8081
unliten.value = await listen<boolean>(
8182
"loading",
8283
({ payload }: { payload: boolean }) => {
8384
loading.value = payload;
8485
}
8586
);
87+
88+
unLitenAsset.value = await listen<UnityAsset[]>(
89+
"unity-bundle-load-done",
90+
({ payload }: { payload: UnityAsset[] }) => {
91+
payload.forEach((element) => {
92+
let idx = unityAsset.value.findIndex(
93+
(value: UnityAsset) => value.id == element.id
94+
);
95+
if (idx == -1) {
96+
unityAsset.value.push(element);
97+
}
98+
});
99+
loadDone();
100+
}
101+
);
86102
});
87103
88104
onUnmounted(() => {
89105
if (unliten.value) {
90106
unliten.value();
91107
}
108+
if (unLitenAsset.value) {
109+
unLitenAsset.value();
110+
}
92111
});
93112
</script>
94113

0 commit comments

Comments
 (0)