mirror-chatterino2/src/messages/Selection.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
3.1 KiB
C++
Raw Normal View History

2018-01-04 23:50:30 +01:00
#pragma once
#include <cstddef>
#include <cstdint>
#include <tuple>
#include <utility>
2018-01-04 23:50:30 +01:00
namespace chatterino {
2018-01-04 23:50:30 +01:00
struct SelectionItem {
size_t messageIndex{0};
size_t charIndex{0};
SelectionItem() = default;
SelectionItem(size_t _messageIndex, size_t _charIndex)
: messageIndex(_messageIndex)
, charIndex(_charIndex)
2018-01-04 23:50:30 +01:00
{
}
2018-01-16 00:26:04 +01:00
bool operator<(const SelectionItem &b) const
{
return std::tie(this->messageIndex, this->charIndex) <
std::tie(b.messageIndex, b.charIndex);
2018-01-16 00:26:04 +01:00
}
2018-01-16 00:26:04 +01:00
bool operator>(const SelectionItem &b) const
2018-01-04 23:50:30 +01:00
{
2018-12-02 17:49:15 +01:00
return !this->operator==(b) && b.operator<(*this);
2018-01-04 23:50:30 +01:00
}
2018-01-16 00:26:04 +01:00
bool operator==(const SelectionItem &b) const
2018-01-04 23:50:30 +01:00
{
2018-01-16 00:26:04 +01:00
return this->messageIndex == b.messageIndex &&
this->charIndex == b.charIndex;
2018-01-04 23:50:30 +01:00
}
bool operator!=(const SelectionItem &b) const
{
return !this->operator==(b);
}
2018-01-04 23:50:30 +01:00
};
2018-01-04 23:50:30 +01:00
struct Selection {
SelectionItem start;
SelectionItem end;
SelectionItem selectionMin;
SelectionItem selectionMax;
Selection() = default;
2018-01-04 23:50:30 +01:00
Selection(const SelectionItem &start, const SelectionItem &end)
: start(start)
, end(end)
, selectionMin(start)
, selectionMax(end)
2018-01-04 23:50:30 +01:00
{
if (selectionMin > selectionMax)
{
std::swap(this->selectionMin, this->selectionMax);
2018-01-04 23:50:30 +01:00
}
}
bool operator==(const Selection &b) const
{
return this->start == b.start && this->end == b.end;
}
bool operator!=(const Selection &b) const
{
return !this->operator==(b);
}
2018-01-04 23:50:30 +01:00
bool isEmpty() const
{
2018-01-16 00:26:04 +01:00
return this->start == this->end;
2018-01-04 23:50:30 +01:00
}
2018-01-04 23:50:30 +01:00
bool isSingleMessage() const
{
return this->selectionMin.messageIndex ==
this->selectionMax.messageIndex;
2018-01-04 23:50:30 +01:00
}
// Shift all message selection indices `offset` back
void shiftMessageIndex(size_t offset)
{
if (offset > this->selectionMin.messageIndex)
{
this->selectionMin.messageIndex = 0;
this->selectionMin.charIndex = 0;
}
else
{
this->selectionMin.messageIndex -= offset;
}
if (offset > this->selectionMax.messageIndex)
{
this->selectionMax.messageIndex = 0;
this->selectionMax.charIndex = 0;
}
else
{
this->selectionMax.messageIndex -= offset;
}
if (offset > this->start.messageIndex)
{
this->start.messageIndex = 0;
this->start.charIndex = 0;
}
else
{
this->start.messageIndex -= offset;
}
if (offset > this->end.messageIndex)
{
this->end.messageIndex = 0;
this->end.charIndex = 0;
}
else
{
this->end.messageIndex -= offset;
}
}
2018-01-04 23:50:30 +01:00
};
2018-10-06 13:43:21 +02:00
struct DoubleClickSelection {
uint32_t originalStart{0};
uint32_t originalEnd{0};
uint32_t origMessageIndex{0};
bool selectingLeft{false};
bool selectingRight{false};
2018-10-06 13:43:21 +02:00
SelectionItem origStartItem;
SelectionItem origEndItem;
};
2018-01-16 00:26:04 +01:00
} // namespace chatterino