From 2c1e6b458d4a4c7a0119248d7cd436c1e3ba433d Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 24 Dec 2021 15:04:51 +0300 Subject: [PATCH] Moved SpoilerClickHandler to separated file. --- CMakeLists.txt | 2 ++ ui/spoiler_click_handler.cpp | 32 ++++++++++++++++++++++++++++++++ ui/spoiler_click_handler.h | 29 +++++++++++++++++++++++++++++ ui/text/text.cpp | 29 +++-------------------------- ui/text/text.h | 4 +++- 5 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 ui/spoiler_click_handler.cpp create mode 100644 ui/spoiler_click_handler.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c4970a7..8fac166 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -230,6 +230,8 @@ PRIVATE ui/rect_part.h ui/round_rect.cpp ui/round_rect.h + ui/spoiler_click_handler.cpp + ui/spoiler_click_handler.h ui/rp_widget.cpp ui/rp_widget.h ui/ui_utility.cpp diff --git a/ui/spoiler_click_handler.cpp b/ui/spoiler_click_handler.cpp new file mode 100644 index 0000000..a982eb8 --- /dev/null +++ b/ui/spoiler_click_handler.cpp @@ -0,0 +1,32 @@ +// This file is part of Desktop App Toolkit, +// a set of libraries for developing nice desktop applications. +// +// For license and copyright information please follow this link: +// https://github.com/desktop-app/legal/blob/master/LEGAL +// +#include "ui/spoiler_click_handler.h" + +#include "ui/text/text_entity.h" + +ClickHandler::TextEntity SpoilerClickHandler::getTextEntity() const { + return { EntityType::Spoiler }; +} + +void SpoilerClickHandler::onClick(ClickContext context) const { + if (!_shown) { + const auto nonconst = const_cast(this); + nonconst->_shown = true; + } +} + +bool SpoilerClickHandler::shown() const { + return _shown; +} + +crl::time SpoilerClickHandler::startMs() const { + return _startMs; +} + +void SpoilerClickHandler::setStartMs(crl::time value) { + _startMs = value; +} diff --git a/ui/spoiler_click_handler.h b/ui/spoiler_click_handler.h new file mode 100644 index 0000000..8655856 --- /dev/null +++ b/ui/spoiler_click_handler.h @@ -0,0 +1,29 @@ +// This file is part of Desktop App Toolkit, +// a set of libraries for developing nice desktop applications. +// +// For license and copyright information please follow this link: +// https://github.com/desktop-app/legal/blob/master/LEGAL +// +#pragma once + +#include "ui/click_handler.h" + +enum class EntityType : uchar; + +class SpoilerClickHandler : public ClickHandler { +public: + SpoilerClickHandler() = default; + + TextEntity getTextEntity() const override; + + void onClick(ClickContext context) const override; + + [[nodiscard]] bool shown() const; + [[nodiscard]] crl::time startMs() const; + void setStartMs(crl::time value); + +private: + bool _shown = false; + crl::time _startMs = 0; + +}; diff --git a/ui/text/text.cpp b/ui/text/text.cpp index 439203b..0e40f89 100644 --- a/ui/text/text.cpp +++ b/ui/text/text.cpp @@ -13,6 +13,7 @@ #include "ui/integration.h" #include "ui/round_rect.h" #include "ui/image/image_prepare.h" +#include "ui/spoiler_click_handler.h" #include "base/platform/base_platform_info.h" #include "base/qt_adapters.h" @@ -270,30 +271,6 @@ const TextParseOptions _textPlainOptions = { namespace Ui { namespace Text { -class String::SpoilerClickHandler final : public ClickHandler { -public: - SpoilerClickHandler() = default; - - TextEntity getTextEntity() const override { - return { EntityType::Spoiler }; - } - - void onClick(ClickContext context) const override { - if (!_shown) { - const auto nonconst = const_cast(this); - nonconst->_shown = true; - } - } - - [[nodiscard]] bool shown() const { - return _shown; - } - -private: - bool _shown = false; - -}; - class Parser { public: Parser( @@ -1024,9 +1001,9 @@ void Parser::finalize(const TextParseOptions &options) { if (spoilerIndex) { _t->_spoilers.resize(spoilerIndex); const auto handler = (options.flags & TextParseLinks) - ? std::make_shared() + ? std::make_shared() : nullptr; - _t->_spoilers[spoilerIndex - 1] = std::move(handler); + _t->setSpoiler(spoilerIndex, std::move(handler)); } const auto shiftedIndex = block->lnkIndex(); if (shiftedIndex <= kStringLinkIndexShift) { diff --git a/ui/text/text.h b/ui/text/text.h index d6125e1..99edc80 100644 --- a/ui/text/text.h +++ b/ui/text/text.h @@ -15,6 +15,8 @@ #include #include +class SpoilerClickHandler; + static const QChar TextCommand(0x0010); enum TextCommands { TextCommandBold = 0x01, @@ -133,6 +135,7 @@ public: void setLink(uint16 lnkIndex, const ClickHandlerPtr &lnk); bool hasLinks() const; + void setSpoiler(uint16 lnkIndex, const std::shared_ptr &lnk); bool hasSkipBlock() const; bool updateSkipBlock(int width, int height); @@ -224,7 +227,6 @@ private: TextBlocks _blocks; TextLinks _links; - class SpoilerClickHandler; QVector> _spoilers; Qt::LayoutDirection _startDir = Qt::LayoutDirectionAuto;