Преглед на файлове

SpaceAnalyzer: Do not reset the path to '/' after a refresh

Clicking analyze or deleting a file would always cause the tree view in
the widget to reset back to viewing the root. This was changed to stay
in the directory currently being viewed
Arda Cinar преди 2 години
родител
ревизия
33ea96912a

+ 18 - 2
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> tree)
 {
     m_tree = tree;
-    m_path.clear();
-    m_viewpoint = 0;
+    recalculate_path_for_new_tree();
+
     if (on_path_change) {
         on_path_change();
     }

+ 1 - 0
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<DeprecatedString> path_to_position(Gfx::IntPoint);
+    void recalculate_path_for_new_tree();
 
     RefPtr<Tree> m_tree;
     Vector<DeprecatedString> m_path;

+ 7 - 4
Userland/Applications/SpaceAnalyzer/main.cpp

@@ -183,7 +183,10 @@ ErrorOr<int> 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<int> 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()));
         }
     });