[libhandy/tabs: 17/62] Another memory error fix




commit 569e189167ce7e2f69a05d2bd6eb6722ad74f2de
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Sat Sep 5 18:41:59 2020 +0500

    Another memory error fix
    
    Reset focus on the toplevel when it's not needed, otherwise we risk
    destroying it and leaving toplevel with an invalid widget.

 src/hdy-tab-bar.c |  4 ++--
 src/hdy-tab-box.c | 19 +++++++++++++++++--
 2 files changed, 19 insertions(+), 4 deletions(-)
---
diff --git a/src/hdy-tab-bar.c b/src/hdy-tab-bar.c
index 3a9500ec..250a64aa 100644
--- a/src/hdy-tab-bar.c
+++ b/src/hdy-tab-bar.c
@@ -115,9 +115,9 @@ page_pinned_cb (HdyTabBar  *self,
 {
   gboolean should_focus = hdy_tab_box_is_page_focused (self->scroll_box, page);
 
-  hdy_tab_box_remove_page (self->scroll_box, page);
   hdy_tab_box_add_page (self->pinned_box, page,
                         hdy_tab_view_get_n_pinned_pages (self->view));
+  hdy_tab_box_remove_page (self->scroll_box, page);
 
   if (should_focus)
     hdy_tab_box_try_focus_selected_tab (self->pinned_box);
@@ -129,9 +129,9 @@ page_unpinned_cb (HdyTabBar  *self,
 {
   gboolean should_focus = hdy_tab_box_is_page_focused (self->pinned_box, page);
 
-  hdy_tab_box_remove_page (self->pinned_box, page);
   hdy_tab_box_add_page (self->scroll_box, page,
                         hdy_tab_view_get_n_pinned_pages (self->view));
+  hdy_tab_box_remove_page (self->pinned_box, page);
 
   if (should_focus)
     hdy_tab_box_try_focus_selected_tab (self->scroll_box);
diff --git a/src/hdy-tab-box.c b/src/hdy-tab-box.c
index 59516dca..170591b0 100644
--- a/src/hdy-tab-box.c
+++ b/src/hdy-tab-box.c
@@ -1354,6 +1354,17 @@ end_dragging (HdyTabBox *self)
 
 /* Selection */
 
+static void
+reset_focus (HdyTabBox *self)
+{
+  GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (self));
+
+  gtk_container_set_focus_child (GTK_CONTAINER (self), NULL);
+
+  if (toplevel && GTK_IS_WINDOW (toplevel))
+    gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
+}
+
 static void
 select_page (HdyTabBox  *self,
              HdyTabPage *page)
@@ -1361,15 +1372,19 @@ select_page (HdyTabBox  *self,
   if (!page) {
     self->selected_tab = NULL;
 
-    gtk_container_set_focus_child (GTK_CONTAINER (self), NULL);
+    reset_focus (self);
 
     return;
   }
 
   self->selected_tab = find_info_for_page (self, page);
 
-  if (!self->selected_tab)
+  if (!self->selected_tab) {
+    if (gtk_container_get_focus_child (GTK_CONTAINER (self)))
+      reset_focus (self);
+
     return;
+  }
 
   if (hdy_tab_bar_tabs_have_visible_focus (self->tab_bar))
     gtk_widget_grab_focus (GTK_WIDGET (self->selected_tab->tab));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]