From 68a398836c0796f246c7934bbba0c1ccc678deae Mon Sep 17 00:00:00 2001 From: Markus Mertama Date: Mon, 23 Oct 2023 20:54:25 +0300 Subject: [PATCH] add RESIZE event const --- gempyrelib/include/gempyre.h | 7 +++++-- gempyrelib/src/element.cpp | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/gempyrelib/include/gempyre.h b/gempyrelib/include/gempyre.h index 1dcb9fdf..96676d5d 100644 --- a/gempyrelib/include/gempyre.h +++ b/gempyrelib/include/gempyre.h @@ -89,6 +89,7 @@ namespace Gempyre { int height; }; + public: /// Copy constructor. Element(const Element& other) = default; @@ -144,7 +145,7 @@ namespace Gempyre { /// @param value - attribute value /// @return this element Element& set_attribute(const std::string& attr, const std::string& value); - /// @brief Set HTML a attribute of this element + /// @brief Set HTML a attribute of this element /// @param attr - attribute name /// @return this element Element& set_attribute(const std::string& attr); @@ -180,7 +181,7 @@ namespace Gempyre { /// @cond INTERNAL const GempyreInternal& ref() const; GempyreInternal& ref(); - static const std::string generateId(const std::string& prefix); + static const std::string generateId(const std::string& prefix); protected: Ui* m_ui; std::string m_id; @@ -225,6 +226,8 @@ namespace Gempyre { static constexpr auto FOCUS_OUT = "focusout"; /// Load static constexpr auto LOAD = "load"; + /// Resize - use ui.root().subscribe(Event::RESIZE, [... for window resize + static constexpr auto RESIZE = "resize"; /// @brief element that has emitted the event, the same that did subscription. Element element; diff --git a/gempyrelib/src/element.cpp b/gempyrelib/src/element.cpp index fdf7fc30..baa598f8 100644 --- a/gempyrelib/src/element.cpp +++ b/gempyrelib/src/element.cpp @@ -7,6 +7,13 @@ #include #include +// helper type for the visitor #4 +template +struct overloaded : Ts... { using Ts::operator()...; }; +// explicit deduction guide (not needed as of C++20) +template +overloaded(Ts...) -> overloaded; + using namespace Gempyre; const std::string Element::generateId(const std::string& prefix) { @@ -68,6 +75,25 @@ Element& Element::set_attribute(const std::string &attr) { "value", "true"); return *this; } +/* +using AttrValueType = std::variant; + +template +void Element::send(Element& el, const std::string& attr, const T& value) { + el.ref().send(el, "set_attribute", "attribute", attr, "value", value); +} + +Element& Element::set_attribute(const std::string& attr, const AttrValueType& value) { + std::visit(overloaded { + [&, this](const bool& v) {send(*this, attr, v);}, + [&, this](const int& v) {send(*this, attr, v);}, + [&, this](const double& v) {send(*this, attr, v);}, + [&, this](const float& v) {send(*this, attr, v);}, + [&, this](const unsigned& v) {send(*this, attr, v);}, + }, value); + return *this; +} +*/ Element& Element::remove_attribute(const std::string &attr) { ref().send(*this, "remove_attribute", @@ -159,7 +185,7 @@ std::optional Element::parent() const { return m_ui->root(); return Gempyre::Element(*m_ui, *pid); } - + Element::~Element() {}