diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-30 03:27:25 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-30 03:27:25 +0100 |
commit | 2c6a597d7767d38fc5c8e743dd57f13733c1287f (patch) | |
tree | 81a62b8d43b7f8be8b580bd82605e8cefebbfc8f /LibGUI | |
parent | f10e0d054612eef415ce96a0e2383910d39668c0 (diff) | |
download | serenity-2c6a597d7767d38fc5c8e743dd57f13733c1287f.zip |
FileManager: Make the tree view follow the directory view navigations.
Diffstat (limited to 'LibGUI')
-rw-r--r-- | LibGUI/GAbstractView.cpp | 4 | ||||
-rw-r--r-- | LibGUI/GAbstractView.h | 3 | ||||
-rw-r--r-- | LibGUI/GFileSystemModel.cpp | 24 | ||||
-rw-r--r-- | LibGUI/GFileSystemModel.h | 1 | ||||
-rw-r--r-- | LibGUI/GModel.cpp | 3 | ||||
-rw-r--r-- | LibGUI/GTreeView.cpp | 29 | ||||
-rw-r--r-- | LibGUI/GTreeView.h | 2 |
7 files changed, 64 insertions, 2 deletions
diff --git a/LibGUI/GAbstractView.cpp b/LibGUI/GAbstractView.cpp index b1f64ae7d4..5c2c704342 100644 --- a/LibGUI/GAbstractView.cpp +++ b/LibGUI/GAbstractView.cpp @@ -35,3 +35,7 @@ void GAbstractView::did_update_model() { model_notification(GModelNotification(GModelNotification::ModelUpdated)); } + +void GAbstractView::did_update_selection() +{ +} diff --git a/LibGUI/GAbstractView.h b/LibGUI/GAbstractView.h index 99fa48e9cd..5048d26048 100644 --- a/LibGUI/GAbstractView.h +++ b/LibGUI/GAbstractView.h @@ -14,10 +14,9 @@ public: GModel* model() { return m_model.ptr(); } const GModel* model() const { return m_model.ptr(); } - void scroll_into_view(const GModelIndex&, Orientation); - virtual bool accepts_focus() const override { return true; } virtual void did_update_model(); + virtual void did_update_selection(); Function<void(const GModelNotification&)> on_model_notification; diff --git a/LibGUI/GFileSystemModel.cpp b/LibGUI/GFileSystemModel.cpp index b13b863838..b7500df2fb 100644 --- a/LibGUI/GFileSystemModel.cpp +++ b/LibGUI/GFileSystemModel.cpp @@ -92,6 +92,30 @@ struct GFileSystemModel::Node { } }; +GModelIndex GFileSystemModel::index(const String& path) const +{ + FileSystemPath canonical_path(path); + const Node* node = m_root; + if (canonical_path.string() == "/") + return m_root->index(*this); + for (int i = 0; i < canonical_path.parts().size(); ++i) { + auto& part = canonical_path.parts()[i]; + bool found = false; + for (auto& child : node->children) { + if (child->name == part) { + node = child; + found = true; + if (i == canonical_path.parts().size() - 1) + return node->index(*this); + break; + } + } + if (!found) + return { }; + } + return { }; +} + String GFileSystemModel::path(const GModelIndex& index) const { if (!index.is_valid()) diff --git a/LibGUI/GFileSystemModel.h b/LibGUI/GFileSystemModel.h index 6e409fc4e0..1169f467de 100644 --- a/LibGUI/GFileSystemModel.h +++ b/LibGUI/GFileSystemModel.h @@ -15,6 +15,7 @@ public: String root_path() const { return m_root_path; } String path(const GModelIndex&) const; + GModelIndex index(const String& path) const; virtual int row_count(const GModelIndex& = GModelIndex()) const override; virtual int column_count(const GModelIndex& = GModelIndex()) const override; diff --git a/LibGUI/GModel.cpp b/LibGUI/GModel.cpp index 4f7742f0da..0447b27d02 100644 --- a/LibGUI/GModel.cpp +++ b/LibGUI/GModel.cpp @@ -41,6 +41,9 @@ void GModel::set_selected_index(const GModelIndex& index) m_selected_index = index; if (on_selection_changed) on_selection_changed(index); + for_each_view([] (auto& view) { + view.did_update_selection(); + }); if (m_activates_on_selection && is_valid(index)) activate(index); } diff --git a/LibGUI/GTreeView.cpp b/LibGUI/GTreeView.cpp index b91c35ec6d..475e3cdd81 100644 --- a/LibGUI/GTreeView.cpp +++ b/LibGUI/GTreeView.cpp @@ -270,3 +270,32 @@ void GTreeView::paint_event(GPaintEvent& event) return IterationDecision::Continue; }); } + +void GTreeView::scroll_into_view(const GModelIndex& a_index, Orientation orientation) +{ + if (!a_index.is_valid()) + return; + Rect found_rect; + traverse_in_paint_order([&] (const GModelIndex& index, const Rect& rect, int) { + if (index == a_index) { + found_rect = rect; + return IterationDecision::Abort; + } + return IterationDecision::Continue; + }); + GScrollableWidget::scroll_into_view(found_rect, orientation); +} + +void GTreeView::did_update_selection() +{ + ASSERT(model()); + auto& model = *this->model(); + auto index = model.selected_index(); + if (!index.is_valid()) + return; + auto parent = index.parent(); + while (parent.is_valid()) { + ensure_metadata_for_index(parent).open = true; + parent = parent.parent(); + } +} diff --git a/LibGUI/GTreeView.h b/LibGUI/GTreeView.h index 3b251ac58f..6bf92778e8 100644 --- a/LibGUI/GTreeView.h +++ b/LibGUI/GTreeView.h @@ -7,6 +7,7 @@ public: explicit GTreeView(GWidget*); virtual ~GTreeView() override; + virtual void scroll_into_view(const GModelIndex&, Orientation); virtual const char* class_name() const override { return "GTreeView"; } GModelIndex index_at_content_position(const Point&) const; @@ -14,6 +15,7 @@ public: protected: virtual void paint_event(GPaintEvent&) override; virtual void mousedown_event(GMouseEvent&) override; + virtual void did_update_selection() override; private: int item_height() const { return 16; } |