| Ticket UUID: | 15c685a976de4d51332855208e73b559db3908f7 | |||
| Title: | Issues with menu cloning | |||
| Type: | Bug | Version: | 8.6.13 | |
| Submitter: | sbron | Created on: | 2023-03-23 11:45:24 | |
| Subsystem: | 10. Generic Menus | Assigned To: | nobody | |
| Priority: | 5 Medium | Severity: | Minor | |
| Status: | Closed | Last Modified: | 2023-03-30 10:06:55 | |
| Resolution: | Fixed | Closed By: | sbron | |
| Closed on: | 2023-03-30 10:06:55 | |||
| Description: |
While working on tip 658, I noticed a some issues with the implementation of the tk::MenuDup procedure: When the original menu has no entries at the moment of cloning, a large part of the proc is skipped. This includes the parts that modify the bind tags and copy the bindings. This means that the bind tags of a cloned menu end up differently depending on at which stage the clone is made:
When there are entries in the original menu, the proc goes through the trouble of copying the bindings form the original menu to the clone. But in the end, the clone doesn't even have its own name in its bind tags, meaning none of the copied bindings will actually ever fire. It seems the method used to generate the bind tags is flawed. It is unclear to me how the manipulation of the bind tags was intended to work. The copying of the bindings suggests that the name of the original menu should not be in the bind tags. If it would be, the bindings would fire twice. But that means that any binding added to the original menu after it is cloned, will be missing from the clone. However, tk::MenuDup doesn't have the final word on the bindings for the cloned menu. The CloneMenu() function in generic/tkMenu.c adds the name of the original menu in the bind tags of the clone after the name of the clone. The manipulation of the bind tags further seems to attempt to replace tags that are children of the original menu with children of the cloned menu, probably presuming that these are submenus that are also going to be cloned. The way the bindings are copied doesn't make sense either. This code replaces occurrences of the event with the name of the cloned menu. Resulting in:
I suppose the intention was to replace $src with $dst. But that's not what it is doing. To me, this all looks rather fragile and only seems to be intended to be able to deal with hardcoded references to the name of the original menu in the bindings. But it runs the risk of easily going wrong, especially with short menu names. My proposal would be to simply delete the whole duplication of binding tags and bindings from the source menu done in tk::MenuDup. Just let CloneMenu() add the name of the original after the clone. That way, any events bound to the original menu will be invoked for the clone as well. It is up to the script developer to use %W in the binding script, where applicable, rather than the hardcoded menu name. If we want to be really fancy, the bind tags of the original menu can be copied to the clone, only replacing the tag that exactly matches the original menu with the clone. This will make any tags that had been added to the menu also apply to the clone. But once again, this only works for changes made to the bind tags before cloning. So it's not a full solution. Considering that the duplication code is currently completely broken, I don't think it should be a problem to make these changes. They may be incompatible with the way the duplication was supposed to work. But duplication hasn't been working the way it was supposed to work since at least 2005, maybe never. Bug #1192095 already seems to have touched upon some of these issues. | |||
| User Comments: |
sbron added on 2023-03-30 10:06:55:
Branch bug-15c685a976 has been merged with Tk 8.6 and 8.7. sbron added on 2023-03-30 10:02:44: Branch bug-15c685a976 has been merged into Tk 8.6 and 8.7. sbron added on 2023-03-29 14:45:11: Yes, I think the fix is complete. fvogel added on 2023-03-28 19:47:17: I believe this can be merged? sbron added on 2023-03-26 19:53:30: That is a very clever way to avoid the issue. Thanks. I can confirm the tests still work on linux. fvogel added on 2023-03-26 16:41:23: On the first section of your answer below: So be it, you have convinced me. I see you have done what you describe in [0200b98c]. On the second part of your answer, dealing with the tests failing on Windows: I have investigated this on my side. See the fix and explanation in [ff954102]. Regarding winMenu.test: many tests use the 'userInteraction' constraint, which sheds some light on your surprise I think (I didn't need to resort to this in [ff954102]). sbron added on 2023-03-26 10:31:48: In the description below, I'm referring to the following code:
Looking at the history, I think the original implementation (1998) copied the bindtags from .m1 to .m2, replacing every occurrence of .m1 with .m2. It did not add .m1 to the bindtags of .m2. To make any bindings that were put on .m1 also work on .m2, these bindings were also copied. I'll ignore for the moment that this was done incorrectly. Later (2005), CloneMenu() added .m1 to the bindtags of .m2. That is a much better solution. But this lead to bug reports about each binding firing twice, because the binding duplication code was left in place. In my opinion, it should have been removed then. I'll be happy to implement the fancy solution I mentioned before, copying the .m1 bindtags to .m2, replacing only the .m1 tag with .m2. But the part that copies the bindings should be removed. I can't imagine anyone making use of the double event firing. But there have been bug reports of people being troubled by it. It is a bug and it should be possible to fix bugs in Tk 8.6. To be really conservative, the change can be tagged as a *** POTENTIAL INCOMPATIBILITY *** in the release notes. It seems that menu events don't work on Windows until the menu has been posted. But, also on Windows, posting a menu stops any further command execution until a mouse button is clicked. So I'm not sure how to fix that. To my surprise there are tests in winMenu.test that post a menu and proceed to do some other things. I don't understand how that can work. Maybe the test suite is doing something special. So maybe adding some fvogel added on 2023-03-25 09:32:39: I understand the story and the rationale for the fix I think. It makes sense. That said, even if the entire thing looks broken, I'm hesitating to commit this in 8.6. Isn't there a risk of breaking some weird user application making use of the bindtags copy? I would rather commit the change in 8.7 (trunk branch) I think. Besides, all four newly added tests menu-20.12 to -20.15 fail for me on Windows with: ---- Result was: ---- Result should have been (exact matching): <<Test>> sbron added on 2023-03-23 17:11:44: Proposed fix and tests in the bug-15c685a976 branch. sbron added on 2023-03-23 16:23:07: I noticed that recently [93fe36b5] fixed some of the issues I mentioned. But some others remain. | |||
Home
Timeline
Branches
Tags
Forum
Tickets
Download
Wiki