From c045ae1a961ada2758f4bb0e663a36aba79805ad Mon Sep 17 00:00:00 2001 From: Arda Cinar Date: Tue, 13 Dec 2022 12:03:12 +0300 Subject: [PATCH] SpaceAnalyzer: Keep the current path via filenames, not indices The tree map widget keeps the current path to allow the user to navigate between directories. This path was being kept as indices into the children array. The indices might change after the tree is regenerated and this change is required to keep the user's current place after a re-analysis --- .../SpaceAnalyzer/TreeMapWidget.cpp | 22 ++++++++++--------- .../SpaceAnalyzer/TreeMapWidget.h | 5 +++-- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp index e65589f782..aa56074b12 100644 --- a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp +++ b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp @@ -6,7 +6,9 @@ */ #include "TreeMapWidget.h" +#include "Tree.h" #include +#include #include #include #include @@ -222,11 +224,11 @@ TreeNode const* TreeMapWidget::path_node(size_t n) const TreeNode const* iter = &m_tree->root(); size_t path_index = 0; while (iter && path_index < m_path.size() && path_index < n) { - size_t child_index = m_path[path_index]; - if (child_index >= iter->num_children()) { + auto child_name = m_path[path_index]; + auto maybe_child = iter->child_with_name(child_name); + if (!maybe_child.has_value()) return nullptr; - } - iter = &iter->child_at(child_index); + iter = &maybe_child.release_value(); path_index++; } return iter; @@ -259,16 +261,16 @@ void TreeMapWidget::paint_event(GUI::PaintEvent& event) } } -Vector TreeMapWidget::path_to_position(Gfx::IntPoint position) +Vector TreeMapWidget::path_to_position(Gfx::IntPoint position) { TreeNode const* node = path_node(m_viewpoint); if (!node) { return {}; } - Vector path; - lay_out_children(*node, frame_inner_rect(), m_viewpoint, [&](TreeNode const&, int index, Gfx::IntRect const& rect, Gfx::IntRect const&, int, HasLabel, IsRemainder is_remainder) { + Vector path; + lay_out_children(*node, frame_inner_rect(), m_viewpoint, [&](TreeNode const& node, int, Gfx::IntRect const& rect, Gfx::IntRect const&, int, HasLabel, IsRemainder is_remainder) { if (is_remainder == IsRemainder::No && rect.contains(position)) { - path.append(index); + path.append(node.name()); } }); return path; @@ -296,7 +298,7 @@ void TreeMapWidget::mousedown_event(GUI::MouseEvent& event) { TreeNode const* node = path_node(m_viewpoint); if (node && !node_is_leaf(*node)) { - Vector path = path_to_position(event.position()); + auto path = path_to_position(event.position()); if (!path.is_empty()) { m_path.shrink(m_viewpoint); m_path.extend(path); @@ -314,7 +316,7 @@ void TreeMapWidget::doubleclick_event(GUI::MouseEvent& event) return; TreeNode const* node = path_node(m_viewpoint); if (node && !node_is_leaf(*node)) { - Vector path = path_to_position(event.position()); + auto path = path_to_position(event.position()); m_path.shrink(m_viewpoint); m_path.extend(path); m_viewpoint = m_path.size(); diff --git a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h index 82cb6d0fe1..2b6241b33f 100644 --- a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h +++ b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h @@ -7,6 +7,7 @@ #pragma once #include "Tree.h" +#include #include #include @@ -49,10 +50,10 @@ private: template void lay_out_children(TreeNode const&, Gfx::IntRect const&, int depth, Function); void paint_cell_frame(GUI::Painter&, TreeNode const&, Gfx::IntRect const&, Gfx::IntRect const&, int depth, HasLabel has_label) const; - Vector path_to_position(Gfx::IntPoint); + Vector path_to_position(Gfx::IntPoint); RefPtr m_tree; - Vector m_path; + Vector m_path; size_t m_viewpoint { 0 }; void const* m_selected_node_cache; };