Allow custom actions on Checkbox click-handlers.
This commit is contained in:
parent
6f856ce495
commit
7a4ea49959
2 changed files with 36 additions and 1 deletions
|
|
@ -7,6 +7,7 @@
|
||||||
#include "ui/widgets/checkbox.h"
|
#include "ui/widgets/checkbox.h"
|
||||||
|
|
||||||
#include "ui/effects/ripple_animation.h"
|
#include "ui/effects/ripple_animation.h"
|
||||||
|
#include "ui/basic_click_handlers.h"
|
||||||
#include "ui/ui_utility.h"
|
#include "ui/ui_utility.h"
|
||||||
|
|
||||||
#include <QtGui/QtEvents>
|
#include <QtGui/QtEvents>
|
||||||
|
|
@ -543,6 +544,27 @@ void Checkbox::setTextBreakEverywhere(bool allow) {
|
||||||
_textBreakEverywhere = allow;
|
_textBreakEverywhere = allow;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Checkbox::setLink(uint16 lnkIndex, const ClickHandlerPtr &lnk) {
|
||||||
|
_text.setLink(lnkIndex, lnk);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Checkbox::setLinksTrusted() {
|
||||||
|
static const auto TrustedLinksFilter = [](
|
||||||
|
const ClickHandlerPtr &link,
|
||||||
|
Qt::MouseButton button) {
|
||||||
|
if (const auto url = dynamic_cast<UrlClickHandler*>(link.get())) {
|
||||||
|
url->UrlClickHandler::onClick({ button });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
setClickHandlerFilter(TrustedLinksFilter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Checkbox::setClickHandlerFilter(ClickHandlerFilter &&filter) {
|
||||||
|
_clickHandlerFilter = std::move(filter);
|
||||||
|
}
|
||||||
|
|
||||||
bool Checkbox::checked() const {
|
bool Checkbox::checked() const {
|
||||||
return _check->checked();
|
return _check->checked();
|
||||||
}
|
}
|
||||||
|
|
@ -709,10 +731,15 @@ void Checkbox::mouseMoveEvent(QMouseEvent *e) {
|
||||||
void Checkbox::mouseReleaseEvent(QMouseEvent *e) {
|
void Checkbox::mouseReleaseEvent(QMouseEvent *e) {
|
||||||
const auto weak = Ui::MakeWeak(this);
|
const auto weak = Ui::MakeWeak(this);
|
||||||
if (auto activated = _activatingHandler = ClickHandler::unpressed()) {
|
if (auto activated = _activatingHandler = ClickHandler::unpressed()) {
|
||||||
|
// _clickHandlerFilter may delete `this`. In that case we don't want
|
||||||
|
// to try to show a context menu or smth like that.
|
||||||
const auto button = e->button();
|
const auto button = e->button();
|
||||||
crl::on_main(this, [=] {
|
crl::on_main(this, [=] {
|
||||||
const auto guard = window();
|
const auto guard = window();
|
||||||
ActivateClickHandler(guard, activated, button);
|
if (!_clickHandlerFilter
|
||||||
|
|| _clickHandlerFilter(activated, button)) {
|
||||||
|
ActivateClickHandler(guard, activated, button);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
RippleButton::mouseReleaseEvent(e);
|
RippleButton::mouseReleaseEvent(e);
|
||||||
|
|
|
||||||
|
|
@ -180,6 +180,12 @@ public:
|
||||||
void setAllowTextLines(int lines = 0);
|
void setAllowTextLines(int lines = 0);
|
||||||
void setTextBreakEverywhere(bool allow = true);
|
void setTextBreakEverywhere(bool allow = true);
|
||||||
|
|
||||||
|
void setLink(uint16 lnkIndex, const ClickHandlerPtr &lnk);
|
||||||
|
void setLinksTrusted();
|
||||||
|
|
||||||
|
using ClickHandlerFilter = Fn<bool(const ClickHandlerPtr&, Qt::MouseButton)>;
|
||||||
|
void setClickHandlerFilter(ClickHandlerFilter &&filter);
|
||||||
|
|
||||||
bool checked() const;
|
bool checked() const;
|
||||||
rpl::producer<bool> checkedChanges() const;
|
rpl::producer<bool> checkedChanges() const;
|
||||||
rpl::producer<bool> checkedValue() const;
|
rpl::producer<bool> checkedValue() const;
|
||||||
|
|
@ -235,6 +241,8 @@ private:
|
||||||
ClickHandlerPtr _activatingHandler;
|
ClickHandlerPtr _activatingHandler;
|
||||||
QPixmap _checkCache;
|
QPixmap _checkCache;
|
||||||
|
|
||||||
|
ClickHandlerFilter _clickHandlerFilter;
|
||||||
|
|
||||||
style::align _checkAlignment = style::al_left;
|
style::align _checkAlignment = style::al_left;
|
||||||
Text::String _text;
|
Text::String _text;
|
||||||
int _allowTextLines = 1;
|
int _allowTextLines = 1;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue