Task 4: ButtonRelease goes to the pressed widget in generated apps

The Python generator is GTK-based and hit-tests to one widget, so it never had the every-widget loop; its bug was that _on_button_release dispatched OnMouseUp to the widget under the release point. It now tracks _PRESSED on ButtonPress (via a new _dispatch_to helper) and dispatches OnMouseUp to _PRESSED on release, then clears it. Motion/key are not wired in the Python generator (RUNTIME_EVENTS is click-only), so there is no every-widget loop for them to fix. New test test_task4_release_goes_to_pressed_widget asserts the emitted code; full suite 282 tests pass. Verified live under Xvfb with a generated 2-button app: press Button A, release over Button B -> only A's OnMouseUp fires (log shows MOUSEUP A, never MOUSEUP B). The designer canvas does not broadcast release/motion to widgets, so it is unchanged.
This commit is contained in:
cnmcdee committed 2026-12-02 05:39:48 +00:00
1 parent 70165968b6
commit 90f7e403ea
2 files changed
+33 -5

No files matched your search

+16 -5
View File
@@ -255,6 +255,7 @@ def _emit_runtime(lines, project, menus):
lines.append("_BACKGROUND = %r" % (project.background or "",))
lines.append("_WIDGETS = []")
lines.append("_LAST_SIZE = [None]")
lines.append("_PRESSED = [None] # Task 4: widget that got the ButtonPress")
lines.append("")
lines.append("")
lines.append("def _hit_widget(widgets, x, y):")
@@ -268,10 +269,8 @@ def _emit_runtime(lines, project, menus):
lines.append(" return None")
lines.append("")
lines.append("")
lines.append("def _dispatch(widgets, x, y, event):")
lines.append(" \"\"\"Call the *event* handler of the widget at (*x*, *y*),")
lines.append(" if it has one wired.\"\"\"")
lines.append(" widget = _hit_widget(widgets, x, y)")
lines.append("def _dispatch_to(widget, event):")
lines.append(" \"\"\"Call the *event* handler of *widget* (already chosen).\"\"\"")
lines.append(" if widget is None:")
lines.append(" return")
lines.append(" if widget.get_prop(\"enabled\", \"true\") in (\"false\", \"0\"):")
@@ -281,6 +280,12 @@ def _emit_runtime(lines, project, menus):
lines.append(" handler(widget)")
lines.append("")
lines.append("")
lines.append("def _dispatch(widgets, x, y, event):")
lines.append(" \"\"\"Call the *event* handler of the widget at (*x*, *y*),")
lines.append(" if it has one wired.\"\"\"")
lines.append(" _dispatch_to(_hit_widget(widgets, x, y), event)")
lines.append("")
lines.append("")
lines.append("def _on_draw(area, cr):")
lines.append(" bg = parse_color(_BACKGROUND, None)")
lines.append(" if bg is None:")
@@ -298,11 +303,17 @@ def _emit_runtime(lines, project, menus):
lines.append(" elif event.type == Gdk.EventType.BUTTON_PRESS:")
lines.append(" _dispatch(_WIDGETS, event.x, event.y, \"OnMouseDown\")")
lines.append(" _dispatch(_WIDGETS, event.x, event.y, \"OnClick\")")
lines.append(" # Task 4: remember the pressed widget; release goes there.")
lines.append(" _PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y)")
lines.append(" return False")
lines.append("")
lines.append("")
lines.append("def _on_button_release(area, event):")
lines.append(" _dispatch(_WIDGETS, event.x, event.y, \"OnMouseUp\")")
lines.append(" # Task 4: release goes to the widget that got the press,")
lines.append(" # even if the pointer moved off it.")
lines.append(" widget = _PRESSED[0]")
lines.append(" _PRESSED[0] = None")
lines.append(" _dispatch_to(widget, \"OnMouseUp\")")
lines.append(" return False")
lines.append("")
lines.append("")
+17
View File
@@ -118,6 +118,23 @@ class GeneratorTests(unittest.TestCase):
self.assertNotIn("('GtkButton_001', 'OnChange')", text)
self.assertIn("NOTE: OnChange has no runtime wiring", text)
def test_task4_release_goes_to_pressed_widget(self):
"""Task 4: the generated app tracks _PRESSED on ButtonPress and
dispatches OnMouseUp to it on ButtonRelease, so a release over
another widget does not fire that widget's handler."""
project, _, _, _ = _sample_project()
text = generator.generate(project)
self.assertIn("_PRESSED = [None]", text)
self.assertIn("def _dispatch_to(widget, event):", text)
self.assertIn("_PRESSED[0] = _hit_widget(_WIDGETS, event.x, event.y)",
text)
self.assertIn("widget = _PRESSED[0]", text)
self.assertIn('_dispatch_to(widget, "OnMouseUp")', text)
# Release must NOT dispatch by hit-testing the release point.
self.assertNotIn('_dispatch(_WIDGETS, event.x, event.y, "OnMouseUp")',
text)
compile(text, "<generated>", "exec")
def test_menubar(self):
project = Project()
project.menubar = "File: New | Open | - | Quit\nHelp: About"