summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-09-16 17:47:34 +0200
committerAndreas Kling <kling@serenityos.org>2020-09-16 21:08:55 +0200
commit4d2782db5a4e1e257500d70c57068250953b3d9b (patch)
treec53adc4b713787821e54539c4e3365a652fb8fc2 /Libraries/LibGUI
parenta9f7b576a484597992670dff0900da50043fe3bd (diff)
downloadserenity-4d2782db5a4e1e257500d70c57068250953b3d9b.zip
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.
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r--Libraries/LibGUI/FileSystemModel.cpp22
-rw-r--r--Libraries/LibGUI/FileSystemModel.h1
2 files changed, 21 insertions, 2 deletions
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<Core::Notifier> m_notifier;
int m_error { 0 };
+ bool m_parent_of_root { false };
ModelIndex index(int column) const;
void traverse_if_needed();