Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Jun 21, 2024
1 parent b16cf98 commit db4b27c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
14 changes: 14 additions & 0 deletions crates/eframe/src/web/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@ pub(crate) fn paint_and_schedule(runner_ref: &WebRunner) -> Result<(), JsValue>
// Only paint and schedule if there has been no panic
if let Some(mut runner_lock) = runner_ref.try_lock() {
paint_if_needed(&mut runner_lock);
drop_files(&mut runner_lock);
drop(runner_lock);
runner_ref.request_animation_frame()?;
}
Ok(())
}

fn drop_files(runner: &mut AppRunner) {
if !runner.input.raw.dropped_files.is_empty() {
for mut dropped_file in runner.input.raw.dropped_files.drain(..) {
if let Some(stream_url) = dropped_file.stream_url {
if *dropped_file.need_drop_url {
let _ = web_sys::Url::revoke_object_url(&stream_url);
dropped_file.need_drop_url = false.into();
}
}
}
}
}

fn paint_if_needed(runner: &mut AppRunner) {
if runner.needs_repaint.needs_repaint() {
if runner.has_outstanding_paint_data() {
Expand Down
8 changes: 3 additions & 5 deletions crates/eframe/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,11 @@ pub fn percent_decode(s: &str) -> String {

/// Load the data from a dropped file.
pub async fn get_data(
dropped_file: &egui::DroppedFile,
drop_file: bool,
dropped_file: &mut egui::DroppedFile,
) -> Result<std::sync::Arc<[u8]>, String> {
use wasm_bindgen_futures::JsFuture;

dropped_file.need_drop_url = false.into();
let url = dropped_file.stream_url.clone().ok_or("No stream URL")?;

let window = web_sys::window().ok_or("No Window object")?;
Expand Down Expand Up @@ -282,9 +282,7 @@ pub async fn get_data(
let bytes = js_sys::Uint8Array::new(&array_buffer).to_vec();
log::debug!("Loaded {} bytes", bytes.len());

if drop_file {
let _ = web_sys::Url::revoke_object_url(&url);
}
dropped_file.need_drop_url = true.into();

Ok(std::sync::Arc::from(bytes.into_boxed_slice()))
}
5 changes: 5 additions & 0 deletions crates/egui/src/data/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! The input needed by egui.

use std::sync::Arc;

use epaint::ColorImage;

use crate::{emath::*, Key, ViewportId, ViewportIdMap};
Expand Down Expand Up @@ -335,6 +337,9 @@ pub struct DroppedFile {
/// Set by the `eframe` web backend.
pub last_modified: Option<std::time::SystemTime>,

/// drop the url
pub need_drop_url: Arc<bool>,

/// Set by the `eframe` web backend.
pub stream_url: Option<String>,
}
Expand Down
5 changes: 2 additions & 3 deletions crates/egui_demo_app/src/wrap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,9 @@ impl WrapApp {
if file.stream_url.is_some()
&& ui.button("Read file (output in console)").clicked()
{
let one_file = file.clone();
let mut one_file = file.clone();
let func = async move {
let res = eframe::web::get_data(&one_file, true);
let res = eframe::web::get_data(&mut one_file);
match res.await {
Ok(data) => {
log::info!("Read {} bytes", data.len());
Expand All @@ -513,7 +513,6 @@ impl WrapApp {
}
};
wasm_bindgen_futures::spawn_local(func);
file.stream_url = None;
}
}
});
Expand Down

0 comments on commit db4b27c

Please sign in to comment.