How to get a GAction from a widget

I need a way to listen to the GAction::notify::state (also works with GActionGroup::state-changed::<name>) signal from a widget lower in the hierarchy to a Widget with a property action installed, that is higher in the hierarchy.

I read in the documentation that the actions are searched in parent layers of the widget, so I though that there could be a function to do that.

Widget.query_action doesn’t work because it only searches in installed actions for the current class, but it doesn’t search in other widgets.

I found GtkActionMuxer class, which is a kind of muxer/router to find actions in the action structure, but it’s private :frowning:

Reference gtk/gtkactionmuxer.c · main · GNOME / gtk · GitLab

You can add actions to a widget using gtk_widget_insert_action_group.

Not adding, but finding, so I can connect to its state value.

What are you actually trying to achieve, here?

You should only be connecting to actions you installed yourself, from within the widget that installed them; you should not be randomly accessing actions on random widgets.

Then why and how GtkCheckButton in radio mode, access the action specified in its action-name prop, and tracks its state value, if its not installed by itself?

I still don’t understand what you’re trying to do.

Please, attach a small, self-contained example of your code.

What I’m trying to achieve is some kind of “context” functionality, that depending on a upper action state value, the widget will style himself, change layout, etc.

For example just like the GtkCheckButton is currently doing, it activates himself when the state value is X, and changes his styling to a radio button (sorry, it changes to radio styling when it has a group assigned, nothing to do with actions).

Typescript pseudo-code:

class FooParentWidget extends gtk.Widget {
  display_mode: "desktop" | "mobile";

  class_init(klass) {
    klass.install_property_action("foo-parent.display-mode", "display_mode");
  }
}

class FooWidget extends gtk.Widget {
  constructed() {
    // hypothetic function, returning a GAction would be dangerous,
    // because then you can mutate it, that's why it would be safe
    // to just expose connecting to state, although GActions are inmutable,
    // so idk
    this.find_action_and_connect_state("foo.bar", (state) => {
      auto_break switch state {
        case "desktop":
          this.child_0.orientation = gtk.Orientation.HORIZONTAL;
          this.child_0.add_css_classes(["foo"]);
          this.child_0.remove_css_classes(["bar"]);

        case "mobile":
          this.child_0.orientation = gtk.Orientation.VERTICAL;
          this.child_0.description = "Oops, we currently not support mobile views";
      }
    });
  }
}