2021-01-02 10:59:20 +00:00
|
|
|
//! [`egui`] bindings for web apps (compiling to WASM).
|
|
|
|
//!
|
|
|
|
//! This library is an [`epi`] backend.
|
|
|
|
//!
|
|
|
|
//! If you are writing an app, you may want to look at [`eframe`](https://docs.rs/eframe) instead.
|
|
|
|
|
2022-03-21 15:54:29 +00:00
|
|
|
#![allow(clippy::missing_errors_doc)] // So many `-> Result<_, JsValue>`
|
2019-01-12 22:07:30 +00:00
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub mod backend;
|
2022-04-29 06:17:49 +00:00
|
|
|
mod events;
|
2021-11-03 18:17:07 +00:00
|
|
|
mod glow_wrapping;
|
2022-02-21 08:23:02 +00:00
|
|
|
mod input;
|
2021-03-08 19:58:01 +00:00
|
|
|
pub mod screen_reader;
|
2022-04-29 06:17:49 +00:00
|
|
|
pub mod storage;
|
2022-02-21 08:23:02 +00:00
|
|
|
mod text_agent;
|
2021-12-31 14:17:55 +00:00
|
|
|
|
2020-07-23 16:54:16 +00:00
|
|
|
pub use backend::*;
|
2022-04-29 06:17:49 +00:00
|
|
|
pub use events::*;
|
|
|
|
pub use storage::*;
|
2020-07-23 16:54:16 +00:00
|
|
|
|
2022-03-10 07:13:32 +00:00
|
|
|
use egui::mutex::{Mutex, MutexGuard};
|
2020-12-29 14:57:13 +00:00
|
|
|
pub use wasm_bindgen;
|
|
|
|
pub use web_sys;
|
|
|
|
|
2022-02-21 08:23:02 +00:00
|
|
|
use input::*;
|
2022-03-10 07:13:32 +00:00
|
|
|
use web_sys::EventTarget;
|
2022-02-21 08:23:02 +00:00
|
|
|
|
2022-02-17 15:46:43 +00:00
|
|
|
use std::collections::BTreeMap;
|
2022-03-10 07:13:32 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2020-07-18 16:35:17 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
|
2019-02-11 19:27:32 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2020-11-17 23:43:58 +00:00
|
|
|
|
2020-11-20 19:35:16 +00:00
|
|
|
/// Current time in seconds (since undefined point in time)
|
2019-02-11 19:27:32 +00:00
|
|
|
pub fn now_sec() -> f64 {
|
|
|
|
web_sys::window()
|
|
|
|
.expect("should have a Window")
|
|
|
|
.performance()
|
|
|
|
.expect("should have a Performance")
|
|
|
|
.now()
|
|
|
|
/ 1000.0
|
|
|
|
}
|
|
|
|
|
2020-10-17 21:54:46 +00:00
|
|
|
pub fn screen_size_in_native_points() -> Option<egui::Vec2> {
|
|
|
|
let window = web_sys::window()?;
|
2022-01-21 09:48:44 +00:00
|
|
|
Some(egui::vec2(
|
2020-10-17 21:54:46 +00:00
|
|
|
window.inner_width().ok()?.as_f64()? as f32,
|
|
|
|
window.inner_height().ok()?.as_f64()? as f32,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn native_pixels_per_point() -> f32 {
|
2020-07-18 08:54:31 +00:00
|
|
|
let pixels_per_point = web_sys::window().unwrap().device_pixel_ratio() as f32;
|
|
|
|
if pixels_per_point > 0.0 && pixels_per_point.is_finite() {
|
|
|
|
pixels_per_point
|
|
|
|
} else {
|
|
|
|
1.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-07 18:56:18 +00:00
|
|
|
pub fn prefer_dark_mode() -> Option<bool> {
|
|
|
|
Some(
|
|
|
|
web_sys::window()?
|
|
|
|
.match_media("(prefers-color-scheme: dark)")
|
|
|
|
.ok()??
|
|
|
|
.matches(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-18 16:00:05 +00:00
|
|
|
pub fn canvas_element(canvas_id: &str) -> Option<web_sys::HtmlCanvasElement> {
|
2020-07-18 08:54:31 +00:00
|
|
|
use wasm_bindgen::JsCast;
|
|
|
|
let document = web_sys::window()?.document()?;
|
|
|
|
let canvas = document.get_element_by_id(canvas_id)?;
|
2020-07-18 16:00:05 +00:00
|
|
|
canvas.dyn_into::<web_sys::HtmlCanvasElement>().ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn canvas_element_or_die(canvas_id: &str) -> web_sys::HtmlCanvasElement {
|
|
|
|
crate::canvas_element(canvas_id)
|
|
|
|
.unwrap_or_else(|| panic!("Failed to find canvas with id '{}'", canvas_id))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
fn canvas_origin(canvas_id: &str) -> egui::Pos2 {
|
|
|
|
let rect = canvas_element(canvas_id)
|
|
|
|
.unwrap()
|
|
|
|
.get_bounding_client_rect();
|
|
|
|
egui::Pos2::new(rect.left() as f32, rect.top() as f32)
|
|
|
|
}
|
|
|
|
|
2020-12-18 21:51:23 +00:00
|
|
|
pub fn canvas_size_in_points(canvas_id: &str) -> egui::Vec2 {
|
|
|
|
let canvas = canvas_element(canvas_id).unwrap();
|
|
|
|
let pixels_per_point = native_pixels_per_point();
|
|
|
|
egui::vec2(
|
|
|
|
canvas.width() as f32 / pixels_per_point,
|
|
|
|
canvas.height() as f32 / pixels_per_point,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-12 16:57:53 +00:00
|
|
|
pub fn resize_canvas_to_screen_size(canvas_id: &str, max_size_points: egui::Vec2) -> Option<()> {
|
2020-07-18 16:00:05 +00:00
|
|
|
let canvas = canvas_element(canvas_id)?;
|
2020-07-18 08:54:31 +00:00
|
|
|
|
2020-12-18 21:51:23 +00:00
|
|
|
let screen_size_points = screen_size_in_native_points()?;
|
2020-10-17 21:54:46 +00:00
|
|
|
let pixels_per_point = native_pixels_per_point();
|
2020-12-18 21:51:23 +00:00
|
|
|
|
2021-02-12 16:57:53 +00:00
|
|
|
let max_size_pixels = pixels_per_point * max_size_points;
|
|
|
|
|
2020-12-18 21:51:23 +00:00
|
|
|
let canvas_size_pixels = pixels_per_point * screen_size_points;
|
|
|
|
let canvas_size_pixels = canvas_size_pixels.min(max_size_pixels);
|
|
|
|
let canvas_size_points = canvas_size_pixels / pixels_per_point;
|
|
|
|
|
2021-02-01 19:44:39 +00:00
|
|
|
// Make sure that the height and width are always even numbers.
|
|
|
|
// otherwise, the page renders blurry on some platforms.
|
|
|
|
// See https://github.com/emilk/egui/issues/103
|
|
|
|
fn round_to_even(v: f32) -> f32 {
|
|
|
|
(v / 2.0).round() * 2.0
|
|
|
|
}
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
canvas
|
|
|
|
.style()
|
2021-02-01 19:44:39 +00:00
|
|
|
.set_property(
|
|
|
|
"width",
|
|
|
|
&format!("{}px", round_to_even(canvas_size_points.x)),
|
|
|
|
)
|
2020-07-18 08:54:31 +00:00
|
|
|
.ok()?;
|
|
|
|
canvas
|
|
|
|
.style()
|
2021-02-01 19:44:39 +00:00
|
|
|
.set_property(
|
|
|
|
"height",
|
|
|
|
&format!("{}px", round_to_even(canvas_size_points.y)),
|
|
|
|
)
|
2020-07-18 08:54:31 +00:00
|
|
|
.ok()?;
|
2021-02-01 19:44:39 +00:00
|
|
|
canvas.set_width(round_to_even(canvas_size_pixels.x) as u32);
|
|
|
|
canvas.set_height(round_to_even(canvas_size_pixels.y) as u32);
|
2020-07-18 08:54:31 +00:00
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2020-12-18 21:51:23 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
pub fn set_cursor_icon(cursor: egui::CursorIcon) -> Option<()> {
|
|
|
|
let document = web_sys::window()?.document()?;
|
|
|
|
document
|
|
|
|
.body()?
|
|
|
|
.style()
|
|
|
|
.set_property("cursor", cursor_web_name(cursor))
|
|
|
|
.ok()
|
|
|
|
}
|
|
|
|
|
2020-12-29 11:42:15 +00:00
|
|
|
#[cfg(web_sys_unstable_apis)]
|
2020-11-15 19:55:41 +00:00
|
|
|
pub fn set_clipboard_text(s: &str) {
|
|
|
|
if let Some(window) = web_sys::window() {
|
2021-08-15 14:55:33 +00:00
|
|
|
if let Some(clipboard) = window.navigator().clipboard() {
|
|
|
|
let promise = clipboard.write_text(s);
|
|
|
|
let future = wasm_bindgen_futures::JsFuture::from(promise);
|
|
|
|
let future = async move {
|
|
|
|
if let Err(err) = future.await {
|
2022-02-01 11:27:39 +00:00
|
|
|
tracing::error!("Copy/cut action denied: {:?}", err);
|
2021-08-15 14:55:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
wasm_bindgen_futures::spawn_local(future);
|
|
|
|
}
|
2020-11-15 19:55:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 23:43:58 +00:00
|
|
|
pub fn spawn_future<F>(future: F)
|
|
|
|
where
|
|
|
|
F: std::future::Future<Output = ()> + 'static,
|
|
|
|
{
|
|
|
|
wasm_bindgen_futures::spawn_local(future);
|
|
|
|
}
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
fn cursor_web_name(cursor: egui::CursorIcon) -> &'static str {
|
|
|
|
match cursor {
|
2021-03-13 11:38:03 +00:00
|
|
|
egui::CursorIcon::Alias => "alias",
|
|
|
|
egui::CursorIcon::AllScroll => "all-scroll",
|
|
|
|
egui::CursorIcon::Cell => "cell",
|
|
|
|
egui::CursorIcon::ContextMenu => "context-menu",
|
|
|
|
egui::CursorIcon::Copy => "copy",
|
|
|
|
egui::CursorIcon::Crosshair => "crosshair",
|
|
|
|
egui::CursorIcon::Default => "default",
|
|
|
|
egui::CursorIcon::Grab => "grab",
|
|
|
|
egui::CursorIcon::Grabbing => "grabbing",
|
|
|
|
egui::CursorIcon::Help => "help",
|
|
|
|
egui::CursorIcon::Move => "move",
|
|
|
|
egui::CursorIcon::NoDrop => "no-drop",
|
|
|
|
egui::CursorIcon::None => "none",
|
|
|
|
egui::CursorIcon::NotAllowed => "not-allowed",
|
|
|
|
egui::CursorIcon::PointingHand => "pointer",
|
|
|
|
egui::CursorIcon::Progress => "progress",
|
|
|
|
egui::CursorIcon::ResizeHorizontal => "ew-resize",
|
|
|
|
egui::CursorIcon::ResizeNeSw => "nesw-resize",
|
|
|
|
egui::CursorIcon::ResizeNwSe => "nwse-resize",
|
|
|
|
egui::CursorIcon::ResizeVertical => "ns-resize",
|
2022-04-19 14:56:27 +00:00
|
|
|
|
|
|
|
egui::CursorIcon::ResizeEast => "e-resize",
|
|
|
|
egui::CursorIcon::ResizeSouthEast => "se-resize",
|
|
|
|
egui::CursorIcon::ResizeSouth => "s-resize",
|
|
|
|
egui::CursorIcon::ResizeSouthWest => "sw-resize",
|
|
|
|
egui::CursorIcon::ResizeWest => "w-resize",
|
|
|
|
egui::CursorIcon::ResizeNorthWest => "nw-resize",
|
|
|
|
egui::CursorIcon::ResizeNorth => "n-resize",
|
|
|
|
egui::CursorIcon::ResizeNorthEast => "ne-resize",
|
|
|
|
egui::CursorIcon::ResizeColumn => "col-resize",
|
|
|
|
egui::CursorIcon::ResizeRow => "row-resize",
|
|
|
|
|
2021-03-13 11:38:03 +00:00
|
|
|
egui::CursorIcon::Text => "text",
|
|
|
|
egui::CursorIcon::VerticalText => "vertical-text",
|
|
|
|
egui::CursorIcon::Wait => "wait",
|
|
|
|
egui::CursorIcon::ZoomIn => "zoom-in",
|
|
|
|
egui::CursorIcon::ZoomOut => "zoom-out",
|
2020-07-18 08:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 16:48:23 +00:00
|
|
|
pub fn open_url(url: &str, new_tab: bool) -> Option<()> {
|
|
|
|
let name = if new_tab { "_blank" } else { "_self" };
|
|
|
|
|
2020-07-18 08:54:31 +00:00
|
|
|
web_sys::window()?
|
2021-03-08 16:48:23 +00:00
|
|
|
.open_with_url_and_target(url, name)
|
2020-07-18 08:54:31 +00:00
|
|
|
.ok()?;
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:46:43 +00:00
|
|
|
/// e.g. "#fragment" part of "www.example.com/index.html#fragment",
|
|
|
|
///
|
|
|
|
/// Percent decoded
|
|
|
|
pub fn location_hash() -> String {
|
|
|
|
percent_decode(
|
|
|
|
&web_sys::window()
|
|
|
|
.unwrap()
|
|
|
|
.location()
|
|
|
|
.hash()
|
|
|
|
.unwrap_or_default(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn percent_decode(s: &str) -> String {
|
|
|
|
percent_encoding::percent_decode_str(s)
|
|
|
|
.decode_utf8_lossy()
|
|
|
|
.to_string()
|
2020-07-18 08:54:31 +00:00
|
|
|
}
|
2020-07-18 16:00:05 +00:00
|
|
|
|
2020-07-18 16:35:17 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2022-01-10 22:19:30 +00:00
|
|
|
pub(crate) fn webgl1_requires_brightening(gl: &web_sys::WebGlRenderingContext) -> bool {
|
|
|
|
// See https://github.com/emilk/egui/issues/794
|
|
|
|
|
|
|
|
// detect WebKitGTK
|
|
|
|
|
|
|
|
// WebKitGTK use WebKit default unmasked vendor and renderer
|
|
|
|
// but safari use same vendor and renderer
|
|
|
|
// so exclude "Mac OS X" user-agent.
|
|
|
|
let user_agent = web_sys::window().unwrap().navigator().user_agent().unwrap();
|
|
|
|
!user_agent.contains("Mac OS X") && crate::is_safari_and_webkit_gtk(gl)
|
|
|
|
}
|
|
|
|
|
2022-03-21 15:54:29 +00:00
|
|
|
/// detecting Safari and `webkitGTK`.
|
2021-12-31 14:17:55 +00:00
|
|
|
///
|
2022-03-21 15:54:29 +00:00
|
|
|
/// Safari and `webkitGTK` use unmasked renderer :Apple GPU
|
2021-12-31 14:17:55 +00:00
|
|
|
///
|
2022-03-21 15:54:29 +00:00
|
|
|
/// If we detect safari or `webkitGTKs` returns true.
|
2021-12-31 14:17:55 +00:00
|
|
|
///
|
|
|
|
/// This function used to avoid displaying linear color with `sRGB` supported systems.
|
2022-01-10 22:19:30 +00:00
|
|
|
fn is_safari_and_webkit_gtk(gl: &web_sys::WebGlRenderingContext) -> bool {
|
|
|
|
// This call produces a warning in Firefox ("WEBGL_debug_renderer_info is deprecated in Firefox and will be removed.")
|
|
|
|
// but unless we call it we get errors in Chrome when we call `get_parameter` below.
|
|
|
|
// TODO: do something smart based on user agent?
|
|
|
|
if gl
|
|
|
|
.get_extension("WEBGL_debug_renderer_info")
|
|
|
|
.unwrap()
|
|
|
|
.is_some()
|
2021-12-31 14:17:55 +00:00
|
|
|
{
|
2022-01-10 22:19:30 +00:00
|
|
|
if let Ok(renderer) =
|
|
|
|
gl.get_parameter(web_sys::WebglDebugRendererInfo::UNMASKED_RENDERER_WEBGL)
|
|
|
|
{
|
|
|
|
if let Some(renderer) = renderer.as_string() {
|
|
|
|
if renderer.contains("Apple") {
|
|
|
|
return true;
|
|
|
|
}
|
2021-12-31 14:17:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|