8#include "../assert.hpp"
15namespace hi::inline v1 {
52 _num_cells =
std::max(_num_cells, last);
53 _constraints.emplace_back(first, last, minimum, preferred, maximum, margin_before, margin_after);
65 void add_constraint(
std::size_t index,
float minimum,
float preferred,
float maximum,
float margin_before,
float margin_after)
noexcept
67 return add_constraint(index, index + 1, minimum, preferred, maximum, margin_before, margin_after);
84 [[nodiscard]]
std::
size_t num_cells() const noexcept
93 [[nodiscard]]
float minimum() const noexcept
116 [[nodiscard]]
float margin_before() const noexcept
118 return _cells.front().margin;
121 [[nodiscard]]
float margin_after() const noexcept
123 return _cells.back().margin;
139 hi_axiom(index < num_cells());
140 return get_position(_cells.begin(), _cells.begin() + index);
151 hi_axiom(first <= last);
152 hi_axiom(last <= _cells.size());
153 return get_size(_cells.begin() + first, _cells.begin() + last);
163 return get_size(index, index + 1);
174 return {get_position(first), get_size(first, last)};
184 return get_position_and_size(index, index + 1);
195 hilet position = get_position(first);
196 hilet size = get_size(first, last);
197 return {position, position + size};
208 return get_positions(index, index + 1);
212 struct constraint_type {
221 [[nodiscard]]
bool is_single_cell() const noexcept
223 return first == last - 1;
226 [[nodiscard]]
bool is_span() const noexcept
228 return not is_single_cell();
253 cell_type() noexcept :
254 size(0.0f), margin(0.0f), minimum(0.0f), preferred(0.0f), maximum(
std::numeric_limits<
float>::infinity())
258 void fix_constraint() noexcept
260 inplace_max(maximum, minimum);
261 inplace_clamp(preferred, minimum, maximum);
264 void set_constraint(constraint_type
const &constraint)
noexcept
266 inplace_max(minimum, constraint.minimum);
267 inplace_max(preferred, constraint.preferred);
268 inplace_min(maximum, constraint.maximum);
271 [[nodiscard]]
bool holds_invariant() const noexcept
273 return minimum <= preferred and preferred <= maximum;
279 using cell_iterator = cell_vector_type::iterator;
280 using cell_const_iterator = cell_vector_type::const_iterator;
283 float _minimum = 0.0f;
284 float _preferred = 0.0f;
285 float _maximum = 0.0f;
286 contraint_vector_type _constraints;
287 cell_vector_type _cells;
295 [[nodiscard]]
float get_position(cell_const_iterator begin, cell_const_iterator first)
const noexcept
298 auto position = 0.0f;
299 while (it != first) {
300 position += it->size;
302 position += it->margin;
313 [[nodiscard]]
static float get_size(cell_const_iterator first, cell_const_iterator last)
noexcept
320 auto size = it->size;
322 for (; it != last; ++it) {
329 void constrain_cells_by_singles() noexcept;
330 void constrain_cells_by_spans(
std::function<
float(constraint_type const &)> const &predicate) noexcept;
331 [[nodiscard]]
bool holds_invariant() const noexcept;
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Grid layout is used to layout widgets along an axis.
Definition grid_layout.hpp:22
float minimum() const noexcept
The minimum size of the total grid_layout.
Definition grid_layout.hpp:93
float maximum() const noexcept
The minimum size of the total grid_layout.
Definition grid_layout.hpp:111
void commit_constraints() noexcept
Commit all the constraints.
float preferred() const noexcept
The minimum size of the total grid_layout.
Definition grid_layout.hpp:102
void add_constraint(std::size_t first, std::size_t last, float minimum, float preferred, float maximum, float margin_before, float margin_after) noexcept
Add a constraint for a widget.
Definition grid_layout.hpp:50
void clear() noexcept
Clear the list of widgets in the layout.
Definition grid_layout.hpp:33
std::pair< float, float > get_positions(std::size_t first, std::size_t last) const noexcept
Get the start and end position of the cells.
Definition grid_layout.hpp:193
std::pair< float, float > get_positions(std::size_t index) const noexcept
Get the start and end position of a cell.
Definition grid_layout.hpp:206
float get_size(std::size_t index) const noexcept
Get size of the cell.
Definition grid_layout.hpp:161
void layout(float size) noexcept
Layout the cells based on the total size.
float get_size(std::size_t first, std::size_t last) const noexcept
Get size of the cells.
Definition grid_layout.hpp:149
std::pair< float, float > get_position_and_size(std::size_t first, std::size_t last) const noexcept
Get the position and size of a cell-span.
Definition grid_layout.hpp:172
float get_position(std::size_t index) const noexcept
Get position of cell.
Definition grid_layout.hpp:137
void add_constraint(std::size_t index, float minimum, float preferred, float maximum, float margin_before, float margin_after) noexcept
Add a constraint for a widget.
Definition grid_layout.hpp:65
std::pair< float, float > get_position_and_size(std::size_t index) const noexcept
Get the position and size of cell.
Definition grid_layout.hpp:182