fwt_software/src/ui/Scroll.cpp

36 lines
1.1 KiB
C++

#include "fgc/ui/Scroll.h"
#include <algorithm>
namespace fgc {
int scrollMax(int content_h, int view_h) {
// An unmeasured view (first frame after an overlay opens or the terminal
// resizes) pins to the top rather than guessing: the next repaint is 100ms
// away and a guess would visibly jump.
if (view_h <= 0) return 0;
return std::max(0, content_h - view_h);
}
int scrollClamp(int top, int content_h, int view_h) {
return std::clamp(top, 0, scrollMax(content_h, view_h));
}
int scrollBy(int top, int delta, int content_h, int view_h) {
return scrollClamp(top + delta, content_h, view_h);
}
int scrollPage(int top, int dir, int content_h, int view_h) {
const int step = std::max(1, view_h - 2);
return scrollClamp(top + dir * step, content_h, view_h);
}
std::size_t gridRowsPerColumn(std::size_t item_count, std::size_t max_cols, std::size_t min_rows) {
max_cols = std::max<std::size_t>(max_cols, 1);
min_rows = std::max<std::size_t>(min_rows, 1);
const std::size_t needed = (item_count + max_cols - 1) / max_cols; // ceil
return std::max(min_rows, needed);
}
} // namespace fgc