Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
menu.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/ui_action.hpp>
6
7namespace portals {
9inline constexpr SizePolicy DEFAULT_MENU_POLICY{.width = {.mode = SizeMode::CONTENT},
10 .height = {.mode = SizeMode::CONTENT}};
11
14 std::size_t maxEntries{256};
15};
16
21class Menu : public Control {
22public:
30 Menu(GraphemeText empty, const std::vector<ui::ActionRef> &actions, MenuOptions options = {}, ControlStyle style = {},
31 SizePolicy policy = DEFAULT_MENU_POLICY);
32 ~Menu() override;
33
35 std::expected<CellSize, TreeError> measure(CellSize available) override;
37 std::expected<void, TreeError> arrange(CellRect local) override;
39 std::expected<void, TreeError> paint(PaintContext &context) override;
41 virtual std::optional<std::size_t> selectedIndex() const noexcept;
43 virtual std::size_t visibleRowCount() const noexcept;
44
45protected:
47 // Built-in PopupMenu override glue, not a public extension interface.
48 friend struct ui::detail::ActionAccess;
49 friend class ManagedScene;
50 struct Prepared;
51
52 struct Input {
53 bool handled{};
54 std::optional<ui::ActionRef> action;
55 ui::ActionInvocation invocation;
56 Node<Dialog> dismiss;
57 };
58
59 virtual std::expected<void, TreeError> prepare(WidgetTree &, Node<Menu>, WidthPolicy);
60 virtual void invalidatePresentation() noexcept;
61 virtual std::expected<Input, TreeError> input(WidgetTree &, Node<Menu>, const RoutedEvent &, const NodeGeometry &);
62
64
65private:
66 bool eligible(std::size_t) const;
67 std::optional<std::size_t> next(std::optional<std::size_t>, bool forward) const;
68
69 std::vector<ui::ActionRef> actions_;
70 MenuOptions options_;
71 std::unique_ptr<Prepared> prepared_;
72 std::optional<std::size_t> selected_;
73 CellRect bounds_;
74};
75} // namespace portals
An owning, invariant-preserving half-open cell rectangle.
Definition geometry.hpp:81
Control(GraphemeText text, std::string_view prefix, std::string_view suffix, ControlStyle style, SizePolicy policy)
Own text/options and create fixed ASCII decorations under the text's validated width policy.
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
Initially visible action-backed command list, without popup or submenu behavior. Actions own callback...
Definition menu.hpp:21
Menu(GraphemeText empty, const std::vector< ui::ActionRef > &actions, MenuOptions options={}, ControlStyle style={}, SizePolicy policy=DEFAULT_MENU_POLICY)
std::expected< void, TreeError > paint(PaintContext &context) override
std::expected< void, TreeError > arrange(CellRect local) override
virtual std::optional< std::size_t > selectedIndex() const noexcept
std::expected< CellSize, TreeError > measure(CellSize available) override
virtual std::size_t visibleRowCount() const noexcept
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
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
@ CONTENT
Prefer intrinsic size, shrink towards minimum, and never grow to consume spare cells.
Definition layout.hpp:16
constexpr SizePolicy DEFAULT_MENU_POLICY
Natural menu extent on both axes; callers may supply an ordinary explicit SizePolicy.
Definition menu.hpp:9
std::variant< ActionEvent, KeyEvent, TextInputEvent, PasteInputEvent, MouseInputEvent > RoutedEvent
Synchronous input value; callback borrows it only until return.
Definition action.hpp:47
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 paint values; no theme engine or behavior is encoded here.
Definition controls.hpp:24
Finite admission limit for an ordinary retained command list.
Definition menu.hpp:13
std::size_t maxEntries
Positive entry bound, at most INT32_MAX; duplicates count separately.
Definition menu.hpp:14
Owning arranged geometry; invalid geometry must never be used for hit-testing.
Definition widget_types.hpp:48
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
Owning synchronous invocation context; no widget or scene ownership.
Definition ui_action.hpp:30