Skip to content

Commit

Permalink
Upload bindings.js and bindings.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
white-axe committed Oct 7, 2023
1 parent 6091710 commit 2b19104
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
56 changes: 56 additions & 0 deletions assets/bindings.js
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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();
}
54 changes: 54 additions & 0 deletions src/web/bindings.rs
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
use wasm_bindgen::prelude::*;

#[wasm_bindgen(module = "/assets/bindings.js")]
extern "C" {
pub fn worker() -> Option<web_sys::DedicatedWorkerGlobalScope>;
pub fn performance(worker: &web_sys::DedicatedWorkerGlobalScope) -> web_sys::Performance;
pub fn filesystem_supported() -> bool;
#[wasm_bindgen(catch)]
async fn _show_directory_picker() -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
async fn _remove_file(file: &web_sys::FileSystemFileHandle) -> Result<JsValue, JsValue>;
#[wasm_bindgen(catch)]
async fn _remove_dir(dir: &web_sys::FileSystemDirectoryHandle) -> Result<JsValue, JsValue>;
pub fn dir_values(dir: &web_sys::FileSystemDirectoryHandle) -> js_sys::AsyncIterator;
}

pub async fn show_directory_picker() -> Result<web_sys::FileSystemDirectoryHandle, js_sys::Error> {
_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<JsValue, js_sys::Error> {
_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<JsValue, js_sys::Error> {
_remove_dir(dir)
.await
.map(|o| o.unchecked_into())
.map_err(|e| e.unchecked_into())
}

0 comments on commit 2b19104

Please sign in to comment.