ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Popup.hpp
Go to the documentation of this file.
1#pragma once
2
15
16#include "Common.hpp"
17#include "Layout.hpp"
18#include "Layouts.hpp"
19
20namespace ZenitUI::UI
21{
25
33 class Popup : public TLayout<Popup, Panel>
34 {
35 public:
36 Popup();
37
41 void setContent(std::shared_ptr<Layout> content);
42
44 void openBelow(std::shared_ptr<Layout> anchor);
45
47 void openAbove(std::shared_ptr<Layout> anchor);
48
50 void openAt(Vec2 screenPos);
51
53 void close();
54
56 bool isOpen() const { return isOpen_; }
57
59 bool closeOnEscape{true};
60 float offsetBelow{4.0f};
61 std::function<void()> onOpen;
62 std::function<void()> onClose;
63
65 void arrange(Rect) override {}
66
67 protected:
68 void onBuild() override;
69 void onUpdate(float dt) override;
70
71 private:
72 std::shared_ptr<Layout> content_;
73 std::weak_ptr<Layout> anchor_;
74 std::weak_ptr<Layout> prevFocus_;
75 Vec2 pos_{0.0f, 0.0f};
76 bool isOpen_{false};
77 bool above_{false};
78
79 void openInternal();
80 void closeInternal(bool restoreFocus);
81 Vec2 computePopupSize();
82 };
83
92 std::shared_ptr<Popup> ContextMenu(
93 std::vector<std::pair<std::string, std::function<void()>>> items);
95} // namespace ZenitUI::UI
Centralized standard library includes used throughout ZenitUI.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
Basic layout containers: VStack, HStack, Panel.
TLayout(Args &&...args)
Definition Layout.hpp:1401
void setContent(std::shared_ptr< Layout > content)
Set the popup's content.
void openAt(Vec2 screenPos)
Open the popup at an absolute screen position.
std::function< void()> onOpen
Fired after open*.
Definition Popup.hpp:61
bool isOpen() const
true while the popup is visible.
Definition Popup.hpp:56
void arrange(Rect) override
No-op: the popup owns its rect via onUpdate.
Definition Popup.hpp:65
void openAbove(std::shared_ptr< Layout > anchor)
Open the popup just above anchor (auto-flips below if clipped).
float offsetBelow
Gap between the anchor and the popup.
Definition Popup.hpp:60
void openBelow(std::shared_ptr< Layout > anchor)
Open the popup just below anchor.
std::function< void()> onClose
Fired after close.
Definition Popup.hpp:62
void onBuild() override
Hook: called once after create() finishes constructing the object.
void close()
Close the popup if open, fading out and firing onClose.
bool closeOnEscape
Close on Escape.
Definition Popup.hpp:59
bool closeOnClickOutside
Close on any button press outside the popup.
Definition Popup.hpp:58
std::shared_ptr< Menu > ContextMenu(std::vector< MenuEntry > entries)
Convenience factory: create a structured context Menu from MenuEntry items.
Axis-aligned rectangle in screen coordinates.
Definition CoreTypes.hpp:46
2D vector with component-wise arithmetic.
Definition CoreTypes.hpp:25