constraint guide: Make strength tweakable

The strength for the natural width can be used
as a tie-breaker to make instable systems behave
in a more predictable way. This can be seen
in the simple constraints demo in gtk-demo.
This commit is contained in:
Matthias Clasen 2019-06-29 14:18:23 +00:00 committed by Emmanuele Bassi
parent 71b52f485e
commit a9dfca04e4
3 changed files with 54 additions and 1 deletions

View File

@ -88,6 +88,7 @@ build_constraints (SimpleGrid *self,
gtk_constraint_guide_set_min_size (guide, 10, 10);
gtk_constraint_guide_set_nat_size (guide, 100, 10);
gtk_constraint_guide_set_max_size (guide, 200, 20);
gtk_constraint_guide_set_strength (guide, GTK_CONSTRAINT_STRENGTH_STRONG);
gtk_constraint_layout_add_guide (manager, guide);
gtk_constraint_layout_add_constraint (manager,

View File

@ -47,6 +47,8 @@ struct _GtkConstraintGuide
char *name;
int strength;
int values[LAST_VALUE];
GtkConstraintLayout *layout;
@ -73,6 +75,7 @@ enum {
PROP_NAT_HEIGHT,
PROP_MAX_WIDTH,
PROP_MAX_HEIGHT,
PROP_STRENGTH,
PROP_NAME,
LAST_PROP
};
@ -137,7 +140,7 @@ gtk_constraint_guide_update_constraint (GtkConstraintGuide *guide,
guide->constraints[index] =
gtk_constraint_solver_add_stay_variable (solver,
var,
GTK_CONSTRAINT_WEIGHT_MEDIUM);
guide->strength);
}
else
{
@ -232,6 +235,10 @@ gtk_constraint_guide_set_property (GObject *gobject,
}
break;
case PROP_STRENGTH:
gtk_constraint_guide_set_strength (self, g_value_get_enum (value));
break;
case PROP_NAME:
gtk_constraint_guide_set_name (self, g_value_get_string (value));
break;
@ -261,6 +268,10 @@ gtk_constraint_guide_get_property (GObject *gobject,
g_value_set_int (value, self->values[prop_id - 1]);
break;
case PROP_STRENGTH:
g_value_set_int (value, self->strength);
break;
case PROP_NAME:
g_value_set_string (value, self->name);
break;
@ -335,6 +346,15 @@ gtk_constraint_guide_class_init (GtkConstraintGuideClass *class)
G_PARAM_READWRITE|
G_PARAM_EXPLICIT_NOTIFY);
guide_props[PROP_STRENGTH] =
g_param_spec_enum ("strength",
"Strength",
"The strength to use for natural size",
GTK_TYPE_CONSTRAINT_STRENGTH,
GTK_CONSTRAINT_STRENGTH_MEDIUM,
G_PARAM_READWRITE|
G_PARAM_EXPLICIT_NOTIFY);
guide_props[PROP_NAME] =
g_param_spec_string ("name",
"Name",
@ -538,3 +558,27 @@ gtk_constraint_guide_set_name (GtkConstraintGuide *guide,
guide->name = g_strdup (name);
g_object_notify_by_pspec (G_OBJECT (guide), guide_props[PROP_NAME]);
}
GtkConstraintStrength
gtk_constraint_guide_get_strength (GtkConstraintGuide *guide)
{
g_return_val_if_fail (GTK_IS_CONSTRAINT_GUIDE (guide),
GTK_CONSTRAINT_STRENGTH_MEDIUM);
return guide->strength;
}
void
gtk_constraint_guide_set_strength (GtkConstraintGuide *guide,
GtkConstraintStrength strength)
{
g_return_if_fail (GTK_IS_CONSTRAINT_GUIDE (guide));
if (guide->strength == strength)
return;
guide->strength = strength;
g_object_notify_by_pspec (G_OBJECT (guide), guide_props[PROP_STRENGTH]);
gtk_constraint_guide_update_constraint (guide, NAT_WIDTH);
gtk_constraint_guide_update_constraint (guide, NAT_HEIGHT);
}

View File

@ -21,6 +21,7 @@
#include <gtk/gtktypes.h>
#include <gtk/gtkenums.h>
#include <gtk/gtktypebuiltins.h>
G_BEGIN_DECLS
@ -67,6 +68,13 @@ GDK_AVAILABLE_IN_ALL
void gtk_constraint_guide_get_max_size (GtkConstraintGuide *guide,
int *width,
int *height);
GDK_AVAILABLE_IN_ALL
GtkConstraintStrength gtk_constraint_guide_get_strength (GtkConstraintGuide *guide);
GDK_AVAILABLE_IN_ALL
void gtk_constraint_guide_set_strength (GtkConstraintGuide *guide,
GtkConstraintStrength strength);
GDK_AVAILABLE_IN_ALL
void gtk_constraint_guide_set_name (GtkConstraintGuide *guide,
const char *name);