Skip to content

Commit

Permalink
LibGUI: Forget some of Window's widgets eagerly on widget unparenting
Browse files Browse the repository at this point in the history
Previously the focused widget would only get cleared on replacement or
on destruction (being a WeakPtr and all.) This could lead to window
dispatching events to a focused widget after it had been removed from
the window's widget tree.

The same issue existed for the hovered widget, etc. So this patch
makes sure that we eagerly clear the various widget pointers in Window
immediately when they are removed from the window's widget tree.
  • Loading branch information
awesomekling committed Mar 5, 2020
1 parent e23c5b7 commit 4d5e144
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Libraries/LibGUI/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ void Widget::child_event(Core::ChildEvent& event)
else
invalidate_layout();
}
if (event.child() && Core::is<Widget>(*event.child()))
window()->did_remove_widget({}, Core::to<Widget>(*event.child()));
update();
}
return Core::Object::child_event(event);
Expand Down
12 changes: 12 additions & 0 deletions Libraries/LibGUI/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,16 @@ void Window::set_size_increment(const Gfx::Size& size_increment)
WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement>(m_window_id, m_base_size, m_size_increment);
}

void Window::did_remove_widget(Badge<Widget>, const Widget& widget)
{
if (m_focused_widget == &widget)
m_focused_widget = nullptr;
if (m_hovered_widget == &widget)
m_hovered_widget = nullptr;
if (m_global_cursor_tracking_widget)
m_global_cursor_tracking_widget = nullptr;
if (m_automatic_cursor_tracking_widget)
m_automatic_cursor_tracking_widget = nullptr;
}

}
2 changes: 2 additions & 0 deletions Libraries/LibGUI/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ class Window : public Core::Object {

Action* action_for_key_event(const KeyEvent&);

void did_remove_widget(Badge<Widget>, const Widget&);

protected:
Window(Core::Object* parent = nullptr);
virtual void wm_event(WMEvent&);
Expand Down

0 comments on commit 4d5e144

Please sign in to comment.