mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:07:34 +00:00
HackStudio: Project templates and New Project dialog
This commit adds a simple project template system to HackStudio, as well as a pretty New Project dialog, inspired by early VS.NET and MS Office.
This commit is contained in:
parent
a6fdc17f3f
commit
b671577223
24 changed files with 1178 additions and 1 deletions
244
Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.cpp
Normal file
244
Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.cpp
Normal file
|
@ -0,0 +1,244 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Nick Vella <nick@nxk.io>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "NewProjectDialog.h"
|
||||
#include "ProjectTemplatesModel.h"
|
||||
#include <DevTools/HackStudio/Dialogs/NewProjectDialogGML.h>
|
||||
#include <DevTools/HackStudio/ProjectTemplate.h>
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/FilePicker.h>
|
||||
#include <LibGUI/IconView.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/RadioButton.h>
|
||||
#include <LibGUI/TextBox.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
#include <LibGfx/Font.h>
|
||||
#include <LibGfx/FontDatabase.h>
|
||||
#include <LibRegex/Regex.h>
|
||||
|
||||
namespace HackStudio {
|
||||
|
||||
static const Regex<PosixExtended> s_project_name_validity_regex("^([A-Za-z0-9_-])*$");
|
||||
|
||||
int NewProjectDialog::show(GUI::Window* parent_window)
|
||||
{
|
||||
auto dialog = NewProjectDialog::construct(parent_window);
|
||||
|
||||
if (parent_window)
|
||||
dialog->set_icon(parent_window->icon());
|
||||
|
||||
auto result = dialog->exec();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
NewProjectDialog::NewProjectDialog(GUI::Window* parent)
|
||||
: Dialog(parent)
|
||||
, m_model(ProjectTemplatesModel::create())
|
||||
{
|
||||
resize(500, 385);
|
||||
center_on_screen();
|
||||
set_resizable(false);
|
||||
set_modal(true);
|
||||
set_title("New project");
|
||||
|
||||
auto& main_widget = set_main_widget<GUI::Widget>();
|
||||
main_widget.load_from_gml(new_project_dialog_gml);
|
||||
|
||||
m_icon_view_container = *main_widget.find_descendant_of_type_named<GUI::Widget>("icon_view_container");
|
||||
m_icon_view = m_icon_view_container->add<GUI::IconView>();
|
||||
m_icon_view->set_always_wrap_item_labels(true);
|
||||
m_icon_view->set_model(m_model);
|
||||
m_icon_view->set_model_column(ProjectTemplatesModel::Column::Name);
|
||||
m_icon_view->on_selection_change = [&]() {
|
||||
update_dialog();
|
||||
};
|
||||
m_icon_view->on_activation = [&]() {
|
||||
if (m_input_valid)
|
||||
do_create_project();
|
||||
};
|
||||
|
||||
m_description_label = *main_widget.find_descendant_of_type_named<GUI::Label>("description_label");
|
||||
m_name_input = *main_widget.find_descendant_of_type_named<GUI::TextBox>("name_input");
|
||||
m_name_input->on_change = [&]() {
|
||||
update_dialog();
|
||||
};
|
||||
m_name_input->on_return_pressed = [&]() {
|
||||
if (m_input_valid)
|
||||
do_create_project();
|
||||
};
|
||||
m_create_in_input = *main_widget.find_descendant_of_type_named<GUI::TextBox>("create_in_input");
|
||||
m_create_in_input->on_change = [&]() {
|
||||
update_dialog();
|
||||
};
|
||||
m_create_in_input->on_return_pressed = [&]() {
|
||||
if (m_input_valid)
|
||||
do_create_project();
|
||||
};
|
||||
m_full_path_label = *main_widget.find_descendant_of_type_named<GUI::Label>("full_path_label");
|
||||
|
||||
m_ok_button = *main_widget.find_descendant_of_type_named<GUI::Button>("ok_button");
|
||||
m_ok_button->on_click = [this](auto) {
|
||||
do_create_project();
|
||||
};
|
||||
|
||||
m_cancel_button = *main_widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
|
||||
m_cancel_button->on_click = [this](auto) {
|
||||
done(ExecResult::ExecCancel);
|
||||
};
|
||||
|
||||
m_browse_button = *find_descendant_of_type_named<GUI::Button>("browse_button");
|
||||
m_browse_button->on_click = [this](auto) {
|
||||
Optional<String> path = GUI::FilePicker::get_open_filepath(this);
|
||||
if (path.has_value())
|
||||
m_create_in_input->set_text(path.value().view());
|
||||
};
|
||||
}
|
||||
|
||||
NewProjectDialog::~NewProjectDialog()
|
||||
{
|
||||
}
|
||||
|
||||
RefPtr<ProjectTemplate> NewProjectDialog::selected_template()
|
||||
{
|
||||
if (m_icon_view->selection().is_empty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto project_template = m_model->template_for_index(m_icon_view->selection().first());
|
||||
ASSERT(!project_template.is_null());
|
||||
|
||||
return project_template;
|
||||
}
|
||||
|
||||
void NewProjectDialog::update_dialog()
|
||||
{
|
||||
auto project_template = selected_template();
|
||||
m_input_valid = true;
|
||||
|
||||
if (project_template) {
|
||||
m_description_label->set_text(project_template->description());
|
||||
} else {
|
||||
m_description_label->set_text("Select a project template to continue.");
|
||||
m_input_valid = false;
|
||||
}
|
||||
|
||||
auto maybe_project_path = get_project_full_path();
|
||||
|
||||
if (maybe_project_path.has_value()) {
|
||||
m_full_path_label->set_text(maybe_project_path.value());
|
||||
} else {
|
||||
m_full_path_label->set_text("Invalid name or creation directory.");
|
||||
m_input_valid = false;
|
||||
}
|
||||
|
||||
m_ok_button->set_enabled(m_input_valid);
|
||||
}
|
||||
|
||||
Optional<String> NewProjectDialog::get_available_project_name()
|
||||
{
|
||||
auto create_in = m_create_in_input->text();
|
||||
auto chosen_name = m_name_input->text();
|
||||
|
||||
// Ensure project name isn't empty or entirely whitespace
|
||||
if (chosen_name.is_empty() || chosen_name.is_whitespace())
|
||||
return {};
|
||||
|
||||
// Validate project name with validity regex
|
||||
if (!s_project_name_validity_regex.has_match(chosen_name))
|
||||
return {};
|
||||
|
||||
if (!Core::File::exists(create_in) || !Core::File::is_directory(create_in))
|
||||
return {};
|
||||
|
||||
// Check for up-to 999 variations of the project name, in case it's already taken
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
auto candidate = (i == 0)
|
||||
? chosen_name
|
||||
: String::formatted("{}-{}", chosen_name, i);
|
||||
|
||||
if (!Core::File::exists(String::formatted("{}/{}", create_in, candidate)))
|
||||
return candidate;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Optional<String> NewProjectDialog::get_project_full_path()
|
||||
{
|
||||
// Do not permit forward-slashes in project names
|
||||
if (m_name_input->text().contains("/"))
|
||||
return {};
|
||||
|
||||
auto create_in = m_create_in_input->text();
|
||||
auto maybe_project_name = get_available_project_name();
|
||||
|
||||
if (!maybe_project_name.has_value()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto project_name = maybe_project_name.value();
|
||||
auto full_path = LexicalPath(String::formatted("{}/{}", create_in, project_name));
|
||||
|
||||
// Do not permit otherwise invalid paths.
|
||||
if (!full_path.is_valid())
|
||||
return {};
|
||||
|
||||
return full_path.string();
|
||||
}
|
||||
|
||||
void NewProjectDialog::do_create_project()
|
||||
{
|
||||
auto project_template = selected_template();
|
||||
if (!project_template) {
|
||||
GUI::MessageBox::show_error(this, "Could not create project: no template selected.");
|
||||
return;
|
||||
}
|
||||
|
||||
auto maybe_project_name = get_available_project_name();
|
||||
auto maybe_project_full_path = get_project_full_path();
|
||||
if (!maybe_project_name.has_value() || !maybe_project_full_path.has_value()) {
|
||||
GUI::MessageBox::show_error(this, "Could not create project: invalid project name or path.");
|
||||
return;
|
||||
}
|
||||
|
||||
auto creation_result = project_template->create_project(maybe_project_name.value(), maybe_project_full_path.value());
|
||||
if (!creation_result.is_error()) {
|
||||
// Succesfully created, attempt to open the new project
|
||||
m_created_project_path = maybe_project_full_path.value();
|
||||
done(ExecResult::ExecOK);
|
||||
} else {
|
||||
GUI::MessageBox::show_error(this, String::formatted("Could not create project: {}", creation_result.error()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
115
Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.gml
Normal file
115
Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.gml
Normal file
|
@ -0,0 +1,115 @@
|
|||
@GUI::Widget {
|
||||
fill_with_background_color: true
|
||||
|
||||
layout: @GUI::VerticalBoxLayout {
|
||||
margins: [4, 4, 4, 4]
|
||||
}
|
||||
|
||||
@GUI::Label {
|
||||
text: "Templates:"
|
||||
text_alignment: "CenterLeft"
|
||||
max_height: 20
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::VerticalBoxLayout {
|
||||
}
|
||||
|
||||
name: "icon_view_container"
|
||||
}
|
||||
|
||||
@GUI::Label {
|
||||
name: "description_label"
|
||||
text_alignment: "CenterLeft"
|
||||
thickness: 2
|
||||
shadow: "Sunken"
|
||||
shape: "Container"
|
||||
max_height: 24
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {
|
||||
}
|
||||
|
||||
max_height: 24
|
||||
|
||||
@GUI::Label {
|
||||
text: "Name:"
|
||||
text_alignment: "CenterLeft"
|
||||
max_width: 75
|
||||
}
|
||||
|
||||
@GUI::TextBox {
|
||||
name: "name_input"
|
||||
}
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {
|
||||
}
|
||||
|
||||
max_height: 24
|
||||
|
||||
@GUI::Label {
|
||||
text: "Create in:"
|
||||
text_alignment: "CenterLeft"
|
||||
max_width: 75
|
||||
}
|
||||
|
||||
@GUI::TextBox {
|
||||
name: "create_in_input"
|
||||
text: "/home/anon/Source"
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "browse_button"
|
||||
text: "Browse"
|
||||
max_width: 75
|
||||
}
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {
|
||||
}
|
||||
|
||||
max_height: 24
|
||||
|
||||
@GUI::Label {
|
||||
text: "Full path:"
|
||||
text_alignment: "CenterLeft"
|
||||
max_width: 75
|
||||
}
|
||||
|
||||
@GUI::Label {
|
||||
name: "full_path_label"
|
||||
text_alignment: "CenterLeft"
|
||||
text: ""
|
||||
thickness: 2
|
||||
shadow: "Sunken"
|
||||
shape: "Container"
|
||||
max_height: 22
|
||||
}
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {
|
||||
}
|
||||
|
||||
max_height: 24
|
||||
|
||||
@GUI::Widget {
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "ok_button"
|
||||
text: "OK"
|
||||
max_width: 75
|
||||
}
|
||||
|
||||
@GUI::Button {
|
||||
name: "cancel_button"
|
||||
text: "Cancel"
|
||||
max_width: 75
|
||||
}
|
||||
}
|
||||
}
|
79
Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.h
Normal file
79
Userland/DevTools/HackStudio/Dialogs/NewProjectDialog.h
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Nick Vella <nick@nxk.io>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ProjectTemplatesModel.h"
|
||||
#include <DevTools/HackStudio/ProjectTemplate.h>
|
||||
|
||||
#include <AK/Result.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibGUI/Button.h>
|
||||
#include <LibGUI/Dialog.h>
|
||||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/TextBox.h>
|
||||
|
||||
namespace HackStudio {
|
||||
|
||||
class NewProjectDialog : public GUI::Dialog {
|
||||
C_OBJECT(NewProjectDialog);
|
||||
|
||||
public:
|
||||
static int show(GUI::Window* parent_window);
|
||||
|
||||
Optional<String> created_project_path() const { return m_created_project_path; }
|
||||
|
||||
private:
|
||||
NewProjectDialog(GUI::Window* parent);
|
||||
virtual ~NewProjectDialog() override;
|
||||
|
||||
void update_dialog();
|
||||
Optional<String> get_available_project_name();
|
||||
Optional<String> get_project_full_path();
|
||||
|
||||
void do_create_project();
|
||||
|
||||
RefPtr<ProjectTemplate> selected_template();
|
||||
|
||||
NonnullRefPtr<ProjectTemplatesModel> m_model;
|
||||
bool m_input_valid { false };
|
||||
|
||||
RefPtr<GUI::Widget> m_icon_view_container;
|
||||
RefPtr<GUI::IconView> m_icon_view;
|
||||
|
||||
RefPtr<GUI::Label> m_description_label;
|
||||
RefPtr<GUI::TextBox> m_name_input;
|
||||
RefPtr<GUI::TextBox> m_create_in_input;
|
||||
RefPtr<GUI::Label> m_full_path_label;
|
||||
|
||||
RefPtr<GUI::Button> m_ok_button;
|
||||
RefPtr<GUI::Button> m_cancel_button;
|
||||
RefPtr<GUI::Button> m_browse_button;
|
||||
|
||||
Optional<String> m_created_project_path;
|
||||
};
|
||||
|
||||
}
|
156
Userland/DevTools/HackStudio/Dialogs/ProjectTemplatesModel.cpp
Normal file
156
Userland/DevTools/HackStudio/Dialogs/ProjectTemplatesModel.cpp
Normal file
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Nick Vella <nick@nxk.io>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "ProjectTemplatesModel.h"
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/QuickSort.h>
|
||||
#include <LibCore/DirIterator.h>
|
||||
#include <LibGUI/Icon.h>
|
||||
#include <LibGUI/Variant.h>
|
||||
#include <LibGfx/TextAlignment.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
namespace HackStudio {
|
||||
|
||||
ProjectTemplatesModel::ProjectTemplatesModel()
|
||||
: m_templates()
|
||||
, m_mapping()
|
||||
{
|
||||
auto watcher_or_error = Core::FileWatcher::watch(ProjectTemplate::templates_path());
|
||||
if (!watcher_or_error.is_error()) {
|
||||
m_file_watcher = watcher_or_error.release_value();
|
||||
m_file_watcher->on_change = [&](auto) {
|
||||
update();
|
||||
};
|
||||
} else {
|
||||
warnln("Unable to watch templates directory, templates will not automatically refresh. Error: {}", watcher_or_error.error());
|
||||
}
|
||||
|
||||
rescan_templates();
|
||||
}
|
||||
|
||||
ProjectTemplatesModel::~ProjectTemplatesModel()
|
||||
{
|
||||
}
|
||||
|
||||
int ProjectTemplatesModel::row_count(const GUI::ModelIndex&) const
|
||||
{
|
||||
return m_mapping.size();
|
||||
}
|
||||
|
||||
int ProjectTemplatesModel::column_count(const GUI::ModelIndex&) const
|
||||
{
|
||||
return Column::__Count;
|
||||
}
|
||||
|
||||
String ProjectTemplatesModel::column_name(int column) const
|
||||
{
|
||||
switch (column) {
|
||||
case Column::Icon:
|
||||
return "Icon";
|
||||
case Column::Id:
|
||||
return "ID";
|
||||
case Column::Name:
|
||||
return "Name";
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
GUI::Variant ProjectTemplatesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
|
||||
{
|
||||
if (static_cast<size_t>(index.row()) >= m_mapping.size())
|
||||
return {};
|
||||
|
||||
if (role == GUI::ModelRole::TextAlignment)
|
||||
return Gfx::TextAlignment::CenterLeft;
|
||||
|
||||
if (role == GUI::ModelRole::Display) {
|
||||
switch (index.column()) {
|
||||
case Column::Name:
|
||||
return m_mapping[index.row()]->name();
|
||||
case Column::Id:
|
||||
return m_mapping[index.row()]->id();
|
||||
}
|
||||
}
|
||||
|
||||
if (role == GUI::ModelRole::Icon) {
|
||||
return m_mapping[index.row()]->icon();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
RefPtr<ProjectTemplate> ProjectTemplatesModel::template_for_index(const GUI::ModelIndex& index)
|
||||
{
|
||||
if (static_cast<size_t>(index.row()) >= m_mapping.size())
|
||||
return {};
|
||||
|
||||
return m_mapping[index.row()];
|
||||
}
|
||||
|
||||
void ProjectTemplatesModel::update()
|
||||
{
|
||||
rescan_templates();
|
||||
did_update();
|
||||
}
|
||||
|
||||
void ProjectTemplatesModel::rescan_templates()
|
||||
{
|
||||
m_templates.clear();
|
||||
|
||||
// Iterate over template manifest INI files in the templates path
|
||||
Core::DirIterator di(ProjectTemplate::templates_path(), Core::DirIterator::SkipDots);
|
||||
if (di.has_error()) {
|
||||
warnln("DirIterator: {}", di.error_string());
|
||||
return;
|
||||
}
|
||||
|
||||
while (di.has_next()) {
|
||||
auto full_path = LexicalPath(di.next_full_path());
|
||||
if (!full_path.has_extension(".ini"))
|
||||
continue;
|
||||
|
||||
auto project_template = ProjectTemplate::load_from_manifest(full_path.string());
|
||||
if (!project_template) {
|
||||
warnln("Template manifest {} is invalid.", full_path.string());
|
||||
continue;
|
||||
}
|
||||
|
||||
m_templates.append(project_template.release_nonnull());
|
||||
}
|
||||
|
||||
// Enumerate the loaded projects into a sorted mapping, by priority value descending.
|
||||
m_mapping.clear();
|
||||
for (auto& project_template : m_templates)
|
||||
m_mapping.append(&project_template);
|
||||
quick_sort(m_mapping, [](auto a, auto b) {
|
||||
return a->priority() > b->priority();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
73
Userland/DevTools/HackStudio/Dialogs/ProjectTemplatesModel.h
Normal file
73
Userland/DevTools/HackStudio/Dialogs/ProjectTemplatesModel.h
Normal file
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Nick Vella <nick@nxk.io>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/NonnullPtrVector.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <DevTools/HackStudio/ProjectTemplate.h>
|
||||
#include <LibCore/FileWatcher.h>
|
||||
#include <LibGUI/Model.h>
|
||||
|
||||
namespace HackStudio {
|
||||
|
||||
class ProjectTemplatesModel final : public GUI::Model {
|
||||
public:
|
||||
static NonnullRefPtr<ProjectTemplatesModel> create()
|
||||
{
|
||||
return adopt(*new ProjectTemplatesModel());
|
||||
}
|
||||
|
||||
enum Column {
|
||||
Icon = 0,
|
||||
Id,
|
||||
Name,
|
||||
__Count
|
||||
};
|
||||
|
||||
virtual ~ProjectTemplatesModel() override;
|
||||
|
||||
RefPtr<ProjectTemplate> template_for_index(const GUI::ModelIndex& index);
|
||||
|
||||
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
|
||||
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
|
||||
virtual String column_name(int) const override;
|
||||
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
|
||||
virtual void update() override;
|
||||
|
||||
void rescan_templates();
|
||||
|
||||
private:
|
||||
explicit ProjectTemplatesModel();
|
||||
|
||||
NonnullRefPtrVector<ProjectTemplate> m_templates;
|
||||
Vector<ProjectTemplate*> m_mapping;
|
||||
|
||||
RefPtr<Core::FileWatcher> m_file_watcher;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue