placesview: Use a popover

Replace the context menu with a popover.
This commit is contained in:
Matthias Clasen 2019-12-26 20:10:34 -05:00
parent 22f6787283
commit 94e238c467

View File

@ -29,6 +29,7 @@
#include "gtkplacesviewrowprivate.h" #include "gtkplacesviewrowprivate.h"
#include "gtktypebuiltins.h" #include "gtktypebuiltins.h"
#include "gtkeventcontrollerkey.h" #include "gtkeventcontrollerkey.h"
#include "gtkpopovermenu.h"
/* /*
* SECTION:gtkplacesview * SECTION:gtkplacesview
@ -86,6 +87,8 @@ struct _GtkPlacesViewPrivate
GCancellable *networks_fetching_cancellable; GCancellable *networks_fetching_cancellable;
GtkPlacesViewRow *row_for_action;
guint local_only : 1; guint local_only : 1;
guint should_open_location : 1; guint should_open_location : 1;
guint should_pulse_entry : 1; guint should_pulse_entry : 1;
@ -425,6 +428,17 @@ gtk_places_view_finalize (GObject *object)
G_OBJECT_CLASS (gtk_places_view_parent_class)->finalize (object); G_OBJECT_CLASS (gtk_places_view_parent_class)->finalize (object);
} }
static void
gtk_places_view_dispose (GObject *object)
{
GtkPlacesView *self = (GtkPlacesView *)object;
GtkPlacesViewPrivate *priv = gtk_places_view_get_instance_private (self);
g_clear_pointer (&priv->popup_menu, gtk_widget_unparent);
G_OBJECT_CLASS (gtk_places_view_parent_class)->dispose (object);
}
static void static void
gtk_places_view_get_property (GObject *object, gtk_places_view_get_property (GObject *object,
guint prop_id, guint prop_id,
@ -443,6 +457,10 @@ gtk_places_view_get_property (GObject *object,
g_value_set_boolean (value, gtk_places_view_get_loading (self)); g_value_set_boolean (value, gtk_places_view_get_loading (self));
break; break;
case PROP_OPEN_FLAGS:
g_value_set_flags (value, gtk_places_view_get_open_flags (self));
break;
case PROP_FETCHING_NETWORKS: case PROP_FETCHING_NETWORKS:
g_value_set_boolean (value, gtk_places_view_get_fetching_networks (self)); g_value_set_boolean (value, gtk_places_view_get_fetching_networks (self));
break; break;
@ -466,6 +484,10 @@ gtk_places_view_set_property (GObject *object,
gtk_places_view_set_local_only (self, g_value_get_boolean (value)); gtk_places_view_set_local_only (self, g_value_get_boolean (value));
break; break;
case PROP_OPEN_FLAGS:
gtk_places_view_set_open_flags (self, g_value_get_flags (value));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
} }
@ -1521,58 +1543,41 @@ mount_volume (GtkPlacesView *view,
g_object_unref (operation); g_object_unref (operation);
} }
/* Callback used when the file list's popup menu is detached */
static void static void
popup_menu_detach_cb (GtkWidget *attach_widget, open_cb (GtkWidget *widget,
GtkMenu *menu) const char *action_name,
GVariant *parameter)
{ {
GtkPlacesViewPrivate *priv; GtkPlacesView *self = GTK_PLACES_VIEW (widget);
GtkPlacesViewPrivate *priv = gtk_places_view_get_instance_private (self);
GtkPlacesOpenFlags flags = GTK_PLACES_OPEN_NORMAL;
priv = gtk_places_view_get_instance_private (GTK_PLACES_VIEW (attach_widget)); if (priv->row_for_action == NULL)
priv->popup_menu = NULL; return;
if (strcmp (action_name, "location.open") == 0)
flags = GTK_PLACES_OPEN_NORMAL;
else if (strcmp (action_name, "location.open-tab") == 0)
flags = GTK_PLACES_OPEN_NEW_TAB;
else if (strcmp (action_name, "location.open-window") == 0)
flags = GTK_PLACES_OPEN_NEW_WINDOW;
activate_row (self, priv->row_for_action, flags);
} }
static void static void
open_cb (GtkMenuItem *item, mount_cb (GtkWidget *widget,
GtkPlacesViewRow *row) const char *action_name,
GVariant *parameter)
{ {
GtkPlacesView *self; GtkPlacesView *self = GTK_PLACES_VIEW (widget);
GtkPlacesViewPrivate *priv = gtk_places_view_get_instance_private (self);
self = GTK_PLACES_VIEW (gtk_widget_get_ancestor (GTK_WIDGET (row), GTK_TYPE_PLACES_VIEW));
activate_row (self, row, GTK_PLACES_OPEN_NORMAL);
}
static void
open_in_new_tab_cb (GtkMenuItem *item,
GtkPlacesViewRow *row)
{
GtkPlacesView *self;
self = GTK_PLACES_VIEW (gtk_widget_get_ancestor (GTK_WIDGET (row), GTK_TYPE_PLACES_VIEW));
activate_row (self, row, GTK_PLACES_OPEN_NEW_TAB);
}
static void
open_in_new_window_cb (GtkMenuItem *item,
GtkPlacesViewRow *row)
{
GtkPlacesView *self;
self = GTK_PLACES_VIEW (gtk_widget_get_ancestor (GTK_WIDGET (row), GTK_TYPE_PLACES_VIEW));
activate_row (self, row, GTK_PLACES_OPEN_NEW_WINDOW);
}
static void
mount_cb (GtkMenuItem *item,
GtkPlacesViewRow *row)
{
GtkPlacesViewPrivate *priv;
GtkWidget *view;
GVolume *volume; GVolume *volume;
view = gtk_widget_get_ancestor (GTK_WIDGET (row), GTK_TYPE_PLACES_VIEW); if (priv->row_for_action == NULL)
priv = gtk_places_view_get_instance_private (GTK_PLACES_VIEW (view)); return;
volume = gtk_places_view_row_get_volume (row);
volume = gtk_places_view_row_get_volume (priv->row_for_action);
/* /*
* When the mount item is activated, it's expected that * When the mount item is activated, it's expected that
@ -1581,23 +1586,27 @@ mount_cb (GtkMenuItem *item,
*/ */
priv->should_open_location = FALSE; priv->should_open_location = FALSE;
gtk_places_view_row_set_busy (row, TRUE); gtk_places_view_row_set_busy (priv->row_for_action, TRUE);
mount_volume (GTK_PLACES_VIEW (view), volume); mount_volume (self, volume);
} }
static void static void
unmount_cb (GtkMenuItem *item, unmount_cb (GtkWidget *widget,
GtkPlacesViewRow *row) const char *action_name,
GVariant *parameter)
{ {
GtkWidget *view; GtkPlacesView *self = GTK_PLACES_VIEW (widget);
GtkPlacesViewPrivate *priv = gtk_places_view_get_instance_private (self);
GMount *mount; GMount *mount;
view = gtk_widget_get_ancestor (GTK_WIDGET (row), GTK_TYPE_PLACES_VIEW); if (priv->row_for_action == NULL)
mount = gtk_places_view_row_get_mount (row); return;
gtk_places_view_row_set_busy (row, TRUE); mount = gtk_places_view_row_get_mount (priv->row_for_action);
unmount_mount (GTK_PLACES_VIEW (view), mount); gtk_places_view_row_set_busy (priv->row_for_action, TRUE);
unmount_mount (self, mount);
} }
static void static void
@ -1646,88 +1655,58 @@ populate_available_protocols_grid (GtkGrid *grid)
attach_protocol_row_to_grid (grid, _("WebDAV"), _("dav:// or davs://")); attach_protocol_row_to_grid (grid, _("WebDAV"), _("dav:// or davs://"));
} }
/* Constructs the popup menu if needed */ static GMenuModel *
static void get_menu_model (void)
build_popup_menu (GtkPlacesView *view,
GtkPlacesViewRow *row)
{ {
GtkPlacesViewPrivate *priv; GMenu *menu;
GtkWidget *item; GMenu *section;
GMount *mount; GMenuItem *item;
GFile *file;
gboolean is_network;
priv = gtk_places_view_get_instance_private (view); menu = g_menu_new ();
mount = gtk_places_view_row_get_mount (row); section = g_menu_new ();
file = gtk_places_view_row_get_file (row); item = g_menu_item_new (_("_Open"), "location.open");
is_network = gtk_places_view_row_get_is_network (row); g_menu_append_item (section, item);
g_object_unref (item);
priv->popup_menu = gtk_menu_new (); item = g_menu_item_new (_("Open in New _Tab"), "location.open-tab");
gtk_style_context_add_class (gtk_widget_get_style_context (priv->popup_menu), g_menu_item_set_attribute (item, "hidden-when", "s", "action-disabled");
GTK_STYLE_CLASS_CONTEXT_MENU); g_menu_append_item (section, item);
g_object_unref (item);
gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu), item = g_menu_item_new (_("Open in New _Window"), "location.open-window");
GTK_WIDGET (view), g_menu_item_set_attribute (item, "hidden-when", "s", "action-disabled");
popup_menu_detach_cb); g_menu_append_item (section, item);
g_object_unref (item);
/* Open item is always present */ g_menu_append_section (menu, NULL, G_MENU_MODEL (section));
item = gtk_menu_item_new_with_mnemonic (_("_Open")); g_object_unref (section);
g_signal_connect (item,
"activate",
G_CALLBACK (open_cb),
row);
gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), item);
if (priv->open_flags & GTK_PLACES_OPEN_NEW_TAB) section = g_menu_new ();
{ item = g_menu_item_new (_("_Disconnect"), "location.disconnect");
item = gtk_menu_item_new_with_mnemonic (_("Open in New _Tab")); g_menu_item_set_attribute (item, "hidden-when", "s", "action-disabled");
g_signal_connect (item, g_menu_append_item (section, item);
"activate", g_object_unref (item);
G_CALLBACK (open_in_new_tab_cb),
row);
gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), item);
}
if (priv->open_flags & GTK_PLACES_OPEN_NEW_WINDOW) item = g_menu_item_new (_("_Unmount"), "location.unmount");
{ g_menu_item_set_attribute (item, "hidden-when", "s", "action-disabled");
item = gtk_menu_item_new_with_mnemonic (_("Open in New _Window")); g_menu_append_item (section, item);
g_signal_connect (item, g_object_unref (item);
"activate",
G_CALLBACK (open_in_new_window_cb),
row);
gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), item);
}
/*
* The only item that contains a file up to now is the Computer
* item, which cannot be mounted or unmounted.
*/
if (file)
return;
/* Separator */ item = g_menu_item_new (_("_Connect"), "location.connect");
item = gtk_separator_menu_item_new (); g_menu_item_set_attribute (item, "hidden-when", "s", "action-disabled");
gtk_menu_shell_insert (GTK_MENU_SHELL (priv->popup_menu), item, -1); g_menu_append_item (section, item);
g_object_unref (item);
/* Mount/Unmount items */ item = g_menu_item_new (_("_Mount"), "location.mount");
if (mount) g_menu_item_set_attribute (item, "hidden-when", "s", "action-disabled");
{ g_menu_append_item (section, item);
item = gtk_menu_item_new_with_mnemonic (is_network ? _("_Disconnect") : _("_Unmount")); g_object_unref (item);
g_signal_connect (item,
"activate", g_menu_append_section (menu, NULL, G_MENU_MODEL (section));
G_CALLBACK (unmount_cb), g_object_unref (section);
row);
gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), item); return G_MENU_MODEL (menu);
}
else
{
item = gtk_menu_item_new_with_mnemonic (is_network ? _("_Connect") : _("_Mount"));
g_signal_connect (item,
"activate",
G_CALLBACK (mount_cb),
row);
gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), item);
}
} }
static void static void
@ -1736,15 +1715,44 @@ popup_menu (GtkPlacesViewRow *row,
{ {
GtkPlacesViewPrivate *priv; GtkPlacesViewPrivate *priv;
GtkWidget *view; GtkWidget *view;
GMount *mount;
GFile *file;
gboolean is_network;
view = gtk_widget_get_ancestor (GTK_WIDGET (row), GTK_TYPE_PLACES_VIEW); view = gtk_widget_get_ancestor (GTK_WIDGET (row), GTK_TYPE_PLACES_VIEW);
priv = gtk_places_view_get_instance_private (GTK_PLACES_VIEW (view)); priv = gtk_places_view_get_instance_private (GTK_PLACES_VIEW (view));
g_clear_pointer (&priv->popup_menu, gtk_widget_destroy); mount = gtk_places_view_row_get_mount (row);
file = gtk_places_view_row_get_file (row);
is_network = gtk_places_view_row_get_is_network (row);
build_popup_menu (GTK_PLACES_VIEW (view), row); gtk_widget_action_set_enabled (GTK_WIDGET (view), "location.disconnect",
!file && mount && is_network);
gtk_widget_action_set_enabled (GTK_WIDGET (view), "location.unmount",
!file && mount && !is_network);
gtk_widget_action_set_enabled (GTK_WIDGET (view), "location.connect",
!file && !mount && is_network);
gtk_widget_action_set_enabled (GTK_WIDGET (view), "location.mount",
!file && !mount && !is_network);
gtk_menu_popup_at_pointer (GTK_MENU (priv->popup_menu), (GdkEvent *) event); if (!priv->popup_menu)
{
GMenuModel *model = get_menu_model ();
priv->popup_menu = gtk_popover_menu_new_from_model (GTK_WIDGET (view), model);
gtk_popover_set_position (GTK_POPOVER (priv->popup_menu), GTK_POS_BOTTOM);
gtk_popover_set_has_arrow (GTK_POPOVER (priv->popup_menu), FALSE);
gtk_widget_set_halign (priv->popup_menu, GTK_ALIGN_START);
g_object_unref (model);
}
gtk_widget_set_halign (priv->popup_menu, GTK_ALIGN_CENTER);
gtk_popover_set_relative_to (GTK_POPOVER (priv->popup_menu), GTK_WIDGET (row));
priv->row_for_action = row;
gtk_popover_popup (GTK_POPOVER (priv->popup_menu));
} }
static gboolean static gboolean
@ -2236,6 +2244,7 @@ gtk_places_view_class_init (GtkPlacesViewClass *klass)
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->finalize = gtk_places_view_finalize; object_class->finalize = gtk_places_view_finalize;
object_class->dispose = gtk_places_view_dispose;
object_class->constructed = gtk_places_view_constructed; object_class->constructed = gtk_places_view_constructed;
object_class->get_property = gtk_places_view_get_property; object_class->get_property = gtk_places_view_get_property;
object_class->set_property = gtk_places_view_set_property; object_class->set_property = gtk_places_view_set_property;
@ -2344,6 +2353,14 @@ gtk_places_view_class_init (GtkPlacesViewClass *klass)
gtk_widget_class_bind_template_callback (widget_class, on_listbox_row_activated); gtk_widget_class_bind_template_callback (widget_class, on_listbox_row_activated);
gtk_widget_class_bind_template_callback (widget_class, on_recent_servers_listbox_row_activated); gtk_widget_class_bind_template_callback (widget_class, on_recent_servers_listbox_row_activated);
gtk_widget_class_install_action (widget_class, "location.open", NULL, open_cb);
gtk_widget_class_install_action (widget_class, "location.open-tab", NULL, open_cb);
gtk_widget_class_install_action (widget_class, "location.open-window", NULL, open_cb);
gtk_widget_class_install_action (widget_class, "location.mount", NULL, mount_cb);
gtk_widget_class_install_action (widget_class, "location.connect", NULL, mount_cb);
gtk_widget_class_install_action (widget_class, "location.unmount", NULL, unmount_cb);
gtk_widget_class_install_action (widget_class, "location.disconnect", NULL, unmount_cb);
gtk_widget_class_set_css_name (widget_class, I_("placesview")); gtk_widget_class_set_css_name (widget_class, I_("placesview"));
} }
@ -2360,6 +2377,9 @@ gtk_places_view_init (GtkPlacesView *self)
priv->path_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); priv->path_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
priv->space_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL); priv->space_size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
gtk_widget_action_set_enabled (GTK_WIDGET (self), "location.open-tab", FALSE);
gtk_widget_action_set_enabled (GTK_WIDGET (self), "location.open-window", FALSE);
gtk_widget_init_template (GTK_WIDGET (self)); gtk_widget_init_template (GTK_WIDGET (self));
controller = gtk_event_controller_key_new (); controller = gtk_event_controller_key_new ();
@ -2427,11 +2447,17 @@ gtk_places_view_set_open_flags (GtkPlacesView *view,
priv = gtk_places_view_get_instance_private (view); priv = gtk_places_view_get_instance_private (view);
if (priv->open_flags != flags) if (priv->open_flags == flags)
{ return;
priv->open_flags = flags; priv->open_flags = flags;
gtk_widget_action_set_enabled (GTK_WIDGET (view), "location.open-tab",
(flags & GTK_PLACES_OPEN_NEW_TAB) != 0);
gtk_widget_action_set_enabled (GTK_WIDGET (view), "location.open-window",
(flags & GTK_PLACES_OPEN_NEW_WINDOW) != 0);
g_object_notify_by_pspec (G_OBJECT (view), properties[PROP_OPEN_FLAGS]); g_object_notify_by_pspec (G_OBJECT (view), properties[PROP_OPEN_FLAGS]);
}
} }
/* /*