st/button: Add :icon-name property

GTK4 added a convenience property for icon-only buttons. While that
use case is not quite as common in the shell as in GTK apps, it still
seems common enough to mirror the GTK API.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2254>
This commit is contained in:
Florian Müllner 2022-03-21 16:07:11 +01:00 committed by Marge Bot
parent bc533af73f
commit d72abf5268
2 changed files with 83 additions and 0 deletions

View File

@ -40,6 +40,7 @@
#include "st-button.h"
#include "st-icon.h"
#include "st-enum-types.h"
#include "st-texture-cache.h"
#include "st-private.h"
@ -51,6 +52,7 @@ enum
PROP_0,
PROP_LABEL,
PROP_ICON_NAME,
PROP_BUTTON_MASK,
PROP_TOGGLE_MODE,
PROP_CHECKED,
@ -400,6 +402,9 @@ st_button_set_property (GObject *gobject,
case PROP_LABEL:
st_button_set_label (button, g_value_get_string (value));
break;
case PROP_ICON_NAME:
st_button_set_icon_name (button, g_value_get_string (value));
break;
case PROP_BUTTON_MASK:
st_button_set_button_mask (button, g_value_get_flags (value));
break;
@ -430,6 +435,9 @@ st_button_get_property (GObject *gobject,
case PROP_LABEL:
g_value_set_string (value, priv->text);
break;
case PROP_ICON_NAME:
g_value_set_string (value, st_button_get_icon_name (ST_BUTTON (gobject)));
break;
case PROP_BUTTON_MASK:
g_value_set_flags (value, priv->button_mask);
break;
@ -495,6 +503,18 @@ st_button_class_init (StButtonClass *klass)
NULL,
ST_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* StButton:icon-name:
*
* The icon name of the #StButton.
*/
props[PROP_ICON_NAME] =
g_param_spec_string ("icon-name",
"Icon name",
"Icon name of the button",
NULL,
ST_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* StButton:button-mask:
*
@ -674,6 +694,66 @@ st_button_set_label (StButton *button,
g_object_notify_by_pspec (G_OBJECT (button), props[PROP_LABEL]);
}
/**
* st_button_get_icon_name:
* @button: a #StButton
*
* Get the icon name of the button. If the button isn't showing an icon,
* the return value will be %NULL.
*
* Returns: (transfer none) (nullable): the icon name of the button
*/
const char *
st_button_get_icon_name (StButton *button)
{
ClutterActor *icon;
g_return_val_if_fail (ST_IS_BUTTON (button), NULL);
icon = st_bin_get_child (ST_BIN (button));
if (ST_IS_ICON (icon))
return st_icon_get_icon_name (ST_ICON (icon));
return NULL;
}
/**
* st_button_set_icon_name:
* @button: a #Stbutton
* @icon_name: an icon name
*
* Adds an `StIcon` with the given icon name as a child.
*
* If @button already contains a child actor, that child will
* be removed and replaced with the icon.
*/
void
st_button_set_icon_name (StButton *button,
const char *icon_name)
{
ClutterActor *icon;
g_return_if_fail (ST_IS_BUTTON (button));
g_return_if_fail (icon_name != NULL);
icon = st_bin_get_child (ST_BIN (button));
if (ST_IS_ICON (icon))
{
st_icon_set_icon_name (ST_ICON (icon), icon_name);
}
else
{
icon = g_object_new (ST_TYPE_ICON,
"icon-name", icon_name,
"x-align", CLUTTER_ACTOR_ALIGN_CENTER,
"y-align", CLUTTER_ACTOR_ALIGN_CENTER,
NULL);
st_bin_set_child (ST_BIN (button), icon);
}
g_object_notify_by_pspec (G_OBJECT (button), props[PROP_ICON_NAME]);
}
/**
* st_button_get_button_mask:
* @button: a #StButton

View File

@ -48,6 +48,9 @@ StWidget *st_button_new_with_label (const gchar *text);
const gchar *st_button_get_label (StButton *button);
void st_button_set_label (StButton *button,
const gchar *text);
const char *st_button_get_icon_name (StButton *button);
void st_button_set_icon_name (StButton *button,
const char *icon_name);
void st_button_set_toggle_mode (StButton *button,
gboolean toggle);
gboolean st_button_get_toggle_mode (StButton *button);