8#include "../assert.hpp"
10#include "widget_baseline.hpp"
16namespace hi::inline v1 {
61 _num_cells =
std::max(_num_cells, last);
62 _constraints.emplace_back(first, last, minimum, preferred, maximum, margin_before, margin_after, baseline);
83 return add_constraint(index, index + 1, minimum, preferred, maximum, margin_before, margin_after, baseline);
100 [[nodiscard]]
std::
size_t num_cells() const noexcept
132 [[nodiscard]]
float margin_before() const noexcept
134 return _cells.front().margin;
137 [[nodiscard]]
float margin_after() const noexcept
139 return _cells.back().margin;
155 hi_axiom(index < num_cells());
156 return get_position(_cells.begin(), _cells.begin() + index);
167 hi_axiom(first <= last);
168 hi_axiom(last <= _cells.size());
169 return get_size(_cells.begin() + first, _cells.begin() + last);
179 return get_size(index, index + 1);
190 return {get_position(first), get_size(first, last)};
200 return get_position_and_size(index, index + 1);
211 hilet position = get_position(first);
212 hilet size = get_size(first, last);
213 return {position, position + size};
224 return get_positions(index, index + 1);
229 for (
auto i = first; i != last; ++i) {
230 if (_cells[i].baseline) {
231 return _cells[i].baseline;
237 [[nodiscard]] widget_baseline get_baseline(
std::size_t index)
const noexcept
239 return get_baseline(index, index + 1);
243 struct constraint_type {
251 widget_baseline baseline;
253 [[nodiscard]]
bool is_single_cell() const noexcept
255 return first == last - 1;
258 [[nodiscard]]
bool is_span() const noexcept
260 return not is_single_cell();
287 widget_baseline baseline;
289 cell_type() noexcept :
294 maximum(
std::numeric_limits<
float>::infinity()),
299 void fix_constraint() noexcept
301 inplace_max(maximum, minimum);
302 inplace_clamp(preferred, minimum, maximum);
305 void set_constraint(constraint_type
const& constraint)
noexcept
307 inplace_max(minimum, constraint.minimum);
308 inplace_max(preferred, constraint.preferred);
309 inplace_min(maximum, constraint.maximum);
312 [[nodiscard]]
bool holds_invariant() const noexcept
314 return minimum <= preferred and preferred <= maximum;
320 using cell_iterator = cell_vector_type::iterator;
321 using cell_const_iterator = cell_vector_type::const_iterator;
324 float _minimum = 0.0f;
325 float _preferred = 0.0f;
326 float _maximum = 0.0f;
327 contraint_vector_type _constraints;
328 cell_vector_type _cells;
336 [[nodiscard]]
float get_position(cell_const_iterator begin, cell_const_iterator first)
const noexcept
339 auto position = 0.0f;
340 while (it != first) {
341 position += it->size;
343 position += it->margin;
354 [[nodiscard]]
static float get_size(cell_const_iterator first, cell_const_iterator last)
noexcept
361 auto size = it->size;
363 for (; it != last; ++it) {
370 void constrain_cells_by_singles() noexcept;
371 void constrain_cells_by_spans(
std::function<
float(constraint_type const&)> const& predicate) noexcept;
372 [[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:23
float minimum() const noexcept
The minimum size of the total grid_layout.
Definition grid_layout.hpp:109
float maximum() const noexcept
The minimum size of the total grid_layout.
Definition grid_layout.hpp:127
void commit_constraints() noexcept
Commit all the constraints.
float preferred() const noexcept
The minimum size of the total grid_layout.
Definition grid_layout.hpp:118
void add_constraint(std::size_t index, float minimum, float preferred, float maximum, float margin_before, float margin_after, widget_baseline baseline=widget_baseline{}) noexcept
Add a constraint for a widget.
Definition grid_layout.hpp:74
void clear() noexcept
Clear the list of widgets in the layout.
Definition grid_layout.hpp:34
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:209
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:222
void add_constraint(std::size_t first, std::size_t last, float minimum, float preferred, float maximum, float margin_before, float margin_after, widget_baseline baseline=widget_baseline{}) noexcept
Add a constraint for a widget.
Definition grid_layout.hpp:51
float get_size(std::size_t index) const noexcept
Get size of the cell.
Definition grid_layout.hpp:177
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:165
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:188
float get_position(std::size_t index) const noexcept
Get position of cell.
Definition grid_layout.hpp:153
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:198
The base-line of a widget on which to set the text and graphics.
Definition widget_baseline.hpp:13