diff --git a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp index aa56074b12..87505d06d4 100644 --- a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp +++ b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp @@ -357,11 +357,27 @@ void TreeMapWidget::context_menu_event(GUI::ContextMenuEvent& context_menu_event on_context_menu_request(context_menu_event); } +void TreeMapWidget::recalculate_path_for_new_tree() +{ + TreeNode const* current = &m_tree->root(); + size_t new_path_length = 0; + for (auto& segment : m_path) { + auto maybe_child = current->child_with_name(segment); + if (!maybe_child.has_value()) + break; + new_path_length++; + current = &maybe_child.release_value(); + } + m_path.shrink(new_path_length); + if (new_path_length < m_viewpoint) + m_viewpoint = new_path_length - 1; +} + void TreeMapWidget::set_tree(RefPtr tree) { m_tree = tree; - m_path.clear(); - m_viewpoint = 0; + recalculate_path_for_new_tree(); + if (on_path_change) { on_path_change(); } diff --git a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h index 2b6241b33f..4c0424aa67 100644 --- a/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h +++ b/Userland/Applications/SpaceAnalyzer/TreeMapWidget.h @@ -51,6 +51,7 @@ private: 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); + void recalculate_path_for_new_tree(); RefPtr m_tree; Vector m_path; diff --git a/Userland/Applications/SpaceAnalyzer/main.cpp b/Userland/Applications/SpaceAnalyzer/main.cpp index b36e5b6a68..31d68091fc 100644 --- a/Userland/Applications/SpaceAnalyzer/main.cpp +++ b/Userland/Applications/SpaceAnalyzer/main.cpp @@ -183,7 +183,10 @@ ErrorOr serenity_main(Main::Arguments arguments) auto& file_menu = window->add_menu("&File"); file_menu.add_action(GUI::Action::create("&Analyze", [&](auto&) { - if (auto result = analyze(tree, treemapwidget, statusbar); result.is_error()) { + // FIXME: Just modify the tree in memory instead of traversing the entire file system + // FIXME: Dispose of the old tree + auto new_tree = adopt_ref(*new Tree("")); + if (auto result = analyze(new_tree, treemapwidget, statusbar); result.is_error()) { GUI::MessageBox::show_error(window, DeprecatedString::formatted("{}", result.error())); } })); @@ -237,9 +240,9 @@ ErrorOr serenity_main(Main::Arguments arguments) } } - // TODO: Refreshing data always causes resetting the viewport back to "/". - // It would be great if we found a way to preserve viewport across refreshes. - if (auto result = analyze(tree, treemapwidget, statusbar); result.is_error()) { + // FIXME: Dispose of the old tree + auto new_tree = adopt_ref(*new Tree("")); + if (auto result = analyze(new_tree, treemapwidget, statusbar); result.is_error()) { GUI::MessageBox::show_error(window, DeprecatedString::formatted("{}", result.error())); } });