73 lines
2.7 KiB
Text
73 lines
2.7 KiB
Text
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
export module Nostro.Ui:Layout;
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import :Geometry;
|
||
|
|
|
||
|
|
// Boxes handed out one after the other: all the layout a screen of rows and forms needs. A screen says how tall each
|
||
|
|
// thing is, or asks the thing itself, and the cursor moves on.
|
||
|
|
|
||
|
|
export namespace Nostro::Ui {
|
||
|
|
// Down a column: each box is as wide as the column and starts where the one before ended, plus the gap.
|
||
|
|
struct Column {
|
||
|
|
Box area;
|
||
|
|
float gap = 0;
|
||
|
|
float cursor = 0; // the top of the next box
|
||
|
|
|
||
|
|
constexpr Column(Box area, float gap = 0) noexcept : area(area), gap(gap), cursor(area.y) {
|
||
|
|
}
|
||
|
|
|
||
|
|
// The box the next thing may use: from the cursor down to the area's end.
|
||
|
|
constexpr Box Next() const noexcept {
|
||
|
|
return {area.x, cursor, area.w, std::max(0.0f, area.Bottom() - cursor)};
|
||
|
|
}
|
||
|
|
// The next thing turned out this tall.
|
||
|
|
constexpr void Advance(float height) noexcept {
|
||
|
|
cursor += height + gap;
|
||
|
|
}
|
||
|
|
// The next box, `height` tall. It may reach past the area's end: a column that scrolls is as long as its content.
|
||
|
|
constexpr Box Take(float height) noexcept {
|
||
|
|
const Box box{area.x, cursor, area.w, height};
|
||
|
|
Advance(height);
|
||
|
|
return box;
|
||
|
|
}
|
||
|
|
// How tall everything taken so far is, without the gap after the last.
|
||
|
|
constexpr float Used() const noexcept {
|
||
|
|
return std::max(0.0f, cursor - gap - area.y);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// Along a row: boxes are taken from its left or its right end, and what is left between them is the rest.
|
||
|
|
struct Row {
|
||
|
|
Box area;
|
||
|
|
float gap = 0;
|
||
|
|
float left = 0;
|
||
|
|
float right = 0;
|
||
|
|
|
||
|
|
constexpr Row(Box area, float gap = 0) noexcept : area(area), gap(gap), left(area.x), right(area.Right()) {
|
||
|
|
}
|
||
|
|
|
||
|
|
constexpr Box TakeLeft(float width) noexcept {
|
||
|
|
const Box box{left, area.y, width, area.h};
|
||
|
|
left += width + gap;
|
||
|
|
return box;
|
||
|
|
}
|
||
|
|
constexpr Box TakeRight(float width) noexcept {
|
||
|
|
right -= width;
|
||
|
|
const Box box{right, area.y, width, area.h};
|
||
|
|
right -= gap;
|
||
|
|
return box;
|
||
|
|
}
|
||
|
|
constexpr Box Rest() const noexcept {
|
||
|
|
return {left, area.y, std::max(0.0f, right - left), area.h};
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// A box at most `maxWidth` wide in the middle of an area's width: what keeps a form readable in a wide window.
|
||
|
|
constexpr Box Centered(Box area, float maxWidth) noexcept {
|
||
|
|
const float width = std::min(area.w, maxWidth);
|
||
|
|
return {area.x + (area.w - width) / 2, area.y, width, area.h};
|
||
|
|
}
|
||
|
|
}
|