menubutton: Add `active` property and getter/setter
authorChristopher Davis <christopherdavis@gnome.org>
Sun, 12 Feb 2023 21:14:43 +0000 (16:14 -0500)
committerChristopher Davis <christopherdavis@gnome.org>
Sun, 12 Feb 2023 21:47:59 +0000 (16:47 -0500)
GtkMenuButton currently does not provide a way to tell
if it's open programmatically. The existing methods,
`popup()` and `popdown()`, do not expose any state
to callers. If someone wanted to know whether or not
a menubutton was open, they needed the popover. Given
that GtkMenuButton can manage the popovers itself,
that's not always an option for app developers.

This commit adds the `active` property and associated
methods, where `gtk_menu_button_set_active ()` replaces
both `gtk_menu_popup ()` and `gtk_menu_popdown ()`.
This addition also mirrors changes in other places,
Such as `GtkWidget:visible` vs `show()`/`hide()`.

gtk/gtkmenubutton.c
gtk/gtkmenubutton.h

index 73a5fe0a650facdd7d5b1ac1a17ae8bcbd804488..872be35f1d490a0a79f74286c63a2fa4fea3167a 100644 (file)
@@ -150,6 +150,7 @@ enum
   PROP_HAS_FRAME,
   PROP_PRIMARY,
   PROP_CHILD,
+  PROP_ACTIVE,
   LAST_PROP
 };
 
@@ -208,6 +209,9 @@ gtk_menu_button_set_property (GObject      *object,
       case PROP_CHILD:
         gtk_menu_button_set_child (self, g_value_get_object (value));
         break;
+      case PROP_ACTIVE:
+        gtk_menu_button_set_active (self, g_value_get_boolean (value));
+        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -253,6 +257,9 @@ gtk_menu_button_get_property (GObject    *object,
       case PROP_CHILD:
         g_value_set_object (value, gtk_menu_button_get_child (self));
         break;
+      case PROP_ACTIVE:
+        g_value_set_boolean (value, gtk_menu_button_get_active (self));
+        break;
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     }
@@ -314,6 +321,8 @@ gtk_menu_button_toggled (GtkMenuButton *self)
                                       GTK_ACCESSIBLE_STATE_EXPANDED);
         }
     }
+
+  g_object_notify_by_pspec (G_OBJECT (self), menu_button_props[PROP_ACTIVE]);
 }
 
 static void
@@ -513,6 +522,18 @@ gtk_menu_button_class_init (GtkMenuButtonClass *klass)
                          GTK_TYPE_WIDGET,
                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
 
+  /**
+   * GtkMenuButton:active: (attributes org.gtk.Property.get=gtk_menu_button_get_active org.gtk.Property.set=gtk_menu_button_set_active)
+   *
+   * Whether the menu button is active.
+   *
+   * Since: 4.10
+   */
+  menu_button_props[PROP_ACTIVE] =
+    g_param_spec_boolean ("active", NULL, NULL,
+                          FALSE,
+                          GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
+
   g_object_class_install_properties (gobject_class, LAST_PROP, menu_button_props);
 
   /**
@@ -1499,3 +1520,45 @@ gtk_menu_button_get_child (GtkMenuButton *menu_button)
 
   return menu_button->child;
 }
+
+/**
+ * gtk_menu_button_set_active: (attributes org.gtk.Method.set_property=active)
+ * @menu_button: a `GtkMenuButton`
+ * @active: whether the menu button is active
+ *
+ * Sets whether menu button acts is active.
+ *
+ * Since: 4.10
+ */
+void
+gtk_menu_button_set_active (GtkMenuButton *menu_button,
+                            gboolean       active)
+{
+  g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button));
+
+  if (active == gtk_menu_button_get_active (menu_button))
+    return;
+
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button->button),
+                                active);
+
+  g_object_notify_by_pspec (G_OBJECT (menu_button), menu_button_props[PROP_ACTIVE]);
+}
+
+/**
+ * gtk_menu_button_get_active: (attributes org.gtk.Method.get_property=active)
+ * @menu_button: a `GtkMenuButton`
+ *
+ * Returns whether the menu button is active.
+ *
+ * Returns: TRUE if the button is active
+ *
+ * Since: 4.10
+ */
+gboolean
+gtk_menu_button_get_active (GtkMenuButton *menu_button)
+{
+  g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), FALSE);
+
+  return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (menu_button->button));
+}
index 412f43ca3b00bcd17be6b818fab2e6de0bbb648c..87d87976b2fa25b0695d69a4833b0e0546ba7bb5 100644 (file)
@@ -127,6 +127,12 @@ void          gtk_menu_button_set_child   (GtkMenuButton *menu_button,
 GDK_AVAILABLE_IN_4_6
 GtkWidget *   gtk_menu_button_get_child   (GtkMenuButton *menu_button);
 
+GDK_AVAILABLE_IN_4_10
+void       gtk_menu_button_set_active (GtkMenuButton *menu_button,
+                                       gboolean       active);
+GDK_AVAILABLE_IN_4_10
+gboolean   gtk_menu_button_get_active (GtkMenuButton *menu_button);
+
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkMenuButton, g_object_unref)
 
 G_END_DECLS