Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
primitives.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/controls.hpp>
6#include <portals/frame.hpp>
7
8#include <array>
9#include <memory>
10#include <optional>
11#include <string>
12#include <utility>
13
14// Spell the existing decoration enum without changing the caller's native Unicode-selection macro.
15#pragma push_macro("UNICODE")
16#undef UNICODE
17
18namespace portals {
25
32
35 // Count constructors and copy construction let MSVC Debug proxy allocation failures propagate.
36 std::string topLeft = std::string(0, '\0');
37 std::string topRight = std::string(0, '\0');
38 std::string bottomLeft = std::string(0, '\0');
39 std::string bottomRight = std::string(0, '\0');
40 std::string horizontal = std::string(0, '\0');
41 std::string vertical = std::string(0, '\0');
42
44 ~FrameGlyphs() = default;
45};
46
48inline constexpr std::size_t MAX_FRAME_TITLE_END_BYTES = 64;
50inline constexpr std::int32_t MAX_FRAME_TITLE_END_CELLS = 16;
51
54 std::string prefix = std::string(0, '\0');
55 std::string suffix = std::string(0, '\0');
56
59};
60
76
77namespace detail {
78struct GlyphProfileAccess;
79struct PreparedGlyphProfiles;
80
88} // namespace detail
89
92 double minimum{};
93 double maximum{1};
94 std::int32_t preferredWidth{10};
96 std::size_t maxPaintCells{16384};
97};
98
105class Border : public SizedWidget, public Layout {
106public:
114 explicit Border(WidthPolicy width = {}, BorderOptions options = {}, ControlStyle style = {}, SizePolicy policy = {});
122 Border(WidthPolicy width, FrameOptions options, ControlStyle style, SizePolicy policy, std::in_place_t advanced);
126 std::expected<CellSize, TreeError> measure(CellSize available) override;
130 std::expected<CellSize, TreeError> childAvailable(CellSize available) const override;
135 std::expected<std::vector<CellRect>, TreeError> arrangeChildren(std::span<const MeasuredChild> children,
136 CellSize available) const override;
137
141 std::expected<CellSize, TreeError> measureChildren(std::span<const MeasuredChild> children) const override;
142
146 std::expected<void, TreeError> arrange(CellRect local) override;
150 std::expected<CellRect, TreeError> childClip(CellSize available) const override;
156 std::expected<void, TreeError> paint(PaintContext &context) override;
158 ComputedStyle styleDefaults() const override;
162 static std::expected<void, TreeError> validate(BorderOptions options);
166 static std::expected<void, TreeError> validateFrame(const FrameOptions &options) noexcept;
170 std::expected<void, TreeError> preparation() const noexcept;
171
172private:
173 friend struct detail::GlyphProfileAccess;
174 friend std::expected<void, TreeError> syncBorder(WidgetTree &, Node<Border>);
175 friend std::expected<void, TreeError> syncBorder(WidgetTree &, Node<Border>, Node<Widget>);
176 friend std::expected<void, TreeError> setBorderTitle(WidgetTree &, Node<Border>, const GraphemeText &);
177 Column layout_;
178 FrameOptions options_;
179 ControlStyle style_;
180 std::optional<detail::PreparedFrameText> text_;
181 std::shared_ptr<const detail::PreparedGlyphProfiles> glyphProfiles_;
182 std::optional<TreeError> preparationError_;
183 CellRect bounds_;
184 bool enabled_{true};
185 bool focused_{};
186};
187
193class Gauge : public SizedWidget {
194public:
202 explicit Gauge(double value, WidthPolicy width = {}, GaugeOptions options = {}, ControlStyle style = {},
203 SizePolicy policy = DEFAULT_CONTENT_POLICY);
207 std::expected<CellSize, TreeError> measure(CellSize available) override;
211 std::expected<void, TreeError> arrange(CellRect local) override;
216 std::expected<void, TreeError> paint(PaintContext &context) override;
218 ComputedStyle styleDefaults() const override;
220 double value() const noexcept;
225 std::expected<void, TreeError> display(double value);
230 static std::expected<void, TreeError> validate(double value, GaugeOptions options);
231
232private:
233 friend std::expected<void, TreeError> setGaugeValue(WidgetTree &, Node<Gauge>, double);
234 friend std::expected<void, TreeError> adjustGaugeValue(WidgetTree &, Node<Gauge>, double);
235 friend std::expected<void, TreeError> syncGauge(WidgetTree &, Node<Gauge>);
236 double value_;
237 GaugeOptions options_;
238 ControlStyle style_;
239 GraphemeText glyph_;
240 CellRect bounds_;
241 bool enabled_{true};
242};
243
249std::expected<void, TreeError> setGaugeValue(WidgetTree &tree, Node<Gauge> node, double value);
256std::expected<void, TreeError> adjustGaugeValue(WidgetTree &tree, Node<Gauge> node, double delta);
261std::expected<void, TreeError> syncBorder(WidgetTree &tree, Node<Border> node);
268std::expected<void, TreeError> syncBorder(WidgetTree &tree, Node<Border> node, Node<Widget> focused);
277std::expected<void, TreeError> setBorderTitle(WidgetTree &tree, Node<Border> node, const GraphemeText &title);
282std::expected<void, TreeError> syncGauge(WidgetTree &tree, Node<Gauge> node);
283} // namespace portals
284
285#pragma pop_macro("UNICODE")
std::expected< std::vector< CellRect >, TreeError > arrangeChildren(std::span< const MeasuredChild > children, CellSize available) const override
friend std::expected< void, TreeError > syncBorder(WidgetTree &, Node< Border >)
std::expected< void, TreeError > paint(PaintContext &context) override
std::expected< CellSize, TreeError > childAvailable(CellSize available) const override
static std::expected< void, TreeError > validate(BorderOptions options)
std::expected< void, TreeError > arrange(CellRect local) override
static std::expected< void, TreeError > validateFrame(const FrameOptions &options) noexcept
std::expected< CellRect, TreeError > childClip(CellSize available) const override
ComputedStyle styleDefaults() const override
Border(WidthPolicy width, FrameOptions options, ControlStyle style, SizePolicy policy, std::in_place_t advanced)
std::expected< CellSize, TreeError > measure(CellSize available) override
Border(WidthPolicy width={}, BorderOptions options={}, ControlStyle style={}, SizePolicy policy={})
std::expected< CellSize, TreeError > measureChildren(std::span< const MeasuredChild > children) const override
std::expected< void, TreeError > preparation() const noexcept
friend std::expected< void, TreeError > setBorderTitle(WidgetTree &, Node< Border >, const GraphemeText &)
Replace a frame title through the existing checked tree mutation boundary.
An owning, invariant-preserving half-open cell rectangle.
Definition geometry.hpp:81
Lay visible children from top to bottom.
Definition layout.hpp:260
double value() const noexcept
ComputedStyle styleDefaults() const override
std::expected< CellSize, TreeError > measure(CellSize available) override
Gauge(double value, WidthPolicy width={}, GaugeOptions options={}, ControlStyle style={}, SizePolicy policy=DEFAULT_CONTENT_POLICY)
friend std::expected< void, TreeError > setGaugeValue(WidgetTree &, Node< Gauge >, double)
std::expected< void, TreeError > arrange(CellRect local) override
std::expected< void, TreeError > paint(PaintContext &context) override
std::expected< void, TreeError > display(double value)
static std::expected< void, TreeError > validate(double value, GaugeOptions options)
friend std::expected< void, TreeError > syncGauge(WidgetTree &, Node< Gauge >)
friend std::expected< void, TreeError > adjustGaugeValue(WidgetTree &, Node< Gauge >, double)
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
Pure container interface; callbacks run inside tree.read and an enclosing tree.frame....
Definition layout.hpp:130
Copyable typed observer; never owns or exposes an unchecked widget pointer.
Definition widget.hpp:92
Borrowed synchronous command sink supplied by the frame owner. submit must consume commands immediate...
Definition widget_types.hpp:66
SizedWidget(SizePolicy policy={})
Own the supplied policies; checked by layout.
Exclusive parent/child ownership on Scheduler's UI thread. Scheduler must outlive this object....
Definition widget.hpp:179
UI-owned extension object. Hooks borrow their arguments for one call. Models are separate explicit ow...
Definition widget.hpp:32
Public Portals API.
Definition action.hpp:8
FramePattern
Built-in frame line patterns, independent of terminal width policy.
Definition frame.hpp:9
@ SINGLE
Single lines and square corners.
Definition frame.hpp:10
PrimitiveGlyphs
One-cell decoration selection under the complete receiving WidthPolicy.
Definition primitives.hpp:20
@ UNICODE
Unicode lines/blocks, falling back to ASCII when a glyph is not one cell.
Definition primitives.hpp:21
TitleDecoration
Optional top-title break decoration; NONE preserves literal legacy titles.
Definition frame.hpp:39
@ NONE
No additional title cells.
Definition frame.hpp:40
constexpr SizePolicy DEFAULT_CONTENT_POLICY
Intrinsic content on both axes; explicit policies remain authoritative.
Definition layout.hpp:44
std::expected< void, TreeError > setGaugeValue(WidgetTree &tree, Node< Gauge > node, double value)
std::expected< void, TreeError > syncBorder(WidgetTree &tree, Node< Border > node)
@ ASCII
Portable vertical bar and hash brushes.
Definition chart.hpp:56
@ UNICODE_GLYPHS
Portable Unicode rounded/inset forms.
Definition glyph_profile.hpp:14
std::expected< void, TreeError > setBorderTitle(WidgetTree &tree, Node< Border > node, const GraphemeText &title)
Replace a frame title through the existing checked tree mutation boundary.
constexpr std::size_t MAX_FRAME_TITLE_END_BYTES
Maximum complete UTF-8 source bytes in each title end.
Definition primitives.hpp:48
FrameTitleAlignment
Alignment of the maximal whole-grapheme title prefix fitting the top edge.
Definition frame.hpp:32
@ START
Place the visible prefix after the leading corner.
Definition frame.hpp:33
std::expected< void, TreeError > adjustGaugeValue(WidgetTree &tree, Node< Gauge > node, double delta)
std::expected< void, TreeError > syncGauge(WidgetTree &tree, Node< Gauge > node)
constexpr std::int32_t MAX_FRAME_TITLE_END_CELLS
Maximum printable one-cell graphemes in each title end.
Definition primitives.hpp:50
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
Owning frame decoration and finite paint-work policy.
Definition primitives.hpp:27
GlyphSelection glyphSelection
Opt-in manual base below stylesheet overrides and literal sources.
Definition primitives.hpp:30
std::size_t maxPaintCells
Positive cap on allocated perimeter cells, even when clipped.
Definition primitives.hpp:29
PrimitiveGlyphs glyphs
Owning glyph selection; no font probing.
Definition primitives.hpp:28
An owning available extent in terminal columns/rows.
Definition geometry.hpp:44
Owning resolved presentation; no actions or model state are encoded.
Definition style_types.hpp:169
Owning paint values; no theme engine or behavior is encoded here.
Definition controls.hpp:24
Owning title ends; each holds at most 64 UTF-8 bytes and 16 printable one-cell graphemes.
Definition primitives.hpp:53
~CustomTitleDecoration()=default
Preserve fallible checked-library transfers.
std::string prefix
Complete left end, including desired spacing.
Definition primitives.hpp:54
std::string suffix
Complete right end, including desired spacing.
Definition primitives.hpp:55
Owning custom source; each glyph must be one printable one-cell grapheme, at most 64 bytes.
Definition primitives.hpp:34
std::string topLeft
Corner joining top and left sides.
Definition primitives.hpp:36
std::string horizontal
Ordinary top/bottom edge.
Definition primitives.hpp:40
std::string bottomRight
Corner joining bottom and right sides.
Definition primitives.hpp:39
std::string bottomLeft
Corner joining bottom and left sides.
Definition primitives.hpp:38
std::string vertical
Ordinary left/right edge.
Definition primitives.hpp:41
std::string topRight
Corner joining top and right sides.
Definition primitives.hpp:37
~FrameGlyphs()=default
Destroy owned glyph sources; keep construction fallible rather than generating a noexcept string move...
Owning advanced frame settings; legacy BorderOptions remains a literal value type.
Definition primitives.hpp:62
std::size_t maxTitleBytes
Positive maximum complete title source bytes, including clipped text.
Definition primitives.hpp:69
std::optional< CustomTitleDecoration > customTitleDecoration
Owning ends overriding the preset.
Definition primitives.hpp:71
BorderOptions border
Existing glyph selection and paint-work cap.
Definition primitives.hpp:63
std::optional< FrameGlyphs > customGlyphs
Optional owning custom glyph source, compiled at construction.
Definition primitives.hpp:66
FrameTitleAlignment titleAlignment
Alignment of the fitting title prefix.
Definition primitives.hpp:68
FramePattern pattern
Built-in line pattern, overridden by customGlyphs.
Definition primitives.hpp:64
TitleDecoration titleDecoration
Opt-in title ends; NONE keeps the literal title.
Definition primitives.hpp:70
FrameSides sides
Independently present one-cell edges and their content insets.
Definition primitives.hpp:65
std::string title
Owning UTF-8 top-edge title; does not increase intrinsic width.
Definition primitives.hpp:67
~FrameOptions()=default
Destroy owned sources; copy construction preserves checked allocation failures in MSVC Debug.
Present frame sides, also defining one-cell content insets.
Definition frame.hpp:19
Owning strictly determinate gauge settings.
Definition primitives.hpp:91
double minimum
Inclusive finite lower bound.
Definition primitives.hpp:92
std::int32_t preferredWidth
Nonnegative intrinsic width in cells; height is one.
Definition primitives.hpp:94
double maximum
Finite upper bound, strictly greater than minimum.
Definition primitives.hpp:93
PrimitiveGlyphs glyphs
Block or ASCII fill, with one-cell fallback.
Definition primitives.hpp:95
std::size_t maxPaintCells
Positive cap on allocated area cells, even when clipped.
Definition primitives.hpp:96
Complete logical selection; resolution and copies never allocate.
Definition glyph_profile.hpp:29
Owning independent policies for width and height.
Definition layout.hpp:35
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42
Construction-time owning frame text retained through each synchronous paint.
Definition primitives.hpp:82
GraphemeText title
Complete validated title; clipping never borrows a temporary prefix.
Definition primitives.hpp:84
GraphemeText prefix
Complete validated left title end.
Definition primitives.hpp:85
GraphemeText suffix
Complete validated right title end.
Definition primitives.hpp:86
std::array< GraphemeText, 6 > glyphs
Validated corners, horizontal and vertical one-cell glyphs.
Definition primitives.hpp:83