TinyMaterialDesign
Loading...
Searching...
No Matches
FlowLayout.h
Go to the documentation of this file.
1#pragma once
2#include "TinyMaterialDesign/Core/Bounds.h"
3
4namespace tinymd {
5
6/**
7 * @brief Computes wrapping rects for items of varying width (chip/tag rows).
8 *
9 * Not a widget - see GridLayout.h for the rationale. Unlike GridLayout
10 * (uniform cells), each item reports its own width; items are packed
11 * left-to-right and wrap to a new row once they would overflow the
12 * container's width:
13 *
14 * FlowLayout flow(Bounds(16, 56, 308, 200));
15 * int32_t widths[] = {60, 90, 70, 100, 50};
16 * for (int i = 0; i < 5; ++i) chips[i].bounds = flow.next(widths[i], chipHeight);
17 *
18 * Calls to next() must be made in order (0, 1, 2, ...) since each call
19 * advances internal cursor state. Use totalHeight() after a full pass, or
20 * reset() to run the same set of widths again once you know it.
21 */
23 public:
24 explicit FlowLayout(Bounds container, int32_t spacing = 8)
25 : container_(container), spacing_(spacing) {
26 reset();
27 }
28
29 /// Bounds for the next item of the given `width`/`height`, wrapping to a
30 /// new row if it doesn't fit in the remaining width of the current row.
31 Bounds next(int32_t width, int32_t height) {
32 if (cursorX_ != container_.x && cursorX_ + width > container_.right()) {
33 cursorX_ = container_.x;
34 cursorY_ += rowHeight_ + spacing_;
35 rowHeight_ = 0;
36 }
37 const Bounds rect(cursorX_, cursorY_, width, height);
38 cursorX_ += width + spacing_;
39 if (height > rowHeight_) rowHeight_ = height;
40 return rect;
41 }
42
43 /// Restarts packing from the container's top-left corner.
44 void reset() {
45 cursorX_ = container_.x;
46 cursorY_ = container_.y;
47 rowHeight_ = 0;
48 }
49
50 /// Total height consumed by items placed since the last reset().
51 int32_t totalHeight() const { return (cursorY_ - container_.y) + rowHeight_; }
52
53 private:
54 Bounds container_;
55 int32_t spacing_;
56 int32_t cursorX_ = 0;
57 int32_t cursorY_ = 0;
58 int32_t rowHeight_ = 0;
59};
60
61} // namespace tinymd
Computes wrapping rects for items of varying width (chip/tag rows).
Definition: FlowLayout.h:22
Bounds next(int32_t width, int32_t height)
Definition: FlowLayout.h:31
FlowLayout(Bounds container, int32_t spacing=8)
Definition: FlowLayout.h:24
int32_t totalHeight() const
Total height consumed by items placed since the last reset().
Definition: FlowLayout.h:51
void reset()
Restarts packing from the container's top-left corner.
Definition: FlowLayout.h:44
Definition: AnchorLayout.h:4
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t right() const
Definition: Bounds.h:30
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22