Modules/Taskboard/private/TaskPanels.cpp
agent 19c381b3d9 Taskboard: use the shared DocumentFolderPanel for skills and workspaces
SkillsPanel and TaskWorkspacePanel were two copies of the same browser-plus-
editor panel. Both are now the shared DocumentFolderPanel: SkillsPanel only
adds createSkill, and TaskWorkspacePanel only adds picking a task's workspace
directory (the browser/editor open and save logic lives in the component).
2026-11-01 13:26:12 +03:00

157 lines
4.7 KiB
C++

#include "TaskPanels.hpp"
#include "BoxLayout.hpp"
#include <algorithm>
#include <cctype>
using namespace tp;
using namespace tp::taskboard;
using namespace tp::ui;
namespace {
constexpr halnf kPadding = 12;
constexpr halnf kGap = 6;
constexpr const char* kMarkdownExtension = ".md";
constexpr const char* kWorkdirTitle = "Workdir";
constexpr const char* kNoTaskHint = "Open a task to browse the files its agent produced.";
constexpr const char* kNoWorkdirHint = "This task has no workdir yet. It appears once its agent session starts.";
bool isMarkdownFile(const std::string& path) {
std::string lowered = path;
std::transform(lowered.begin(), lowered.end(), lowered.begin(), [](unsigned char c) {
return char(std::tolower(c));
});
const std::string extension = kMarkdownExtension;
return lowered.size() > extension.size() &&
lowered.compare(lowered.size() - extension.size(), extension.size(), extension) == 0;
}
std::string relativeTo(const std::string& root, const std::string& path) {
if (path.rfind(root, 0) != 0) return path;
std::string relative = path.substr(root.size());
while (!relative.empty() && relative.front() == '/')
relative.erase(relative.begin());
return relative;
}
void mutedHint(Label& label) {
label.setTone(Label::Tone::Muted);
label.setWrap(true);
label.setSizing(AxisSizing::fill(), AxisSizing::fit());
}
}
TaskDescriptionPanel::TaskDescriptionPanel(TaskBoard& board) :
header(board),
mBoard(board) {
setDebugName("task description panel");
buildLayout();
bindEditors();
showTask(std::nullopt);
}
void TaskDescriptionPanel::buildLayout() {
setSizing(AxisSizing::fill(), AxisSizing::fill());
emplaceLayout<ColumnLayout>(kGap).setPadding(Insets::uniform(kPadding));
mutedHint(hint);
editor.sourceArea().setPlaceholder("What should the agent do? Markdown is supported.");
editor.setMode(MarkdownEditor::Mode::Preview);
addChild(hint);
addChild(header);
addChild(editor);
}
void TaskDescriptionPanel::setOnChanged(std::function<void()> callback) {
mOnChanged = callback;
header.setOnChanged(std::move(callback));
}
void TaskDescriptionPanel::setOnEditingChanged(EditingCallback callback) {
mOnEditingChanged = callback;
header.setOnEditingChanged(std::move(callback));
}
void TaskDescriptionPanel::bindEditors() {
editor.setOnChanged([this](const std::string& text) { change([&](Task& task) { task.description = text; }); });
editor.sourceArea().setOnEditingStarted([this] { reportEditing(TaskField::Description, true); });
editor.sourceArea().setOnCommitted([this](const std::string&) { reportEditing(TaskField::Description, false); });
}
void TaskDescriptionPanel::reportEditing(TaskField field, bool editing) {
if (!mOnEditingChanged) return;
if (!editing) {
mOnEditingChanged(std::nullopt);
return;
}
if (mShown) mOnEditingChanged(std::make_pair(*mShown, field));
}
void TaskDescriptionPanel::change(const std::function<void(Task&)>& edit) {
Task* task = mShown ? mBoard.task(*mShown) : nullptr;
if (!task || mLoading) return;
edit(*task);
if (mOnChanged) mOnChanged();
}
void TaskDescriptionPanel::showTask(std::optional<NodeId> id) {
mShown = id;
const Task* task = id ? mBoard.task(*id) : nullptr;
hint.setVisible(task == nullptr);
header.setVisible(task != nullptr);
header.showTask(id);
editor.setVisible(task != nullptr);
if (task) refreshFields(*task);
}
void TaskDescriptionPanel::refreshFields(const Task& task) {
const bool wasLoading = mLoading;
mLoading = true;
if (!editor.sourceArea().isEditing() && editor.markdown() != task.description) editor.setMarkdown(task.description);
mLoading = wasLoading;
}
TaskWorkspacePanel::TaskWorkspacePanel(TaskBoard& board, FileSystem& files) :
DocumentFolderPanel("", kWorkdirTitle, files),
mBoard(board) {
setDebugName("task workspace panel");
setDocumentMode(MarkdownEditor::Mode::Preview);
setDocumentTitle([this](const std::string& path) { return relativeTo(mWorkspace, path); });
showTask(std::nullopt);
}
void TaskWorkspacePanel::showTask(std::optional<NodeId> id) {
const Task* task = id ? mBoard.task(*id) : nullptr;
if (!task) {
mWorkspace.clear();
hint.setText(kNoTaskHint);
hint.setVisible(true);
browser.setVisible(false);
if (pages().depth() > 1) showLibrary();
return;
}
showWorkspace(task->workspace);
}
void TaskWorkspacePanel::showWorkspace(const std::string& workspace) {
const bool changed = workspace != mWorkspace;
mWorkspace = workspace;
if (changed && pages().depth() > 1) showLibrary();
bool listed = false;
if (!workspace.empty() && changed) {
browser.setRootDirectory(workspace);
listed = browser.setDirectory(workspace);
} else if (!workspace.empty()) {
browser.refresh();
listed = browser.error().empty();
}
hint.setText(kNoWorkdirHint);
hint.setVisible(!listed);
browser.setVisible(listed);
}