Skip to content

Commit cd0f66b

Browse files
authored
eframe web: ctrl-P and cmd-P will not open the print dialog (#2598)
1 parent 60b4f5e commit cd0f66b

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

crates/eframe/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ NOTE: [`egui-winit`](../egui-winit/CHANGELOG.md), [`egui_glium`](../egui_glium/C
99
#### Desktop/Native:
1010
* `eframe::run_native` now returns a `Result` ([#2433](https://github.com/emilk/egui/pull/2433)).
1111

12+
#### Web:
13+
* Prevent ctrl-P/cmd-P from opening the print dialog ([#2598](https://github.com/emilk/egui/pull/2598)).
14+
1215

1316
## 0.20.1 - 2022-12-11
1417
* Fix docs.rs build ([#2420](https://github.com/emilk/egui/pull/2420)).

crates/eframe/src/web/events.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use super::*;
21
use std::sync::atomic::{AtomicBool, Ordering};
32

3+
use egui::Key;
4+
5+
use super::*;
6+
47
struct IsDestroyed(pub bool);
58

69
pub fn paint_and_schedule(
@@ -64,8 +67,9 @@ pub fn install_document_events(runner_container: &mut AppRunnerContainer) -> Res
6467
runner_lock.input.raw.modifiers = modifiers;
6568

6669
let key = event.key();
70+
let egui_key = translate_key(&key);
6771

68-
if let Some(key) = translate_key(&key) {
72+
if let Some(key) = egui_key {
6973
runner_lock.input.raw.events.push(egui::Event::Key {
7074
key,
7175
pressed: true,
@@ -85,10 +89,13 @@ pub fn install_document_events(runner_container: &mut AppRunnerContainer) -> Res
8589

8690
let egui_wants_keyboard = runner_lock.egui_ctx().wants_keyboard_input();
8791

88-
let prevent_default = if matches!(event.key().as_str(), "Tab") {
92+
#[allow(clippy::if_same_then_else)]
93+
let prevent_default = if egui_key == Some(Key::Tab) {
8994
// Always prevent moving cursor to url bar.
9095
// egui wants to use tab to move to the next text field.
9196
true
97+
} else if egui_key == Some(Key::P) {
98+
true // Prevent ctrl-P opening the print dialog. Users may want to use it for a command palette.
9299
} else if egui_wants_keyboard {
93100
matches!(
94101
event.key().as_str(),
@@ -112,6 +119,7 @@ pub fn install_document_events(runner_container: &mut AppRunnerContainer) -> Res
112119

113120
if prevent_default {
114121
event.prevent_default();
122+
// event.stop_propagation();
115123
}
116124
},
117125
)?;
@@ -198,15 +206,21 @@ pub fn install_document_events(runner_container: &mut AppRunnerContainer) -> Res
198206
pub fn install_canvas_events(runner_container: &mut AppRunnerContainer) -> Result<(), JsValue> {
199207
let canvas = canvas_element(runner_container.runner.lock().canvas_id()).unwrap();
200208

201-
{
209+
let prevent_default_events = [
202210
// By default, right-clicks open a context menu.
203211
// We don't want to do that (right clicks is handled by egui):
204-
let event_name = "contextmenu";
212+
"contextmenu",
213+
// Allow users to use ctrl-p for e.g. a command palette
214+
"afterprint",
215+
];
205216

217+
for event_name in prevent_default_events {
206218
let closure =
207219
move |event: web_sys::MouseEvent,
208220
mut _runner_lock: egui::mutex::MutexGuard<'_, AppRunner>| {
209221
event.prevent_default();
222+
// event.stop_propagation();
223+
// tracing::debug!("Preventing event {:?}", event_name);
210224
};
211225

212226
runner_container.add_event_listener(&canvas, event_name, closure)?;

0 commit comments

Comments
 (0)