From 6e1cccdb4088c7db1c0873f233eb5b3e17d14582 Mon Sep 17 00:00:00 2001 From: coding-agent Date: Tue, 10 Nov 2026 18:18:42 +0000 Subject: [PATCH] Basic widgets --- src/Engine/UI/CMakeLists.txt | 2 + src/Engine/UI/Element/Accordion.h | 90 +++++++++++++++ src/Engine/UI/Element/CMakeLists.txt | 22 ++++ src/Engine/UI/Element/Checkbox.h | 68 +++++++++++ src/Engine/UI/Element/HR.h | 35 ++++++ src/Engine/UI/Element/InputField.h | 98 ++++++++++++++++ src/Engine/UI/Element/Progressbar.h | 56 +++++++++ src/Engine/UI/Element/Radio.h | 66 +++++++++++ src/Engine/UI/Element/SelectBox.h | 97 ++++++++++++++++ src/Engine/UI/Element/Slider.h | 68 +++++++++++ src/Engine/UI/Element/TabBar.h | 72 ++++++++++++ src/Engine/UI/Element/ToggleSwitch.h | 108 ++++++++++++++++++ src/Engine/UI/Element/Tooltip.h | 100 +++++++++++++++++ src/Engine/UI/Widgets.h | 33 ++++++ src/Engine/UI/widget_smoke_test.cpp | 162 +++++++++++++++++++++++++++ 15 files changed, 1077 insertions(+) create mode 100644 src/Engine/UI/Element/Accordion.h create mode 100644 src/Engine/UI/Element/Checkbox.h create mode 100644 src/Engine/UI/Element/HR.h create mode 100644 src/Engine/UI/Element/InputField.h create mode 100644 src/Engine/UI/Element/Progressbar.h create mode 100644 src/Engine/UI/Element/Radio.h create mode 100644 src/Engine/UI/Element/SelectBox.h create mode 100644 src/Engine/UI/Element/Slider.h create mode 100644 src/Engine/UI/Element/TabBar.h create mode 100644 src/Engine/UI/Element/ToggleSwitch.h create mode 100644 src/Engine/UI/Element/Tooltip.h create mode 100644 src/Engine/UI/Widgets.h create mode 100644 src/Engine/UI/widget_smoke_test.cpp diff --git a/src/Engine/UI/CMakeLists.txt b/src/Engine/UI/CMakeLists.txt index a0ebf3d..6088f76 100644 --- a/src/Engine/UI/CMakeLists.txt +++ b/src/Engine/UI/CMakeLists.txt @@ -7,6 +7,7 @@ target_sources(Engine FlexLayout.cpp System.h System.cpp + Widgets.h ) target_sources(Engine @@ -16,6 +17,7 @@ target_sources(Engine Element.h FlexLayout.h System.h + Widgets.h ) add_subdirectory(Style/) diff --git a/src/Engine/UI/Element/Accordion.h b/src/Engine/UI/Element/Accordion.h new file mode 100644 index 0000000..015a958 --- /dev/null +++ b/src/Engine/UI/Element/Accordion.h @@ -0,0 +1,90 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Element/Text.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// An accordion: a collapsible section with a header that toggles the body. +// HTML analog:
+// +// The Accordion is a vertical container with a header (a clickable label) +// and a body (arbitrary children). The body's visibility is toggled by the +// owning view in response to the header's onClick. The Accordion itself does +// not track open/closed state — the view does. +// +// Usage: +// auto* header = new Text("Section Title"); +// auto* body = new Div(UI::Attributes{}); +// auto* acc = new Accordion<>(UI::Attributes{}, OElement(header), OElement(body)); +// // The view owns the "open" state and toggles body's visibility: +// // if (clicked) { bodyVisible = !bodyVisible; acc->setBodyVisible(bodyVisible); } +template +class Accordion : public Div { + public: + using Div::attr; + using Div::style; + using Div::markDirty; + using Div::getTheme; + using Div::children; + + Accordion(Attributes attr, OElement header, OElement body) + : Div(std::move(attr), std::initializer_list{}) { + _header = PElement(header); + _body = PElement(body); + + children.add(std::move(header)); + children.add(std::move(body)); + if (children.size() >= 2) { + children.back()->setParent(this); + } + + style.backgroundColor = Vector(0.10f, 0.10f, 0.12f); + style.gap = 0; + + if (_header != nullptr) { + auto* h = _header.getHandle(); + if (h != nullptr) { + auto& hs = h->getStyle(); + hs.backgroundColor = Vector(0.14f, 0.14f, 0.16f); + hs.paddingLeft = 12; + hs.paddingRight = 12; + hs.paddingTop = 8; + hs.paddingBottom = 8; + } + } + } + virtual ~Accordion() override {} + + void setBodyVisible(bool visible) { + if (_body != nullptr) { + auto* b = _body.getHandle(); + if (b != nullptr) { + b->getStyle().display = visible ? Display::Flex : Display::Hidden; + b->markDirty(); + } + } + } + bool isBodyVisible() const { + if (_body == nullptr) { + return false; + } + auto* b = _body.getHandle(); + return b != nullptr && b->getStyle().display == Display::Flex; + } + + PElement getHeader() const { return _header; } + PElement getBody() const { return _body; } + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + style.backgroundColor = Vector(0.10f, 0.10f, 0.12f); + style.gap = 0; + } + + private: + PElement _header; + PElement _body; +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Element/CMakeLists.txt b/src/Engine/UI/Element/CMakeLists.txt index b3d3d16..fa2af78 100644 --- a/src/Engine/UI/Element/CMakeLists.txt +++ b/src/Engine/UI/Element/CMakeLists.txt @@ -4,6 +4,17 @@ target_sources(Engine Text.h Button.h Panel.h + Checkbox.h + ToggleSwitch.h + Radio.h + SelectBox.h + InputField.h + Slider.h + Progressbar.h + TabBar.h + Tooltip.h + Accordion.h + HR.h ) target_sources(Engine @@ -13,4 +24,15 @@ target_sources(Engine Text.h Button.h Panel.h + Checkbox.h + ToggleSwitch.h + Radio.h + SelectBox.h + InputField.h + Slider.h + Progressbar.h + TabBar.h + Tooltip.h + Accordion.h + HR.h ) diff --git a/src/Engine/UI/Element/Checkbox.h b/src/Engine/UI/Element/Checkbox.h new file mode 100644 index 0000000..554c7e0 --- /dev/null +++ b/src/Engine/UI/Element/Checkbox.h @@ -0,0 +1,68 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// A checkbox: a square indicator + optional label. +// HTML analog: +// +// Usage: +// auto* cb = new Checkbox<>(); +// // per-frame: cb->setChecked(viewState.flag) +// +// Pure display widget. The view drives checked state via setChecked(). +// The checkbox does NOT handle its own onClick — the view wires the +// toggle logic via attr.onClick on the owning container. +template +class Checkbox : public Div { + public: + using Div::attr; + using Div::style; + using Div::markDirty; + using Div::getTheme; + using Div::children; + + Checkbox(Attributes attr = Attributes{}, std::initializer_list children = {}) + : Div(std::move(attr), children) { + style.height = 20; + style.width = 20; + } + virtual ~Checkbox() override {} + + void setChecked(bool checked) { + _checked = checked; + markDirty(); + } + bool isChecked() const { return _checked; } + + void setHovered(bool hovered) override { _hovered = hovered; } + void setFocused(bool focused) override { _focused = focused; } + bool isFocusable() const override { return attr.focusable; } + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + } + + // The System calls applyOverrides() during style() after applyStyle. + // We use it to reflect checked/hover/focus in the background color. + void applyOverrides() override { + const Theme& th = getTheme(); + if (_checked) { + style.backgroundColor = th.color(ColorToken::Green); + } else if (_hovered) { + style.backgroundColor = Vector(0.22f, 0.24f, 0.28f); + } else if (_focused) { + style.backgroundColor = Vector(0.20f, 0.22f, 0.26f); + } else { + style.backgroundColor = Vector(0.14f, 0.14f, 0.16f); + } + } + + private: + bool _checked = false; + bool _hovered = false; + bool _focused = false; +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Element/HR.h b/src/Engine/UI/Element/HR.h new file mode 100644 index 0000000..bdd4240 --- /dev/null +++ b/src/Engine/UI/Element/HR.h @@ -0,0 +1,35 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// A horizontal rule: a thin decorative divider line. +// HTML analog:
+// +// A simple 1px tall element with a subtle background color. No interactivity. +template +class HR : public Div { + public: + using Div::attr; + using Div::style; + + HR(Attributes attr = Attributes{}, std::initializer_list children = {}) + : Div(std::move(attr), children) { + style.height = 1; + style.backgroundColor = Vector(0.25f, 0.25f, 0.28f); + style.marginTop = 8; + style.marginBottom = 8; + } + virtual ~HR() override {} + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + style.height = 1; + style.backgroundColor = Vector(0.25f, 0.25f, 0.28f); + style.marginTop = 8; + style.marginBottom = 8; + } +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Element/InputField.h b/src/Engine/UI/Element/InputField.h new file mode 100644 index 0000000..6bc09e6 --- /dev/null +++ b/src/Engine/UI/Element/InputField.h @@ -0,0 +1,98 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Element/Text.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// A text input field: a box that displays a placeholder (when empty) or a +// text value (when non-empty). The actual text editing (key handling, cursor, +// IME) is the owning view's responsibility — the view intercepts key events +// and calls setValue() with the updated text. +// +// HTML analog: +// +// Usage: +// auto* field = new InputField(UI::Attributes{}, "Enter name..."); +// // per-frame: field->setValue(viewState.inputText); +template +class InputField : public Div { + public: + using Div::attr; + using Div::style; + using Div::markDirty; + using Div::getTheme; + using Div::children; + + InputField(Attributes attr, std::string placeholder = "") + : Div(std::move(attr), std::initializer_list{}) { + attr.focusable = true; + _placeholder = std::move(placeholder); + + auto* t = new Text(_placeholder); + children.add(OElement(t)); + children.back()->setParent(this); + _text = PElement(t); + + style.paddingLeft = 12; + style.paddingRight = 12; + style.paddingTop = 8; + style.paddingBottom = 8; + style.backgroundColor = Vector(0.10f, 0.10f, 0.12f); + } + virtual ~InputField() override {} + + void setValue(std::string value) { + _value = std::move(value); + markDirty(); + } + std::string getValue() const { return _value; } + std::string getPlaceholder() const { return _placeholder; } + + void setFocused(bool focused) override { + _focused = focused; + } + + void setHovered(bool hovered) override { _hovered = hovered; } + bool isFocusable() const override { return attr.focusable; } + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + } + + void applyOverrides() override { + const Theme& th = getTheme(); + if (_focused) { + style.backgroundColor = Vector(0.14f, 0.15f, 0.18f); + } else if (_hovered) { + style.backgroundColor = Vector(0.12f, 0.12f, 0.14f); + } else { + style.backgroundColor = Vector(0.10f, 0.10f, 0.12f); + } + // Switch between placeholder (dimmed) and value (full color) display. + if (_text == nullptr) { + return; + } + auto* t = dynamic_cast*>(_text.getHandle()); + if (t == nullptr) { + return; + } + if (_value.empty()) { + t->setText(_placeholder); + style.textColor = th.color(ColorToken::Gray600); + } else { + t->setText(_value); + style.textColor = th.color(ColorToken::White); + } + markDirty(); + } + + private: + std::string _placeholder; + std::string _value; + bool _focused = false; + bool _hovered = false; + PElement _text; +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Element/Progressbar.h b/src/Engine/UI/Element/Progressbar.h new file mode 100644 index 0000000..9fe457e --- /dev/null +++ b/src/Engine/UI/Element/Progressbar.h @@ -0,0 +1,56 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// A progress bar: a horizontal bar that fills proportionally to a 0.0-1.0 value. +// HTML analog: +// +// Usage: +// auto* bar = new Progressbar<>(); +// // per-frame: bar->setValue(fraction) // 0.0 - 1.0 +// +// Pure display widget. The bar's background is a lerp between a dimmed +// "empty" color and an accent "full" color, giving a visual fill effect. +template +class Progressbar : public Div { + public: + using Div::attr; + using Div::style; + using Div::markDirty; + using Div::getTheme; + + Progressbar(Attributes attr = Attributes{}, std::initializer_list children = {}) + : Div(std::move(attr), children) { + style.height = 8; + } + virtual ~Progressbar() override {} + + void setValue(float value) { + _value = value < 0.0f ? 0.0f : (value > 1.0f ? 1.0f : value); + markDirty(); + } + float getValue() const { return _value; } + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + } + + void applyOverrides() override { + const Theme& th = getTheme(); + // Lerp between empty (dimmed) and full (green) based on value. + Vector empty(0.14f, 0.14f, 0.16f); + Vector full = th.color(ColorToken::Green); + float v = _value; + style.backgroundColor = Vector( + empty.x + (full.x - empty.x) * v, + empty.y + (full.y - empty.y) * v, + empty.z + (full.z - empty.z) * v); + } + + private: + float _value = 0.0f; +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Element/Radio.h b/src/Engine/UI/Element/Radio.h new file mode 100644 index 0000000..2f54263 --- /dev/null +++ b/src/Engine/UI/Element/Radio.h @@ -0,0 +1,66 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// A radio button: a circular indicator + optional label. +// HTML analog: +// +// Usage: +// auto* r1 = new Radio<>(); +// auto* r2 = new Radio<>(); +// // per-frame: r1->setSelected(viewState.choice == 0) +// // r2->setSelected(viewState.choice == 1) +// +// Pure display widget. The view owns the selection state and drives +// setSelected() on each radio in the group. +template +class Radio : public Div { + public: + using Div::attr; + using Div::style; + using Div::markDirty; + using Div::getTheme; + + Radio(Attributes attr = Attributes{}, std::initializer_list children = {}) + : Div(std::move(attr), children) { + style.height = 20; + style.width = 20; + } + virtual ~Radio() override {} + + void setSelected(bool selected) { + _selected = selected; + markDirty(); + } + bool isSelected() const { return _selected; } + + void setHovered(bool hovered) override { _hovered = hovered; } + void setFocused(bool focused) override { _focused = focused; } + bool isFocusable() const override { return attr.focusable; } + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + } + + void applyOverrides() override { + const Theme& th = getTheme(); + if (_selected) { + style.backgroundColor = th.color(ColorToken::Blue); + } else if (_hovered) { + style.backgroundColor = Vector(0.22f, 0.24f, 0.28f); + } else if (_focused) { + style.backgroundColor = Vector(0.20f, 0.22f, 0.26f); + } else { + style.backgroundColor = Vector(0.14f, 0.14f, 0.16f); + } + } + + private: + bool _selected = false; + bool _hovered = false; + bool _focused = false; +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Element/SelectBox.h b/src/Engine/UI/Element/SelectBox.h new file mode 100644 index 0000000..e259cc2 --- /dev/null +++ b/src/Engine/UI/Element/SelectBox.h @@ -0,0 +1,97 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Element/Text.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// A select box (dropdown): a clickable box that displays the current selection. +// HTML analog: +// +// Usage: +// auto* slider = new Slider<>(); +// // per-frame: slider->setValue(viewState.volume) // 0.0 - 1.0 +// +// Pure display widget. The slider's background is a lerp between a dimmed +// "track" color and an accent "fill" color based on the value, giving a +// visual fill effect. Mouse drag handling is the owning view's job. +template +class Slider : public Div { + public: + using Div::attr; + using Div::style; + using Div::markDirty; + using Div::getTheme; + + Slider(Attributes attr = Attributes{}, std::initializer_list children = {}) + : Div(std::move(attr), children) { + attr.focusable = true; + style.height = 8; + } + virtual ~Slider() override {} + + void setValue(float value) { + _value = value < 0.0f ? 0.0f : (value > 1.0f ? 1.0f : value); + markDirty(); + } + float getValue() const { return _value; } + + void setHovered(bool hovered) override { _hovered = hovered; } + void setFocused(bool focused) override { _focused = focused; } + bool isFocusable() const override { return attr.focusable; } + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + } + + void applyOverrides() override { + const Theme& th = getTheme(); + if (_focused || _hovered) { + style.backgroundColor = th.color(ColorToken::Blue); + } else { + // Lerp between track (dimmed) and accent (blue) for fill effect. + Vector track(0.18f, 0.18f, 0.20f); + Vector accent = th.color(ColorToken::Blue); + float v = _value; + style.backgroundColor = Vector( + track.x + (accent.x - track.x) * v, + track.y + (accent.y - track.y) * v, + track.z + (accent.z - track.z) * v); + } + } + + private: + float _value = 0.0f; + bool _hovered = false; + bool _focused = false; +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Element/TabBar.h b/src/Engine/UI/Element/TabBar.h new file mode 100644 index 0000000..835ef6b --- /dev/null +++ b/src/Engine/UI/Element/TabBar.h @@ -0,0 +1,72 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Element/Button.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// A tab bar: a horizontal row of tab buttons. One tab is highlighted as active. +// HTML analog:
    +// +// Usage: +// auto* tab1 = new Button<>(UI::Attributes{}); +// auto* tab2 = new Button<>(UI::Attributes{}); +// auto* tabs = new TabBar<>(UI::Attributes{}, {OElement(tab1), OElement(tab2)}); +// // per-frame: tabs->setActiveTab(0) +// +// The tab bar takes a fixed set of Button children at construction (the +// fixed-children constraint). The active tab gets an accent color; the +// others get a dimmed color. The view handles onClick on each button to +// change the active tab. +template +class TabBar : public Div { + public: + using Div::attr; + using Div::style; + using Div::markDirty; + using Div::getTheme; + using Div::children; + + TabBar(Attributes attr, std::initializer_list tabs) + : Div(std::move(attr), tabs) { + style.direction = FlexDirection::Row; + style.gap = 4; + } + virtual ~TabBar() override {} + + void setActiveTab(int index) { + _activeTab = index; + markDirty(); + } + int getActiveTab() const { return _activeTab; } + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + style.direction = FlexDirection::Row; + style.gap = 4; + } + + void applyOverrides() override { + const Theme& th = getTheme(); + int i = 0; + for (auto& child : children) { + if (child == nullptr) { + continue; + } + auto& cs = child->getStyle(); + if (i == _activeTab) { + cs.backgroundColor = th.color(ColorToken::Blue); + cs.textColor = th.color(ColorToken::White); + } else { + cs.backgroundColor = Vector(0.14f, 0.14f, 0.16f); + cs.textColor = th.color(ColorToken::Gray500); + } + i++; + } + } + + private: + int _activeTab = 0; +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Element/ToggleSwitch.h b/src/Engine/UI/Element/ToggleSwitch.h new file mode 100644 index 0000000..03d8637 --- /dev/null +++ b/src/Engine/UI/Element/ToggleSwitch.h @@ -0,0 +1,108 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Element/Text.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// A toggle switch: a pill-shaped on/off switch with an optional label. +// HTML analog: styled as a switch (iOS-style) +// +// Usage: +// auto* toggle = new ToggleSwitch(UI::Attributes{}, "Wi-Fi"); +// // per-frame: toggle->setOn(viewState.wifi) +// +// The toggle is a Div containing a label Text and an indicator pill. +// The pill's background shifts between off (dimmed) and on (green) based +// on the `on` state. The knob is a child div positioned to the left (off) +// or right (on). +template +class ToggleSwitch : public Div { + public: + using Div::attr; + using Div::style; + using Div::markDirty; + using Div::getTheme; + using Div::children; + + ToggleSwitch(Attributes attr = Attributes{}, std::string label = "") + : Div(std::move(attr), std::initializer_list{}) { + // Build the label (if any) + if (!label.empty()) { + auto* lbl = new Text(label); + children.add(OElement(lbl)); + children.back()->setParent(this); + } + + // Build the toggle pill: a rounded indicator (60x28) + auto* pill = new Div<>(UI::Attributes{}); + { + auto& ps = pill->getStyle(); + ps.width = 60; + ps.height = 28; + ps.backgroundColor = Vector(0.20f, 0.20f, 0.22f); + } + _pill = PElement(pill); + children.add(OElement(pill)); + children.back()->setParent(this); + + // Build the knob: a circle inside the pill (20x20) + auto* knob = new Div<>(UI::Attributes{}); + { + auto& ks = knob->getStyle(); + ks.width = 20; + ks.height = 20; + ks.backgroundColor = Vector(0.70f, 0.70f, 0.72f); + ks.left = 4; + ks.position = Position::Absolute; + } + _knob = PElement(knob); + children.add(OElement(knob)); + children.back()->setParent(this); + + style.height = 28; + } + virtual ~ToggleSwitch() override {} + + void setOn(bool on) { + _on = on; + markDirty(); + } + bool isOn() const { return _on; } + + void setHovered(bool hovered) override { _hovered = hovered; } + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + } + + void applyOverrides() override { + const Theme& th = getTheme(); + if (_pill != nullptr) { + auto* p = _pill.getHandle(); + if (p != nullptr) { + if (_on) { + p->getStyle().backgroundColor = th.color(ColorToken::Green); + } else { + p->getStyle().backgroundColor = Vector(0.20f, 0.20f, 0.22f); + } + p->markDirty(); + } + } + if (_knob != nullptr) { + auto* k = _knob.getHandle(); + if (k != nullptr) { + k->getStyle().left = _on ? 36 : 4; + k->markDirty(); + } + } + } + + private: + bool _on = false; + bool _hovered = false; + PElement _pill; + PElement _knob; +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Element/Tooltip.h b/src/Engine/UI/Element/Tooltip.h new file mode 100644 index 0000000..a499efe --- /dev/null +++ b/src/Engine/UI/Element/Tooltip.h @@ -0,0 +1,100 @@ +#pragma once +#include "UI/Element/Div.h" +#include "UI/Element/Text.h" +#include "UI/Style/Class.h" + +namespace Seele { +namespace UI { +// A tooltip: an element that shows a tooltip bubble when hovered. +// HTML analog: title="..." attribute / CSS :hover tooltip +// +// The Tooltip wraps a child element and, when the child is hovered, shows +// a floating label with the tooltip text. The tooltip is rendered as a +// child element (a Text with a dark background) that is hidden until hover. +// +// Usage: +// auto* target = new Button<>(UI::Attributes{}); +// auto* tip = new Tooltip<>(UI::Attributes{}, OElement(target), "Helpful info"); +// +// NOTE: the tooltip child is added to the tree at construction. Its position +// is set by the view (absolute positioning) — the Tooltip itself does not +// compute the tooltip's position. +template +class Tooltip : public Div { + public: + using Div::attr; + using Div::style; + using Div::markDirty; + using Div::getTheme; + using Div::children; + + Tooltip(Attributes attr, OElement target, std::string text) + : Div(std::move(attr), std::initializer_list{}) { + // Wrap the target + children.add(std::move(target)); + if (children.size() > 0) { + children.back()->setParent(this); + } + _tipText = std::move(text); + + // Build the tooltip bubble: a dark panel with the text + auto* bubble = new Text(_tipText); + { + auto& bs = bubble->getStyle(); + bs.backgroundColor = Vector(0.05f, 0.05f, 0.06f); + bs.paddingLeft = 8; + bs.paddingRight = 8; + bs.paddingTop = 4; + bs.paddingBottom = 4; + bs.display = Display::Hidden; + } + _bubble = PElement(bubble); + children.add(OElement(bubble)); + children.back()->setParent(this); + } + virtual ~Tooltip() override {} + + void setText(std::string text) { + _tipText = std::move(text); + if (_bubble != nullptr) { + if (auto* t = dynamic_cast*>(_bubble.getHandle())) { + t->setText(_tipText); + } + } + markDirty(); + } + + void setHovered(bool hovered) override { + _hovered = hovered; + if (_bubble != nullptr) { + if (auto* b = dynamic_cast(_bubble.getHandle())) { + if (b != nullptr) { + b->getStyle().display = hovered ? Display::Flex : Display::Hidden; + b->markDirty(); + } + } + } + } + + void applyStyle(Style parentStyle, const Theme& theme) override { + Div::applyStyle(parentStyle, theme); + } + + void applyOverrides() override { + const Theme& th = getTheme(); + if (_bubble != nullptr) { + if (auto* b = dynamic_cast*>(_bubble.getHandle())) { + if (b != nullptr) { + b->getStyle().textColor = th.color(ColorToken::White); + } + } + } + } + + private: + std::string _tipText; + PElement _bubble; + bool _hovered = false; +}; +} // namespace UI +} // namespace Seele diff --git a/src/Engine/UI/Widgets.h b/src/Engine/UI/Widgets.h new file mode 100644 index 0000000..f47678c --- /dev/null +++ b/src/Engine/UI/Widgets.h @@ -0,0 +1,33 @@ +#pragma once +// Seele UI: reusable component library. +// +// HTML-analog widgets built on the existing Element/Div/Text/Button base. +// All components are header-only template classes (matching the existing +// Button/Text/Div/Panel pattern). No .cpp files are needed. +// +// Design constraint: "children fixed at construction" — the UI tree's +// structure is immutable at runtime. Dynamic content (dropdown options, +// tab pages, scrollable items) is pre-allocated as a fixed pool at +// construction and driven per-frame by the owning view via setter methods +// (setText, setValue, setChecked, setVisible, etc.). +// +// Interaction model: the only sanctioned input path is Element::attr.onClick +// (delivered by System::processInput on click-release). Components that +// need to react to clicks expose the Button/Div onClick handler; the owning +// view reads state in its per-frame update() and calls the component's +// setters to reflect the new state. + +// Form controls +#include "UI/Element/Checkbox.h" +#include "UI/Element/ToggleSwitch.h" +#include "UI/Element/Radio.h" +#include "UI/Element/SelectBox.h" +#include "UI/Element/InputField.h" +#include "UI/Element/Slider.h" + +// Display / navigation +#include "UI/Element/Progressbar.h" +#include "UI/Element/TabBar.h" +#include "UI/Element/Tooltip.h" +#include "UI/Element/Accordion.h" +#include "UI/Element/HR.h" diff --git a/src/Engine/UI/widget_smoke_test.cpp b/src/Engine/UI/widget_smoke_test.cpp new file mode 100644 index 0000000..6acb097 --- /dev/null +++ b/src/Engine/UI/widget_smoke_test.cpp @@ -0,0 +1,162 @@ +// Syntax-check TU for the new UI widget components. +// Instantiates each component with template args to force full template +// instantiation, then references key members to ensure they exist. +#include "UI/Widgets.h" +#include "UI/Element/Button.h" +#include "UI/Element/Div.h" +#include "UI/Element/Text.h" +#include "UI/Style/Fonts.h" + +using namespace Seele; +using namespace Seele::UI; + +// A translation-unit-level function that exercises every new widget type. +// The compiler must fully instantiate each template here. +void widget_smoke_test() { + // --- Checkbox --- + { + auto* cb = new Checkbox(); + cb->setChecked(true); + (void)cb->isChecked(); + cb->setHovered(true); + cb->setFocused(true); + cb->applyOverrides(); + (void)cb->style; + (void)cb->attr; + (void)cb->children; + cb->markDirty(); + delete cb; + } + + // --- ToggleSwitch --- + { + auto* ts = new ToggleSwitch(UI::Attributes{}, "Wi-Fi"); + ts->setOn(true); + (void)ts->isOn(); + ts->setHovered(true); + ts->applyOverrides(); + (void)ts->style; + (void)ts->attr; + (void)ts->children; + ts->markDirty(); + delete ts; + } + + // --- Radio --- + { + auto* r1 = new Radio(); + r1->setSelected(true); + (void)r1->isSelected(); + r1->applyOverrides(); + (void)r1->style; + (void)r1->attr; + r1->markDirty(); + delete r1; + } + + // --- SelectBox --- + { + auto* sel = new SelectBox(UI::Attributes{}, "Option A"); + sel->setSelection("Option B"); + (void)sel->getSelection(); + sel->setOpen(true); + (void)sel->isOpen(); + sel->applyOverrides(); + (void)sel->style; + (void)sel->attr; + (void)sel->children; + sel->markDirty(); + delete sel; + } + + // --- InputField --- + { + auto* input = new InputField(UI::Attributes{}, "Enter name..."); + input->setValue("hello"); + (void)input->getValue(); + (void)input->getPlaceholder(); + input->setFocused(true); + input->applyOverrides(); + (void)input->style; + (void)input->attr; + (void)input->children; + input->markDirty(); + delete input; + } + + // --- Slider --- + { + auto* slider = new Slider(); + slider->setValue(0.75f); + (void)slider->getValue(); + slider->applyOverrides(); + (void)slider->style; + (void)slider->attr; + slider->markDirty(); + delete slider; + } + + // --- Progressbar --- + { + auto* bar = new Progressbar(); + bar->setValue(0.5f); + (void)bar->getValue(); + bar->applyOverrides(); + (void)bar->style; + (void)bar->attr; + bar->markDirty(); + delete bar; + } + + // --- TabBar --- + { + auto* tab1 = new Button(UI::Attributes{}); + auto* tab2 = new Button(UI::Attributes{}); + auto* tabs = new TabBar<>(UI::Attributes{}, {OElement(tab1), OElement(tab2)}); + tabs->setActiveTab(0); + (void)tabs->getActiveTab(); + tabs->applyOverrides(); + (void)tabs->style; + (void)tabs->attr; + (void)tabs->children; + tabs->markDirty(); + delete tabs; + } + + // --- Tooltip --- + { + auto* target = new Button(UI::Attributes{}); + auto* tip = new Tooltip(UI::Attributes{}, OElement(target), "Helpful"); + tip->setHovered(true); + tip->applyOverrides(); + (void)tip->style; + (void)tip->attr; + (void)tip->children; + tip->markDirty(); + delete tip; + } + + // --- Accordion --- + { + auto* header = new Text("Section"); + auto* body = new Div<>(UI::Attributes{}); + auto* acc = new Accordion<>(UI::Attributes{}, OElement(header), OElement(body)); + acc->setBodyVisible(false); + (void)acc->isBodyVisible(); + (void)acc->getHeader(); + (void)acc->getBody(); + (void)acc->style; + (void)acc->attr; + (void)acc->children; + acc->markDirty(); + delete acc; + } + + // --- HR --- + { + auto* hr = new HR(); + (void)hr->style; + (void)hr->attr; + delete hr; + } +} -- 2.54.1