Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/nanogui/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class NANOGUI_EXPORT Screen : public Widget {
int mMouseState, mModifiers;
Vector2i mMousePos;
bool mDragActive;
Widget *mDragWidget = nullptr;
ref<Widget> mDragWidget;
double mLastInteraction;
bool mProcessEvents;
Color mBackground;
Expand Down
16 changes: 14 additions & 2 deletions src/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,12 @@ bool Screen::cursorPosCallbackEvent(double x, double y) {
try {
p -= Vector2i(1, 2);

// Make sure mDragWidget isn't the only remaining reference
if (mDragActive && mDragWidget->getRefCount() == 1) {
mDragActive = false;
mDragWidget = nullptr;
}

if (!mDragActive) {
Widget *widget = findWidget(p);
if (widget != nullptr && widget->cursor() != mCursor) {
Expand Down Expand Up @@ -522,6 +528,12 @@ bool Screen::mouseButtonCallbackEvent(int button, int action, int modifiers) {
else
mMouseState &= ~(1 << button);

// Make sure mDragWidget isn't the only remaining reference
if (mDragActive && mDragWidget->getRefCount() == 1) {
mDragActive = false;
mDragWidget = nullptr;
}

auto dropWidget = findWidget(mMousePos);
if (mDragActive && action == GLFW_RELEASE &&
dropWidget != mDragWidget)
Expand All @@ -536,7 +548,7 @@ bool Screen::mouseButtonCallbackEvent(int button, int action, int modifiers) {

if (action == GLFW_PRESS && (button == GLFW_MOUSE_BUTTON_1 || button == GLFW_MOUSE_BUTTON_2)) {
mDragWidget = findWidget(mMousePos);
if (mDragWidget == this)
if (mDragWidget.get() == this)
mDragWidget = nullptr;
mDragActive = mDragWidget != nullptr;
if (!mDragActive)
Expand Down Expand Up @@ -649,7 +661,7 @@ void Screen::updateFocus(Widget *widget) {
void Screen::disposeWindow(Window *window) {
if (std::find(mFocusPath.begin(), mFocusPath.end(), window) != mFocusPath.end())
mFocusPath.clear();
if (mDragWidget == window)
if (mDragWidget.get() == window)
mDragWidget = nullptr;
removeChild(window);
}
Expand Down
11 changes: 9 additions & 2 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,19 @@ void Widget::addChild(Widget * widget) {
}

void Widget::removeChild(const Widget *widget) {
mChildren.erase(std::remove(mChildren.begin(), mChildren.end(), widget), mChildren.end());
widget->decRef();
auto it = std::find(mChildren.begin(), mChildren.end(), widget);
if (it == mChildren.end()) {
return;
}
removeChild(it - mChildren.begin());
}

void Widget::removeChild(int index) {
Widget *widget = mChildren[index];
if (widget->focused()) {
requestFocus();
}

mChildren.erase(mChildren.begin() + index);
widget->decRef();
}
Expand Down