From 1935d564d4d8b8f009931d41a5e7c3b175594ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 9 May 2025 10:19:55 +0200 Subject: [PATCH] message-toolbar: Fix inserting mention before another mention --- .../message_toolbar/composer_state.rs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/session/view/content/room_history/message_toolbar/composer_state.rs b/src/session/view/content/room_history/message_toolbar/composer_state.rs index f2739607..a9c0d72c 100644 --- a/src/session/view/content/room_history/message_toolbar/composer_state.rs +++ b/src/session/view/content/room_history/message_toolbar/composer_state.rs @@ -236,18 +236,19 @@ mod imp { /// Add the given widget at the position of the given iter to this /// state. - pub(super) fn add_widget(&self, widget: impl IsA, iter: &mut gtk::TextIter) { - let widget = widget.upcast(); - - let anchor = match iter.child_anchor() { - Some(anchor) => anchor, - None => self.buffer.create_child_anchor(iter), + pub(super) fn add_widget(&self, widget: gtk::Widget, iter: &mut gtk::TextIter) { + let Some(view) = self.view.upgrade() else { + return; }; - if let Some(view) = self.view.upgrade() { - view.add_child_at_anchor(&widget, &anchor); - } + // Reuse the child anchor at the iter if it does not have a child widget, + // otherwise create a new one. + let anchor = iter + .child_anchor() + .filter(|anchor| self.widget_at_anchor(anchor).is_none()) + .unwrap_or_else(|| self.buffer.create_child_anchor(iter)); + view.add_child_at_anchor(&widget, &anchor); self.widgets.borrow_mut().push((widget, anchor)); } @@ -311,7 +312,7 @@ mod imp { match DraftMention::new(&room, &text[content_start..content_end]) { DraftMention::Source(source) => { - self.add_widget(source.to_pill(), &mut end_iter); + self.add_widget(source.to_pill().upcast(), &mut end_iter); } DraftMention::Text(s) => { self.buffer.insert(&mut end_iter, s); @@ -410,7 +411,7 @@ mod imp { self.buffer.insert(&mut iter, &text[pos..start]); } - self.add_widget(pill, &mut iter); + self.add_widget(pill.upcast(), &mut iter); pos = end; } @@ -489,7 +490,7 @@ impl ComposerState { /// Add the given widget at the position of the given iter to this state. pub(crate) fn add_widget(&self, widget: impl IsA, iter: &mut gtk::TextIter) { - self.imp().add_widget(widget, iter); + self.imp().add_widget(widget.upcast(), iter); } /// Get the widget at the given anchor, if any.