2020-07-23 16:54:16 +00:00
|
|
|
use crate::*;
|
|
|
|
|
2021-01-02 16:02:18 +00:00
|
|
|
pub use egui::{pos2, Color32};
|
2020-07-23 16:54:16 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2021-11-03 12:45:51 +00:00
|
|
|
fn create_painter(canvas_id: &str) -> Result<Box<dyn Painter>, JsValue> {
|
2021-12-31 14:17:55 +00:00
|
|
|
// Glow takes precedence:
|
|
|
|
#[cfg(all(feature = "glow"))]
|
2021-11-03 18:17:07 +00:00
|
|
|
return Ok(Box::new(crate::glow_wrapping::WrappedGlowPainter::new(
|
|
|
|
canvas_id,
|
|
|
|
)));
|
2021-12-31 14:17:55 +00:00
|
|
|
|
|
|
|
#[cfg(all(feature = "webgl", not(feature = "glow")))]
|
2021-11-03 12:45:51 +00:00
|
|
|
if let Ok(webgl2_painter) = webgl2::WebGl2Painter::new(canvas_id) {
|
|
|
|
console_log("Using WebGL2 backend");
|
|
|
|
Ok(Box::new(webgl2_painter))
|
|
|
|
} else {
|
|
|
|
console_log("Falling back to WebGL1 backend");
|
|
|
|
let webgl1_painter = webgl1::WebGlPainter::new(canvas_id)?;
|
|
|
|
Ok(Box::new(webgl1_painter))
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
2021-12-31 14:17:55 +00:00
|
|
|
|
|
|
|
#[cfg(all(not(feature = "webgl"), not(feature = "glow")))]
|
|
|
|
compile_error!("Either the 'glow' or 'webgl' feature of egui_web must be enabled!");
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// Data gathered between frames.
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct WebInput {
|
2021-01-25 17:50:19 +00:00
|
|
|
/// Required because we don't get a position on touched
|
|
|
|
pub latest_touch_pos: Option<egui::Pos2>,
|
|
|
|
|
Basic multi touch support (issue #279) (#306)
* translate touch events from glium to egui
Unfortunately, winit does not seem to create _Touch_ events for the touch pad
on my mac. Only _TouchpadPressure_ events are sent.
Found some issues (like
[this](https://github.com/rust-windowing/winit/issues/54)), but I am not sure
what they exactly mean: Sometimes, touch events are mixed with
touch-to-pointer translation in the discussions.
* translate touch events from web_sys to egui
The are a few open topics:
- egui_web currently translates touch events into pointer events.
I guess this should change, such that egui itself performs this kind of
conversion.
- `pub fn egui_web::pos_from_touch_event` is a public function, but I
would like to change the return type to an `Option`. Shouldn't this
function be private, anyway?
* introduce `TouchState` and `Gesture`
InputState.touch was introduced with type `TouchState`, just as
InputState.pointer is of type `Pointer`.
The TouchState internally relies on a collection of `Gesture`s. This commit
provides the first rudimentary implementation of a Gesture, but has no
functionality, yet.
* add method InputState::zoom()
So far, the method always returns `None`, but it should work as soon as the
`Zoom` gesture is implemented.
* manage one `TouchState` per individual device
Although quite unlikely, it is still possible to connect more than one touch
device. (I have three touch pads connected to my MacBook in total, but
unfortunately `winit` sends touch events for none of them.)
We do not want to mix-up the touches from different devices.
* implement control loop for gesture detection
The basic idea is that each gesture can focus on detection logic and does not
have to care (too much) about managing touch state in general.
* streamline `Gesture` trait, simplifying impl's
* implement first version of Zoom gesture
* fix failing doctest
a simple `TODO` should be enough
* get rid of `Gesture`s
* Provide a Zoom/Rotate window in the demo app
For now, it works for two fingers only. The third finger interrupts the
gesture.
Bugs:
- Pinching in the demo window also moves the window -> Pointer events must be
ignored when touch is active
- Pinching also works when doing it outside the demo window -> it would be nice
to return the touch info in the `Response` of the painter allocation
* fix comments and non-idiomatic code
* update touch state *each frame*
* change egui_demo to use *relative* touch data
* support more than two fingers
This commit includes an improved Demo Window for egui_demo, and a complete
re-write of the gesture detection. The PR should be ready for review, soon.
* cleanup code and comments for review
* minor code simplifications
* oops – forgot the changelog
* resolve comment https://github.com/emilk/egui/pull/306/files/fee8ed83dbe715b5b70433faacfe74b59c99e4a4#r623226656
* accept suggestion https://github.com/emilk/egui/pull/306#discussion_r623229228
Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
* fix syntax error (dough!)
* remove `dbg!` (why didnt clippy see this?)
* apply suggested diffs from review
* fix conversion of physical location to Pos2
* remove redundanct type `TouchAverages`
* remove trailing space
* avoid initial translation jump in plot demo
* extend the demo so it shows off translation
Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-05-06 19:01:10 +00:00
|
|
|
/// Required to maintain a stable touch position for multi-touch gestures.
|
|
|
|
pub latest_touch_pos_id: Option<egui::TouchId>,
|
|
|
|
|
2020-11-15 13:21:21 +00:00
|
|
|
pub raw: egui::RawInput,
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl WebInput {
|
2020-12-18 21:51:23 +00:00
|
|
|
pub fn new_frame(&mut self, canvas_size: egui::Vec2) -> egui::RawInput {
|
2020-07-23 16:54:16 +00:00
|
|
|
egui::RawInput {
|
2020-12-18 21:51:23 +00:00
|
|
|
screen_rect: Some(egui::Rect::from_min_size(Default::default(), canvas_size)),
|
2021-02-21 10:23:33 +00:00
|
|
|
pixels_per_point: Some(native_pixels_per_point()), // We ALWAYS use the native pixels-per-point
|
2020-12-16 20:22:45 +00:00
|
|
|
time: Some(now_sec()),
|
2020-11-15 13:21:21 +00:00
|
|
|
..self.raw.take()
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-11-20 19:35:16 +00:00
|
|
|
use std::sync::atomic::Ordering::SeqCst;
|
|
|
|
|
|
|
|
pub struct NeedRepaint(std::sync::atomic::AtomicBool);
|
|
|
|
|
|
|
|
impl Default for NeedRepaint {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(true.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NeedRepaint {
|
|
|
|
pub fn fetch_and_clear(&self) -> bool {
|
|
|
|
self.0.swap(false, SeqCst)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_true(&self) {
|
|
|
|
self.0.store(true, SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 20:21:28 +00:00
|
|
|
impl epi::backend::RepaintSignal for NeedRepaint {
|
2020-11-20 19:35:16 +00:00
|
|
|
fn request_repaint(&self) {
|
|
|
|
self.0.store(true, SeqCst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub struct AppRunner {
|
2021-12-26 20:21:28 +00:00
|
|
|
frame: epi::Frame,
|
2021-11-03 12:45:51 +00:00
|
|
|
egui_ctx: egui::CtxRef,
|
|
|
|
painter: Box<dyn Painter>,
|
2020-12-31 13:31:11 +00:00
|
|
|
pub(crate) input: WebInput,
|
|
|
|
app: Box<dyn epi::App>,
|
|
|
|
pub(crate) needs_repaint: std::sync::Arc<NeedRepaint>,
|
|
|
|
storage: LocalStorage,
|
|
|
|
last_save_time: f64,
|
2021-03-08 19:58:01 +00:00
|
|
|
screen_reader: crate::screen_reader::ScreenReader,
|
2021-10-23 14:23:29 +00:00
|
|
|
pub(crate) text_cursor_pos: Option<egui::Pos2>,
|
|
|
|
pub(crate) mutable_text_under_cursor: bool,
|
2021-12-26 20:21:28 +00:00
|
|
|
pending_texture_destructions: Vec<u64>,
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppRunner {
|
2021-11-03 12:45:51 +00:00
|
|
|
pub fn new(canvas_id: &str, app: Box<dyn epi::App>) -> Result<Self, JsValue> {
|
2021-12-26 20:21:28 +00:00
|
|
|
let painter = create_painter(canvas_id)?;
|
2021-06-07 18:56:18 +00:00
|
|
|
|
2021-06-07 18:53:33 +00:00
|
|
|
let prefer_dark_mode = crate::prefer_dark_mode();
|
2021-06-07 18:56:18 +00:00
|
|
|
|
2021-12-26 20:21:28 +00:00
|
|
|
let needs_repaint: std::sync::Arc<NeedRepaint> = Default::default();
|
|
|
|
|
|
|
|
let frame = epi::Frame::new(epi::backend::FrameData {
|
|
|
|
info: epi::IntegrationInfo {
|
|
|
|
name: painter.name(),
|
|
|
|
web_info: Some(epi::WebInfo {
|
|
|
|
web_location_hash: location_hash().unwrap_or_default(),
|
|
|
|
}),
|
|
|
|
prefer_dark_mode,
|
|
|
|
cpu_usage: None,
|
|
|
|
native_pixels_per_point: Some(native_pixels_per_point()),
|
|
|
|
},
|
|
|
|
output: Default::default(),
|
|
|
|
repaint_signal: needs_repaint.clone(),
|
|
|
|
});
|
|
|
|
|
|
|
|
let egui_ctx = egui::CtxRef::default();
|
|
|
|
load_memory(&egui_ctx);
|
2021-06-07 18:56:18 +00:00
|
|
|
if prefer_dark_mode == Some(true) {
|
2021-11-03 12:45:51 +00:00
|
|
|
egui_ctx.set_visuals(egui::Visuals::dark());
|
2021-06-07 18:56:18 +00:00
|
|
|
} else {
|
2021-11-03 12:45:51 +00:00
|
|
|
egui_ctx.set_visuals(egui::Visuals::light());
|
2021-06-07 18:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let storage = LocalStorage::default();
|
|
|
|
|
2021-06-07 18:53:33 +00:00
|
|
|
let mut runner = Self {
|
2021-12-26 20:21:28 +00:00
|
|
|
frame,
|
2021-11-03 12:45:51 +00:00
|
|
|
egui_ctx,
|
2021-12-26 20:21:28 +00:00
|
|
|
painter,
|
2020-11-15 13:21:21 +00:00
|
|
|
input: Default::default(),
|
2020-07-23 16:54:16 +00:00
|
|
|
app,
|
2021-12-26 20:21:28 +00:00
|
|
|
needs_repaint,
|
2020-12-19 19:50:00 +00:00
|
|
|
storage,
|
|
|
|
last_save_time: now_sec(),
|
2021-03-08 19:58:01 +00:00
|
|
|
screen_reader: Default::default(),
|
2021-10-23 14:23:29 +00:00
|
|
|
text_cursor_pos: None,
|
|
|
|
mutable_text_under_cursor: false,
|
2021-12-26 20:21:28 +00:00
|
|
|
pending_texture_destructions: Default::default(),
|
2021-06-07 18:53:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
{
|
2021-11-03 12:45:51 +00:00
|
|
|
runner
|
|
|
|
.app
|
2021-12-26 20:21:28 +00:00
|
|
|
.setup(&runner.egui_ctx, &runner.frame, Some(&runner.storage));
|
2021-06-07 18:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(runner)
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 11:22:19 +00:00
|
|
|
pub fn egui_ctx(&self) -> &egui::CtxRef {
|
2021-11-03 12:45:51 +00:00
|
|
|
&self.egui_ctx
|
2021-01-17 11:22:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 19:50:00 +00:00
|
|
|
pub fn auto_save(&mut self) {
|
|
|
|
let now = now_sec();
|
|
|
|
let time_since_last_save = now - self.last_save_time;
|
|
|
|
|
|
|
|
if time_since_last_save > self.app.auto_save_interval().as_secs_f64() {
|
2021-09-29 06:45:13 +00:00
|
|
|
if self.app.persist_egui_memory() {
|
2021-11-03 12:45:51 +00:00
|
|
|
save_memory(&self.egui_ctx);
|
2021-09-29 06:45:13 +00:00
|
|
|
}
|
2020-12-19 19:50:00 +00:00
|
|
|
self.app.save(&mut self.storage);
|
|
|
|
self.last_save_time = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub fn canvas_id(&self) -> &str {
|
2021-11-03 12:45:51 +00:00
|
|
|
self.painter.canvas_id()
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 13:42:43 +00:00
|
|
|
pub fn warm_up(&mut self) -> Result<(), JsValue> {
|
|
|
|
if self.app.warm_up_enabled() {
|
2021-11-03 12:45:51 +00:00
|
|
|
let saved_memory = self.egui_ctx.memory().clone();
|
|
|
|
self.egui_ctx.memory().set_everything_is_visible(true);
|
2021-01-02 13:42:43 +00:00
|
|
|
self.logic()?;
|
2021-11-03 12:45:51 +00:00
|
|
|
*self.egui_ctx.memory() = saved_memory; // We don't want to remember that windows were huge.
|
|
|
|
self.egui_ctx.clear_animations();
|
2021-01-02 13:42:43 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-07 18:53:33 +00:00
|
|
|
pub fn logic(&mut self) -> Result<(egui::Output, Vec<egui::ClippedMesh>), JsValue> {
|
2021-11-03 12:45:51 +00:00
|
|
|
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());
|
2021-06-07 18:53:33 +00:00
|
|
|
let raw_input = self.input.new_frame(canvas_size);
|
2021-04-01 20:11:29 +00:00
|
|
|
|
2021-11-03 19:11:25 +00:00
|
|
|
let (egui_output, shapes) = self.egui_ctx.run(raw_input, |egui_ctx| {
|
2021-12-26 20:21:28 +00:00
|
|
|
self.app.update(egui_ctx, &self.frame);
|
2021-11-03 19:11:25 +00:00
|
|
|
});
|
2021-11-03 12:45:51 +00:00
|
|
|
let clipped_meshes = self.egui_ctx.tessellate(shapes);
|
|
|
|
|
|
|
|
self.handle_egui_output(&egui_output);
|
2020-10-17 10:33:30 +00:00
|
|
|
|
|
|
|
{
|
2021-12-26 20:21:28 +00:00
|
|
|
let app_output = self.frame.take_app_output();
|
2020-12-31 13:31:11 +00:00
|
|
|
let epi::backend::AppOutput {
|
2021-11-03 12:45:51 +00:00
|
|
|
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
|
2021-12-26 20:21:28 +00:00
|
|
|
tex_allocation_data,
|
2020-10-17 21:54:46 +00:00
|
|
|
} = app_output;
|
2021-12-26 20:21:28 +00:00
|
|
|
|
|
|
|
for (id, image) in tex_allocation_data.creations {
|
|
|
|
self.painter.set_texture(id, image);
|
|
|
|
}
|
|
|
|
self.pending_texture_destructions = tex_allocation_data.destructions;
|
2020-10-17 10:33:30 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 20:21:28 +00:00
|
|
|
self.frame.lock().info.cpu_usage = Some((now_sec() - frame_start) as f32);
|
2021-01-25 20:43:17 +00:00
|
|
|
Ok((egui_output, clipped_meshes))
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 20:43:17 +00:00
|
|
|
pub fn paint(&mut self, clipped_meshes: Vec<egui::ClippedMesh>) -> Result<(), JsValue> {
|
2021-12-28 20:02:23 +00:00
|
|
|
self.painter
|
|
|
|
.upload_egui_texture(&self.egui_ctx.font_image());
|
2021-11-03 12:45:51 +00:00
|
|
|
self.painter.clear(self.app.clear_color());
|
|
|
|
self.painter
|
2021-12-26 20:21:28 +00:00
|
|
|
.paint_meshes(clipped_meshes, self.egui_ctx.pixels_per_point())?;
|
|
|
|
for id in self.pending_texture_destructions.drain(..) {
|
|
|
|
self.painter.free_texture(id);
|
|
|
|
}
|
|
|
|
Ok(())
|
2021-11-03 12:45:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-07-23 16:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 20:15:07 +00:00
|
|
|
/// Install event listeners to register different input events
|
2020-12-29 14:57:13 +00:00
|
|
|
/// and start running the given app.
|
2020-12-29 13:15:46 +00:00
|
|
|
pub fn start(canvas_id: &str, app: Box<dyn epi::App>) -> Result<AppRunnerRef, JsValue> {
|
2021-11-03 12:45:51 +00:00
|
|
|
let mut runner = AppRunner::new(canvas_id, app)?;
|
2021-01-02 13:42:43 +00:00
|
|
|
runner.warm_up()?;
|
2020-12-19 20:15:07 +00:00
|
|
|
start_runner(runner)
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
/// Install event listeners to register different input events
|
|
|
|
/// and starts running the given `AppRunner`.
|
2020-12-19 20:15:07 +00:00
|
|
|
fn start_runner(app_runner: AppRunner) -> Result<AppRunnerRef, JsValue> {
|
2020-07-23 16:54:16 +00:00
|
|
|
let runner_ref = AppRunnerRef(Arc::new(Mutex::new(app_runner)));
|
|
|
|
install_canvas_events(&runner_ref)?;
|
|
|
|
install_document_events(&runner_ref)?;
|
2021-03-26 12:56:26 +00:00
|
|
|
install_text_agent(&runner_ref)?;
|
2020-11-18 00:07:32 +00:00
|
|
|
repaint_every_ms(&runner_ref, 1000)?; // just in case. TODO: make it a parameter
|
2020-07-23 16:54:16 +00:00
|
|
|
paint_and_schedule(runner_ref.clone())?;
|
|
|
|
Ok(runner_ref)
|
|
|
|
}
|