From eb34015748568120c74b37599e70ed40fc2f348a Mon Sep 17 00:00:00 2001 From: Arif Orhun Uzun Date: Wed, 30 Mar 2022 22:44:00 +0300 Subject: [PATCH] SpaceAnalyzer: Fix the crash caused by using arrow keys Previously, SpaceAnalyzer set focus on the selected BreadcrumbButton. Using arrow keys triggered the keydown_event of the AbstractButton, which later on caused a Function object to be deleted while it is still being used. This change sets the focus on TreeMapWidget and adds an event handler to TreeMapWidget for keydown events. Fixes #13254. --- Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp | 8 ++++++++ Userland/Applications/SpaceAnalyzer/TreeMapWidget.h | 1 + Userland/Applications/SpaceAnalyzer/main.cpp | 2 ++ 3 files changed, 11 insertions(+) diff --git a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp index 5409b358ff..d9a8160ed7 100644 --- a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp +++ b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp @@ -306,6 +306,14 @@ void TreeMapWidget::doubleclick_event(GUI::MouseEvent& event) } } +void TreeMapWidget::keydown_event(GUI::KeyEvent& event) +{ + if (event.key() == KeyCode::Key_Left) + set_viewpoint(m_viewpoint == 0 ? m_path.size() : m_viewpoint - 1); + else if (event.key() == KeyCode::Key_Right) + set_viewpoint(m_viewpoint == m_path.size() ? 0 : m_viewpoint + 1); +} + void TreeMapWidget::mousewheel_event(GUI::MouseEvent& event) { int delta = event.wheel_delta_y(); diff --git a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h index 1e3ee8fc2c..9c0ef8382d 100644 --- a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h +++ b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h @@ -47,6 +47,7 @@ private: virtual void doubleclick_event(GUI::MouseEvent&) override; virtual void mousewheel_event(GUI::MouseEvent&) override; virtual void context_menu_event(GUI::ContextMenuEvent&) override; + virtual void keydown_event(GUI::KeyEvent&) override; bool rect_can_contain_children(const Gfx::IntRect& rect) const; bool rect_can_contain_label(const Gfx::IntRect& rect) const; diff --git a/Userland/Applications/SpaceAnalyzer/main.cpp b/Userland/Applications/SpaceAnalyzer/main.cpp index 8e6307ad09..b53b67be15 100644 --- a/Userland/Applications/SpaceAnalyzer/main.cpp +++ b/Userland/Applications/SpaceAnalyzer/main.cpp @@ -320,6 +320,8 @@ ErrorOr serenity_main(Main::Arguments arguments) auto& treemapwidget = *mainwidget.find_descendant_of_type_named("tree_map"); auto& statusbar = *mainwidget.find_descendant_of_type_named("statusbar"); + treemapwidget.set_focus(true); + auto& file_menu = window->add_menu("&File"); file_menu.add_action(GUI::Action::create("&Analyze", [&](auto&) { analyze(tree, treemapwidget, statusbar);