ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Dropdown.hpp
Go to the documentation of this file.
1#pragma once
2
32
33#include "Common.hpp"
34#include "Layout.hpp"
35#include "Button.hpp"
36#include "Text.hpp"
37#include "ScrollView.hpp"
38
39namespace ZenitUI::UI
40{
44
52 class Dropdown : public TLayout<Dropdown>
53 {
54 public:
62 Dropdown(std::vector<std::string> options, int selected = 0);
63
65 int getSelected() const { return selected; }
66
68 const std::string &getSelectedText() const;
69
71 const std::vector<std::string> &getOptions() const { return options; }
72
74 void setOptions(std::vector<std::string> newOptions, int newSelected = 0, bool fireChange = false);
75
77 void setSelected(int idx, bool fireChange = true);
78
83 bool setSelectedByText(const std::string &value, bool fireChange = false);
84
86 bool getOpen() const { return isOpen; }
87
93 void setOpen(bool o);
94
96 std::function<void(int, const std::string &)> onChange;
98 std::function<void(bool)> onOpenChanged;
99
100 protected:
101 void onEnabledChanged(bool nowEnabled) override;
102 void onUpdate(float dt) override;
103 void onBuild() override;
104 void arrange(Rect space) override;
105 void translateSubtree(float dx, float dy) override;
106
107 private:
108 std::vector<std::string> options;
109 int selected{0};
110 bool isOpen{false};
111 std::shared_ptr<Button> button;
112 std::shared_ptr<ScrollView> listContainer;
113 std::weak_ptr<Layout> prevFocus_;
114
115 void toggleOpen();
116 void closeInternal(bool restoreFocus);
117 void applyOpenState();
118 void rebuildList();
119 void positionList(float scrollX, float scrollY);
120 void refreshButtonText();
121 void updateListStyles();
122 };
123
124} // namespace ZenitUI::UI
A clickable button with an optional text label.
Centralized standard library includes used throughout ZenitUI.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
A generic scrolling container.
A read-only text node backed by UIContext::textEngine.
TLayout(Args &&...args)
Definition Layout.hpp:1401
std::function< void(bool)> onOpenChanged
Fired when the list is opened or closed.
Definition Dropdown.hpp:98
void onEnabledChanged(bool nowEnabled) override
Called from setEnabled after the state change.
void setOptions(std::vector< std::string > newOptions, int newSelected=0, bool fireChange=false)
Replace the option list at runtime and rebuild the popup items.
void setOpen(bool o)
Open or close the option list.
int getSelected() const
Currently selected index.
Definition Dropdown.hpp:65
void onBuild() override
Hook: called once after create() finishes constructing the object.
const std::string & getSelectedText() const
Currently selected option string, or empty if none.
Dropdown(std::vector< std::string > options, int selected=0)
Construct with options and an initial selection.
bool setSelectedByText(const std::string &value, bool fireChange=false)
Select the first option whose label equals value.
std::function< void(int, const std::string &)> onChange
Fired when an item is selected.
Definition Dropdown.hpp:96
void arrange(Rect space) override
Assign a screen rect to this node and lay out its children.
void setSelected(int idx, bool fireChange=true)
Select the option at idx.
bool getOpen() const
true while the option list is visible.
Definition Dropdown.hpp:86
void onUpdate(float dt) override
Called every frame from update, after input dispatch.
const std::vector< std::string > & getOptions() const
Current option list.
Definition Dropdown.hpp:71
void translateSubtree(float dx, float dy) override
Translate the subtree (children and their descendants) by (dx, dy).
Axis-aligned rectangle in screen coordinates.
Definition CoreTypes.hpp:46