Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
github is being fucking stupid (or i am)
  • Loading branch information
2048khz-gachi-rmx committed Dec 17, 2022
1 parent f5020e2 commit 4173c0f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 20 deletions.
2 changes: 1 addition & 1 deletion include/nanogui/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class NANOGUI_EXPORT Screen : public Widget {

/* Internal helper functions */
void update_focus(Widget *widget);
void dispose_window(Window *window);
void dispose_widget(Widget *widget);
void center_window(Window *window);
void move_window_to_front(Window *window);
void draw_widgets();
Expand Down
19 changes: 19 additions & 0 deletions include/nanogui/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,18 @@ class NANOGUI_EXPORT Widget : public Object {
*/
void set_icon_extra_scale(float scale) { m_icon_extra_scale = scale; }

/**
* The amount of extra scaling applied to *icon* fonts.
* See \ref nanogui::Widget::mIconExtraScale.
*/
float iconExtraScale() const { return mIconExtraScale; }

/**
* Sets the amount of extra scaling applied to *icon* fonts.
* See \ref nanogui::Widget::mIconExtraScale.
*/
void setIconExtraScale(float scale) { mIconExtraScale = scale; }

/// Return a pointer to the cursor of the widget
Cursor cursor() const { return m_cursor; }
/// Set the cursor of the widget
Expand Down Expand Up @@ -270,6 +282,13 @@ class NANOGUI_EXPORT Widget : public Object {
*/
float icon_scale() const { return m_theme->m_icon_scale * m_icon_extra_scale; }

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

protected:
Widget *m_parent;
ref<Theme> m_theme;
Expand Down
19 changes: 13 additions & 6 deletions src/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,12 +915,19 @@ void Screen::update_focus(Widget *widget) {
move_window_to_front((Window *) window);
}

void Screen::dispose_window(Window *window) {
if (std::find(m_focus_path.begin(), m_focus_path.end(), window) != m_focus_path.end())
m_focus_path.clear();
if (m_drag_widget == window)
m_drag_widget = nullptr;
remove_child(window);
void Screen::dispose_widget(Widget *widget) {
if (std::find(mFocusPath.begin(), mFocusPath.end(), widget) != mFocusPath.end())
mFocusPath.clear();

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

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

remove_child(widget);
}

void Screen::center_window(Window *window) {
Expand Down
29 changes: 17 additions & 12 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,20 +177,24 @@ void Widget::add_child(Widget * widget) {
}

void Widget::remove_child(const Widget *widget) {
size_t child_count = m_children.size();
m_children.erase(std::remove(m_children.begin(), m_children.end(), widget),
m_children.end());
if (m_children.size() == child_count)
throw std::runtime_error("Widget::remove_child(): widget not found!");
widget->dec_ref();
removeChildHelper(std::find(m_children.begin(), m_children.end(), widget));
}

void Widget::remove_child_at(int index) {
if (index < 0 || index >= (int) m_children.size())
throw std::runtime_error("Widget::remove_child_at(): out of bounds!");
Widget *widget = m_children[index];
m_children.erase(m_children.begin() + index);
widget->dec_ref();
assert(index >= 0);
assert(index < child_count);
removeChildHelper(m_children.begin() + index);
}

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

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

widget->decRef();
}

int Widget::child_index(Widget *widget) const {
Expand All @@ -216,7 +220,8 @@ Screen *Widget::screen() {
Widget *widget = this;
while (true) {
if (!widget)
return nullptr;
throw std::runtime_error(
"Widget:internal error (could not find parent screen)");
Screen *screen = dynamic_cast<Screen *>(widget);
if (screen)
return screen;
Expand Down
2 changes: 1 addition & 1 deletion src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void Window::dispose() {
Widget *widget = this;
while (widget->parent())
widget = widget->parent();
((Screen *) widget)->dispose_window(this);
((Screen *) widget)->remove_child(this);
}

void Window::center() {
Expand Down

0 comments on commit 4173c0f

Please sign in to comment.