skip to content
==> include/tui/layout.h <==
1/* @ajax/tui-layout: ratatui's Layout. A rectangle is split along one direction into segments, one
2 * per constraint, by the same Cassowary problem ratatui builds (the same variables, constraints,
3 * strengths and rounding), so every segment lands on the same cell. Results are cached, as
4 * ratatui's are. */
5#ifndef TUI_LAYOUT_H
6#define TUI_LAYOUT_H
7
8#include "tui/rect.h"
9
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13
14enum { TUI_C_MIN, TUI_C_MAX, TUI_C_LENGTH, TUI_C_PERCENTAGE, TUI_C_RATIO, TUI_C_FILL };
15
16typedef struct {
17    uint8_t kind;
18    uint32_t a, b; /* the value; Ratio: a / b */
19} tui_constraint;
20
21#define TUI_LENGTH(n) ((tui_constraint){TUI_C_LENGTH, (uint32_t)(n), 0})
22#define TUI_MIN(n) ((tui_constraint){TUI_C_MIN, (uint32_t)(n), 0})
23#define TUI_MAX(n) ((tui_constraint){TUI_C_MAX, (uint32_t)(n), 0})
24#define TUI_PERCENTAGE(n) ((tui_constraint){TUI_C_PERCENTAGE, (uint32_t)(n), 0})
25#define TUI_RATIO(n, d) ((tui_constraint){TUI_C_RATIO, (uint32_t)(n), (uint32_t)(d)})
26#define TUI_FILL(n) ((tui_constraint){TUI_C_FILL, (uint32_t)(n), 0})
27
28typedef enum {
29    TUI_FLEX_LEGACY,
30    TUI_FLEX_START, /* ratatui's default */
31    TUI_FLEX_END,
32    TUI_FLEX_CENTER,
33    TUI_FLEX_SPACE_BETWEEN,
34    TUI_FLEX_SPACE_AROUND,
35    TUI_FLEX_SPACE_EVENLY,
36} tui_flex;
37
38typedef struct {
39    bool vertical;
40    const tui_constraint *constraints;
41    size_t n;
42    uint16_t margin_h, margin_v;
43    tui_flex flex;
44    int16_t spacing; /* negative: overlap */
45} tui_layout;
46
47/* Writes n segments to out. spacers (optional) receives the n + 1 gaps around them. */
48void tui_layout_split(const tui_layout *l, tui_rect area, tui_rect *out, tui_rect *spacers);
49/* Shorthands with the defaults (Flex::Start, no spacing, no margin). */
50void tui_vsplit(tui_rect area, size_t n, const tui_constraint *c, tui_rect *out);
51void tui_hsplit(tui_rect area, size_t n, const tui_constraint *c, tui_rect *out);
52/* Forgets every cached split. */
53void tui_layout_cache_clear(void);
54
55#endif