Chatterino
Selection.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <tuple>
5 #include <utility>
6 
7 namespace chatterino {
8 
9 struct SelectionItem {
10  uint32_t messageIndex{0};
11  uint32_t charIndex{0};
12 
13  SelectionItem() = default;
14 
15  SelectionItem(uint32_t _messageIndex, uint32_t _charIndex)
16  : messageIndex(_messageIndex)
17  , charIndex(_charIndex)
18  {
19  }
20 
21  bool operator<(const SelectionItem &b) const
22  {
23  return std::tie(this->messageIndex, this->charIndex) <
24  std::tie(b.messageIndex, b.charIndex);
25  }
26 
27  bool operator>(const SelectionItem &b) const
28  {
29  return !this->operator==(b) && b.operator<(*this);
30  }
31 
32  bool operator==(const SelectionItem &b) const
33  {
34  return this->messageIndex == b.messageIndex &&
35  this->charIndex == b.charIndex;
36  }
37 
38  bool operator!=(const SelectionItem &b) const
39  {
40  return this->operator==(b);
41  }
42 };
43 
44 struct Selection {
49 
50  Selection() = default;
51 
52  Selection(const SelectionItem &start, const SelectionItem &end)
53  : start(start)
54  , end(end)
55  , selectionMin(start)
56  , selectionMax(end)
57  {
58  if (selectionMin > selectionMax)
59  {
60  std::swap(this->selectionMin, this->selectionMax);
61  }
62  }
63 
64  bool isEmpty() const
65  {
66  return this->start == this->end;
67  }
68 
69  bool isSingleMessage() const
70  {
71  return this->selectionMin.messageIndex ==
72  this->selectionMax.messageIndex;
73  }
74 
75  // Shift all message selection indices `offset` back
76  void shiftMessageIndex(uint32_t offset)
77  {
78  if (offset > this->selectionMin.messageIndex)
79  {
80  this->selectionMin.messageIndex = 0;
81  }
82  else
83  {
84  this->selectionMin.messageIndex -= offset;
85  }
86 
87  if (offset > this->selectionMax.messageIndex)
88  {
89  this->selectionMax.messageIndex = 0;
90  }
91  else
92  {
93  this->selectionMax.messageIndex -= offset;
94  }
95 
96  if (offset > this->start.messageIndex)
97  {
98  this->start.messageIndex = 0;
99  }
100  else
101  {
102  this->start.messageIndex -= offset;
103  }
104 
105  if (offset > this->end.messageIndex)
106  {
107  this->end.messageIndex = 0;
108  }
109  else
110  {
111  this->end.messageIndex -= offset;
112  }
113  }
114 };
115 
117  uint32_t originalStart{0};
118  uint32_t originalEnd{0};
119  uint32_t origMessageIndex{0};
120  bool selectingLeft{false};
121  bool selectingRight{false};
124 };
125 
126 } // namespace chatterino
Definition: Selection.hpp:9
SelectionItem selectionMin
Definition: Selection.hpp:47
Definition: Selection.hpp:44
SelectionItem(uint32_t _messageIndex, uint32_t _charIndex)
Definition: Selection.hpp:15
Definition: Application.cpp:48
SelectionItem end
Definition: Selection.hpp:46
uint32_t messageIndex
Definition: Selection.hpp:10
bool operator<(const SelectionItem &b) const
Definition: Selection.hpp:21
Selection(const SelectionItem &start, const SelectionItem &end)
Definition: Selection.hpp:52
SelectionItem selectionMax
Definition: Selection.hpp:48
SelectionItem start
Definition: Selection.hpp:45
Definition: Selection.hpp:116
void shiftMessageIndex(uint32_t offset)
Definition: Selection.hpp:76
bool operator==(const SelectionItem &b) const
Definition: Selection.hpp:32
bool operator!=(const SelectionItem &b) const
Definition: Selection.hpp:38
bool operator>(const SelectionItem &b) const
Definition: Selection.hpp:27
uint32_t charIndex
Definition: Selection.hpp:11
SelectionItem origEndItem
Definition: Selection.hpp:123
SelectionItem origStartItem
Definition: Selection.hpp:122
bool isSingleMessage() const
Definition: Selection.hpp:69
bool isEmpty() const
Definition: Selection.hpp:64