TermOx
unique_space.hpp
1 #ifndef TERMOX_WIDGET_LAYOUTS_DETAIL_UNIQUE_SPACE_HPP
2 #define TERMOX_WIDGET_LAYOUTS_DETAIL_UNIQUE_SPACE_HPP
3 #include <cstddef>
4 #include <utility>
5 #include <vector>
6 
7 #include <termox/widget/widget.hpp>
8 
9 namespace ox::layout::detail {
10 
12 template <typename Parameters>
13 class Unique_space {
14  private:
15  using Length_list = std::vector<int>;
16  using Position_list = std::vector<int>;
17 
18  public:
19  [[nodiscard]] auto calculate_lengths(Widget& parent) -> Length_list
20  {
21  auto result = Length_list{};
22  auto const limit = typename Parameters::Secondary::get_length{}(parent);
23  auto children = parent.get_children();
24  auto begin = std::next(std::begin(children), offset_);
25  auto const end = std::end(children);
26 
27  for (; begin != end; ++begin) {
28  auto const& policy =
29  typename Parameters::Secondary::get_policy{}(*begin);
30  if (limit > policy.max())
31  result.push_back(policy.max());
32  else if (limit < policy.min() && !policy.can_ignore_min())
33  result.push_back(0);
34  else
35  result.push_back(limit);
36  }
37  return result;
38  }
39 
40  [[nodiscard]] auto calculate_positions(Length_list const& lengths)
41  -> Position_list
42  {
43  return Position_list(lengths.size(), 0);
44  }
45 
47  [[nodiscard]] auto get_offset() const -> std::size_t { return offset_; }
48 
50  void set_offset(std::size_t index) { offset_ = index; }
51 
52  private:
53  std::size_t offset_ = 0;
54 };
55 
56 } // namespace ox::layout::detail
57 #endif // TERMOX_WIDGET_LAYOUTS_DETAIL_UNIQUE_SPACE_HPP
Definition: widget.hpp:31
auto get_children()
Get a range containing Widget& to each child.
Definition: widget.hpp:214
Gives space to child Widgets where each gets full use of the length.
Definition: unique_space.hpp:13
void set_offset(std::size_t index)
Sets the child Widget offset, does not do bounds checking.
Definition: unique_space.hpp:50
auto get_offset() const -> std::size_t
Return the child Widget offset, the first widget included in the layout.
Definition: unique_space.hpp:47