From 4d2782db5a4e1e257500d70c57068250953b3d9b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 16 Sep 2020 17:47:34 +0200 Subject: LibGUI: Allow FileSystemModel to be rooted one level above "/" You can now construct a FileSystemModel with a null String() as the root path. This will root it one level above "/" which makes the root directory itself selectable as a child. This will be useful in some places, e.g the FileManager application. --- Libraries/LibGUI/FileSystemModel.cpp | 22 ++++++++++++++++++++-- Libraries/LibGUI/FileSystemModel.h | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'Libraries/LibGUI') diff --git a/Libraries/LibGUI/FileSystemModel.cpp b/Libraries/LibGUI/FileSystemModel.cpp index 6e5d11eab6..690258c091 100644 --- a/Libraries/LibGUI/FileSystemModel.cpp +++ b/Libraries/LibGUI/FileSystemModel.cpp @@ -89,7 +89,18 @@ void FileSystemModel::Node::traverse_if_needed() { if (!is_directory() || has_traversed) return; + has_traversed = true; + + if (m_parent_of_root) { + auto root = adopt_own(*new Node(m_model)); + root->fetch_data("/", true); + root->name = "/"; + root->parent = this; + children.append(move(root)); + return; + } + total_size = 0; auto full_path = this->full_path(); @@ -149,7 +160,7 @@ void FileSystemModel::Node::reify_if_needed() traverse_if_needed(); if (mode != 0) return; - fetch_data(full_path(), parent == nullptr); + fetch_data(full_path(), parent == nullptr || parent->m_parent_of_root); } String FileSystemModel::Node::full_path() const @@ -290,7 +301,10 @@ void FileSystemModel::update_node_on_selection(const ModelIndex& index, const bo void FileSystemModel::set_root_path(const StringView& root_path) { - m_root_path = LexicalPath::canonicalized_path(root_path); + if (root_path.is_null()) + m_root_path = {}; + else + m_root_path = LexicalPath::canonicalized_path(root_path); update(); if (m_root->has_error()) { @@ -304,6 +318,10 @@ void FileSystemModel::set_root_path(const StringView& root_path) void FileSystemModel::update() { m_root = adopt_own(*new Node(*this)); + + if (m_root_path.is_null()) + m_root->m_parent_of_root = true; + m_root->reify_if_needed(); did_update(); diff --git a/Libraries/LibGUI/FileSystemModel.h b/Libraries/LibGUI/FileSystemModel.h index 7948f128d0..1afe6946e2 100644 --- a/Libraries/LibGUI/FileSystemModel.h +++ b/Libraries/LibGUI/FileSystemModel.h @@ -109,6 +109,7 @@ public: RefPtr m_notifier; int m_error { 0 }; + bool m_parent_of_root { false }; ModelIndex index(int column) const; void traverse_if_needed(); -- cgit v1.2.3