How to dynamically enable/disable menu actions on-demand

David Nolden david.nolden.kdevelop at art-master.de
Thu Apr 2 14:38:15 UTC 2009


Am Donnerstag 02 April 2009 15:53:08 schrieb Andreas Pakulat:
> The usual way to do this (apart from the state stuff above) is by
> enabling/disabling the action whenever the state of the IDE changes, i.e.
> if a file is opened check wether its part of any project. If not enable the
> action, else disable it. Then also check for closing documents. All that in
> the plugin where the action is provided.
Yeah but I need this for at least 5 different places already, and the checks 
that would need to be done permanently are quite complex. For example the 
"Open project for current file" action would need to be updated when:
- A project was opened
- A project was closed
- A different file was activated

And it's even worse for actions that do stuff based on the item currently 
under the cursor.

That would extremely uglify the code, and also lead to a lot of permanent 
activity(updating the states), that would be needed only exactly once in the 
moment the menu is shown.

> > If there is no such mechanism yet, I'm proposing we use something like
> > this: Create a subclass of KAction, called KDevAction, that has a signal
> > "showing(KAction*)", that should always be emitted when a menu containing
> > the action is shown, and that can be used by the action owners to check
> > the context, and enable/disable it.
>
> I don't like that at all, simply because we'd most probably end up with 2
> or three such classes as we'd need it not just for KAction but possibly
> also some of its subclasses.
But it would save us from a lot of state-watching all over the code. I don't 
even want to do that watching, because I already smell the bugs coming from 
all the needed code and signal/slot connections.

Another option would be using some system similar to our context menu stuff, 
where the actions also do their checks in the moment the menu is shown.

There's one problem with my proposal that comes into my mind: I'm not sure if 
the shortcuts still work while the action is disabled. So I'm waiting for 
better proposals. :)

Greetings, David





More information about the KDevelop-devel mailing list