Created
November 15, 2025 19:48
-
-
Save gdotdesign/09fa3753c633fabf4832f38f8aa4277a to your computer and use it in GitHub Desktop.
Tauri (v1) Functions Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Tauri.File { | |
| fun download (file : File) : Promise(Never, Void) { | |
| if (`window.__TAURI__`) { | |
| ` | |
| new Promise(async (resolve, reject) => { | |
| const path = | |
| await Tauri.dialog.save({ defaultPath: #{File.name(file)} }) | |
| if (path) { | |
| const contents = | |
| await #{File.readAsArrayBuffer(file, false)} | |
| Tauri.fs.writeBinaryFile({ | |
| contents: new Uint8Array(contents), | |
| path: path | |
| }) | |
| } | |
| }) | |
| ` | |
| } else { | |
| File.download(file) | |
| } | |
| } | |
| fun select (accept : String) : Promise(Never, File) { | |
| if (`window.__TAURI__`) { | |
| ` | |
| new Promise(async (resolve, reject) => { | |
| const path = | |
| await Tauri.dialog.open({ | |
| directory: false, | |
| multiple: false | |
| }) | |
| if (path) { | |
| const file = | |
| await #{toFile(`path`)}; | |
| resolve(file) | |
| } | |
| }) | |
| ` | |
| } else { | |
| File.select(accept) | |
| } | |
| } | |
| fun selectMultiple (accept : String) : Promise(Never, Array(File)) { | |
| if (`window.__TAURI__`) { | |
| ` | |
| new Promise(async (resolve, reject) => { | |
| const paths = | |
| await Tauri.dialog.open({ | |
| directory: false, | |
| multiple: true | |
| }) | |
| if (paths) { | |
| const results = [] | |
| for (let path of paths) { | |
| const file = | |
| await #{toFile(`path`)}; | |
| results.push(file) | |
| } | |
| resolve(results) | |
| } | |
| }) | |
| ` | |
| } else { | |
| File.selectMultiple(accept) | |
| } | |
| } | |
| /* Reads a file from the users disk. */ | |
| fun toFile (path : String) : Promise(Never, File) { | |
| ` | |
| new Promise(async (resolve, reject) => { | |
| const contents = | |
| await Tauri.fs.readBinaryFile(#{path}) | |
| const filename = | |
| await Tauri.tauri.invoke("get_filename", {path: #{path}}) | |
| const type = | |
| await Tauri.tauri.invoke("get_mime_type", {path: #{path}}) | |
| const bytes = | |
| new Uint8Array(contents.length); | |
| for (let i = 0; i < contents.length; i++) { | |
| bytes[i] = contents[i] | |
| } | |
| resolve(new File([bytes], filename, { type: type })) | |
| }) | |
| ` | |
| } | |
| } | |
| module Tauri.Fs { | |
| fun readTextFile (path : String) : Promise(Never, String) { | |
| `Tauri.fs.readTextFile(#{path})` | |
| } | |
| fun writeFile (path : String, contents : String) : Promise(Never, Void) { | |
| ` | |
| Tauri.fs.writeFile({ | |
| path: #{path}, | |
| contents: #{contents} | |
| }) | |
| ` | |
| } | |
| } | |
| module Tauri.Path { | |
| fun resolvePath (path : String) : Promise(Never, String) { | |
| `Tauri.path.resolvePath(#{path}, Tauri.fs.BaseDirectory.Config)` | |
| } | |
| } | |
| module Tauri.Notification { | |
| fun sendNotification (title : String, body : String, icon : String) { | |
| `Tauri.notification.sendNotification({ title: #{title}, body: #{body}, icon: #{icon} })` | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment