10#include <portals/frame.hpp>
11#include <portals/widget_types.hpp>
65 metrics.viewport != 0 && metrics.extent > metrics.viewport ? metrics.extent - metrics.viewport : 0;
67 return {maximum, maximum + 1};
75 return offset.fraction >= 0 && offset.fraction < 1;
83[[nodiscard]]
constexpr std::uint64_t
scrollPageStep(std::uint64_t viewport, std::uint64_t overlap = 1) noexcept {
88 return viewport > overlap ? viewport - overlap : 1;
100constexpr ScrollDivision scrollMultiplyDivide(std::uint64_t a, std::uint64_t b, std::uint64_t d)
noexcept {
107 for (
int bit = 63; bit >= 0; --bit) {
108 result.quotient *= 2;
110 if (result.remainder >= d - result.remainder) {
111 result.remainder -= d - result.remainder;
114 result.remainder += result.remainder;
117 if (((b >> bit) & 1) != 0) {
118 if (result.remainder >= d - a) {
119 result.remainder -= d - a;
122 result.remainder += a;
131constexpr std::uint64_t scrollFractionUnits(
double fraction, std::uint64_t units)
noexcept {
132 static_assert(std::numeric_limits<double>::is_iec559 && std::numeric_limits<double>::digits == 53);
133 constexpr std::uint32_t MANTISSA_BITS = 52;
134 constexpr std::uint32_t EXPONENT_BIAS = 1023;
135 constexpr std::uint64_t EXPONENT_MASK = 0x7ff;
136 constexpr auto INTEGER_BITS = std::numeric_limits<std::uint64_t>::digits;
137 const auto bits = std::bit_cast<std::uint64_t>(fraction);
138 const auto exponent =
static_cast<std::uint32_t
>((bits >> MANTISSA_BITS) & EXPONENT_MASK);
139 auto mantissa = bits & ((std::uint64_t{1} << MANTISSA_BITS) - 1);
140 const auto shift = exponent ? MANTISSA_BITS + EXPONENT_BIAS - exponent : MANTISSA_BITS + EXPONENT_BIAS - 1;
143 mantissa |= std::uint64_t{1} << MANTISSA_BITS;
146 const auto divisionShift = std::min(shift,
static_cast<std::uint32_t
>(INTEGER_BITS - 1));
147 const auto remainingShift = shift - divisionShift;
149 if (remainingShift >= INTEGER_BITS) {
153 return scrollMultiplyDivide(mantissa, units, std::uint64_t{1} << divisionShift).quotient >> remainingShift;
157[[nodiscard]]
constexpr ScrollOffset preciseScrollOffsetForThumb(std::uint64_t position, std::uint64_t travel,
158 std::uint64_t maximum)
noexcept {
159 if (travel == 0 || maximum == 0) {
163 const auto value = scrollMultiplyDivide(std::min(position, travel), maximum, travel);
164 constexpr double MAXIMUM_FRACTION = 0x1.fffffffffffffp-1;
165 const auto fraction =
static_cast<double>(value.remainder) /
static_cast<double>(travel);
168 return {value.quotient, std::min(fraction, MAXIMUM_FRACTION)};
188 std::uint64_t offset,
189 std::int32_t trackLength,
190 std::int32_t minimumThumb = 1) noexcept {
191 if (trackLength < 0 || minimumThumb <= 0) {
195 if (trackLength == 0) {
201 if (range.maximum == 0) {
205 const auto proportional = detail::scrollMultiplyDivide(std::min(metrics.
viewport, metrics.
extent),
206 static_cast<std::uint64_t
>(trackLength), metrics.
extent);
208 std::clamp(
static_cast<std::int32_t
>(proportional.quotient), std::min(minimumThumb, trackLength), trackLength);
209 const auto travel = trackLength - length;
211 detail::scrollMultiplyDivide(std::min(offset, range.maximum),
static_cast<std::uint64_t
>(travel), range.maximum);
213 return ScrollThumb{
static_cast<std::int32_t
>(start.quotient), length, travel};
231[[nodiscard]]
constexpr std::expected<ScrollFractionalThumb, TreeError>
233 std::int32_t minimumThumb = 1) noexcept {
237 if (trackLength == 0) {
240 constexpr std::uint64_t EIGHTHS_PER_CELL = 8;
242 const auto track =
static_cast<std::uint64_t
>(trackLength) * EIGHTHS_PER_CELL;
246 const auto proportional =
247 detail::scrollMultiplyDivide(std::min(metrics.
viewport, metrics.
extent), track, metrics.
extent);
248 const auto minimum =
static_cast<std::uint64_t
>(std::min(minimumThumb, trackLength)) * EIGHTHS_PER_CELL;
249 const auto length =
static_cast<std::int64_t
>(std::clamp(proportional.quotient, minimum, track));
250 const auto travel = track -
static_cast<std::uint64_t
>(length);
251 if (maximum == 0 || travel == 0) {
254 if (offset.
raster >= maximum) {
255 return ScrollFractionalThumb{
static_cast<std::int64_t
>(travel), length,
static_cast<std::int64_t
>(travel)};
257 const auto position = detail::scrollMultiplyDivide(offset.
raster, travel, maximum);
258 const auto fraction = detail::scrollFractionUnits(offset.
fraction, travel);
259 const auto remainder = fraction % maximum;
261 const auto start = position.quotient + fraction / maximum + (position.remainder >= maximum - remainder ? 1 : 0);
262 return ScrollFractionalThumb{
static_cast<std::int64_t
>(start), length,
static_cast<std::int64_t
>(travel)};
272[[nodiscard]]
constexpr std::expected<std::uint64_t, TreeError>
278 if (position <= 0 || travel == 0 || maximum == 0) {
282 if (position >= travel) {
286 const auto denominator =
static_cast<std::uint64_t
>(travel);
287 auto value = detail::scrollMultiplyDivide(
static_cast<std::uint64_t
>(position), maximum, denominator);
289 if (value.remainder > denominator - value.remainder) {
293 return value.quotient;
336[[nodiscard]]
constexpr std::expected<ScrollAreaGeometry, TreeError>
342 if (!validPolicy(options.horizontal) || !validPolicy(options.vertical) ||
351 const auto width = std::max(allocation.
width, 0);
352 const auto height = std::max(allocation.
height, 0);
353 const auto padding = options.frame ? options.frameSides.insets() :
LayoutPadding{};
354 const auto innerWidth = std::max(width - padding.left - padding.right, 0);
355 const auto innerHeight = std::max(height - padding.top - padding.bottom, 0);
358 result.
viewportOrigin = {std::min(width, padding.left), std::min(height, padding.top)};
362 const auto viewport = [&] {
363 return CellSize{std::max(innerWidth - (result.verticalBar ? 1 : 0), 0),
364 std::max(innerHeight - (result.horizontalBar ? 1 : 0), 0)};
368 for (
int pass = 0; pass < 3; ++pass) {
372 result.horizontalBar |=
374 result.verticalBar |=
384 if (result.horizontalBar && innerWidth != 0 && height > padding.top) {
385 result.horizontal = {{padding.left, height - 1}, innerWidth};
388 if (result.verticalBar && innerHeight != 0 && width > padding.left) {
389 result.vertical = {{width - 1, padding.top}, innerHeight};
395 if (result.horizontalBar && innerHeight != 0 && result.childClipSize.width != 0) {
396 result.horizontal = {{result.viewportOrigin.x, result.viewportOrigin.y + innerHeight - 1},
397 result.childClipSize.width};
400 if (result.verticalBar && innerWidth != 0 && result.childClipSize.height != 0) {
401 result.vertical = {{result.viewportOrigin.x + innerWidth - 1, result.viewportOrigin.y},
402 result.childClipSize.height};
405 result.hasCorner = result.horizontalBar && result.verticalBar && innerWidth != 0 && innerHeight != 0;
407 if (result.hasCorner) {
408 result.corner = {result.viewportOrigin.x + innerWidth - 1, result.viewportOrigin.y + innerHeight - 1};
Public Portals API.
Definition action.hpp:8
constexpr ScrollRange scrollRange(ScrollAxisMetrics metrics) noexcept
Compute an exact range without unsigned subtraction or addition overflow.
Definition scroll_geometry.hpp:63
ScrollBarPolicy
Presence policy, independent of whether the content can scroll.
Definition scroll_geometry.hpp:21
@ NEVER
Do not allocate or paint a bar.
Definition scroll_geometry.hpp:22
@ AUTO
Show a bar only when its logical extent exceeds the viewport.
Definition scroll_geometry.hpp:23
@ ALWAYS
Show a bar even when its thumb cannot move.
Definition scroll_geometry.hpp:24
constexpr std::expected< ScrollAreaGeometry, TreeError > solveScrollLayout(CellSize allocation, ScrollExtent extent, ScrollLayoutOptions options={}) noexcept
Solve both bar policies together without cross-frame oscillation.
Definition scroll_geometry.hpp:337
ScrollPlacement
Explicit relationship between bars, content and an optional frame.
Definition scroll_geometry.hpp:28
@ OVERLAY
Bars obscure edge cells of the unchanged logical viewport.
Definition scroll_geometry.hpp:30
@ ON_FRAME
Bars occupy the right/bottom frame lines, excluding corners.
Definition scroll_geometry.hpp:31
@ RESERVED
Bars occupy one-cell gutters outside the logical viewport.
Definition scroll_geometry.hpp:29
constexpr std::uint64_t scrollPageStep(std::uint64_t viewport, std::uint64_t overlap=1) noexcept
Compute one page movement using guarded unsigned subtraction.
Definition scroll_geometry.hpp:83
constexpr std::expected< ScrollThumb, TreeError > scrollThumb(ScrollAxisMetrics metrics, std::uint64_t offset, std::int32_t trackLength, std::int32_t minimumThumb=1) noexcept
Map exact logical position to a thumb without a floating-point index round trip.
Definition scroll_geometry.hpp:187
constexpr bool validScrollOffset(ScrollOffset offset) noexcept
Validate an offset remainder without losing its integral index.
Definition scroll_geometry.hpp:74
ScrollAxis
Logical scrolling axis; X increases right and Y increases down.
Definition scroll_geometry.hpp:15
@ VERTICAL
Rows.
Definition scroll_geometry.hpp:17
@ HORIZONTAL
Columns.
Definition scroll_geometry.hpp:16
constexpr std::expected< std::uint64_t, TreeError > scrollOffsetForThumb(std::int64_t position, std::int32_t travel, std::uint64_t maximum) noexcept
Inverse thumb mapping, rounded to nearest integer with ties toward the start.
Definition scroll_geometry.hpp:273
constexpr std::expected< ScrollFractionalThumb, TreeError > scrollFractionalThumb(ScrollAxisMetrics metrics, ScrollOffset offset, std::int32_t trackLength, std::int32_t minimumThumb=1) noexcept
Map an exact logical position to eighth-cell thumb placement.
Definition scroll_geometry.hpp:232
@ INVALID_ARGUMENT
An option, enum value or operation argument is invalid.
Definition widget_types.hpp:27
An owning value in terminal columns/rows, x right and y down.
Definition geometry.hpp:24
An owning available extent in terminal columns/rows.
Definition geometry.hpp:44
std::int32_t height
Available number of rows, possibly negative before normalization.
Definition geometry.hpp:48
std::int32_t width
Available number of columns, possibly negative before normalization.
Definition geometry.hpp:46
Present frame sides, also defining one-cell content insets.
Definition frame.hpp:19
Owning nonnegative padding in terminal cells.
Definition layout.hpp:185