From ab544baf9b7fc39d671f922e827a4f2d6179a7f7 Mon Sep 17 00:00:00 2001 From: Rubén Beltrán del Río Date: Sun, 18 Jan 2026 23:16:54 +0100 Subject: Fix bug where focus is lost when editing maps --- src/preferences/pages/templates.rs | 71 +++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/preferences/pages/templates.rs b/src/preferences/pages/templates.rs index 1760085..029b3e9 100644 --- a/src/preferences/pages/templates.rs +++ b/src/preferences/pages/templates.rs @@ -177,11 +177,37 @@ impl TemplatesPage { } pub fn refresh(&self, prefs: &UserPreferences) { - // Clear existing rows - while let Some(child) = self.list_box.first_child() { - self.list_box.remove(&child); + // Check if we need to recreate rows (templates added/removed/reordered) + let current_row_count = { + let mut count = 0; + let mut child = self.list_box.first_child(); + while child.is_some() { + count += 1; + child = child.and_then(|c| c.next_sibling()); + } + count + }; + + let needs_recreate = current_row_count != prefs.map_templates.len(); + + if needs_recreate { + // Clear existing rows + while let Some(child) = self.list_box.first_child() { + self.list_box.remove(&child); + } + self.populate_templates(prefs); + } else { + // Just update existing rows in place + let mut child = self.list_box.first_child(); + let mut index = 0; + while let Some(row) = child { + if let Some(template) = prefs.map_templates.get(index) { + TemplatesPage::update_template_row(&row, template); + } + child = row.next_sibling(); + index += 1; + } } - self.populate_templates(prefs); // Update editor if selected template still exists let selected = *self.selected_id.borrow(); @@ -236,6 +262,43 @@ impl TemplatesPage { } } + fn update_template_row(row: >k::Widget, template: &Template) { + if let Some(list_row) = row.downcast_ref::() + && let Some(hbox) = list_row.child().and_then(|c| c.downcast::().ok()) + { + let mut child = hbox.first_child(); + let mut widget_index = 0; + + while let Some(widget) = child { + if widget_index == 0 { + // Update default button + if let Some(button) = widget.downcast_ref::() { + if template.is_default { + button.set_icon_name("emblem-default-symbolic"); + button + .set_tooltip_text(Some(&tr!("preferences.templates.help.default"))); + } else { + button.set_icon_name("radio-symbolic"); + button.set_tooltip_text(Some(&tr!( + "preferences.templates.help.set_as_default" + ))); + } + } + } else if widget_index == 1 { + // Update name entry + if let Some(entry) = widget.downcast_ref::() { + // Only update if different to avoid cursor reset + if entry.text() != template.name { + entry.set_text(&template.name); + } + } + } + child = widget.next_sibling(); + widget_index += 1; + } + } + } + fn create_template_row(&self, template: &Template) -> gtk::ListBoxRow { let row = gtk::ListBoxRow::new(); let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 10); -- cgit