Merge branch 'wip/carlosg/for-master' into 'master'

Cascade-close popovers by default

Closes #3200

See merge request GNOME/gtk!2685
This commit is contained in:
Matthias Clasen 2020-10-13 19:25:59 +00:00
commit 18b6c4e69d
5 changed files with 98 additions and 31 deletions

View File

@ -6150,6 +6150,8 @@ gtk_popover_get_has_arrow
gtk_popover_set_offset
gtk_popover_get_offset
gtk_popover_set_default_widget
gtk_popover_set_cascade_popdown
gtk_popover_get_cascade_popdown
<SUBSECTION Standard>
GTK_TYPE_POPOVER
GTK_IS_POPOVER

View File

@ -939,22 +939,6 @@ switch_menu (GtkModelButton *button)
gtk_stack_set_visible_child_name (GTK_STACK (stack), button->menu_name);
}
static void
close_menu (GtkModelButton *self)
{
GtkWidget *popover;
popover = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_POPOVER);
while (popover != NULL)
{
gtk_popover_popdown (GTK_POPOVER (popover));
if (GTK_IS_POPOVER_MENU (popover))
popover = gtk_popover_menu_get_parent_menu (GTK_POPOVER_MENU (popover));
else
popover = NULL;
}
}
static void
gtk_model_button_clicked (GtkModelButton *self)
{
@ -975,7 +959,11 @@ gtk_model_button_clicked (GtkModelButton *self)
}
else if (self->role == GTK_BUTTON_ROLE_NORMAL)
{
close_menu (self);
GtkWidget *popover;
popover = gtk_widget_get_ancestor (GTK_WIDGET (self), GTK_TYPE_POPOVER);
if (popover)
gtk_popover_popdown (GTK_POPOVER (popover));
}
if (self->action_helper)

View File

@ -149,6 +149,7 @@ typedef struct {
gboolean has_arrow;
gboolean mnemonics_visible;
gboolean disable_auto_mnemonics;
gboolean cascade_popdown;
int x_offset;
int y_offset;
@ -181,6 +182,7 @@ enum {
PROP_HAS_ARROW,
PROP_MNEMONICS_VISIBLE,
PROP_CHILD,
PROP_CASCADE_POPDOWN,
NUM_PROPERTIES
};
@ -594,19 +596,6 @@ gtk_popover_native_check_resize (GtkNative *native)
present_popup (popover);
}
static void
close_menu (GtkPopover *popover)
{
while (popover)
{
gtk_popover_popdown (popover);
if (GTK_IS_POPOVER_MENU (popover))
popover = (GtkPopover *)gtk_popover_menu_get_parent_menu (GTK_POPOVER_MENU (popover));
else
popover = NULL;
}
}
static gboolean
gtk_popover_has_mnemonic_modifier_pressed (GtkPopover *popover)
{
@ -718,7 +707,7 @@ gtk_popover_key_pressed (GtkWidget *widget,
if (keyval == GDK_KEY_Escape)
{
close_menu (popover);
gtk_popover_popdown (popover);
return TRUE;
}
@ -852,6 +841,7 @@ gtk_popover_init (GtkPopover *popover)
priv->final_position = GTK_POS_BOTTOM;
priv->autohide = TRUE;
priv->has_arrow = TRUE;
priv->cascade_popdown = TRUE;
controller = gtk_event_controller_key_new ();
g_signal_connect_swapped (controller, "key-pressed", G_CALLBACK (gtk_popover_key_pressed), popover);
@ -1479,6 +1469,10 @@ gtk_popover_set_property (GObject *object,
gtk_popover_set_child (popover, g_value_get_object (value));
break;
case PROP_CASCADE_POPDOWN:
gtk_popover_set_cascade_popdown (popover, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -1524,6 +1518,10 @@ gtk_popover_get_property (GObject *object,
g_value_set_object (value, gtk_popover_get_child (popover));
break;
case PROP_CASCADE_POPDOWN:
g_value_set_boolean (value, priv->cascade_popdown);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -1674,6 +1672,13 @@ gtk_popover_class_init (GtkPopoverClass *klass)
GTK_TYPE_WIDGET,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
properties[PROP_CASCADE_POPDOWN] =
g_param_spec_boolean ("cascade-popdown",
P_("Cascade popdown"),
P_("Wether the popover pops down after a child popover"),
TRUE,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, NUM_PROPERTIES, properties);
signals[CLOSED] =
@ -2047,6 +2052,31 @@ gtk_popover_popup (GtkPopover *popover)
gtk_widget_show (GTK_WIDGET (popover));
}
static void
cascade_popdown (GtkPopover *popover)
{
GtkWidget *parent;
/* Do not trigger cascade close from non-modal popovers */
if (!gtk_popover_get_autohide (popover))
return;
parent = gtk_widget_get_parent (GTK_WIDGET (popover));
while (parent)
{
if (GTK_IS_POPOVER (parent))
{
if (gtk_popover_get_cascade_popdown (GTK_POPOVER (parent)))
gtk_widget_hide (parent);
else
break;
}
parent = gtk_widget_get_parent (parent);
}
}
/**
* gtk_popover_popdown:
* @popover: a #GtkPopover
@ -2061,6 +2091,8 @@ gtk_popover_popdown (GtkPopover *popover)
g_return_if_fail (GTK_IS_POPOVER (popover));
gtk_widget_hide (GTK_WIDGET (popover));
cascade_popdown (popover);
}
GtkWidget *
@ -2223,3 +2255,40 @@ gtk_popover_get_offset (GtkPopover *popover,
if (y_offset)
*y_offset = priv->y_offset;
}
/**
* gtk_popover_set_cascade_popdown:
* @popover: A #GtkPopover
* @cascade_popdown: #TRUE if the popover should follow a child closing
*
* If @cascade_popdown is #TRUE, the popover will be closed when a child
* modal popover is closed. If #FALSE, @popover will stay visible.
**/
void
gtk_popover_set_cascade_popdown (GtkPopover *popover,
gboolean cascade_popdown)
{
GtkPopoverPrivate *priv = gtk_popover_get_instance_private (popover);
if (priv->cascade_popdown != !!cascade_popdown)
{
priv->cascade_popdown = !!cascade_popdown;
g_object_notify (G_OBJECT (popover), "cascade-popdown");
}
}
/**
* gtk_popover_get_cascade_popdown:
* @popover: a #GtkPopover
*
* Returns whether the popover will close after a modal child is closed.
*
* Returns: #TRUE if @popover will close after a modal child.
**/
gboolean
gtk_popover_get_cascade_popdown (GtkPopover *popover)
{
GtkPopoverPrivate *priv = gtk_popover_get_instance_private (popover);
return priv->cascade_popdown;
}

View File

@ -111,6 +111,11 @@ GDK_AVAILABLE_IN_ALL
void gtk_popover_get_offset (GtkPopover *popover,
int *x_offset,
int *y_offset);
GDK_AVAILABLE_IN_ALL
void gtk_popover_set_cascade_popdown (GtkPopover *popover,
gboolean cascade_popdown);
GDK_AVAILABLE_IN_ALL
gboolean gtk_popover_get_cascade_popdown (GtkPopover *popover);
GDK_AVAILABLE_IN_ALL
void gtk_popover_set_default_widget (GtkPopover *popover,

View File

@ -809,6 +809,9 @@ _gtk_widget_grab_notify (GtkWidget *widget,
gtk_event_controller_reset (controller);
}
if (GTK_IS_NATIVE (widget))
gtk_widget_hide (widget);
}
static void