Skip to content

Commit

Permalink
Refactor egui_web slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 3, 2021
1 parent c1e9950 commit f0d31b4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
57 changes: 45 additions & 12 deletions egui_web/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,12 @@ impl AppRunner {
}

pub fn logic(&mut self) -> Result<(egui::Output, Vec<egui::ClippedMesh>), JsValue> {
let frame_start = now_sec();

resize_canvas_to_screen_size(self.canvas_id(), self.app.max_size_points());
let canvas_size = canvas_size_in_points(self.canvas_id());
let raw_input = self.input.new_frame(canvas_size);

let frame_start = now_sec();
self.egui_ctx.begin_frame(raw_input);

let mut app_output = epi::backend::AppOutput::default();
Expand All @@ -199,23 +200,19 @@ impl AppRunner {
let (egui_output, shapes) = self.egui_ctx.end_frame();
let clipped_meshes = self.egui_ctx.tessellate(shapes);

self.previous_frame_time = Some((now_sec() - frame_start) as f32);

if self.egui_ctx.memory().options.screen_reader {
self.screen_reader.speak(&egui_output.events_description());
}
handle_output(&egui_output, self);
self.handle_egui_output(&egui_output);

{
let epi::backend::AppOutput {
quit: _, // Can't quit a web page
window_size: _, // Can't resize a web page
window_title: _,
decorated: _, // Can't show decorations
drag_window: _, // Can't be dragged
quit: _, // Can't quit a web page
window_size: _, // Can't resize a web page
window_title: _, // TODO: change title of window
decorated: _, // Can't toggle decorations
drag_window: _, // Can't be dragged
} = app_output;
}

self.previous_frame_time = Some((now_sec() - frame_start) as f32);
Ok((egui_output, clipped_meshes))
}

Expand All @@ -225,6 +222,42 @@ impl AppRunner {
self.painter
.paint_meshes(clipped_meshes, self.egui_ctx.pixels_per_point())
}

fn handle_egui_output(&mut self, output: &egui::Output) {
if self.egui_ctx.memory().options.screen_reader {
self.screen_reader.speak(&output.events_description());
}

let egui::Output {
cursor_icon,
open_url,
copied_text,
needs_repaint: _, // handled elsewhere
events: _, // already handled
mutable_text_under_cursor,
text_cursor_pos,
} = output;

set_cursor_icon(*cursor_icon);
if let Some(open) = open_url {
crate::open_url(&open.url, open.new_tab);
}

#[cfg(web_sys_unstable_apis)]
if !copied_text.is_empty() {
set_clipboard_text(copied_text);
}

#[cfg(not(web_sys_unstable_apis))]
let _ = copied_text;

self.mutable_text_under_cursor = *mutable_text_under_cursor;

if &self.text_cursor_pos != text_cursor_pos {
move_text_cursor(text_cursor_pos, self.canvas_id());
self.text_cursor_pos = *text_cursor_pos;
}
}
}

/// Install event listeners to register different input events
Expand Down
32 changes: 0 additions & 32 deletions egui_web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,38 +293,6 @@ impl epi::Storage for LocalStorage {

// ----------------------------------------------------------------------------

pub fn handle_output(output: &egui::Output, runner: &mut AppRunner) {
let egui::Output {
cursor_icon,
open_url,
copied_text,
needs_repaint: _, // handled elsewhere
events: _, // we ignore these (TODO: accessibility screen reader)
mutable_text_under_cursor,
text_cursor_pos,
} = output;

set_cursor_icon(*cursor_icon);
if let Some(open) = open_url {
crate::open_url(&open.url, open.new_tab);
}

#[cfg(web_sys_unstable_apis)]
if !copied_text.is_empty() {
set_clipboard_text(copied_text);
}

#[cfg(not(web_sys_unstable_apis))]
let _ = copied_text;

runner.mutable_text_under_cursor = *mutable_text_under_cursor;

if &runner.text_cursor_pos != text_cursor_pos {
move_text_cursor(text_cursor_pos, runner.canvas_id());
runner.text_cursor_pos = *text_cursor_pos;
}
}

pub fn set_cursor_icon(cursor: egui::CursorIcon) -> Option<()> {
let document = web_sys::window()?.document()?;
document
Expand Down

0 comments on commit f0d31b4

Please sign in to comment.