mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 05:40:03 -04:00
Rename should_propagate_event & add should_prevent_default (#5779)
* [x] I have followed the instructions in the PR template Currently eframe [calls `prevent_default()`](962c7c7516/crates/eframe/src/web/events.rs (L307-L369)) for all copy / paste events on the [*document*](962c7c7516/crates/eframe/src/web/events.rs (L88)), making embedding an egui application in a page (e.g. an react application) hard (as all copy & paste functionality for other elements on the page is broken by this). I'm not sure what the motivation for this is, if any. This commit / PR adds a callback (`should_prevent_default`), similar to `should_propgate_event`, that an egui application can use to overwrite this behavior. It defaults to returning `true` for all events, to keep the existing behavior. I call `should_prevent_default` in every place that `should_propagate_event` is called (which is not all places that `prevent_default` is called!). I'm not sure for the motivation of not calling `should_propagate_event` everywhere that `stop_propagation` is called, but I kept that behavior for the `should_prevent_default` callback too. Please let me know if I'm missing some existing functionality that would allow me to do this, or if there's a reason that we don't want applications to be able to customize this (i.e. if there's a reason to always `prevent_default` for all copy / paste events on the whole document)
This commit is contained in:
@@ -499,10 +499,16 @@ pub struct WebOptions {
|
||||
/// If the web event corresponding to an egui event should be propagated
|
||||
/// to the rest of the web page.
|
||||
///
|
||||
/// The default is `false`, meaning
|
||||
/// The default is `true`, meaning
|
||||
/// [`stopPropagation`](https://developer.mozilla.org/en-US/docs/Web/API/Event/stopPropagation)
|
||||
/// is called on every event.
|
||||
pub should_propagate_event: Box<dyn Fn(&egui::Event) -> bool>,
|
||||
/// is called on every event, and the event is not propagated to the rest of the web page.
|
||||
pub should_stop_propagation: Box<dyn Fn(&egui::Event) -> bool>,
|
||||
|
||||
/// Whether the web event corresponding to an egui event should have `prevent_default` called
|
||||
/// on it or not.
|
||||
///
|
||||
/// Defaults to true.
|
||||
pub should_prevent_default: Box<dyn Fn(&egui::Event) -> bool>,
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
@@ -519,7 +525,8 @@ impl Default for WebOptions {
|
||||
|
||||
dithering: true,
|
||||
|
||||
should_propagate_event: Box::new(|_| false),
|
||||
should_stop_propagation: Box::new(|_| true),
|
||||
should_prevent_default: Box::new(|_| true),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user