mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 23:27:42 +00:00
HackStudio: Show the project file list in a tree view
Replace the boring list view with an awesome tree view :^)
This commit is contained in:
parent
c1b4e8aef0
commit
d9706ee882
3 changed files with 187 additions and 15 deletions
|
@ -1,5 +1,39 @@
|
||||||
#include "Project.h"
|
#include "Project.h"
|
||||||
|
#include <AK/FileSystemPath.h>
|
||||||
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibCore/CFile.h>
|
#include <LibCore/CFile.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
struct Project::ProjectTreeNode {
|
||||||
|
enum class Type {
|
||||||
|
Invalid,
|
||||||
|
Project,
|
||||||
|
Directory,
|
||||||
|
File,
|
||||||
|
};
|
||||||
|
|
||||||
|
ProjectTreeNode& find_or_create_subdirectory(const String& name)
|
||||||
|
{
|
||||||
|
for (auto& child : children) {
|
||||||
|
if (child->type == Type::Directory && child->name == name)
|
||||||
|
return *child;
|
||||||
|
}
|
||||||
|
auto new_child = make<ProjectTreeNode>();
|
||||||
|
new_child->type = Type::Directory;
|
||||||
|
new_child->name = name;
|
||||||
|
new_child->parent = this;
|
||||||
|
auto* ptr = new_child.ptr();
|
||||||
|
children.append(move(new_child));
|
||||||
|
return *ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Type type { Type::Invalid };
|
||||||
|
String name;
|
||||||
|
Vector<OwnPtr<ProjectTreeNode>> children;
|
||||||
|
ProjectTreeNode* parent { nullptr };
|
||||||
|
};
|
||||||
|
|
||||||
class ProjectModel final : public GModel {
|
class ProjectModel final : public GModel {
|
||||||
public:
|
public:
|
||||||
|
@ -8,22 +42,80 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual int row_count(const GModelIndex& = GModelIndex()) const override { return m_project.m_files.size(); }
|
virtual int row_count(const GModelIndex& index) const override
|
||||||
virtual int column_count(const GModelIndex& = GModelIndex()) const override { return 1; }
|
{
|
||||||
|
if (!index.is_valid())
|
||||||
|
return 1;
|
||||||
|
auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
|
||||||
|
return node->children.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual int column_count(const GModelIndex&) const override
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
virtual GVariant data(const GModelIndex& index, Role role = Role::Display) const override
|
virtual GVariant data(const GModelIndex& index, Role role = Role::Display) const override
|
||||||
{
|
{
|
||||||
int row = index.row();
|
auto* node = static_cast<Project::ProjectTreeNode*>(index.internal_data());
|
||||||
if (role == Role::Display) {
|
if (role == Role::Display) {
|
||||||
return m_project.m_files.at(row).name();
|
return FileSystemPath(node->name).basename();
|
||||||
|
}
|
||||||
|
if (role == Role::Custom) {
|
||||||
|
return node->name;
|
||||||
|
}
|
||||||
|
if (role == Role::Icon) {
|
||||||
|
if (node->type == Project::ProjectTreeNode::Type::Project)
|
||||||
|
return m_project.m_project_icon;
|
||||||
|
if (node->type == Project::ProjectTreeNode::Type::Directory)
|
||||||
|
return m_project.m_directory_icon;
|
||||||
|
if (node->name.ends_with(".cpp"))
|
||||||
|
return m_project.m_cplusplus_icon;
|
||||||
|
if (node->name.ends_with(".h"))
|
||||||
|
return m_project.m_header_icon;
|
||||||
|
return m_project.m_file_icon;
|
||||||
}
|
}
|
||||||
if (role == Role::Font) {
|
if (role == Role::Font) {
|
||||||
extern String g_currently_open_file;
|
extern String g_currently_open_file;
|
||||||
if (m_project.m_files.at(row).name() == g_currently_open_file)
|
if (node->name == g_currently_open_file)
|
||||||
return Font::default_bold_font();
|
return Font::default_bold_font();
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual GModelIndex index(int row, int column = 0, const GModelIndex& parent = GModelIndex()) const override
|
||||||
|
{
|
||||||
|
if (!parent.is_valid()) {
|
||||||
|
return create_index(row, column, &m_project.root_node());
|
||||||
|
}
|
||||||
|
auto& node = *static_cast<Project::ProjectTreeNode*>(parent.internal_data());
|
||||||
|
return create_index(row, column, node.children.at(row).ptr());
|
||||||
|
}
|
||||||
|
|
||||||
|
GModelIndex parent_index(const GModelIndex& index) const override
|
||||||
|
{
|
||||||
|
if (!index.is_valid())
|
||||||
|
return {};
|
||||||
|
auto& node = *static_cast<Project::ProjectTreeNode*>(index.internal_data());
|
||||||
|
if (!node.parent)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
if (!node.parent->parent) {
|
||||||
|
return create_index(0, 0, &m_project.root_node());
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int row = 0; row < node.parent->parent->children.size(); ++row) {
|
||||||
|
if (node.parent->parent->children[row].ptr() == node.parent)
|
||||||
|
return create_index(row, 0, node.parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT_NOT_REACHED();
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
virtual void update() override
|
virtual void update() override
|
||||||
{
|
{
|
||||||
did_update();
|
did_update();
|
||||||
|
@ -36,10 +128,23 @@ private:
|
||||||
Project::Project(const String& path, Vector<String>&& filenames)
|
Project::Project(const String& path, Vector<String>&& filenames)
|
||||||
: m_path(path)
|
: m_path(path)
|
||||||
{
|
{
|
||||||
|
m_file_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
|
||||||
|
m_cplusplus_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-cplusplus.png"));
|
||||||
|
m_header_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-header.png"));
|
||||||
|
m_directory_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/filetype-folder.png"));
|
||||||
|
m_project_icon = GIcon(GraphicsBitmap::load_from_file("/res/icons/16x16/app-hack-studio.png"));
|
||||||
|
|
||||||
for (auto& filename : filenames) {
|
for (auto& filename : filenames) {
|
||||||
m_files.append(ProjectFile::construct_with_name(filename));
|
m_files.append(ProjectFile::construct_with_name(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
m_model = adopt(*new ProjectModel(*this));
|
m_model = adopt(*new ProjectModel(*this));
|
||||||
|
|
||||||
|
rebuild_tree();
|
||||||
|
}
|
||||||
|
|
||||||
|
Project::~Project()
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
OwnPtr<Project> Project::load_from_file(const String& path)
|
OwnPtr<Project> Project::load_from_file(const String& path)
|
||||||
|
@ -88,3 +193,58 @@ ProjectFile* Project::get_file(const String& filename)
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Project::rebuild_tree()
|
||||||
|
{
|
||||||
|
auto root = make<ProjectTreeNode>();
|
||||||
|
root->type = ProjectTreeNode::Type::Project;
|
||||||
|
|
||||||
|
for (auto& file : m_files) {
|
||||||
|
FileSystemPath path(file.name());
|
||||||
|
ProjectTreeNode* current = root.ptr();
|
||||||
|
StringBuilder partial_path;
|
||||||
|
|
||||||
|
for (int i = 0; i < path.parts().size(); ++i) {
|
||||||
|
auto& part = path.parts().at(i);
|
||||||
|
if (part == ".")
|
||||||
|
continue;
|
||||||
|
if (i != path.parts().size() - 1) {
|
||||||
|
current = ¤t->find_or_create_subdirectory(part);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
struct stat st;
|
||||||
|
if (lstat(path.string().characters(), &st) < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (S_ISDIR(st.st_mode)) {
|
||||||
|
current = ¤t->find_or_create_subdirectory(part);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto file_node = make<ProjectTreeNode>();
|
||||||
|
file_node->name = file.name();
|
||||||
|
file_node->type = Project::ProjectTreeNode::Type::File;
|
||||||
|
file_node->parent = current;
|
||||||
|
current->children.append(move(file_node));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Function<void(ProjectTreeNode&, int indent)> dump_tree = [&](ProjectTreeNode& node, int indent) {
|
||||||
|
for (int i = 0; i < indent; ++i)
|
||||||
|
printf(" ");
|
||||||
|
if (node.name.is_null())
|
||||||
|
printf("(null)\n");
|
||||||
|
else
|
||||||
|
printf("%s\n", node.name.characters());
|
||||||
|
for (auto& child : node.children) {
|
||||||
|
dump_tree(*child, indent + 2);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
printf("begin tree dump\n");
|
||||||
|
dump_tree(*root, 0);
|
||||||
|
printf("end tree dump\n");
|
||||||
|
|
||||||
|
m_root_node = move(root);
|
||||||
|
m_model->update();
|
||||||
|
}
|
||||||
|
|
|
@ -4,12 +4,15 @@
|
||||||
#include <AK/Noncopyable.h>
|
#include <AK/Noncopyable.h>
|
||||||
#include <AK/NonnullRefPtrVector.h>
|
#include <AK/NonnullRefPtrVector.h>
|
||||||
#include <AK/OwnPtr.h>
|
#include <AK/OwnPtr.h>
|
||||||
|
#include <LibGUI/GIcon.h>
|
||||||
#include <LibGUI/GModel.h>
|
#include <LibGUI/GModel.h>
|
||||||
|
|
||||||
class Project {
|
class Project {
|
||||||
AK_MAKE_NONCOPYABLE(Project)
|
AK_MAKE_NONCOPYABLE(Project)
|
||||||
AK_MAKE_NONMOVABLE(Project)
|
AK_MAKE_NONMOVABLE(Project)
|
||||||
public:
|
public:
|
||||||
|
~Project();
|
||||||
|
|
||||||
static OwnPtr<Project> load_from_file(const String& path);
|
static OwnPtr<Project> load_from_file(const String& path);
|
||||||
|
|
||||||
[[nodiscard]] bool add_file(const String& filename);
|
[[nodiscard]] bool add_file(const String& filename);
|
||||||
|
@ -26,12 +29,22 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class ProjectModel;
|
friend class ProjectModel;
|
||||||
|
struct ProjectTreeNode;
|
||||||
explicit Project(const String& path, Vector<String>&& files);
|
explicit Project(const String& path, Vector<String>&& files);
|
||||||
|
|
||||||
|
const ProjectTreeNode& root_node() const { return *m_root_node; }
|
||||||
|
void rebuild_tree();
|
||||||
|
|
||||||
String m_path;
|
String m_path;
|
||||||
RefPtr<GModel> m_model;
|
RefPtr<GModel> m_model;
|
||||||
NonnullRefPtrVector<ProjectFile> m_files;
|
NonnullRefPtrVector<ProjectFile> m_files;
|
||||||
|
OwnPtr<ProjectTreeNode> m_root_node;
|
||||||
|
|
||||||
|
GIcon m_directory_icon;
|
||||||
|
GIcon m_file_icon;
|
||||||
|
GIcon m_cplusplus_icon;
|
||||||
|
GIcon m_header_icon;
|
||||||
|
GIcon m_project_icon;
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
#include <LibGUI/GFilePicker.h>
|
#include <LibGUI/GFilePicker.h>
|
||||||
#include <LibGUI/GInputBox.h>
|
#include <LibGUI/GInputBox.h>
|
||||||
#include <LibGUI/GLabel.h>
|
#include <LibGUI/GLabel.h>
|
||||||
#include <LibGUI/GListView.h>
|
|
||||||
#include <LibGUI/GMenu.h>
|
#include <LibGUI/GMenu.h>
|
||||||
#include <LibGUI/GMenuBar.h>
|
#include <LibGUI/GMenuBar.h>
|
||||||
#include <LibGUI/GMessageBox.h>
|
#include <LibGUI/GMessageBox.h>
|
||||||
|
@ -43,7 +42,7 @@ RefPtr<EditorWrapper> g_current_editor_wrapper;
|
||||||
String g_currently_open_file;
|
String g_currently_open_file;
|
||||||
OwnPtr<Project> g_project;
|
OwnPtr<Project> g_project;
|
||||||
RefPtr<GWindow> g_window;
|
RefPtr<GWindow> g_window;
|
||||||
RefPtr<GListView> g_project_list_view;
|
RefPtr<GTreeView> g_project_tree_view;
|
||||||
RefPtr<GStackWidget> g_right_hand_stack;
|
RefPtr<GStackWidget> g_right_hand_stack;
|
||||||
RefPtr<GSplitter> g_text_inner_splitter;
|
RefPtr<GSplitter> g_text_inner_splitter;
|
||||||
RefPtr<GWidget> g_form_inner_container;
|
RefPtr<GWidget> g_form_inner_container;
|
||||||
|
@ -120,10 +119,10 @@ int main(int argc, char** argv)
|
||||||
auto toolbar = GToolBar::construct(widget);
|
auto toolbar = GToolBar::construct(widget);
|
||||||
|
|
||||||
auto outer_splitter = GSplitter::construct(Orientation::Horizontal, widget);
|
auto outer_splitter = GSplitter::construct(Orientation::Horizontal, widget);
|
||||||
g_project_list_view = GListView::construct(outer_splitter);
|
g_project_tree_view = GTreeView::construct(outer_splitter);
|
||||||
g_project_list_view->set_model(g_project->model());
|
g_project_tree_view->set_model(g_project->model());
|
||||||
g_project_list_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
g_project_tree_view->set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
|
||||||
g_project_list_view->set_preferred_size(140, 0);
|
g_project_tree_view->set_preferred_size(140, 0);
|
||||||
|
|
||||||
g_right_hand_stack = GStackWidget::construct(outer_splitter);
|
g_right_hand_stack = GStackWidget::construct(outer_splitter);
|
||||||
|
|
||||||
|
@ -306,8 +305,8 @@ int main(int argc, char** argv)
|
||||||
toolbar->add_action(GCommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
|
toolbar->add_action(GCommonActions::make_redo_action([&](auto&) { current_editor().redo_action().activate(); }));
|
||||||
toolbar->add_separator();
|
toolbar->add_separator();
|
||||||
|
|
||||||
g_project_list_view->on_activation = [&](auto& index) {
|
g_project_tree_view->on_activation = [&](auto& index) {
|
||||||
auto filename = g_project_list_view->model()->data(index).to_string();
|
auto filename = g_project_tree_view->model()->data(index, GModel::Role::Custom).to_string();
|
||||||
open_file(filename);
|
open_file(filename);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -512,7 +511,7 @@ void open_file(const String& filename)
|
||||||
|
|
||||||
g_currently_open_file = filename;
|
g_currently_open_file = filename;
|
||||||
g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
|
g_window->set_title(String::format("%s - HackStudio", g_currently_open_file.characters()));
|
||||||
g_project_list_view->update();
|
g_project_tree_view->update();
|
||||||
|
|
||||||
current_editor_wrapper().filename_label().set_text(filename);
|
current_editor_wrapper().filename_label().set_text(filename);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue