Allow attaching properties to click handlers.

This commit is contained in:
John Preston 2021-11-29 13:32:45 +04:00
parent 725654654d
commit 5cf6981bbb
2 changed files with 16 additions and 0 deletions

View file

@ -154,6 +154,16 @@ auto ClickHandler::getTextEntity() const -> TextEntity {
return { EntityType::Invalid };
}
void ClickHandler::setProperty(int id, QVariant value) {
_properties[id] = std::move(value);
}
const QVariant &ClickHandler::property(int id) const {
static const QVariant kEmpty;
const auto i = _properties.find(id);
return (i != end(_properties)) ? i->second : kEmpty;
}
void ActivateClickHandler(
not_null<QWidget*> guard,
ClickHandlerPtr handler,

View file

@ -7,6 +7,7 @@
#pragma once
#include "base/basic_types.h"
#include "base/flat_map.h"
#include <QtCore/QVariant>
@ -62,6 +63,9 @@ public:
};
virtual TextEntity getTextEntity() const;
void setProperty(int id, QVariant value);
[[nodiscard]] const QVariant &property(int id) const;
// This method should be called on mouse over a click handler.
// It returns true if the active handler was changed or false otherwise.
static bool setActive(const ClickHandlerPtr &p, ClickHandlerHost *host = nullptr);
@ -88,6 +92,8 @@ private:
static ClickHandlerHost *_activeHost;
static ClickHandlerHost *_pressedHost;
base::flat_map<int, QVariant> _properties;
};
class LeftButtonClickHandler : public ClickHandler {