Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Text fields stop resetting the input method every frame, so keys no longer repeat endlessly on Wayland (#585) (#723)

Ian Swopefe4243ba1fac
diff --git a/crates/ui-egui/src/channels_panel.rs b/crates/ui-egui/src/channels_panel.rs
index 3d1cfc98c..44fa4fd36 100644
--- a/crates/ui-egui/src/channels_panel.rs
+++ b/crates/ui-egui/src/channels_panel.rs
@@ -211,7 +211,9 @@ pub fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
if let Some(mut text) = ctx.data(|d| d.get_temp::<String>(rename_id)) {
let edit_rect = Rect::from_min_max(pos2(text_pos.x - 3.0, rect.center().y - 11.0), pos2(rect.right() - 36.0, rect.center().y + 11.0));
let te = ui.put(edit_rect, egui::TextEdit::singleline(&mut text).font(egui::FontId::proportional(12.5)));
- te.request_focus();
+ if !te.has_focus() && !te.lost_focus() {
+ te.request_focus();
+ }
let (enter, esc) = ui.input(|i| (i.key_pressed(egui::Key::Enter), i.key_pressed(egui::Key::Escape)));
if esc {
ctx.data_mut(|d| d.remove::<String>(rename_id));
@@ -380,4 +382,26 @@ mod tests {
assert_eq!(load_operation(m(false, true)), "subtract");
assert_eq!(load_operation(m(true, true)), "intersect");
}
+
+ #[test]
+ fn rename_field_interrupts_the_ime_only_when_it_takes_focus() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 32, "height": 32})).unwrap();
+ app.run("channel.new", json!({})).unwrap();
+ let ctx = egui::Context::default();
+ crate::theme::install_fonts(&ctx);
+ ctx.run_ui(Default::default(), |_| {}).textures_delta.clear();
+ let doc = app.session.active().unwrap().doc.id.0;
+ let name = app.session.active().unwrap().doc.channels[0].name.clone();
+ ctx.data_mut(|d| d.insert_temp(egui::Id::new(("chan-rename", doc, format!("{:?}", Row::Alpha(0).reference()))), name));
+ // The field takes focus on the first pass and owns the IME from the second on (#585).
+ let interrupts: Vec<Option<bool>> = (0..4)
+ .map(|_| {
+ let mut out = ctx.run_ui(Default::default(), |ui| show(&mut app, ui));
+ out.textures_delta.clear();
+ out.platform_output.ime.map(|ime| ime.should_interrupt_composition)
+ })
+ .collect();
+ assert_eq!(interrupts, [None, Some(false), Some(false), Some(false)]);
+ }
}
diff --git a/crates/ui-egui/src/palette.rs b/crates/ui-egui/src/palette.rs
index 3016039f8..c41d14296 100644
--- a/crates/ui-egui/src/palette.rs
+++ b/crates/ui-egui/src/palette.rs
@@ -76,7 +76,11 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
.font(egui::FontId::proportional(15.0))
.desired_width(width - 40.0);
let resp = ui.add(te);
- resp.request_focus();
+ // Only when focus is missing: every request_focus interrupts the IME, and
+ // doing that each frame makes Wayland input methods drop key releases (#585).
+ if !resp.has_focus() {
+ resp.request_focus();
+ }
});
ui.add_space(6.0);
crate::widgets::hairline(ui);
@@ -183,6 +187,25 @@ mod tests {
use super::fuzzy_score;
use crate::PhotocraftApp;
+ #[test]
+ fn open_palette_interrupts_the_ime_only_when_it_takes_focus() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ let ctx = egui::Context::default();
+ crate::theme::install_fonts(&ctx);
+ // Fonts bind on the next pass; this one opens nothing.
+ ctx.run_ui(Default::default(), |_| {}).textures_delta.clear();
+ app.ui.palette_open = true;
+ // The field takes focus on the first pass and owns the IME from the second on.
+ let interrupts: Vec<Option<bool>> = (0..4)
+ .map(|_| {
+ let mut out = ctx.run_ui(Default::default(), |ui| super::show(&mut app, ui.ctx()));
+ out.textures_delta.clear();
+ out.platform_output.ime.map(|ime| ime.should_interrupt_composition)
+ })
+ .collect();
+ assert_eq!(interrupts, [None, Some(false), Some(false), Some(false)]);
+ }
+
#[test]
fn fuzzy_ranks_prefix_and_contiguous_higher() {
assert!(fuzzy_score("hue", "Hue/Saturation…").unwrap() > fuzzy_score("hue", "Channel Mixer Hue").unwrap_or(-99));