Skip to content

Commit

Permalink
cleanup files and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
forgottosave committed Nov 3, 2024
1 parent f374d6a commit bdca1d2
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 476 deletions.
4 changes: 0 additions & 4 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ add_library(ostree-tui_core commit.cpp
manager.hpp
OSTreeTUI.cpp
OSTreeTUI.hpp
scroller.cpp
scroller.hpp
commitComponent.cpp
commitComponent.hpp
)

target_link_libraries(ostree-tui_core
Expand Down
8 changes: 2 additions & 6 deletions src/core/OSTreeTUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
#include <string>
#include <unordered_map>
#include <thread>

#include <fcntl.h>

#include "commit.hpp"
#include "ftxui/component/component.hpp" // for Renderer, ResizableSplitBottom, ResizableSplitLeft, ResizableSplitRight, ResizableSplitTop
#include "ftxui/component/component_base.hpp" // for ComponentBase
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for Element, operator|, text, center, border

#include "scroller.hpp"
#include "commitComponent.hpp"

#include "commit.hpp"
#include "footer.hpp"
#include "manager.hpp"

Expand Down Expand Up @@ -123,7 +119,7 @@ int OSTreeTUI::main(const std::string& repo, const std::vector<std::string>& sta
visibleCommitViewMap = parseVisibleCommitMap(ostreeRepo, visibleBranches);
for (auto& hash : visibleCommitViewMap) {
commitComponents.push_back(
CommitComponent(i, scrollOffset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, columnToBranchMap, hash, ostreeRepo, refresh)
CommitRender::CommitComponent(i, scrollOffset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, columnToBranchMap, hash, ostreeRepo, refresh)
);
i++;
}
Expand Down
327 changes: 326 additions & 1 deletion src/core/commit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,337 @@
#include <vector>
#include <algorithm>

#include "ftxui/dom/elements.hpp" // for Element, operator|, text, center, border
#include <ftxui/component/component_base.hpp> // for Component, ComponentBase
#include "ftxui/component/component.hpp"// for Make
#include <ftxui/component/event.hpp> // for Event, Event::ArrowDown, Event::ArrowUp, Event::End, Event::Home, Event::PageDown, Event::PageUp
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::WheelDown, Mouse::WheelUp
#include "ftxui/dom/deprecated.hpp" // for text
#include "ftxui/dom/elements.hpp" // for operator|, Element, size, vbox, EQUAL, HEIGHT, dbox, reflect, focus, inverted, nothing, select, vscroll_indicator, yflex, yframe
#include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/requirement.hpp" // for Requirement
#include "ftxui/screen/box.hpp" // for Box
#include <ftxui/component/component_options.hpp>
#include <ftxui/component/screen_interactive.hpp> // for ScreenInteractive
#include "ftxui/dom/elements.hpp" // for text, window, hbox, vbox, size, clear_under, reflect, emptyElement
#include "ftxui/screen/color.hpp" // for Color
#include "ftxui/screen/screen.hpp" // for Screen

#include "../util/cpplibostree.hpp"

namespace CommitRender {

namespace {
using namespace ftxui;

/// From https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
Decorator PositionAndSize(int left, int top, int width, int height) {
return [=](Element element) {
element |= size(WIDTH, EQUAL, width);
element |= size(HEIGHT, EQUAL, height);

auto padding_left = emptyElement() | size(WIDTH, EQUAL, left);
auto padding_top = emptyElement() | size(HEIGHT, EQUAL, top);

return vbox({
padding_top,
hbox({
padding_left,
element,
}),
});
};
}

/// From https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
Element DefaultRenderState(const WindowRenderState& state) {
Element element = state.inner;
if (!state.active) {
element |= dim;
}

element = window(text(state.title), element);
element |= clear_under;

return element;
}

/// Draggable commit window, including ostree-tui logic for overlap detection, etc.
/// Partially inspired from https://github.com/ArthurSonzogni/FTXUI/blob/main/src/ftxui/component/window.cpp
class CommitComponentImpl : public ComponentBase, public WindowOptions {
public:
explicit CommitComponentImpl(int position,
int& scrollOffset,
bool& inPromotionSelection,
std::string& promotionHash,
std::string& promotionBranch,
std::unordered_map<std::string, bool>& visibleBranches,
std::vector<std::string>& columnToBranchMap,
std::string commit,
cpplibostree::OSTreeRepo& ostreerepo,
bool& refresh) :
scrollOffset(scrollOffset),
inPromotionSelection(inPromotionSelection),
promotionHash(promotionHash),
promotionBranch(promotionBranch),
visibleBranches(visibleBranches),
columnToBranchMap(columnToBranchMap),
hash(commit),
ostreerepo(ostreerepo),
refresh(refresh) {
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);

title = hash.substr(0, 8);
top = position * 4;
left = 1;
width = 30;
height = 4;
}

private:
Element Render() final {
auto element = ComponentBase::Render();

const WindowRenderState state = {
element,
title(),
Active(),
drag_
};

element = render ? render(state) : DefaultRenderState(state);

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

return element;
}

void resetWindow() {
left() = drag_initial_x;
top() = drag_initial_y;
width() = width_initial;
height() = height_initial;
// reset window contents
DetachAllChildren();
Add(simpleCommit);
}
void startPromotionWindow() {
left() = std::max(left(),-2);
width() = width_initial + 8;
height() = height_initial + 10;
// change inner to promotion layout
simpleCommit = inner;
DetachAllChildren();
Add(promotionView);
}

bool OnEvent(Event event) final {
if (ComponentBase::OnEvent(event)) {
return true;
}

if (!event.is_mouse()) {
return false;
}

if (inPromotionSelection && ! drag_) {
return true;
}

mouse_hover_ = box_window_.Contain(event.mouse().x, event.mouse().y);

if (mouse_hover_) {
// TODO indicate window is draggable?
}

if (captured_mouse_) {
if (event.mouse().motion == Mouse::Released) {
// reset mouse
captured_mouse_ = nullptr;
// check if position matches branch & do something if it does
if (promotionBranch.size() != 0) {
// initiate promotion
inPromotionSelection = true;
promotionHash = hash;
startPromotionWindow();
} else {
// not promotion
inPromotionSelection = false;
promotionBranch = "";
resetWindow();
}
return true;
}

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
promotionBranch = "";
int branch_pos = event.mouse().x / 2;
int count{0};
for (auto& [branch,visible] : visibleBranches) {
if (visible) {
++count;
}
if (count == branch_pos) {
// problem -> branch sorting not stored anywhere...
// promotionBranch = branch;
promotionBranch = columnToBranchMap.at(event.mouse().x / 2 - 1);
break;
}
}
} else {
// not promotion
inPromotionSelection = false;
promotionBranch = "";
}

// Clamp the window size.
width() = std::max<int>(width(), static_cast<int>(title().size() + 2));
height() = std::max<int>(height(), 2);

return true;
}

if (!mouse_hover_) {
return false;
}

if (!CaptureMouse(event)) {
return true;
}

if (event.mouse().button != Mouse::Left) {
return true;
}
if (event.mouse().motion != Mouse::Pressed) {
return true;
}

TakeFocus();

captured_mouse_ = CaptureMouse(event);
if (!captured_mouse_) {
return true;
}

drag_start_x = event.mouse().x - left() - box_.x_min;
drag_start_y = event.mouse().y - top() - box_.y_min;

bool drag_old = drag_;
drag_ = true;
if (!drag_old && drag_) { // if we start dragging
drag_initial_x = left();
drag_initial_y = top();
}
return true;
}

// window specific members
Box box_;
Box box_window_;

CapturedMouse captured_mouse_;
int drag_start_x = 0;
int drag_start_y = 0;

int drag_initial_x = 0;
int drag_initial_y = 0;
int width_initial = 30;
int height_initial = 4;

bool mouse_hover_ = false;
bool drag_ = false;

int& scrollOffset;

// ostree-tui specific members
// TODO store commit data
std::string hash;
cpplibostree::OSTreeRepo& ostreerepo;
std::unordered_map<std::string, bool>& visibleBranches;
std::vector<std::string>& columnToBranchMap;

bool& refresh;

// common / shared variables to set the promotion state
bool& inPromotionSelection;
std::string& promotionHash;
std::string& promotionBranch;

// promotion view
std::string newSubject;
Component simpleCommit = Renderer([] {
return text("error in commit window creation");
});
Component promotionView = Container::Vertical({
Renderer([&] {
return vbox({
text(""),
text(" Promote Commit...") | bold,
text(""),
text(" ☐ todocommithash") | bold,
});
}),
Container::Horizontal({
Renderer([&] {
return text(" ┆ subject: ");
}),
Input(&newSubject, "enter new subject...") | underlined
}),
// TODO render other metadata strings, if available (like version?)
Renderer([&] {
return vbox({
text(""),
text(" ┆ to branch:"),
text("" + promotionBranch) | bold,
text("") | bold
});
}),
Container::Horizontal({
Button(" Cancel [n] ", [&] {
inPromotionSelection = false;
resetWindow();
}) | color(Color::Red) | flex,
Button(" Promote [y] ", [&] {
inPromotionSelection = false;
// promote on the ostree repo
ostreerepo.promoteCommit(hash, promotionBranch, {}, newSubject, true);
resetWindow();
// TODO refresh the ostree-tui to show the new commit
refresh = true;
}) | color(Color::Green) | flex,
})
});
};

} // namespace

ftxui::Component CommitComponent(int position,
int& scrollOffset,
bool& inPromotionSelection,
std::string& promotionHash,
std::string& promotionBranch,
std::unordered_map<std::string, bool>& visibleBranches,
std::vector<std::string>& columnToBranchMap,
std::string commit,
cpplibostree::OSTreeRepo& ostreerepo,
bool& refresh) {
return ftxui::Make<CommitComponentImpl>(position, scrollOffset, inPromotionSelection, promotionHash, promotionBranch, visibleBranches, columnToBranchMap, commit, ostreerepo, refresh);
}

ftxui::Element commitRender(cpplibostree::OSTreeRepo& repo,
const std::vector<std::string>& visibleCommitMap,
const std::unordered_map<std::string, bool>& visibleBranches,
Expand Down
Loading

0 comments on commit bdca1d2

Please sign in to comment.