From 96ae24bb90baa2de341e7bf7355b28266ae79577 Mon Sep 17 00:00:00 2001 From: YgorSouza <43298013+YgorSouza@users.noreply.github.com> Date: Tue, 6 Oct 2026 09:42:22 +0200 Subject: [PATCH] Add option to turn off the default clipboard keyboard shortcuts in egui-winit and eframe (#5615) * Closes * Closes * [x] I have followed the instructions in the PR template --------- Co-authored-by: Emil Ernerfeldt --- crates/eframe/src/epi.rs | 21 ++++++++++++ crates/eframe/src/native/glow_integration.rs | 10 ++++-- crates/eframe/src/native/wgpu_integration.rs | 33 ++++++++++++++++--- crates/egui-winit/src/lib.rs | 34 +++++++++++++++++++- 4 files changed, 90 insertions(+), 8 deletions(-) diff --git a/crates/eframe/src/epi.rs b/crates/eframe/src/epi.rs index 7511815f..50d0d99b 100644 --- a/crates/eframe/src/epi.rs +++ b/crates/eframe/src/epi.rs @@ -403,6 +403,25 @@ pub struct NativeOptions { /// Defaults to true. pub dithering: bool, + /// Should the platform clipboard keyboard shortcuts + /// (e.g. Cmd/Ctrl+X/C/V) + /// be translated into [`egui::Event::Cut`], [`egui::Event::Copy`] and [`egui::Event::Paste`]? + /// + /// Set this to `false` if you want to handle these key combinations yourself. + /// They will then arrive as ordinary [`egui::Event::Key`] events instead. + /// Note that built-in widgets such as [`egui::TextEdit`] will then no longer + /// respond to these shortcuts. + /// + /// This applies to all viewports. + /// See also `egui_winit::State::set_clipboard_shortcuts`. + /// + /// There is no web equivalent: on the web, Cut/Copy/Paste come from the browser's + /// clipboard events (which can also be triggered from e.g. the browser menu), + /// and the key presses are always also delivered as [`egui::Event::Key`]. + /// + /// Defaults to true. + pub clipboard_shortcuts: bool, + /// Android application for `winit`'s event loop. /// /// This value is required on Android to correctly create the event loop. See @@ -480,6 +499,8 @@ impl Default for NativeOptions { dithering: true, + clipboard_shortcuts: true, + #[cfg(target_os = "android")] android_app: None, } diff --git a/crates/eframe/src/native/glow_integration.rs b/crates/eframe/src/native/glow_integration.rs index 9401a075..5a3182b1 100644 --- a/crates/eframe/src/native/glow_integration.rs +++ b/crates/eframe/src/native/glow_integration.rs @@ -108,6 +108,9 @@ struct GlutinWindowContext { max_texture_side: Option, + /// Passed on to each [`egui_winit::State`]. See [`NativeOptions::clipboard_shortcuts`]. + clipboard_shortcuts: bool, + current_gl_context: Option, not_current_gl_context: Option, @@ -1300,6 +1303,7 @@ impl GlutinWindowContext { viewports, viewport_from_window, max_texture_side: None, + clipboard_shortcuts: native_options.clipboard_shortcuts, window_from_viewport, focused_viewport: Some(ViewportId::ROOT), }; @@ -1354,14 +1358,16 @@ impl GlutinWindowContext { viewport.egui_winit.get_or_insert_with(|| { log::debug!("Initializing egui_winit for viewport {viewport_id:?}"); - egui_winit::State::new( + let mut egui_winit = egui_winit::State::new( self.egui_ctx.clone(), viewport_id, event_loop, Some(window.scale_factor() as f32), event_loop.system_theme(), self.max_texture_side, - ) + ); + egui_winit.set_clipboard_shortcuts(self.clipboard_shortcuts); + egui_winit }); if viewport.gl_surface.is_none() { diff --git a/crates/eframe/src/native/wgpu_integration.rs b/crates/eframe/src/native/wgpu_integration.rs index 88f1236a..e2a12f1a 100644 --- a/crates/eframe/src/native/wgpu_integration.rs +++ b/crates/eframe/src/native/wgpu_integration.rs @@ -90,6 +90,9 @@ pub struct SharedState { viewport_from_window: HashMap, focused_viewport: Option, resized_viewport: Option, + + /// Passed on to each [`egui_winit::State`]. See [`NativeOptions::clipboard_shortcuts`]. + clipboard_shortcuts: bool, } pub type Viewports = egui::OrderedViewportIdMap; @@ -161,6 +164,7 @@ impl<'app> WgpuWinitApp<'app> { viewports, painter, viewport_from_window, + clipboard_shortcuts, .. } = &mut *shared; @@ -170,6 +174,7 @@ impl<'app> WgpuWinitApp<'app> { &running.integration.egui_ctx, viewport_from_window, painter, + *clipboard_shortcuts, ); } } @@ -181,6 +186,7 @@ impl<'app> WgpuWinitApp<'app> { viewports, viewport_from_window, painter, + clipboard_shortcuts, .. } = &mut *running.shared.borrow_mut(); @@ -192,7 +198,13 @@ impl<'app> WgpuWinitApp<'app> { None, painter, ) - .initialize_window(event_loop, egui_ctx, viewport_from_window, painter); + .initialize_window( + event_loop, + egui_ctx, + viewport_from_window, + painter, + *clipboard_shortcuts, + ); } #[cfg(target_os = "android")] @@ -294,7 +306,6 @@ impl<'app> WgpuWinitApp<'app> { }); } - #[allow(clippy::allow_attributes, unused_mut)] // used for accesskit let mut egui_winit = egui_winit::State::new( egui_ctx.clone(), ViewportId::ROOT, @@ -303,6 +314,7 @@ impl<'app> WgpuWinitApp<'app> { event_loop.system_theme(), painter.max_texture_side(), ); + egui_winit.set_clipboard_shortcuts(self.native_options.clipboard_shortcuts); #[cfg(feature = "accesskit")] { @@ -360,6 +372,7 @@ impl<'app> WgpuWinitApp<'app> { painter, focused_viewport: Some(ViewportId::ROOT), resized_viewport: None, + clipboard_shortcuts: self.native_options.clipboard_shortcuts, })); { @@ -1076,6 +1089,7 @@ impl Viewport { egui_ctx: &egui::Context, windows_id: &mut HashMap, painter: &mut egui_wgpu::winit::Painter, + clipboard_shortcuts: bool, ) { if self.window.is_some() { return; // we already have one @@ -1097,14 +1111,16 @@ impl Viewport { log::error!("on set_window: viewport_id {viewport_id:?} {err}"); } - self.egui_winit = Some(egui_winit::State::new( + let mut egui_winit = egui_winit::State::new( egui_ctx.clone(), viewport_id, event_loop, Some(window.scale_factor() as f32), event_loop.system_theme(), painter.max_texture_side(), - )); + ); + egui_winit.set_clipboard_shortcuts(clipboard_shortcuts); + self.egui_winit = Some(egui_winit); egui_winit::update_viewport_info(&mut self.info, egui_ctx, &window, true); self.window = Some(window); @@ -1176,6 +1192,7 @@ fn render_immediate_viewport( viewports, painter, viewport_from_window, + clipboard_shortcuts, .. } = &mut *shared.borrow_mut(); @@ -1189,7 +1206,13 @@ fn render_immediate_viewport( ); if viewport.window.is_none() { event_loop_context::with_current_event_loop(|event_loop| { - viewport.initialize_window(event_loop, egui_ctx, viewport_from_window, painter); + viewport.initialize_window( + event_loop, + egui_ctx, + viewport_from_window, + painter, + *clipboard_shortcuts, + ); }); } diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 30599ae5..cec06289 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -129,6 +129,10 @@ pub struct State { pub accesskit: Option, allow_ime: bool, + + /// See [`Self::set_clipboard_shortcuts`]. + clipboard_shortcuts: bool, + ime_rect_px: Option, old_ime_purpose: egui::IMEPurpose, @@ -190,6 +194,7 @@ impl State { accesskit: None, allow_ime: false, + clipboard_shortcuts: true, ime_rect_px: None, old_ime_purpose: egui::IMEPurpose::Normal, #[cfg(target_os = "macos")] @@ -259,6 +264,33 @@ impl State { self.allow_ime = allow; } + /// Are clipboard keyboard shortcuts translated into clipboard events? + /// + /// See [`Self::set_clipboard_shortcuts`]. + #[inline] + pub fn clipboard_shortcuts(&self) -> bool { + self.clipboard_shortcuts + } + + /// Should the platform clipboard keyboard shortcuts be translated into + /// [`egui::Event::Cut`], [`egui::Event::Copy`] and [`egui::Event::Paste`]? + /// + /// The shortcuts are Cmd/Ctrl+X/C/V, + /// the dedicated Cut/Copy/Paste keys, and on Windows also + /// Shift+Delete, Ctrl+Insert and Shift+Insert. + /// + /// If `false`, these key presses are instead passed on as ordinary [`egui::Event::Key`] events, + /// so your app can bind them to whatever it wants. + /// Note that this means built-in widgets such as [`egui::TextEdit`] will no longer + /// respond to these shortcuts, since they listen for the clipboard events. + /// You can still push [`egui::Event::Cut`] etc. yourself. + /// + /// Default: `true`. + #[inline] + pub fn set_clipboard_shortcuts(&mut self, enabled: bool) { + self.clipboard_shortcuts = enabled; + } + #[inline] pub fn egui_ctx(&self) -> &egui::Context { &self.egui_ctx @@ -1083,7 +1115,7 @@ impl State { // are mapped to the physical keys that normally contain C, X, V, etc. // See also: https://github.com/emilk/egui/issues/3653 if let Some(active_key) = logical_key.or(physical_key) { - if pressed { + if pressed && self.clipboard_shortcuts { if is_cut_command(self.modifiers, active_key) { self.egui_input.events.push(egui::Event::Cut); return;