Draft: Edit, moved resetTrackers for context menu actions to main module

The wrapper allows to call resetTrackers in the main module after the callback to the GuiTools is executed.

This is the last commit, many thanks to @matthijskooijman for having menthored me :)

I think it's helpful to have @matthijskooijman explanation on this use of the wrapper:

This defines a new wrapper function, that calls the original callback and then calls resetTrackers. Note that this creates a new function for every loop iteration, so each of these wrapper functions captures potentially different callback, self and obj values so things work as expected. Note I did something weird with the callback value there: Since functions like these capture a variable, not its value at the time of function definition, and loop variables like label and callback are a single variable shared between all loop iterations, capturing callback directly ends up with all wrappers calling the last callback (i.e. they all capture the same variable and by the time the wrappers are called, that variable will contain the last of the callbacks). This is commonly solved by using a default value in the function definition, since such a default value uses the value of the (in this case) callback variable, not capturing the variable.
This commit is contained in:
carlopav
2021-05-25 00:04:38 +02:00
parent 4db4d1c34f
commit d2d570f875
2 changed files with 14 additions and 47 deletions

View File

@@ -703,8 +703,12 @@ class Edit(gui_base_original.Modifier):
return
for (label, callback) in actions:
def wrapper(callback=callback):
callback()
self.resetTrackers(obj)
action = self.tracker_menu.addAction(label)
action.setData(callback)
action.setData(wrapper)
self.tracker_menu.popup(Gui.getMainWindow().cursor().pos())