Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
spinner.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/text_value.hpp>
7
8#include <array>
9#include <initializer_list>
10#include <string_view>
11
12namespace portals {
14using SpinnerLabelResolver = std::function<std::expected<std::string, TextResourceError>(std::string_view)>;
30
32inline constexpr std::array<std::string_view, 12> SPINNER_PRESET_NAMES{"DOTS", "LINE", "BRAILLE", "BAR_BOUNCE",
33 "ARC", "CIRCLE", "SQUARE", "TRIANGLE",
34 "ARROW", "BLOCKS", "ELLIPSIS", "PULSE"};
35
39inline SpinnerPreset spinnerPreset(std::string_view name) noexcept {
40 for (std::size_t i = 0; i < SPINNER_PRESET_NAMES.size(); ++i) {
41 const auto candidate = SPINNER_PRESET_NAMES[i];
42 if (candidate.size() != name.size())
43 continue;
44 bool equal = true;
45 for (std::size_t j = 0; j < name.size(); ++j) {
46 char c = name[j];
47 if (c >= 'a' && c <= 'z')
48 c = static_cast<char>(c - 'a' + 'A');
49 if (c == '-')
50 c = '_';
51 if (c != candidate[j]) {
52 equal = false;
53 break;
54 }
55 }
56 if (equal)
57 return static_cast<SpinnerPreset>(i);
58 }
59 return static_cast<SpinnerPreset>(-1);
60}
61
62namespace detail {
64inline std::string spinnerFrames(std::initializer_list<std::string_view> values) {
65 std::string result;
66 bool first = true;
67 for (const auto value : values) {
68 if (!first)
69 result += '\n';
70 first = false;
71 if (value.empty() || value.find_first_of("\r\n") != std::string_view::npos)
72 result += '\r';
73 else
74 result += value;
75 }
76 return result;
77}
78} // namespace detail
79
85
104
105namespace detail {
106struct SpinnerState;
107}
108class ManagedScene;
109
115class Spinner : public SizedWidget {
116public:
124 explicit Spinner(WidthPolicy width = {}, SpinnerOptions options = {}, ControlStyle style = {},
125 SizePolicy policy = DEFAULT_CONTENT_POLICY, SpinnerLabelResolver resolver = {});
127 ~Spinner() override;
128 Spinner(const Spinner &) = delete;
129 Spinner &operator=(const Spinner &) = delete;
134 static std::expected<void, TreeError> validate(WidthPolicy width, SpinnerOptions options);
136 std::expected<void, TreeError> preparation() const noexcept;
140 std::expected<CellSize, TreeError> measure(CellSize available) override;
144 std::expected<void, TreeError> arrange(CellRect local) override;
148 std::expected<void, TreeError> paint(PaintContext &context) override;
150 ComputedStyle styleDefaults() const override;
152 std::size_t frameIndex() const noexcept;
155
156private:
157 friend class ManagedScene;
158 friend std::expected<void, TreeError> setSpinnerActive(WidgetTree &, Node<Spinner>, bool);
159 friend std::expected<void, TreeError> setSpinner(WidgetTree &, Node<Spinner>, const SpinnerOptions &,
161 std::shared_ptr<detail::SpinnerState> state_;
162 ControlStyle style_;
163 CellRect bounds_;
164};
165
171std::expected<void, TreeError> setSpinnerActive(WidgetTree &tree, Node<Spinner> node, bool active);
172
180std::expected<void, TreeError> setSpinner(WidgetTree &tree, Node<Spinner> node, const SpinnerOptions &options,
181 SpinnerLabelResolver resolver = {});
182} // namespace portals
An owning, invariant-preserving half-open cell rectangle.
Definition geometry.hpp:81
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.
Retained indeterminate indicator using the receiving scene's existing scheduler. Construct through ui...
Definition spinner.hpp:115
friend std::expected< void, TreeError > setSpinner(WidgetTree &, Node< Spinner >, const SpinnerOptions &, SpinnerLabelResolver)
SpinnerOptions options() const
ComputedStyle styleDefaults() const override
std::expected< void, TreeError > paint(PaintContext &context) override
static std::expected< void, TreeError > validate(WidthPolicy width, SpinnerOptions options)
std::size_t frameIndex() const noexcept
std::expected< void, TreeError > preparation() const noexcept
std::expected< void, TreeError > arrange(CellRect local) override
friend std::expected< void, TreeError > setSpinnerActive(WidgetTree &, Node< Spinner >, bool)
~Spinner() override
Cancel this widget's timer on UI, without retaining the scene.
Spinner(WidthPolicy width={}, SpinnerOptions options={}, ControlStyle style={}, SizePolicy policy=DEFAULT_CONTENT_POLICY, SpinnerLabelResolver resolver={})
std::expected< CellSize, TreeError > measure(CellSize available) override
Owning literal bytes or a resource key, measured only at resolution.
Definition text_value.hpp:42
Exclusive parent/child ownership on Scheduler's UI thread. Scheduler must outlive this object....
Definition widget.hpp:179
Public Portals API.
Definition action.hpp:8
@ FORWARD
Prefer the following item; an end boundary falls back to the preceding item.
Definition model_position.hpp:43
@ LINE
Join consecutive finite closes, leaving gaps disconnected.
Definition chart.hpp:50
@ BLOCKS
Shaded track and solid filled prefix.
Definition slider.hpp:15
std::function< std::expected< std::string, TextResourceError >(std::string_view)> SpinnerLabelResolver
Owning synchronous label lookup, invoked only while staging a configuration.
Definition spinner.hpp:14
std::expected< void, TreeError > setSpinnerActive(WidgetTree &tree, Node< Spinner > node, bool active)
constexpr std::array< std::string_view, 12 > SPINNER_PRESET_NAMES
Canonical names in SpinnerPreset ordinal order.
Definition spinner.hpp:32
@ SQUARE
Square brackets and inner spaces.
Definition frame.hpp:42
constexpr SizePolicy DEFAULT_CONTENT_POLICY
Intrinsic content on both axes; explicit policies remain authoritative.
Definition layout.hpp:44
SpinnerDirection
Traversal of the finite owning frame sequence.
Definition spinner.hpp:81
@ REVERSE
Decreasing frame indices with wraparound.
Definition spinner.hpp:83
@ FORWARD
Increasing frame indices with wraparound.
Definition spinner.hpp:82
LayoutAlign
Alignment; center assigns floor(free/2) before the child/group.
Definition layout.hpp:172
@ START
Place at the leading edge of the available space.
Definition layout.hpp:173
std::expected< void, TreeError > setSpinner(WidgetTree &tree, Node< Spinner > node, const SpinnerOptions &options, SpinnerLabelResolver resolver={})
SpinnerPreset
Original single-row indeterminate animation sequences.
Definition spinner.hpp:16
@ CIRCLE
Circle phases.
Definition spinner.hpp:22
@ ELLIPSIS
Growing dots.
Definition spinner.hpp:27
@ PULSE
Pulsing indicator.
Definition spinner.hpp:28
@ DOTS
Three positions, moving left to right.
Definition spinner.hpp:17
@ ARROW
Arrow directions.
Definition spinner.hpp:25
@ ARC
Rotating arc.
Definition spinner.hpp:21
@ BAR_BOUNCE
Five positions traversed in both directions.
Definition spinner.hpp:20
@ BRAILLE
Eight one-cell braille positions; ASCII fallback for incompatible widths.
Definition spinner.hpp:19
std::uint64_t SchedulerTime
Monotonic milliseconds; the clock must not move backwards.
Definition scheduler.hpp:18
SpinnerPreset spinnerPreset(std::string_view name) noexcept
Definition spinner.hpp:39
@ TRIANGLE
Triangle glyphs with the control width/glyph fallback.
Definition scrollbar_skin.hpp:34
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
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 single-row indicator configuration; no thread or timer is created by this value.
Definition spinner.hpp:87
std::size_t maxFrameBytes
Positive complete sequence source-byte budget.
Definition spinner.hpp:97
TextValue label
Owning label resolved before widget publication.
Definition spinner.hpp:95
bool reducedMotion
True selects static frame zero without a timer.
Definition spinner.hpp:94
std::size_t maxPaintCells
Positive allocated-area paint budget.
Definition spinner.hpp:99
LayoutAlign alignment
START/CENTER/END alignment of indicator and label.
Definition spinner.hpp:92
bool active
False pauses animation without polling.
Definition spinner.hpp:93
SpinnerPreset preset
Sequence used when frames is empty.
Definition spinner.hpp:88
SpinnerDirection direction
Finite sequence traversal order.
Definition spinner.hpp:91
std::size_t maxFrames
Positive maximum complete frame count.
Definition spinner.hpp:96
~SpinnerOptions()=default
Preserve fallible owning copies, including MSVC Debug iterator proxy allocations.
SchedulerTime interval
Positive monotonic milliseconds between frames.
Definition spinner.hpp:90
std::size_t maxFrameCells
Positive maximum cells per frame.
Definition spinner.hpp:98
std::string frames
Custom UTF-8 frames separated by LF; no empty frames.
Definition spinner.hpp:89
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42