Skip to content

Commit

Permalink
use rfd because dialog is broken in tauri 2.0 right now
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabwhy committed Feb 29, 2024
1 parent 5f51b58 commit 9fcdb3d
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
59 changes: 55 additions & 4 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rayon = "1.8.1"
tauri-plugin-dialog = "2.0.0-beta.1"
tauri-plugin-shell = "2.0.0-beta.1"
tauri-plugin-os = "2.0.0-beta.1"
rfd = "0.14.0"

[profile.release]
strip = true
Expand Down
5 changes: 1 addition & 4 deletions src-tauri/capabilities/migrated.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
],
"platforms": [
"linux",
"macOS",
"windows",
"android",
"iOS"
"windows"
]
}
2 changes: 1 addition & 1 deletion src-tauri/gen/schemas/capabilities.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"migrated":{"identifier":"migrated","description":"permissions that were migrated from v1","local":true,"windows":["main"],"permissions":["path:default","event:default","window:default","app:default","resources:default","menu:default","tray:default","shell:allow-open","dialog:allow-open","os:allow-platform","os:allow-version","os:allow-os-type","os:allow-family","os:allow-arch","os:allow-exe-extension","os:allow-locale","os:allow-hostname"],"platforms":["linux","macOS","windows","android","iOS"]}}
{"migrated":{"identifier":"migrated","description":"permissions that were migrated from v1","local":true,"windows":["main"],"permissions":["path:default","event:default","window:default","app:default","resources:default","menu:default","tray:default","shell:allow-open","dialog:allow-open","os:allow-platform","os:allow-version","os:allow-os-type","os:allow-family","os:allow-arch","os:allow-exe-extension","os:allow-locale","os:allow-hostname"],"platforms":["linux","windows"]}}
22 changes: 22 additions & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use rayon::prelude::*;
use rfd::AsyncFileDialog;
use serde::Serialize;
use sourcepak::common::format::PakReader;
use sourcepak::pak::v1::format::VPKVersion1;
Expand Down Expand Up @@ -65,6 +66,26 @@ impl ExtractState {
}
}

#[tauri::command]
async fn select_vpk(
window: Window,
state: tauri::State<'_, AppState>,
) -> Result<Option<String>, String> {
let file = AsyncFileDialog::new()
.set_parent(&window)
.set_title("Select a VPK file")
.add_filter("VPK Files", &["vpk"])
.pick_file()
.await;

match file {
Some(vpk_file) => {
Ok(Some(vpk_file.path().to_str().ok_or("Invalid path")?.to_string()))
}
None => Ok(None),
}
}

#[tauri::command]
async fn load_vpk(state: tauri::State<'_, AppState>, vpk_path: String) -> Result<(), String> {
let mut file = File::open(&vpk_path).expect("Failed to open file");
Expand Down Expand Up @@ -954,6 +975,7 @@ fn main() {
.plugin(tauri_plugin_dialog::init())
.manage(AppState::new())
.invoke_handler(tauri::generate_handler![
select_vpk,
load_vpk,
get_file_list,
get_file_entry,
Expand Down
12 changes: 8 additions & 4 deletions src/stores/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const useStore = defineStore('main', () => {

try {
await invoke('load_vpk', { vpkPath: path });
getFiles();
await getFiles();
loading.value = false;
loaded.value = true;
hasError.value = false;
Expand All @@ -44,9 +44,13 @@ export const useStore = defineStore('main', () => {
}

async function openVPK() {
let res = await open({ title: 'Select a VPK file', filters: [{ name: 'VPK files', extensions: ['vpk'] }], multiple: false })
if (res !== null) {
loadFromPath(res.path);
try {
let path = await invoke<string | null>('select_vpk');
if (path !== null) {
loadFromPath(path);
}
} catch (e: any) {
console.error(e);
}
}

Expand Down

0 comments on commit 9fcdb3d

Please sign in to comment.