Type string for a string value obtained from an Expression lookup

I have a .ui file where I am using a Gtk.Expression to access a string value. The value must be bound to the action-target property like so:

<object class="GtkButton">
  <style>
    <class name="suggested-action" />
  </style>
  <property name="label">Copy</property>
  <property name="action-name">win.copy-color</property>
  <!-- <property name="action-target">'hello'</property> -->
  <binding name="action-target">
    <lookup name="color" type="Color">
      <lookup name="item">GtkListItem</lookup>
    </lookup>
  </binding>
</object>

My Gio.SimpleAction instance declaration looks like so:

const copyColorAction = new Gio.SimpleAction({
  name: "copy-color",
  parameterType: GLib.VariantType.new("s"),
 });

I get the error below when I run the code above.

actionhelper: action win.copy-color can't be activated due to parameter type mismatch (parameter type s, target type NULL)

unable to set property 'action-target' of type 'GVariant' from value of type 'gchararray'

It only works when I replace the lookup with the code below.

<property name="action-target">'hello world'</property>

I suspect I am not using the correct type string , type string combination, or there is something I should be doing in the lookup to convert the string value to a string enclosed in quotes. What could be the problem?

This is actually just a (possibly unavoidable) quirk of GActions with parameters. If you set the action-target first (i.e. “above” the action-name, you should avoid the critical warning.

1 Like

Unfortunately, moving the action-target property above its action-name sibling still gives the same warning and error. It doesn’t register the action at all because the button remains inactive. This could be a lookup bug or something related to lookup. Hard-coding the string in quotes seems fine but looking it up dynamically is where things start to fail.

Gtk-WARNING **: 08:48:40.452: actionhelper: action win.copy-color can't be activated due to parameter type mismatch (parameter type s, target type NULL)

GLib-GObject-CRITICAL **: 08:48:40.452: unable to set property 'action-target' of type 'GVariant' from value of type 'gchararray'

However, when I create an additional property of the GVariant type to wrap the string I need in the lookup Gobject, it registers the action and the critical error seems to disappear but the warning still remains.

color_variant_type: GObject.param_spec_variant(
  "color_variant_type",
  "colorVariantType",
  "Picked color in gVariantType datatype",
  GLib.VariantType.new("s"),
  null,
  GObject.ParamFlags.READWRITE
 ),

I still get tons of this warning:

Gtk-WARNING **: 09:02:23.990: actionhelper: action win.copy-color can't be activated due to parameter type mismatch (parameter type s, target type NULL)

Ah, I see. I guess that the expression is resolving after the action-name is set. If the parameter type is a string, you should probably be able to set the action-name to a detailed action, such as win.copy-color::color or win.copy-color('hello world').

Still getting the same error.

EDIT

Using detailed action name removes the error but the action is not being registered. The button remains inactive. Even hard-coding the value like this doesn’t register the action.

<property name="action-name">win.copy-color::test</property>

Ah, my mistake. I don’t have a little snippet to test this with easily, but I wonder if you do something like:

<object class="GtkButton">
  <style>
    <class name="suggested-action" />
  </style>
  <property name="label">Copy</property>
  <property name="action-target">''</property> <!-- empty placeholder target -->
  <property name="action-name">win.copy-color</property>
  <binding name="action-target">
    <lookup name="color" type="Color">
      <lookup name="item">GtkListItem</lookup>
    </lookup>
  </binding>
</object>

If that might work, and get rid of the warning/critical? I’m making an assumption here, but I guess these buttons aren’t intended to ever be insensitive or not have a target?

I feel like I might not be giving you the best advice here, but I’m missing the larger picture of the application, so hopefully I’m not taking you down the wrong road :slightly_smiling_face:

1 Like

Thanks. This resolves the warning. I wonder if this is a JavaScript thing or other bindings have such issues that require workarounds.

1 Like

I’ve seen the same in C, so I believe it’s just a quirk of how actions with parameters work. I guess it’s just necessary to assume the programmer has made a mistake if the action is set before the target is known, otherwise you’d have to warn them at activation-time, and that might be inefficient or crashy.

1 Like

This topic was automatically closed 45 days after the last reply. New replies are no longer allowed.