ibus wayland fix

This commit is contained in:
Skyler Lehmkuhl
2026-02-16 03:36:31 -05:00
parent 978ec6c870
commit eb1756df3f
4 changed files with 378 additions and 18 deletions
+150 -18
View File
@@ -106,6 +106,14 @@ pub struct State {
allow_ime: bool,
ime_rect_px: Option<egui::Rect>,
/// When true, IME events are completely ignored and text input relies solely
/// on keyboard events. This is a workaround for systems where IME causes issues,
/// such as IBus on Wayland where each character triggers an IME commit that
/// disrupts normal typing flow.
///
/// See <https://github.com/emilk/egui/issues/7485>
ime_disabled: bool,
}
impl State {
@@ -148,6 +156,8 @@ impl State {
allow_ime: false,
ime_rect_px: None,
ime_disabled: should_disable_ime_for_buggy_systems(),
};
slf.egui_input
@@ -205,6 +215,30 @@ impl State {
self.allow_ime = allow;
}
/// Returns whether IME is disabled as a workaround for buggy systems.
///
/// When IME is disabled, text input relies solely on keyboard events.
/// This is automatically enabled on systems where IME causes issues,
/// such as IBus on Wayland.
///
/// See <https://github.com/emilk/egui/issues/7485>
pub fn ime_disabled(&self) -> bool {
self.ime_disabled
}
/// Explicitly enable or disable the IME workaround.
///
/// When set to `true`, IME events are ignored and text input relies
/// solely on keyboard events. This is useful for systems where IME
/// causes issues, such as IBus on Wayland.
///
/// By default, this is automatically detected based on the environment.
///
/// See <https://github.com/emilk/egui/issues/7485>
pub fn set_ime_disabled(&mut self, disabled: bool) {
self.ime_disabled = disabled;
}
#[inline]
pub fn egui_ctx(&self) -> &egui::Context {
&self.egui_ctx
@@ -350,6 +384,31 @@ impl State {
}
WindowEvent::Ime(ime) => {
// When IME is disabled as a workaround for buggy systems (like IBus on Wayland),
// we convert IME Commit events to regular Text events instead of using IME protocol.
// This works around the bug where IBus on Wayland sends each character as an
// IME commit which disrupts normal text editing flow.
// See https://github.com/emilk/egui/issues/7485
if self.ime_disabled {
// Only handle Commit events - convert them to regular Text events
if let winit::event::Ime::Commit(text) = ime {
if !text.is_empty() && text != "\n" && text != "\r" {
self.egui_input
.events
.push(egui::Event::Text(text.clone()));
return EventResponse {
repaint: true,
consumed: self.egui_ctx.wants_keyboard_input(),
};
}
}
// Ignore all other IME events (Enabled, Disabled, Preedit)
return EventResponse {
repaint: false,
consumed: false,
};
}
// on Mac even Cmd-C is pressed during ime, a `c` is pushed to Preedit.
// So no need to check is_mac_cmd.
//
@@ -907,7 +966,10 @@ impl State {
self.set_cursor_icon(window, cursor_icon);
let allow_ime = ime.is_some();
// When IME is disabled as a workaround for buggy systems, don't enable IME at all.
// This ensures the system doesn't try to intercept keyboard input.
// See https://github.com/emilk/egui/issues/7485
let allow_ime = ime.is_some() && !self.ime_disabled;
if self.allow_ime != allow_ime {
self.allow_ime = allow_ime;
profiling::scope!("set_ime_allowed");
@@ -915,23 +977,28 @@ impl State {
}
if let Some(ime) = ime {
let pixels_per_point = pixels_per_point(&self.egui_ctx, window);
let ime_rect_px = pixels_per_point * ime.rect;
if self.ime_rect_px != Some(ime_rect_px)
|| self.egui_ctx.input(|i| !i.events.is_empty())
{
self.ime_rect_px = Some(ime_rect_px);
profiling::scope!("set_ime_cursor_area");
window.set_ime_cursor_area(
winit::dpi::PhysicalPosition {
x: ime_rect_px.min.x,
y: ime_rect_px.min.y,
},
winit::dpi::PhysicalSize {
width: ime_rect_px.width(),
height: ime_rect_px.height(),
},
);
// Skip IME cursor positioning if IME is disabled
if self.ime_disabled {
self.ime_rect_px = None;
} else {
let pixels_per_point = pixels_per_point(&self.egui_ctx, window);
let ime_rect_px = pixels_per_point * ime.rect;
if self.ime_rect_px != Some(ime_rect_px)
|| self.egui_ctx.input(|i| !i.events.is_empty())
{
self.ime_rect_px = Some(ime_rect_px);
profiling::scope!("set_ime_cursor_area");
window.set_ime_cursor_area(
winit::dpi::PhysicalPosition {
x: ime_rect_px.min.x,
y: ime_rect_px.min.y,
},
winit::dpi::PhysicalSize {
width: ime_rect_px.width(),
height: ime_rect_px.height(),
},
);
}
}
} else {
self.ime_rect_px = None;
@@ -1073,6 +1140,71 @@ fn open_url_in_browser(_url: &str) {
}
}
/// Detects if we're running on a system where IME is known to cause issues.
///
/// Currently detects IBus on Wayland, which has a bug where each character
/// triggers an IME commit that disrupts normal typing flow.
///
/// The detection checks:
/// 1. We're on Linux
/// 2. We're running under Wayland (XDG_SESSION_TYPE=wayland or WAYLAND_DISPLAY is set)
/// 3. IBus is the active input method (GTK_IM_MODULE=ibus or IBUS_* env vars are set)
///
/// Users can override this by setting the environment variable:
/// - `EGUI_IME_DISABLED=1` to force IME disabled (use keyboard events only)
/// - `EGUI_IME_DISABLED=0` to force IME enabled (normal behavior)
///
/// See <https://github.com/emilk/egui/issues/7485>
fn should_disable_ime_for_buggy_systems() -> bool {
// Allow explicit override via environment variable
if let Ok(val) = std::env::var("EGUI_IME_DISABLED") {
return val == "1" || val.eq_ignore_ascii_case("true");
}
// Only applies to Linux
if !cfg!(target_os = "linux") {
return false;
}
// Check if we're on Wayland
let is_wayland = std::env::var("XDG_SESSION_TYPE")
.map(|v| v == "wayland")
.unwrap_or(false)
|| std::env::var("WAYLAND_DISPLAY").is_ok();
if !is_wayland {
return false;
}
// Check if IBus is the input method
// IBus can be detected via several environment variables:
// - GTK_IM_MODULE=ibus (GTK apps)
// - QT_IM_MODULE=ibus (Qt apps)
// - XMODIFIERS=@im=ibus (X11 input method)
// - IBUS_DAEMON_PID (set when ibus-daemon is running)
let is_ibus = std::env::var("GTK_IM_MODULE")
.map(|v| v == "ibus")
.unwrap_or(false)
|| std::env::var("QT_IM_MODULE")
.map(|v| v == "ibus")
.unwrap_or(false)
|| std::env::var("XMODIFIERS")
.map(|v| v.contains("ibus"))
.unwrap_or(false)
|| std::env::var("IBUS_DAEMON_PID").is_ok();
if is_ibus {
log::warn!(
"Detected IBus on Wayland - disabling IME to work around text input bug. \
Set EGUI_IME_DISABLED=0 to override. \
See https://github.com/emilk/egui/issues/7485"
);
return true;
}
false
}
/// Winit sends special keys (backspace, delete, F1, …) as characters.
/// Ignore those.
/// We also ignore '\r', '\n', '\t'.