Skip to content

Commit

Permalink
Fix selection rendering (#4830)
Browse files Browse the repository at this point in the history
The rendering of selections was not aligned to the actual selection that took place for newlines at the end of messages, if they were the only part that was selected of that message.

In addition to that fix, we've already refactored the MessageLayoutContainer to try to make it a little bit more sane to work with in the future.

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
  • Loading branch information
Nerixyz and pajlada authored Sep 23, 2023
1 parent c71e912 commit 6860c70
Show file tree
Hide file tree
Showing 9 changed files with 884 additions and 655 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
- Bugfix: Fixed selection of tabs after closing a tab when using "Live Tabs Only". (#4770)
- Bugfix: Fixed input in reply thread popup losing focus when dragging. (#4815)
- Bugfix: Fixed the Quick Switcher (CTRL+K) from sometimes showing up on the wrong window. (#4819)
- Bugfix: Fixed too much text being copied when copying chat messages. (#4812)
- Bugfix: Fixed too much text being copied when copying chat messages. (#4812, #4830)
- Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791)
- Dev: Temporarily disable High DPI scaling on Qt6 builds on Windows. (#4767)
- Dev: Tests now run on Ubuntu 22.04 instead of 20.04 to loosen C++ restrictions in tests. (#4774)
Expand Down
9 changes: 5 additions & 4 deletions src/messages/Selection.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#pragma once

#include <cstddef>
#include <cstdint>
#include <tuple>
#include <utility>

namespace chatterino {

struct SelectionItem {
uint32_t messageIndex{0};
uint32_t charIndex{0};
size_t messageIndex{0};
size_t charIndex{0};

SelectionItem() = default;

SelectionItem(uint32_t _messageIndex, uint32_t _charIndex)
SelectionItem(size_t _messageIndex, size_t _charIndex)
: messageIndex(_messageIndex)
, charIndex(_charIndex)
{
Expand Down Expand Up @@ -73,7 +74,7 @@ struct Selection {
}

// Shift all message selection indices `offset` back
void shiftMessageIndex(uint32_t offset)
void shiftMessageIndex(size_t offset)
{
if (offset > this->selectionMin.messageIndex)
{
Expand Down
10 changes: 5 additions & 5 deletions src/messages/layouts/MessageLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void MessageLayout::actuallyLayout(int width, MessageElementFlags flags)
bool hideSimilar = getSettings()->hideSimilar;
bool hideReplies = !flags.has(MessageElementFlag::RepliedMessage);

this->container_.begin(width, this->scale_, messageFlags);
this->container_.beginLayout(width, this->scale_, messageFlags);

for (const auto &element : this->message_->elements)
{
Expand Down Expand Up @@ -184,7 +184,7 @@ void MessageLayout::actuallyLayout(int width, MessageElementFlags flags)
this->deleteBuffer();
}

this->container_.end();
this->container_.endLayout();
this->height_ = this->container_.getHeight();

// collapsed state
Expand Down Expand Up @@ -424,17 +424,17 @@ const MessageLayoutElement *MessageLayout::getElementAt(QPoint point)
return this->container_.getElementAt(point);
}

int MessageLayout::getLastCharacterIndex() const
size_t MessageLayout::getLastCharacterIndex() const
{
return this->container_.getLastCharacterIndex();
}

int MessageLayout::getFirstMessageCharacterIndex() const
size_t MessageLayout::getFirstMessageCharacterIndex() const
{
return this->container_.getFirstMessageCharacterIndex();
}

int MessageLayout::getSelectionIndex(QPoint position)
size_t MessageLayout::getSelectionIndex(QPoint position) const
{
return this->container_.getSelectionIndex(position);
}
Expand Down
20 changes: 17 additions & 3 deletions src/messages/layouts/MessageLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,23 @@ class MessageLayout

// Elements
const MessageLayoutElement *getElementAt(QPoint point);
int getLastCharacterIndex() const;
int getFirstMessageCharacterIndex() const;
int getSelectionIndex(QPoint position);

/**
* Get the index of the last character in this message's container
* This is the sum of all the characters in `elements_`
*/
size_t getLastCharacterIndex() const;

/**
* Get the index of the first visible character in this message's container
* This is not always 0 in case there elements that are skipped
*/
size_t getFirstMessageCharacterIndex() const;

/**
* Get the character index at the given position, in the context of selections
*/
size_t getSelectionIndex(QPoint position) const;
void addSelectionText(QString &str, uint32_t from = 0,
uint32_t to = UINT32_MAX,
CopyMode copymode = CopyMode::Everything);
Expand Down
Loading

0 comments on commit 6860c70

Please sign in to comment.