Skip to content

Commit

Permalink
Implement scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Oct 29, 2024
1 parent 0d21626 commit d259c60
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
20 changes: 9 additions & 11 deletions src/core/OSTreeTUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,7 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
cpplibostree::Commit& commit = ostreeRepo.getCommitList().at(hash);
commitComponents.push_back(
// TODO make the commits scrollable (maybe common y offset variable)
CommitComponent(scroll_offset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, commit, {
.inner = Renderer([commit] {
return vbox({
text(commit.subject),
text(std::format("{:%Y-%m-%d %T %Ez}", std::chrono::time_point_cast<std::chrono::seconds>(commit.timestamp))),
});
}),
CommitComponent(scroll_offset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, hash, ostreeRepo, {
.title = hash.substr(0, 8),
.top = i * 4,
})
Expand Down Expand Up @@ -227,9 +221,9 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
promotionBranchColorMap.insert({str,Color::GrayDark});
}
}
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, promotionBranchColorMap, selectedCommit);
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, promotionBranchColorMap, scroll_offset, selectedCommit);
}
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, branchColorMap, selectedCommit);
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, branchColorMap, scroll_offset, selectedCommit);
}),
// commit list
commitComponents.size() == 0
Expand All @@ -241,10 +235,14 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
commitTree = CatchEvent(commitTree, [&](Event event) {
// switch through commits
if (event == Event::ArrowUp || event == Event::Character('k') || (event.is_mouse() && event.mouse().button == Mouse::WheelUp)) {
return prev_commit();
if (scroll_offset < 0) {
++scroll_offset;
}
return prev_commit();
}
if (event == Event::ArrowDown || event == Event::Character('j') || (event.is_mouse() && event.mouse().button == Mouse::WheelDown)) {
return next_commit();
--scroll_offset;
return next_commit();
}
return false;
});
Expand Down
16 changes: 11 additions & 5 deletions src/core/commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ ftxui::Element commitRender(cpplibostree::OSTreeRepo& repo,
const std::vector<std::string>& visibleCommitMap,
const std::unordered_map<std::string, bool>& visibleBranches,
const std::unordered_map<std::string, ftxui::Color>& branchColorMap,
size_t selectedCommit) {
int scroll_offset,
size_t selectedCommit) {
using namespace ftxui;

// check empty commit list
Expand Down Expand Up @@ -48,11 +49,16 @@ ftxui::Element commitRender(cpplibostree::OSTreeRepo& repo,
// treeElements, commElements, commit, highlight, usedBranches, branchColorMap);
}
// commit
treeElements.push_back(addTreeLine(RenderTree::TREE_LINE_NODE, commit, usedBranches, branchColorMap));
treeElements.push_back(addTreeLine(RenderTree::TREE_LINE_TREE, commit, usedBranches, branchColorMap));
treeElements.push_back(addTreeLine(RenderTree::TREE_LINE_TREE, commit, usedBranches, branchColorMap));
treeElements.push_back(addTreeLine(RenderTree::TREE_LINE_TREE, commit, usedBranches, branchColorMap));
if (scroll_offset++ >= 0) {
treeElements.push_back(addTreeLine(RenderTree::TREE_LINE_NODE, commit, usedBranches, branchColorMap));
}
for (int i{0}; i < 3; i++) {
if (scroll_offset++ >= 0) {
treeElements.push_back(addTreeLine(RenderTree::TREE_LINE_TREE, commit, usedBranches, branchColorMap));
}
}
}
Box box_{};

return hbox({
vbox(std::move(treeElements)),
Expand Down
3 changes: 2 additions & 1 deletion src/core/commit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace CommitRender {
const std::vector<std::string>& visibleCommitMap,
const std::unordered_map<std::string, bool>& visibleBranches,
const std::unordered_map<std::string, ftxui::Color>& branchColorMap,
int scroll_offset,
size_t selectedCommit = 0);

/**
Expand All @@ -70,5 +71,5 @@ namespace CommitRender {
const cpplibostree::Commit& commit,
const std::unordered_map<std::string, int>& usedBranches,
const std::unordered_map<std::string, ftxui::Color>& branchColorMap);

} // namespace CommitRender
43 changes: 29 additions & 14 deletions src/core/commitComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,28 @@ Element DefaultRenderState(const WindowRenderState& state) {
/// @brief Draggable commit window, including ostree-tui logic for overlap detection, etc.
class CommitComponentImpl : public ComponentBase, public WindowOptions {
public:
explicit CommitComponentImpl(int& scroll_offset, bool& inPromotionSelection, std::string& promotionHash, std::string& promotionBranch, std::unordered_map<std::string, bool>& visibleBranches, cpplibostree::Commit& commit, WindowOptions option) :
explicit CommitComponentImpl(int& scroll_offset,
bool& inPromotionSelection,
std::string& promotionHash,
std::string& promotionBranch,
std::unordered_map<std::string, bool>& visibleBranches,
std::string commit,
cpplibostree::OSTreeRepo& ostreerepo,
WindowOptions option) :
scroll_offset(scroll_offset),
inPromotionSelection(inPromotionSelection),
promotionHash(promotionHash),
promotionBranch(promotionBranch),
visibleBranches(visibleBranches),
hash(commit),
ostreerepo(ostreerepo),
WindowOptions(std::move(option)) {
//inner = Renderer([&] {
// return vbox({
// text(commit.subject),
// text(std::format("{:%Y-%m-%d %T %Ez}", std::chrono::time_point_cast<std::chrono::seconds>(commit.timestamp))),
// });
//});
if (!inner) {
inner = Make<ComponentBase>();
}
inner = Renderer([&] {
return vbox({
text(ostreerepo.getCommitList().at(hash).subject),
text(std::format("{:%Y-%m-%d %T %Ez}", std::chrono::time_point_cast<std::chrono::seconds>(ostreerepo.getCommitList().at(hash).timestamp))),
});
});
simpleCommit = inner;
Add(inner);

Expand All @@ -115,7 +121,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {

// Position and record the drawn area of the window.
element |= reflect(box_window_);
element |= PositionAndSize(left(), top(), width(), height());
element |= PositionAndSize(left(), top() + scroll_offset, width(), height());
element |= reflect(box_);

return element;
Expand Down Expand Up @@ -168,7 +174,7 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
// initiate promotion
inPromotionSelection = true;
// TODO do not hardcode
promotionHash = "49febbd98626719cf10b36e6c859a5132e2163f4752c10d841bc8ac596f9d0f7";
promotionHash = hash;
startPromotionWindow();
} else {
// not promotion
Expand Down Expand Up @@ -266,6 +272,8 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {

// ostree-tui specific members
// TODO store commit data
std::string hash;
cpplibostree::OSTreeRepo& ostreerepo;
// common / shared variables to set the promotion state
bool& inPromotionSelection;
std::string& promotionHash;
Expand Down Expand Up @@ -324,8 +332,15 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
} // namespace


Component CommitComponent(int& scroll_offset, bool& inPromotionSelection, std::string& promotionHash, std::string& promotionBranch, std::unordered_map<std::string, bool>& visibleBranches, cpplibostree::Commit& commit, WindowOptions option) {
return Make<CommitComponentImpl>(scroll_offset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, commit, std::move(option));
Component CommitComponent(int& scroll_offset,
bool& inPromotionSelection,
std::string& promotionHash,
std::string& promotionBranch,
std::unordered_map<std::string, bool>& visibleBranches,
std::string commit,
cpplibostree::OSTreeRepo& ostreerepo,
WindowOptions option) {
return Make<CommitComponentImpl>(scroll_offset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, commit, ostreerepo, std::move(option));
}

}; // namespace ftxui
Expand Down
3 changes: 2 additions & 1 deletion src/core/commitComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ namespace ftxui {
std::string& promotionHash,
std::string& promotionBranch,
std::unordered_map<std::string, bool>& visibleBranches,
cpplibostree::Commit& commit,
std::string commit,
cpplibostree::OSTreeRepo& ostreerepo,
WindowOptions option);

} // namespace ftxui
Expand Down

0 comments on commit d259c60

Please sign in to comment.