diff --git a/include/widgets.hpp b/include/widgets.hpp index 1487f2d2..5f8f3a94 100644 --- a/include/widgets.hpp +++ b/include/widgets.hpp @@ -112,7 +112,7 @@ struct Widget { virtual void onMouseEnter() {} /** Called when another widget begins responding to `onMouseMove` events */ virtual void onMouseLeave() {} - virtual void onFocus() {} + virtual bool onFocus() {return false;} virtual void onDefocus() {} virtual bool onFocusText(int codepoint) {return false;} virtual bool onFocusKey(int key) {return false;} @@ -409,7 +409,7 @@ struct TextField : OpaqueWidget { Widget *onMouseDown(Vec pos, int button); bool onFocusText(int codepoint); bool onFocusKey(int scancode); - void onFocus(); + bool onFocus(); void insertText(std::string newText); }; diff --git a/src/app/ModuleWidget.cpp b/src/app/ModuleWidget.cpp index ca486bf9..daa944f5 100644 --- a/src/app/ModuleWidget.cpp +++ b/src/app/ModuleWidget.cpp @@ -152,14 +152,16 @@ void ModuleWidget::draw(NVGcontext *vg) { } Widget *ModuleWidget::onMouseMove(Vec pos, Vec mouseRel) { - // Instead of checking key-down events, delete the module even if key-repeat hasn't fired yet and the cursor is hovering over the widget. - if (glfwGetKey(gWindow, GLFW_KEY_DELETE) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_BACKSPACE) == GLFW_PRESS) { - if (!guiIsModPressed() && !guiIsShiftPressed()) { - gRackWidget->deleteModule(this); - this->finalizeEvents(); - delete this; - // Kinda sketchy because events will be passed further down the tree - return NULL; + if (!gFocusedWidget) { + // Instead of checking key-down events, delete the module even if key-repeat hasn't fired yet and the cursor is hovering over the widget. + if (glfwGetKey(gWindow, GLFW_KEY_DELETE) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_BACKSPACE) == GLFW_PRESS) { + if (!guiIsModPressed() && !guiIsShiftPressed()) { + gRackWidget->deleteModule(this); + this->finalizeEvents(); + delete this; + // Kinda sketchy because events will be passed further down the tree + return NULL; + } } } return OpaqueWidget::onMouseMove(pos, mouseRel); diff --git a/src/gui.cpp b/src/gui.cpp index 4c3b2b83..8487002c 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -59,12 +59,16 @@ void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods) { if (w != gFocusedWidget) { if (gFocusedWidget) { + // onDefocus w->onDefocus(); } + gFocusedWidget = NULL; if (w) { - w->onFocus(); + // onFocus + if (w->onFocus()) { + gFocusedWidget = w; + } } - gFocusedWidget = w; } } } diff --git a/src/widgets/TextField.cpp b/src/widgets/TextField.cpp index c223b657..e1d3585c 100644 --- a/src/widgets/TextField.cpp +++ b/src/widgets/TextField.cpp @@ -103,9 +103,10 @@ bool TextField::onFocusKey(int key) { return true; } -void TextField::onFocus() { +bool TextField::onFocus() { begin = 0; end = text.size(); + return true; } void TextField::insertText(std::string newText) {