Skip to content

Commit

Permalink
add drag-n-drop branch selection (currently still hardcoded)
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Oct 24, 2024
1 parent 0cb9237 commit 8a12777
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
20 changes: 19 additions & 1 deletion src/core/OSTreeTUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
* would go is still to be figured out. It this would be one, or many different elements mainly
* depends on how the overlap detection with branches would work, when dragging commits.
*/
// commit promotion state
// shared with all relevant components to monitor and react to
// TODO extend with keyboard functionality
bool inPromotionSelection{false};
std::string promotionHash{""};
std::string promotionBranch{""};
// parse all commits
Components commitComponents;
int scroll_offset{0};
Expand All @@ -178,7 +184,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, commit, {
CommitComponent(scroll_offset, inPromotionSelection, promotionHash, promotionBranch, commit, {
.inner = Renderer([commit] {
return vbox({
text(commit.subject),
Expand Down Expand Up @@ -210,6 +216,18 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
visibleCommitViewMap = parseVisibleCommitMap(ostreeRepo, visibleBranches);
refresh_commitComponents();
selectedCommit = std::min(selectedCommit, visibleCommitViewMap.size() - 1);
// TODO check for promotion & pass information if needed
if (inPromotionSelection && promotionBranch.size() != 0) {
std::unordered_map<std::string, Color> promotionBranchColorMap{};
for (auto& [str,col] : branchColorMap) {
if (str == promotionBranch) {
promotionBranchColorMap.insert({str,col});
} else {
promotionBranchColorMap.insert({str,Color::Black});
}
}
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, promotionBranchColorMap, selectedCommit);
}
return CommitRender::commitRender(ostreeRepo, visibleCommitViewMap, visibleBranches, branchColorMap, selectedCommit);
}),
// commit list
Expand Down
41 changes: 37 additions & 4 deletions src/core/commitComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,12 @@ 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, cpplibostree::Commit& commit, WindowOptions option) : scroll_offset(scroll_offset), WindowOptions(std::move(option)) {
explicit CommitComponentImpl(int& scroll_offset, bool& inPromotionSelection, std::string& promotionHash, std::string& promotionBranch, cpplibostree::Commit& commit, WindowOptions option) :
scroll_offset(scroll_offset),
inPromotionSelection(inPromotionSelection),
promotionHash(promotionHash),
promotionBranch(promotionBranch),
WindowOptions(std::move(option)) {
//inner = Renderer([&] {
// return vbox({
// text(commit.subject),
Expand Down Expand Up @@ -147,9 +152,19 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {

if (captured_mouse_) {
if (event.mouse().motion == Mouse::Released) {
// reset mouse
captured_mouse_ = nullptr;
// reset window position
left() = drag_initial_x;
top() = drag_initial_y;
// TODO check if position matches branch & do something if it does
if (false) {
// initiate promotion
} else {
// not promotion
inPromotionSelection = false;
promotionBranch = "";
}
return true;
}

Expand All @@ -174,6 +189,22 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
if (drag_) {
left() = event.mouse().x - drag_start_x - box_.x_min;
top() = event.mouse().y - drag_start_y - box_.y_min;
// promotion
inPromotionSelection = true;
// calculate which branch currently is hovered over
// TODO proper check, not hardcoded
std::vector<std::string> branch_mapping{
"", "oof", "foo", "fo/of/x86_64"
};
if (event.mouse().x < branch_mapping.size() * 2) {
promotionBranch = branch_mapping.at(event.mouse().x / 2);
} else {
promotionBranch = "";
}
} else {
// not promotion
inPromotionSelection = false;
promotionBranch = "";
}

// Clamp the window size.
Expand Down Expand Up @@ -258,14 +289,16 @@ class CommitComponentImpl : public ComponentBase, public WindowOptions {
int& scroll_offset;

// ostree-tui specific members

bool& inPromotionSelection;
std::string& promotionHash;
std::string& promotionBranch;
};

} // namespace


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

}; // namespace ftxui
Expand Down
7 changes: 6 additions & 1 deletion src/core/commitComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ namespace ftxui {
*
* @return Component
*/
Component CommitComponent(int& scroll_offset, cpplibostree::Commit& commit, WindowOptions option);
Component CommitComponent(int& scroll_offset,
bool& inPromotionSelection,
std::string& promotionHash,
std::string& promotionBranch,
cpplibostree::Commit& commit,
WindowOptions option);

} // namespace ftxui
#endif /* end of include guard: COMMITCOMPONENT_H */

0 comments on commit 8a12777

Please sign in to comment.