GTK Python MenuButton with GioAction weird behavior (second try)

Hello,

I recently opened a topic that was closed without getting a valuable answer as my question was misunderstood and then not answered again after clarification…

I will try to explain the problem again in the hopes that this time it may be clearer…

An action with a shortcut (accelerator) in the example case <primary>p can successfully called via shortcut. But as soon as a PopoverMenu is opened and closed again, the previously working shortcut ceases to work.

What I found out already is, that if the Popover Menu has no items in it, this bug will not be triggered. But as soon as an item is added, even though it has nothing to do with the initially registered action and shortcut, said shortcut will no longer be working.

Here is the code again:

# -*- coding: utf-8 -*-
import gi

gi.require_version(namespace='Gtk', version='4.0')
from gi.repository import Gio, Gtk

TRIGGER_BUG = True


class ExampleWindow(Gtk.ApplicationWindow):
    def __init__(self):
        super().__init__()
        self.set_title(title='Menu Shortcut Bug PoC')
        gio_menu = Gio.Menu.new()
        if TRIGGER_BUG:
            gio_menu.append(label='test')
        popover_menu = Gtk.PopoverMenu.new_from_model(gio_menu)
        headerbar = Gtk.HeaderBar.new()
        self.set_titlebar(titlebar=headerbar)
        menu_button = Gtk.MenuButton.new()
        menu_button.set_popover(popover=popover_menu)
        headerbar.pack_end(child=menu_button)


class ExampleApplication(Gtk.Application):
    def __init__(self):
        super().__init__(application_id='org.poc')
        action = Gio.SimpleAction.new('poc', None)
        action.connect('activate', self.on_poc_action)
        self.add_action(action)
        self.set_accels_for_action(f'app.poc', ['<primary>p'])

    def do_activate(self):
        win = self.props.active_window
        if not win:
            win = ExampleWindow(application=self)
        win.present()

    def on_poc_action(self, action, param):
        print('Action activated')


if __name__ == '__main__':
    app = ExampleApplication()
    app.run()

If anyone runs into the same problem…

I got it to work through the use of action groups that i then registered on a widget.

Somehow by not registering an action directly on the application but via the action group the bug was not triggered. I’m still not sure why this was necessary in the first place, and if someone knows why, I would be interested in your explanation.

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