Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dangling children from Screen #341

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@ -179,7 +179,7 @@ class NANOGUI_EXPORT Screen : public Widget {

/* Internal helper functions */
void updateFocus(Widget *widget);
void disposeWindow(Window *window);
void disposeWidget(Widget *widget);
void centerWindow(Window *window);
void moveWindowToFront(Window *window);
void drawWidgets();
Expand Down
7 changes: 7 additions & 0 deletions include/nanogui/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ class NANOGUI_EXPORT Widget : public Object {
*/
inline float icon_scale() const { return mTheme->mIconScale * mIconExtraScale; }

private:
/**
* Convenience function to share logic between both signatures of
* ``removeChild``.
*/
void removeChildHelper(const std::vector<Widget *>::iterator& child_it);

protected:
Widget *mParent;
ref<Theme> mTheme;
Expand Down
2 changes: 1 addition & 1 deletion python/py_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2598,7 +2598,7 @@ static const char *__doc_nanogui_Screen_charCallbackEvent = R"doc()doc";

static const char *__doc_nanogui_Screen_cursorPosCallbackEvent = R"doc()doc";

static const char *__doc_nanogui_Screen_disposeWindow = R"doc()doc";
static const char *__doc_nanogui_Screen_disposeWidget = R"doc(Remove Screen's references to a widget and its children)doc";

static const char *__doc_nanogui_Screen_drawAll = R"doc(Draw the Screen contents)doc";

Expand Down
13 changes: 9 additions & 4 deletions src/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,17 @@ void Screen::updateFocus(Widget *widget) {
moveWindowToFront((Window *) window);
}

void Screen::disposeWindow(Window *window) {
if (std::find(mFocusPath.begin(), mFocusPath.end(), window) != mFocusPath.end())
void Screen::disposeWidget(Widget *widget) {
if (std::find(mFocusPath.begin(), mFocusPath.end(), widget) != mFocusPath.end())
mFocusPath.clear();
if (mDragWidget == window)

if (mDragWidget == widget) {
mDragWidget = nullptr;
removeChild(window);
mDragActive = false;
}

for (auto child : widget->children())
disposeWidget(child);
}

void Screen::centerWindow(Window *window) {
Expand Down
18 changes: 14 additions & 4 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,23 @@ void Widget::addChild(Widget * widget) {
}

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

void Widget::removeChild(int index) {
Widget *widget = mChildren[index];
mChildren.erase(mChildren.begin() + index);
assert(index >= 0);
assert(index < childCount());
removeChildHelper(mChildren.begin() + index);
}

void Widget::removeChildHelper(const std::vector<Widget *>::iterator& child_it) {
if (child_it == mChildren.end())
return;
Widget *widget = *child_it;

screen()->disposeWidget(widget);
mChildren.erase(child_it);

widget->decRef();
}

Expand Down
2 changes: 1 addition & 1 deletion src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void Window::dispose() {
Widget *widget = this;
while (widget->parent())
widget = widget->parent();
((Screen *) widget)->disposeWindow(this);
((Screen *) widget)->removeChild(this);
}

void Window::center() {
Expand Down