From 2b19104f7ff2094b7eb5509e7d322498747c7501 Mon Sep 17 00:00:00 2001 From: white-axe Date: Sat, 7 Oct 2023 00:51:53 +0000 Subject: [PATCH] Upload bindings.js and bindings.rs --- assets/bindings.js | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/web/bindings.rs | 54 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 assets/bindings.js create mode 100644 src/web/bindings.rs diff --git a/assets/bindings.js b/assets/bindings.js new file mode 100644 index 00000000..b2fd6d31 --- /dev/null +++ b/assets/bindings.js @@ -0,0 +1,56 @@ +// Copyright (C) 2023 Lily Lyons +// +// This file is part of Luminol. +// +// Luminol is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Luminol is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Luminol. If not, see . + +export function is_worker() { + return typeof DedicatedWorkerGlobalScope === 'function' + && self instanceof DedicatedWorkerGlobalScope; +} + +export function worker() { + return is_worker() ? self : null; +} + +// A binding for this attribute was added in July 2023 but hasn't made its way into a release of +// web-sys as of September 2023 +export function performance(worker) { + return worker.performance; +} + +export function filesystem_supported() { + return typeof window?.showOpenFilePicker === 'function' + && typeof window?.showDirectoryPicker === 'function' + && typeof FileSystemFileHandle === 'function' + && typeof FileSystemWritableFileStream === 'function' + && typeof FileSystemFileHandle?.prototype?.remove === 'function' + && typeof FileSystemDirectoryHandle?.prototype?.remove === 'function'; +} + +export async function _show_directory_picker() { + return await window.showDirectoryPicker({ mode: 'readwrite' }); +} + +export async function _remove_file(file) { + await file.remove(); +} + +export async function _remove_dir(dir) { + await dir.remove(); +} + +export function dir_values(dir) { + return dir.values(); +} diff --git a/src/web/bindings.rs b/src/web/bindings.rs new file mode 100644 index 00000000..84e1b49b --- /dev/null +++ b/src/web/bindings.rs @@ -0,0 +1,54 @@ +// Copyright (C) 2023 Lily Lyons +// +// This file is part of Luminol. +// +// Luminol is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Luminol is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Luminol. If not, see . +use wasm_bindgen::prelude::*; + +#[wasm_bindgen(module = "/assets/bindings.js")] +extern "C" { + pub fn worker() -> Option; + pub fn performance(worker: &web_sys::DedicatedWorkerGlobalScope) -> web_sys::Performance; + pub fn filesystem_supported() -> bool; + #[wasm_bindgen(catch)] + async fn _show_directory_picker() -> Result; + #[wasm_bindgen(catch)] + async fn _remove_file(file: &web_sys::FileSystemFileHandle) -> Result; + #[wasm_bindgen(catch)] + async fn _remove_dir(dir: &web_sys::FileSystemDirectoryHandle) -> Result; + pub fn dir_values(dir: &web_sys::FileSystemDirectoryHandle) -> js_sys::AsyncIterator; +} + +pub async fn show_directory_picker() -> Result { + _show_directory_picker() + .await + .map(|o| o.unchecked_into()) + .map_err(|e| e.unchecked_into()) +} + +pub async fn remove_file(file: &web_sys::FileSystemFileHandle) -> Result { + _remove_file(file) + .await + .map(|o| o.unchecked_into()) + .map_err(|e| e.unchecked_into()) +} + +pub async fn remove_dir( + dir: &web_sys::FileSystemDirectoryHandle, +) -> Result { + _remove_dir(dir) + .await + .map(|o| o.unchecked_into()) + .map_err(|e| e.unchecked_into()) +}