From 834139dd5c9747e8e49dfd735f9d67250831ccdb Mon Sep 17 00:00:00 2001 From: mayank Date: Mon, 19 Aug 2024 19:59:43 +0530 Subject: [PATCH] feat(space): add option to open an existing space from filesystem (#11) - get rid of `@tauri-apps/plugin-dialog` node pkg and add `open_directory_dialog` invoke command --- .changeset/rude-plants-invent.md | 5 ++++ package.json | 1 - pnpm-lock.yaml | 10 ------- src-tauri/src/core/commands/dialog.rs | 36 ++++++++++++++++++++++++ src-tauri/src/core/commands/mod.rs | 1 + src-tauri/src/main.rs | 3 +- src/lib/commands/index.ts | 11 ++++++++ src/routes/+page.svelte | 40 +++++++++++++++++++-------- 8 files changed, 84 insertions(+), 23 deletions(-) create mode 100644 .changeset/rude-plants-invent.md create mode 100644 src-tauri/src/core/commands/dialog.rs create mode 100644 src/lib/commands/index.ts diff --git a/.changeset/rude-plants-invent.md b/.changeset/rude-plants-invent.md new file mode 100644 index 0000000..5aa605c --- /dev/null +++ b/.changeset/rude-plants-invent.md @@ -0,0 +1,5 @@ +--- +"zaku": patch +--- + +Add option to open existing space from filesystem diff --git a/package.json b/package.json index 7001c38..305015e 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,6 @@ "@codemirror/view": "6.29.0", "@lezer/highlight": "1.2.0", "@tauri-apps/api": "2.0.0-rc.1", - "@tauri-apps/plugin-dialog": "2.0.0-rc.0", "@tauri-apps/plugin-http": "2.0.0-rc.1", "bits-ui": "0.21.12", "clsx": "2.1.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2269068..633ec05 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,9 +35,6 @@ importers: '@tauri-apps/api': specifier: 2.0.0-rc.1 version: 2.0.0-rc.1 - '@tauri-apps/plugin-dialog': - specifier: 2.0.0-rc.0 - version: 2.0.0-rc.0 '@tauri-apps/plugin-http': specifier: 2.0.0-rc.1 version: 2.0.0-rc.1 @@ -689,9 +686,6 @@ packages: engines: {node: '>= 10'} hasBin: true - '@tauri-apps/plugin-dialog@2.0.0-rc.0': - resolution: {integrity: sha512-DPOXYe8SQ6Radk/67EOdaomlxL7oF99JO/ZUaPp1IBEs3Wro7lhlz63CfdKIBfKIZTLJLzP1R7/EiPL/GTA3Bg==} - '@tauri-apps/plugin-http@2.0.0-rc.1': resolution: {integrity: sha512-j4WdTEKx0CFa6u8ubke0mo75pCrnu6XtrFtvjsh+zjuNYgMG/l0+A1woWXHm73f2Levskhs+KbKcLQA/nr8k2w==} @@ -2788,10 +2782,6 @@ snapshots: '@tauri-apps/cli-win32-ia32-msvc': 2.0.0-rc.4 '@tauri-apps/cli-win32-x64-msvc': 2.0.0-rc.4 - '@tauri-apps/plugin-dialog@2.0.0-rc.0': - dependencies: - '@tauri-apps/api': 2.0.0-rc.1 - '@tauri-apps/plugin-http@2.0.0-rc.1': dependencies: '@tauri-apps/api': 2.0.0-rc.1 diff --git a/src-tauri/src/core/commands/dialog.rs b/src-tauri/src/core/commands/dialog.rs new file mode 100644 index 0000000..dce6791 --- /dev/null +++ b/src-tauri/src/core/commands/dialog.rs @@ -0,0 +1,36 @@ +use serde::{Deserialize, Serialize}; +use tauri::AppHandle; +use tauri_plugin_dialog::DialogExt; + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct OpenDirectoryDialogOptions { + pub title: Option, +} + +#[tauri::command] +pub async fn open_directory_dialog( + options: Option, + app_handle: AppHandle, +) -> Result, String> { + let mut dialog_builder = app_handle.dialog().file(); + + match options { + Some(OpenDirectoryDialogOptions { + title: Some(ref title), + }) => { + dialog_builder = dialog_builder.set_title(title); + } + _ => {} + } + + let directory_path = dialog_builder.blocking_pick_folder(); + + match directory_path { + Some(path) => { + return Ok(Some(path.to_string_lossy().to_string())); + } + None => { + return Ok(None); + } + } +} diff --git a/src-tauri/src/core/commands/mod.rs b/src-tauri/src/core/commands/mod.rs index 2ead8d4..e851156 100644 --- a/src-tauri/src/core/commands/mod.rs +++ b/src-tauri/src/core/commands/mod.rs @@ -1,2 +1,3 @@ +pub mod dialog; pub mod space; pub mod window; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 1d05ee5..d8dc8e1 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -26,7 +26,8 @@ fn main() { commands::space::get_active_space, commands::space::set_active_space, commands::space::delete_active_space, - commands::window::show_main_window + commands::window::show_main_window, + commands::dialog::open_directory_dialog ]); app.run(tauri::generate_context!()) diff --git a/src/lib/commands/index.ts b/src/lib/commands/index.ts new file mode 100644 index 0000000..6d8d18d --- /dev/null +++ b/src/lib/commands/index.ts @@ -0,0 +1,11 @@ +import { invoke } from "@tauri-apps/api/core"; + +export type OpenDirectoryDialog = { + title?: string; +}; + +export async function openDirectoryDialog(options?: OpenDirectoryDialog) { + const path: string | null = await invoke("open_directory_dialog", { options }); + + return path; +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index b2d7b87..3317113 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -11,20 +11,18 @@ } from "$lib/components/primitives/dialog"; import { Label } from "$lib/components/primitives/label"; import { Input } from "$lib/components/primitives/input"; - import { open } from "@tauri-apps/plugin-dialog"; import { tick } from "svelte"; import { Struct } from "$lib/utils/struct"; import { goto } from "$app/navigation"; - import { createSpace } from "$lib/store"; + import { activeSpace, createSpace } from "$lib/store"; + import { openDirectoryDialog } from "$lib/commands"; let spaceName: string = ""; let spacePath: string = ""; - async function handleBrowse() { - const selected = await open({ - directory: true, - multiple: false, - }); + async function handleCreateSpaceBrowse() { + const selected = await openDirectoryDialog({ title: "Create a new Space" }); + if (selected !== null) { spacePath = selected; @@ -57,6 +55,20 @@ console.error(err); } } + + async function handleOpenExistingSpace() { + try { + const selectedPath = await openDirectoryDialog({ title: "Open an existing Space" }); + + if (selectedPath !== null) { + await activeSpace.set(selectedPath); + await goto("/space"); + } + } catch (err) { + // TODO - show error toast + console.error(err); + } + }
@@ -72,7 +84,7 @@ Create a new Space - Make changes to your profile here. Click save when you're done. + Separate your projects, work and more. Choose a name and where to save it.
@@ -86,12 +98,12 @@ +