From fbd1ccd6a97b8fd48789a6f63d7426914f22d689 Mon Sep 17 00:00:00 2001 From: umajho Date: Thu, 11 Jun 2026 17:38:39 +0800 Subject: [PATCH] Refactor: move Windows Korean IME workaround from `egui` to `egui-winit` The bug is specific to `egui-winit` and is unlikely present in other implementations. Applying the workaround in `egui` degrades implementations that already behave correctly. --- crates/egui-winit/src/lib.rs | 15 ++++++++++++++- crates/egui/src/text_selection/visuals.rs | 14 -------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index c31fc1373..908ac8ff3 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -706,7 +706,20 @@ impl State { text.get(..start_bytes).map(|s| s.chars().count()), text.get(start_bytes..end_bytes).map(|s| s.chars().count()), ) { - Some(start_chars..start_chars + middle_chars) + if cfg!(target_os = "windows") && start_chars == 0 && middle_chars == 0 + { + // Workaround for a bug on Windows where `winit` + // incorrectly reports the cursor position at + // the start of the preedit text during + // composition with the builtin Korean IME. + // See: https://github.com/emilk/egui/pull/8083#issuecomment-4206742668 + // TODO(umajho): Remove this workaround once the + // `winit` bug is fixed and we've updated to a + // version that includes the fix. + None + } else { + Some(start_chars..start_chars + middle_chars) + } } else { log::warn!("ignoring {ime:?}'s range because it is invalid"); None diff --git a/crates/egui/src/text_selection/visuals.rs b/crates/egui/src/text_selection/visuals.rs index a716a7f83..28f329599 100644 --- a/crates/egui/src/text_selection/visuals.rs +++ b/crates/egui/src/text_selection/visuals.rs @@ -161,20 +161,6 @@ pub(crate) fn paint_ime_preedit_text_visuals( relative_active_range.end.index = preedit_range.end.index - preedit_range.start.index; } - if matches!(ui.ctx().os(), crate::os::OperatingSystem::Windows) - && let Some(r) = &relative_active_range - && r.start.index == 0 - && r.end.index == 0 - { - // Workaround for a bug on Windows where `winit` incorrectly reports - // the cursor position at the start of the preedit text during - // composition with the builtin Korean IME. - // See: https://github.com/emilk/egui/pull/8083#issuecomment-4206742668 - // TODO(umajho): Remove this workaround once the `winit` bug is fixed - // and we've updated to a version that includes the fix. - relative_active_range = None; - } - let visuals = ui.visuals(); let active_underline_stroke = visuals.ime_composition.active_underline_stroke; let inactive_underline_stroke = visuals.ime_composition.inactive_underline_stroke;