From 4d5e144a6ba6601e5fe8f9a75d677f4cdd616473 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 5 Mar 2020 15:30:11 +0100 Subject: [PATCH] LibGUI: Forget some of Window's widgets eagerly on widget unparenting 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. --- Libraries/LibGUI/Widget.cpp | 2 ++ Libraries/LibGUI/Window.cpp | 12 ++++++++++++ Libraries/LibGUI/Window.h | 2 ++ 3 files changed, 16 insertions(+) diff --git a/Libraries/LibGUI/Widget.cpp b/Libraries/LibGUI/Widget.cpp index b225598d5a..6b41646092 100644 --- a/Libraries/LibGUI/Widget.cpp +++ b/Libraries/LibGUI/Widget.cpp @@ -123,6 +123,8 @@ void Widget::child_event(Core::ChildEvent& event) else invalidate_layout(); } + if (event.child() && Core::is(*event.child())) + window()->did_remove_widget({}, Core::to(*event.child())); update(); } return Core::Object::child_event(event); diff --git a/Libraries/LibGUI/Window.cpp b/Libraries/LibGUI/Window.cpp index 07aab281d7..1424c58892 100644 --- a/Libraries/LibGUI/Window.cpp +++ b/Libraries/LibGUI/Window.cpp @@ -689,4 +689,16 @@ void Window::set_size_increment(const Gfx::Size& size_increment) WindowServerConnection::the().send_sync(m_window_id, m_base_size, m_size_increment); } +void Window::did_remove_widget(Badge, 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; +} + } diff --git a/Libraries/LibGUI/Window.h b/Libraries/LibGUI/Window.h index c056637c50..20fa27089f 100644 --- a/Libraries/LibGUI/Window.h +++ b/Libraries/LibGUI/Window.h @@ -179,6 +179,8 @@ public: Action* action_for_key_event(const KeyEvent&); + void did_remove_widget(Badge, const Widget&); + protected: Window(Core::Object* parent = nullptr); virtual void wm_event(WMEvent&);