text_extended_data.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "ui/text/text_extended_data.h"
  8. #include "ui/text/text.h"
  9. #include "ui/integration.h"
  10. namespace Ui::Text {
  11. SpoilerClickHandler::SpoilerClickHandler(
  12. not_null<String*> text,
  13. Fn<bool(const ClickContext&)> filter)
  14. : _text(text)
  15. , _filter(std::move(filter)) {
  16. }
  17. not_null<String*> SpoilerClickHandler::text() const {
  18. return _text;
  19. }
  20. void SpoilerClickHandler::setText(not_null<String*> text) {
  21. _text = text;
  22. }
  23. void SpoilerClickHandler::onClick(ClickContext context) const {
  24. if (_filter && !_filter(context)) {
  25. return;
  26. }
  27. _text->setSpoilerRevealed(true, anim::type::normal);
  28. }
  29. PreClickHandler::PreClickHandler(
  30. not_null<String*> text,
  31. uint16 offset,
  32. uint16 length)
  33. : _text(text)
  34. , _offset(offset)
  35. , _length(length) {
  36. }
  37. not_null<String*> PreClickHandler::text() const {
  38. return _text;
  39. }
  40. void PreClickHandler::setText(not_null<String*> text) {
  41. _text = text;
  42. }
  43. void PreClickHandler::onClick(ClickContext context) const {
  44. if (context.button != Qt::LeftButton) {
  45. return;
  46. }
  47. const auto till = uint16(_offset + _length);
  48. auto text = _text->toTextForMimeData({ _offset, till });
  49. if (text.empty()) {
  50. return;
  51. } else if (!text.rich.text.endsWith('\n')) {
  52. text.rich.text.append('\n');
  53. }
  54. if (!text.expanded.endsWith('\n')) {
  55. text.expanded.append('\n');
  56. }
  57. if (Integration::Instance().copyPreOnClick(context.other)) {
  58. TextUtilities::SetClipboardText(std::move(text));
  59. }
  60. }
  61. BlockquoteClickHandler::BlockquoteClickHandler(
  62. not_null<String*> text,
  63. int quoteIndex)
  64. : _text(text)
  65. , _quoteIndex(quoteIndex) {
  66. }
  67. not_null<String*> BlockquoteClickHandler::text() const {
  68. return _text;
  69. }
  70. void BlockquoteClickHandler::setText(not_null<String*> text) {
  71. _text = text;
  72. }
  73. void BlockquoteClickHandler::onClick(ClickContext context) const {
  74. if (context.button != Qt::LeftButton) {
  75. return;
  76. }
  77. _text->setBlockquoteExpanded(
  78. _quoteIndex,
  79. !_text->blockquoteExpanded(_quoteIndex));
  80. }
  81. } // namespace Ui::Text