From ee1519ee5a563612bd94ed76551ee3b3ffd8596e Mon Sep 17 00:00:00 2001 From: cnmcdee Date: Wed, 2 Dec 2026 05:33:11 +0000 Subject: [PATCH] Task 4: route ButtonRelease, MotionNotify and KeyPress to the right widget in generated apps Same pressed/focused routing as the C generator: generated event loop tracks pressed_widget (set on a handled ButtonPress) and focused_widget (click-to-focus). ButtonRelease goes to the pressed widget even if the pointer moved off it; MotionNotify goes to the pressed widget while a press is held (drag), otherwise to the topmost widget under the pointer; KeyPress goes to the focused widget only. ButtonPress hit-testing, Expose, ConfigureNotify and ClientMessage unchanged. Verified live under Xvfb on hot_chicago: the Rust designer generates main.cpp with the new loop. The generated loop is line-for-line the same one proven live in C with an instrumented 4-widget app (keys only to the focused entry, hover only to the widget under the pointer, drag/release only to the pressed button). The Rust designer canvas does NOT share this loop (it already targets release/motion/keys by selection/hit-test), so it is unchanged. --- crates/lightgui-gen/src/gen.rs | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/crates/lightgui-gen/src/gen.rs b/crates/lightgui-gen/src/gen.rs index 431d1c3..be0e5a4 100644 --- a/crates/lightgui-gen/src/gen.rs +++ b/crates/lightgui-gen/src/gen.rs @@ -3426,6 +3426,11 @@ fn emit_main_cpp(project: &Project, widgets: &[Widget]) -> String { o.push_str(" };\n"); o.push_str(" redraw_all();\n"); o.push_str(" bool running = true;\n"); + o.push_str(" // Task 4: event routing. pressed_widget is the widget that\n"); + o.push_str(" // received the ButtonPress (release + drag motion go there);\n"); + o.push_str(" // focused_widget is the last widget clicked (keys go there).\n"); + o.push_str(" lg::IWidget* pressed_widget = nullptr;\n"); + o.push_str(" lg::IWidget* focused_widget = nullptr;\n"); o.push_str(" while (running) {\n"); o.push_str(" XEvent event;\n"); o.push_str(" XNextEvent(display, &event);\n"); @@ -3439,25 +3444,40 @@ fn emit_main_cpp(project: &Project, widgets: &[Widget]) -> String { o.push_str(" for (auto it = widgets.rbegin(); it != widgets.rend(); ++it) {\n"); o.push_str(" if ((*it)->contains(event.xbutton.x, event.xbutton.y)) {\n"); o.push_str(" handled = (*it)->on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);\n"); + o.push_str(" if (handled) { pressed_widget = *it; focused_widget = *it; }\n"); o.push_str(" break;\n"); o.push_str(" }\n"); o.push_str(" }\n"); o.push_str(" break;\n"); o.push_str(" case ButtonRelease:\n"); - o.push_str(" for (lg::IWidget* wd : widgets)\n"); - o.push_str(" handled = wd->on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) || handled;\n"); + o.push_str(" // Release goes to the widget that got the press,\n"); + o.push_str(" // even if the pointer moved off it.\n"); + o.push_str(" if (pressed_widget) {\n"); + o.push_str(" handled = pressed_widget->on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);\n"); + o.push_str(" pressed_widget = nullptr;\n"); + o.push_str(" }\n"); o.push_str(" break;\n"); o.push_str(" case MotionNotify:\n"); - o.push_str(" for (lg::IWidget* wd : widgets)\n"); - o.push_str(" handled = wd->on_motion(event.xmotion.x, event.xmotion.y) || handled;\n"); + o.push_str(" if (pressed_widget) {\n"); + o.push_str(" // Drag: motion goes to the pressed widget.\n"); + o.push_str(" handled = pressed_widget->on_motion(event.xmotion.x, event.xmotion.y);\n"); + o.push_str(" } else {\n"); + o.push_str(" // Hover: topmost widget under the pointer.\n"); + o.push_str(" for (auto it = widgets.rbegin(); it != widgets.rend(); ++it) {\n"); + o.push_str(" if ((*it)->contains(event.xmotion.x, event.xmotion.y)) {\n"); + o.push_str(" handled = (*it)->on_motion(event.xmotion.x, event.xmotion.y);\n"); + o.push_str(" break;\n"); + o.push_str(" }\n"); + o.push_str(" }\n"); + o.push_str(" }\n"); o.push_str(" break;\n"); o.push_str(" case KeyPress: {\n"); o.push_str(" char buffer[32];\n"); o.push_str(" KeySym key = 0;\n"); o.push_str(" int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);\n"); o.push_str(" buffer[n] = 0;\n"); - o.push_str(" for (lg::IWidget* wd : widgets)\n"); - o.push_str(" handled = wd->on_key(key, buffer) || handled;\n"); + o.push_str(" if (focused_widget)\n"); + o.push_str(" handled = focused_widget->on_key(key, buffer);\n"); o.push_str(" break;\n"); o.push_str(" }\n"); o.push_str(" case ConfigureNotify:\n");