1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Remove Modifiers from RawInput and make it a egui::Event (#8336)

- needs #8335 

Previously it was impossible to correctly set modifiers via egui
inspection, since that only passes egui::Event and egui::Event had no
way to generally express modifiers.
Now modifiers are passed to egui as a Event and not as a field of
`RawInput`.

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
Lucas Meurer
2026-07-27 16:17:22 +02:00
committed by GitHub
parent e6eb00a31c
commit 2cb071f7f6
8 changed files with 106 additions and 69 deletions

View File

@@ -84,6 +84,13 @@ pub struct State {
viewport_id: ViewportId,
start_time: web_time::Instant,
egui_input: egui::RawInput,
/// The current modifier state.
///
/// We keep a copy so we can stamp
/// it onto per-event `modifiers` fields and emit [`egui::Event::ModifiersChanged`].
modifiers: egui::Modifiers,
pointer_pos_in_points: Option<egui::Pos2>,
any_pointer_button_down: bool,
current_cursor_icon: Option<egui::CursorIcon>,
@@ -146,6 +153,7 @@ impl State {
.unwrap_or_else(web_time::Instant::now),
egui_ctx,
egui_input,
modifiers: egui::Modifiers::default(),
pointer_pos_in_points: None,
any_pointer_button_down: false,
current_cursor_icon: None,
@@ -422,6 +430,10 @@ impl State {
};
self.egui_input.focused = focused;
if !focused {
// Avoid sticky modifiers when focus is lost (egui clears its own copy too).
self.modifiers = egui::Modifiers::default();
}
self.egui_input
.events
.push(egui::Event::WindowFocused(focused));
@@ -473,16 +485,20 @@ impl State {
let shift = state.shift_key();
let super_ = state.super_key();
self.egui_input.modifiers.alt = alt;
self.egui_input.modifiers.ctrl = ctrl;
self.egui_input.modifiers.shift = shift;
self.egui_input.modifiers.mac_cmd = cfg!(target_os = "macos") && super_;
self.egui_input.modifiers.command = if cfg!(target_os = "macos") {
self.modifiers.alt = alt;
self.modifiers.ctrl = ctrl;
self.modifiers.shift = shift;
self.modifiers.mac_cmd = cfg!(target_os = "macos") && super_;
self.modifiers.command = if cfg!(target_os = "macos") {
super_
} else {
ctrl
};
self.egui_input
.events
.push(egui::Event::ModifiersChanged(self.modifiers));
EventResponse {
repaint: true,
consumed: false,
@@ -541,7 +557,7 @@ impl State {
unit: egui::MouseWheelUnit::Point,
delta: Vec2::new(delta.x, delta.y) / pixels_per_point,
phase: to_egui_touch_phase(*phase),
modifiers: self.egui_input.modifiers,
modifiers: self.modifiers,
});
EventResponse {
repaint: true,
@@ -790,7 +806,7 @@ impl State {
pos,
button,
pressed,
modifiers: self.egui_input.modifiers,
modifiers: self.modifiers,
});
if self.simulate_touch_screen {
@@ -937,7 +953,7 @@ impl State {
),
};
let phase = to_egui_touch_phase(phase);
let modifiers = self.egui_input.modifiers;
let modifiers = self.modifiers;
self.egui_input.events.push(egui::Event::MouseWheel {
unit,
delta,
@@ -998,13 +1014,13 @@ impl State {
// See also: https://github.com/emilk/egui/issues/3653
if let Some(active_key) = logical_key.or(physical_key) {
if pressed {
if is_cut_command(self.egui_input.modifiers, active_key) {
if is_cut_command(self.modifiers, active_key) {
self.egui_input.events.push(egui::Event::Cut);
return;
} else if is_copy_command(self.egui_input.modifiers, active_key) {
} else if is_copy_command(self.modifiers, active_key) {
self.egui_input.events.push(egui::Event::Copy);
return;
} else if is_paste_command(self.egui_input.modifiers, active_key) {
} else if is_paste_command(self.modifiers, active_key) {
if let Some(contents) = self.clipboard.get() {
let contents = contents.replace("\r\n", "\n");
if !contents.is_empty() {
@@ -1020,7 +1036,7 @@ impl State {
physical_key,
pressed,
repeat: false, // egui will fill this in for us!
modifiers: self.egui_input.modifiers,
modifiers: self.modifiers,
});
}
@@ -1036,9 +1052,8 @@ impl State {
// We need to ignore these characters that are side-effects of commands.
// Also make sure the key is pressed (not released). On Linux, text might
// contain some data even when the key is released.
let is_cmd = self.egui_input.modifiers.ctrl
|| self.egui_input.modifiers.command
|| self.egui_input.modifiers.mac_cmd;
let is_cmd =
self.modifiers.ctrl || self.modifiers.command || self.modifiers.mac_cmd;
if pressed && !is_cmd {
self.egui_input
.events