Add parsing for GtkAlternativeTrigger

Alternative triggers are separate by a pipe character.
This commit is contained in:
Emmanuele Bassi 2020-03-26 16:58:55 +00:00
parent c75fdda8dd
commit 6719d3044d
2 changed files with 44 additions and 2 deletions

View File

@ -120,6 +120,8 @@ gtk_shortcut_trigger_trigger (GtkShortcutTrigger *self,
* - `never`, for #GtkNeverTrigger
* - a string parsed by gtk_accelerator_parse(), for a #GtkKeyvalTrigger
* - underscore, followed by a single character, for #GtkMnemonicTrigger
* - two valid trigger strings, separated by a `|` character, for a
* #GtkAlternativeTrigger
*
* Returns: (nullable) (transfer full): a new #GtkShortcutTrigger
* or %NULL on error
@ -129,9 +131,44 @@ gtk_shortcut_trigger_parse_string (const char *string)
{
GdkModifierType modifiers;
guint keyval;
const char *sep;
g_return_val_if_fail (string != NULL, NULL);
if ((sep = strchr (string, '|')) != NULL)
{
char *frag_a = g_strndup (string, sep - string);
const char *frag_b = sep + 1;
GtkShortcutTrigger *t1, *t2;
/* empty first slot */
if (*frag_a == '\0')
return NULL;
/* empty second slot */
if (*frag_b == '\0')
return NULL;
t1 = gtk_shortcut_trigger_parse_string (frag_a);
if (t1 == NULL)
{
g_free (frag_a);
return NULL;
}
t2 = gtk_shortcut_trigger_parse_string (frag_b);
if (t2 == NULL)
{
g_object_unref (t1);
g_free (frag_a);
return NULL;
}
g_free (frag_a);
return gtk_alternative_trigger_new (t1, t2);
}
if (g_str_equal (string, "never"))
return g_object_ref (gtk_never_trigger_get ());
@ -398,7 +435,7 @@ static void
gtk_never_trigger_print (GtkShortcutTrigger *trigger,
GString *string)
{
g_string_append (string, "<never>");
g_string_append (string, "never");
}
static gboolean
@ -1091,7 +1128,7 @@ gtk_alternative_trigger_print (GtkShortcutTrigger *trigger,
GtkAlternativeTrigger *self = GTK_ALTERNATIVE_TRIGGER (trigger);
gtk_shortcut_trigger_print (self->first, string);
g_string_append (string, ", ");
g_string_append (string, "|");
gtk_shortcut_trigger_print (self->second, string);
}

View File

@ -157,6 +157,7 @@ test_trigger_parse (void)
{
TRIGGER_KEYVAL,
TRIGGER_MNEMONIC,
TRIGGER_ALT,
TRIGGER_NEVER,
TRIGGER_INVALID
};
@ -178,6 +179,7 @@ test_trigger_parse (void)
{ "_s", 0, GDK_KEY_s, TRIGGER_MNEMONIC },
{ "foo", 0, 0, TRIGGER_INVALID },
{ "<Nyaa>B", 0, 0, TRIGGER_INVALID },
{ "<Control>U|<Shift><Control>U", GDK_CONTROL_MASK, 'u', TRIGGER_ALT }
};
GtkShortcutTrigger *trigger;
int i;
@ -200,6 +202,9 @@ test_trigger_parse (void)
==,
tests[i].keyval);
break;
case TRIGGER_ALT:
g_assert_true (GTK_IS_ALTERNATIVE_TRIGGER (trigger));
break;
case TRIGGER_NEVER:
g_assert_true (GTK_IS_NEVER_TRIGGER (trigger));
break;