[nautilus] placessidebar: Point to row instead of reparenting
- From: António Fernandes <antoniof src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [nautilus] placessidebar: Point to row instead of reparenting
- Date: Sat, 7 May 2022 11:17:05 +0000 (UTC)
commit 978099000af9068a08cde9e26bb79e16e8203be3
Author: António Fernandes <antoniof gnome org>
Date: Sat May 7 11:25:41 2022 +0100
placessidebar: Point to row instead of reparenting
In GTK 3 we used to move the popover around using set_relative_to();
this is gone in GTK 4 and the apparent direct replacement is setting
the target widget as the new parent.
But this requires a lot of careful handling least the popover become
orfan, which gets us ready to crash at any moment.
Since we only care about positioning the popover relative to a row,
let's use the set_pointing_to() instead of reparenting. Now, the
sidebar is always the parent.
src/gtk/nautilusgtkplacessidebar.c | 59 +++++++++++++++++++++-----------------
1 file changed, 33 insertions(+), 26 deletions(-)
---
diff --git a/src/gtk/nautilusgtkplacessidebar.c b/src/gtk/nautilusgtkplacessidebar.c
index 021871f1a..78ac9c492 100644
--- a/src/gtk/nautilusgtkplacessidebar.c
+++ b/src/gtk/nautilusgtkplacessidebar.c
@@ -2216,16 +2216,6 @@ rename_entry_changed (GtkEntry *entry,
found ? _("This name is already taken") : "");
}
-static void
-reparent_popover (GtkWidget *widget,
- GtkWidget *parent)
-{
- g_object_ref (widget);
- gtk_widget_unparent (widget);
- gtk_widget_set_parent (widget, parent);
- g_object_unref (widget);
-}
-
static void
do_rename (GtkButton *button,
NautilusGtkPlacesSidebar *sidebar)
@@ -2242,8 +2232,6 @@ do_rename (GtkButton *button,
if (sidebar->rename_popover)
{
gtk_popover_popdown (GTK_POPOVER (sidebar->rename_popover));
- reparent_popover (sidebar->rename_popover, GTK_WIDGET (sidebar));
- reparent_popover (sidebar->popover, GTK_WIDGET (sidebar));
}
_nautilus_gtk_bookmarks_manager_set_bookmark_label (sidebar->bookmarks_manager, file, new_text, NULL);
@@ -2351,22 +2339,41 @@ update_popover_shadowing (GtkWidget *row,
}
static void
-set_prelight (GtkPopover *popover)
+set_prelight (NautilusGtkPlacesSidebar *sidebar)
+{
+ update_popover_shadowing (GTK_WIDGET (sidebar->context_row), TRUE);
+}
+
+static void
+unset_prelight (NautilusGtkPlacesSidebar *sidebar)
{
- update_popover_shadowing (gtk_widget_get_parent (GTK_WIDGET (popover)), TRUE);
+ update_popover_shadowing (GTK_WIDGET (sidebar->context_row), FALSE);
}
static void
-unset_prelight (GtkPopover *popover)
+setup_popover_shadowing (GtkWidget *popover,
+ NautilusGtkPlacesSidebar *sidebar)
{
- update_popover_shadowing (gtk_widget_get_parent (GTK_WIDGET (popover)), FALSE);
+ g_signal_connect_swapped (popover, "map", G_CALLBACK (set_prelight), sidebar);
+ g_signal_connect_swapped (popover, "unmap", G_CALLBACK (unset_prelight), sidebar);
}
static void
-setup_popover_shadowing (GtkWidget *popover)
+_popover_set_pointing_to_widget (GtkPopover *popover,
+ GtkWidget *target)
{
- g_signal_connect (popover, "map", G_CALLBACK (set_prelight), NULL);
- g_signal_connect (popover, "unmap", G_CALLBACK (unset_prelight), NULL);
+ GtkWidget *parent;
+ double x, y, w, h;
+
+ parent = gtk_widget_get_parent (GTK_WIDGET (popover));
+
+ if (!gtk_widget_translate_coordinates (target, parent, 0, 0, &x, &y))
+ return;
+
+ w = gtk_widget_get_allocated_width (GTK_WIDGET (target));
+ h = gtk_widget_get_allocated_height (GTK_WIDGET (target));
+
+ gtk_popover_set_pointing_to (popover, &(GdkRectangle){x, y, w, h});
}
static void
@@ -2389,9 +2396,11 @@ show_rename_popover (NautilusGtkSidebarRow *row)
sidebar->rename_uri = g_strdup (uri);
gtk_editable_set_text (GTK_EDITABLE (sidebar->rename_entry), name);
- reparent_popover (sidebar->rename_popover, GTK_WIDGET (row));
- setup_popover_shadowing (sidebar->rename_popover);
+ _popover_set_pointing_to_widget (GTK_POPOVER (sidebar->rename_popover),
+ GTK_WIDGET (row));
+
+ setup_popover_shadowing (sidebar->rename_popover, sidebar);
gtk_popover_popup (GTK_POPOVER (sidebar->rename_popover));
gtk_widget_grab_focus (sidebar->rename_entry);
@@ -2467,7 +2476,6 @@ remove_bookmark (NautilusGtkSidebarRow *row)
if (type == NAUTILUS_GTK_PLACES_BOOKMARK)
{
- reparent_popover (sidebar->popover, GTK_WIDGET (sidebar));
file = g_file_new_for_uri (uri);
_nautilus_gtk_bookmarks_manager_remove_bookmark (sidebar->bookmarks_manager, file, NULL);
g_object_unref (file);
@@ -3391,9 +3399,10 @@ create_row_popover (NautilusGtkPlacesSidebar *sidebar,
sidebar->popover = gtk_popover_menu_new_from_model (G_MENU_MODEL (menu));
g_object_unref (menu);
+ gtk_widget_set_parent (sidebar->popover, GTK_WIDGET (sidebar));
g_signal_connect (sidebar->popover, "destroy", G_CALLBACK (on_row_popover_destroy), sidebar);
- setup_popover_shadowing (sidebar->popover);
+ setup_popover_shadowing (sidebar->popover, sidebar);
}
static void
@@ -3403,11 +3412,9 @@ show_row_popover (NautilusGtkSidebarRow *row)
g_object_get (row, "sidebar", &sidebar, NULL);
- g_clear_pointer (&sidebar->popover, gtk_widget_unparent);
-
create_row_popover (sidebar, row);
- gtk_widget_set_parent (sidebar->popover, GTK_WIDGET (row));
+ _popover_set_pointing_to_widget (GTK_POPOVER (sidebar->popover), GTK_WIDGET (row));
sidebar->context_row = row;
gtk_popover_popup (GTK_POPOVER (sidebar->popover));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]