GTK Python MenuButton with GioAction weird behavior

Hello,

I just now discovered some weird behavior during the development of an application with GTK4 and Python. I have reduced the problem to this minimal PoC. In essence when pressing the shortcut Ctrl+P a Gio.Action is triggered. But as soon as the Menu Popover with at least one item is created, this shortcut stops working.

As I am new to GTK development, it can be, that i used the Gio.Actions wrong, and this is behavior is expected. If so, i would be interested why this is the case and maybe a resource or short example, of how it would be done correctly.
If it is unintended behavior it would be interesting to reproduce this potential bug with c, to see whether this is part of the python bindings or in GTK itself.

# -*- 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, **kwargs):
        super().__init__(**kwargs)
        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__':
    import sys

    app = ExampleApplication()
    app.run(sys.argv)

CtrlP seems to work fine here?

What I will say though is using append with a kwarg is an odd choice, and using it without an action is nonsensical

Yeah I should have deleted the kwargs aswell for the PoC. Thanks for the hint.

But is the Ctrl + P actually working for you even after you have opened the menu popover via the menu button? Because for me at this point it stops working…

With True or False the accel doesn’t do anything when the menu is open, since it’s trying to find something in the menu — and of course the item isn’t actually associated with the action (and thus not the accell)

With the fixed up append call I see:
image

And can CtrlP with the menu open

Yes i am aware of that. In the real application i do have your proposed menu item that is associated with the action, but that is not the point i am trying to make.

What i am trying to describe is that after the menu is closed again the shortcut Ctrl+P, that worked initially, before the menu was opened, then stops working. And this shortcut not working is independent of whether the action was bound to the menu or not. The variable TRIGGER_BUG shows that simply opening up a menu does not impact the functioning of the shortcut, only after appending at least one item (that is in no relation to the action as it is not associated) to the menu this behavior is triggered and the shortcut ceases to function.

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