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");