ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
Toggle.hpp
Go to the documentation of this file.
1#pragma once
2
11
12#include "Common.hpp"
13#include "Layout.hpp"
14
15namespace ZenitUI::UI
16{
20
28 class Toggle : public TLayout<Toggle>
29 {
30 public:
35 Toggle(bool state = false)
37 {
38 setInteractive(true);
39 setFocusable(true);
40 setKeyboardActivates(true);
41 setStyleTag("Toggle");
42
43 auto &defs = getInlineDefaults();
44 defs.width = Px(44.0f);
45 defs.height = Px(24.0f);
46 defs.radius = Px(12.0f);
47 defs.background = state ? Color{37, 99, 235, 255} : Color{55, 62, 78, 255};
48 defs.cursor = CursorShape::Pointer;
49
50 setChecked(state);
51
52 onClick = [this]()
53 {
54 toggle(true);
55 };
56 }
57
59 void toggle(bool notify = true)
60 {
61 const bool next = !getChecked();
62 setChecked(next);
63 if (notify && onToggle)
64 onToggle(next);
65 }
66
68 std::function<void(bool)> onToggle = nullptr;
71
72 protected:
73 void onCheckedChanged(bool nowChecked) override
74 {
75 isChecked = nowChecked;
76 getInlineDefaults().background = nowChecked ? Color{37, 99, 235, 255} : Color{55, 62, 78, 255};
77 if (knob)
78 knob->getInlineDefaults().left = nowChecked ? Px(23.0f) : Px(3.0f);
79 }
80
81 void onBuild() override
82 {
83 knob = std::make_shared<Layout>(LayoutType::Stack);
84 knob->cls("toggle-knob");
85 knob->setInteractive(false);
86 knob->setBlocksRaycast(false);
87 auto &kdefs = knob->getInlineDefaults();
88 kdefs.position = Position::Absolute;
89 kdefs.width = Px(18.0f);
90 kdefs.height = Px(18.0f);
91 kdefs.radius = Px(9.0f);
92 kdefs.top = Px(3.0f);
93 kdefs.left = getChecked() ? Px(23.0f) : Px(3.0f);
94 kdefs.background = Colors::White;
95 addChild(knob);
96 }
97
98 private:
99 std::shared_ptr<Layout> knob;
100 };
101
102} // namespace ZenitUI::UI
Centralized standard library includes used throughout ZenitUI.
The node class: tree structure, layout pass, style resolution, input dispatch, render orchestration,...
void setStyleTag(const std::string &t)
Set the node's style tag.
Style & getInlineDefaults()
Direct mutable access to the inline defaults (lowest-priority inline).
Definition Layout.hpp:735
void setChecked(bool c)
Set the :checked pseudo-class state.
Definition Layout.hpp:447
void addChild(std::shared_ptr< Layout > child)
Append child to this node's child list.
Definition Layout.hpp:302
TLayout(Args &&...args)
Definition Layout.hpp:1401
bool isChecked
Legacy mirror of getChecked().
Definition Toggle.hpp:70
void onBuild() override
Hook: called once after create() finishes constructing the object.
Definition Toggle.hpp:81
std::function< void(bool)> onToggle
Fired after the checked state changes via user interaction or toggle(true).
Definition Toggle.hpp:68
void toggle(bool notify=true)
Flip the checked state and optionally fire onToggle.
Definition Toggle.hpp:59
void onCheckedChanged(bool nowChecked) override
Called from setChecked after the checked state changes.
Definition Toggle.hpp:73
Toggle(bool state=false)
Construct with an initial state.
Definition Toggle.hpp:35
LayoutType
Positioning strategy applied to a Layout node's direct children.
Definition UIEnums.hpp:29
@ Stack
Children overlap each other; Z-order controls paint order.
Definition UIEnums.hpp:30
@ Pointer
Pointing hand (buttons, links, interactive controls).
Definition UIEnums.hpp:64
8-bit-per-channel RGBA color, non-premultiplied.
Definition CoreTypes.hpp:78