|
| 1 | +export module CppUtils.Terminal.Scrollable; |
| 2 | + |
| 3 | +import std; |
| 4 | +import CppUtils.Container.Size; |
| 5 | +export import CppUtils.Terminal.Area; |
| 6 | +import CppUtils.Terminal.CharAttributes; |
| 7 | +import CppUtils.Terminal.TextColor; |
| 8 | +import CppUtils.Terminal.BackgroundColor; |
| 9 | + |
| 10 | +export namespace CppUtils::Terminal |
| 11 | +{ |
| 12 | + class Scrollable: public Widget |
| 13 | + { |
| 14 | + public: |
| 15 | + enum ScrollbarMode : std::uint8_t |
| 16 | + { |
| 17 | + None = 0b00, |
| 18 | + Vertical = 0b01, |
| 19 | + Horizontal = 0b10, |
| 20 | + Both = Vertical | Horizontal |
| 21 | + }; |
| 22 | + |
| 23 | + inline Scrollable(const Container::Size2& viewportSize, const Container::Size2& fullContentSize): |
| 24 | + m_viewportBuffer{viewportSize}, |
| 25 | + m_fullContentArea{fullContentSize} |
| 26 | + {} |
| 27 | + |
| 28 | + template<std::derived_from<Widget> T> |
| 29 | + inline auto addWidget(std::unique_ptr<T> widget) -> T& |
| 30 | + { |
| 31 | + return m_fullContentArea.addWidget(std::move(widget)); |
| 32 | + } |
| 33 | + |
| 34 | + inline auto setScroll(const Container::Size2& position) noexcept -> void |
| 35 | + { |
| 36 | + m_scrollPosition = position; |
| 37 | + requestUpdate(std::chrono::milliseconds{10}); |
| 38 | + } |
| 39 | + |
| 40 | + [[nodiscard]] inline auto getScroll() const noexcept -> const Container::Size2& |
| 41 | + { |
| 42 | + return m_scrollPosition; |
| 43 | + } |
| 44 | + |
| 45 | + [[nodiscard]] inline auto getContentArea() noexcept -> Area& |
| 46 | + { |
| 47 | + return m_fullContentArea; |
| 48 | + } |
| 49 | + |
| 50 | + inline auto showScrollbars(std::uint8_t mode = ScrollbarMode::Both) noexcept -> void |
| 51 | + { |
| 52 | + m_scrollbarMode = mode; |
| 53 | + } |
| 54 | + |
| 55 | + [[nodiscard]] inline auto getScrollbarMode() const noexcept -> std::uint8_t |
| 56 | + { |
| 57 | + return m_scrollbarMode; |
| 58 | + } |
| 59 | + |
| 60 | + [[nodiscard]] inline auto getSize() const noexcept -> Container::Size2 override |
| 61 | + { |
| 62 | + return m_viewportBuffer.getSize(); |
| 63 | + } |
| 64 | + |
| 65 | + inline void setWidgetManager(WidgetManager& widgetManager) noexcept override |
| 66 | + { |
| 67 | + Widget::setWidgetManager(widgetManager); |
| 68 | + m_fullContentArea.setWidgetManager(widgetManager); |
| 69 | + } |
| 70 | + |
| 71 | + inline auto draw(WritableAreaView& view) noexcept -> void override |
| 72 | + { |
| 73 | + { |
| 74 | + auto fullContentAreaView = m_fullContentArea.getWritableView(); |
| 75 | + m_fullContentArea.draw(fullContentAreaView); |
| 76 | + } |
| 77 | + |
| 78 | + const auto& contentSize = m_fullContentArea.getSize(); |
| 79 | + const auto viewportSize = getSize(); |
| 80 | + |
| 81 | + const auto maxScrollX = (contentSize.width() > viewportSize.width()) ? contentSize.width() - viewportSize.width() : 0; |
| 82 | + const auto maxScrollY = (contentSize.height() > viewportSize.height()) ? contentSize.height() - viewportSize.height() : 0; |
| 83 | + |
| 84 | + if (m_scrollPosition.x() > maxScrollX) |
| 85 | + m_scrollPosition.x() = maxScrollX; |
| 86 | + if (m_scrollPosition.y() > maxScrollY) |
| 87 | + m_scrollPosition.y() = maxScrollY; |
| 88 | + |
| 89 | + Viewport{viewportSize}.forEach([&](const auto& position) { |
| 90 | + const auto sourcePosition = position + m_scrollPosition; |
| 91 | + |
| 92 | + if (sourcePosition.x() < contentSize.width() and sourcePosition.y() < contentSize.height()) |
| 93 | + m_viewportBuffer.setChar(position, m_fullContentArea.getChar(sourcePosition)); |
| 94 | + else |
| 95 | + m_viewportBuffer.setChar(position, CharAttributes{U' '}); |
| 96 | + }); |
| 97 | + |
| 98 | + if (m_scrollbarMode != ScrollbarMode::None) |
| 99 | + { |
| 100 | + auto internalView = WritableAreaView{m_viewportBuffer}; |
| 101 | + drawScrollbars(internalView, contentSize, viewportSize, maxScrollX, maxScrollY); |
| 102 | + } |
| 103 | + |
| 104 | + view.applyArea(m_viewportBuffer); |
| 105 | + drawFinished(); |
| 106 | + } |
| 107 | + |
| 108 | + private: |
| 109 | + inline auto drawScrollbars(WritableAreaView& view, const Container::Size2& contentSize, const Container::Size2& viewSize, std::size_t maxScrollX, std::size_t maxScrollY) -> void |
| 110 | + { |
| 111 | + const bool showVertical = (m_scrollbarMode & ScrollbarMode::Vertical) and (contentSize.height() > viewSize.height()); |
| 112 | + const bool showHorizontal = (m_scrollbarMode & ScrollbarMode::Horizontal) and (contentSize.width() > viewSize.width()); |
| 113 | + |
| 114 | + if (showVertical) |
| 115 | + { |
| 116 | + const auto barX = viewSize.width() - 1; |
| 117 | + const auto heightForBar = showHorizontal ? viewSize.height() - 1 : viewSize.height(); |
| 118 | + |
| 119 | + for (auto y = 0uz; y < heightForBar; ++y) |
| 120 | + view.setChar({barX, y}, CharAttributes{U'│', TextColor::TextColorEnum::White}); |
| 121 | + |
| 122 | + const auto ratio = static_cast<double>(heightForBar) / static_cast<double>(contentSize.height()); |
| 123 | + const auto thumbHeight = std::max(1uz, static_cast<std::size_t>(ratio * static_cast<double>(heightForBar))); |
| 124 | + const auto scrollRatio = (maxScrollY > 0) ? static_cast<double>(m_scrollPosition.y()) / static_cast<double>(maxScrollY) : 0.0; |
| 125 | + const auto thumbY = static_cast<std::size_t>(scrollRatio * static_cast<double>(heightForBar - thumbHeight)); |
| 126 | + |
| 127 | + for (auto i = 0uz; i < thumbHeight; ++i) |
| 128 | + view.setChar({barX, thumbY + i}, CharAttributes{U'█', TextColor::TextColorEnum::White}); |
| 129 | + } |
| 130 | + |
| 131 | + if (showHorizontal) |
| 132 | + { |
| 133 | + const auto barY = viewSize.height() - 1; |
| 134 | + const auto widthForBar = showVertical ? viewSize.width() - 1 : viewSize.width(); |
| 135 | + |
| 136 | + for (auto x = 0uz; x < widthForBar; ++x) |
| 137 | + view.setChar({x, barY}, CharAttributes{U'─', TextColor::TextColorEnum::White}); |
| 138 | + |
| 139 | + const auto ratio = static_cast<double>(widthForBar) / static_cast<double>(contentSize.width()); |
| 140 | + const auto thumbWidth = std::max(1uz, static_cast<std::size_t>(ratio * static_cast<double>(widthForBar))); |
| 141 | + const auto scrollRatio = (maxScrollX > 0) ? static_cast<double>(m_scrollPosition.x()) / static_cast<double>(maxScrollX) : 0.0; |
| 142 | + const auto thumbX = static_cast<std::size_t>(scrollRatio * static_cast<double>(widthForBar - thumbWidth)); |
| 143 | + |
| 144 | + for (auto i = 0uz; i < thumbWidth; ++i) |
| 145 | + view.setChar({thumbX + i, barY}, CharAttributes{U'█', TextColor::TextColorEnum::White}); |
| 146 | + } |
| 147 | + |
| 148 | + if (showVertical and showHorizontal) |
| 149 | + view.setChar({viewSize.width() - 1, viewSize.height() - 1}, CharAttributes{U'┘', TextColor::TextColorEnum::White}); |
| 150 | + } |
| 151 | + |
| 152 | + std::uint8_t m_scrollbarMode = ScrollbarMode::Both; |
| 153 | + Container::Size2 m_scrollPosition{0, 0}; |
| 154 | + DynamicAreaBuffer m_viewportBuffer; |
| 155 | + Area m_fullContentArea; |
| 156 | + }; |
| 157 | +} |
0 commit comments