From 7f7d5f2c0b4304fbcd81c050c3701cf455c802a6 Mon Sep 17 00:00:00 2001 From: Tomoki Miyauchi Date: Wed, 31 Jul 2024 22:18:57 +0900 Subject: [PATCH] feat: export save file dialog feature via FFI --- src/deno/binding.ts | 7 +++++++ src/deno/generated.ts | 14 ++++++++++++++ src/file_dialog/src/lib.rs | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/deno/binding.ts b/src/deno/binding.ts index b79ad1e..13f3ec2 100644 --- a/src/deno/binding.ts +++ b/src/deno/binding.ts @@ -25,6 +25,13 @@ export const symbols = { result: "pointer", nonblocking: false, }, + __Dialog_save_file: { + parameters: [ + "pointer", + ], + result: "pointer", + nonblocking: false, + }, __Dialog_set_directory: { parameters: [ "pointer", diff --git a/src/deno/generated.ts b/src/deno/generated.ts index 317b67f..7a4b4f5 100644 --- a/src/deno/generated.ts +++ b/src/deno/generated.ts @@ -57,6 +57,14 @@ function __Dialog_pick_directory( ); } +function __Dialog_save_file( + arg0: Deno.PointerObject | null, +): Deno.PointerObject | null { + return symbols.__Dialog_save_file( + arg0, + ); +} + function __Dialog_set_directory( arg0: Deno.PointerObject | null, arg1: Uint8Array, @@ -127,6 +135,12 @@ export class Dialog { ); } + save_file(): Deno.PointerObject | null { + return __Dialog_save_file( + this.ptr, + ); + } + set_directory(arg0: Uint8Array): Dialog { return __Dialog_set_directory( this.ptr, diff --git a/src/file_dialog/src/lib.rs b/src/file_dialog/src/lib.rs index 43d17ab..e2dc2a9 100644 --- a/src/file_dialog/src/lib.rs +++ b/src/file_dialog/src/lib.rs @@ -75,6 +75,24 @@ impl Dialog { c_string.into_raw() } + pub fn save_file(&self) -> *mut c_char { + let path_buf = self.dialog.clone().save_file(); + + let result = match path_buf { + Some(path) => json!({ + "success": true, + "data": path + }), + None => json!({ + "success": false + }), + }; + + let c_string = CString::new(result.to_string()).unwrap(); + + c_string.into_raw() + } + pub fn set_directory(&self, p: &[u8]) -> Dialog { let path_str = str::from_utf8(p).expect("Invalid UTF-8 sequence"); let path = Path::new(path_str);