Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix selection rendering #4830

Merged
merged 28 commits into from
Sep 23, 2023
Merged

Fix selection rendering #4830

merged 28 commits into from
Sep 23, 2023

Conversation

Nerixyz
Copy link
Contributor

@Nerixyz Nerixyz commented Sep 18, 2023

Description

After 3265df7, the selection rendering was off. Selecting the trailing newline of a message (upwards) would render the whole last line of a message to be selected. However, when copying the text, only the trailing newline of the message would be included.

This PR refactors the rendering of the selection to be a bit more readable and to reduce sign-compare warnings in the code (see #4825). In addition, a special case for the newline is added to be consistent with rendering the selection of the "trailing newline" (downwards).

Even though the selection is now correctly rendered, this might not be the exact behavior one would expect. Not including the trailing newline might be desired. Feel free to discuss this here and I can potentially adjust the behavior.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

src/messages/Selection.hpp Show resolved Hide resolved
src/messages/Selection.hpp Outdated Show resolved Hide resolved
src/messages/Selection.hpp Show resolved Hide resolved
src/messages/layouts/MessageLayoutContainer.cpp Outdated Show resolved Hide resolved
src/messages/layouts/MessageLayoutContainer.cpp Outdated Show resolved Hide resolved
src/messages/layouts/MessageLayoutContainer.cpp Outdated Show resolved Hide resolved
@pajlada
Copy link
Member

pajlada commented Sep 23, 2023

I'm unable to reproduce the reported issue, could you provide some more reproduction instructions?
EDIT: nvm I can reproduce 🤓

Copy link
Member

@pajlada pajlada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Great job @Nerixyz!

@pajlada pajlada enabled auto-merge (squash) September 23, 2023 14:41
@pajlada pajlada merged commit 6860c70 into Chatterino:master Sep 23, 2023
17 checks passed
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

for (auto i = this->lines_.back().startIndex;
i < this->elements_.size(); i++)
{
QPoint prevPos = this->elements_[i]->getRect().topLeft();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'prevPos' of type 'QPoint' can be declared 'const' [misc-const-correctness]

Suggested change
QPoint prevPos = this->elements_[i]->getRect().topLeft();
QPoint const prevPos = this->elements_[i]->getRect().topLeft();


int xOffset = 0;

if (this->flags_.has(MessageFlag::Centered) && this->elements_.size() > 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]

Suggested change
if (this->flags_.has(MessageFlag::Centered) && this->elements_.size() > 0)
if (this->flags_.has(MessageFlag::Centered) && !this->elements_.empty())
Additional context

/usr/include/c++/11/bits/stl_vector.h:1006: method 'vector'::empty() defined here

      empty() const _GLIBCXX_NOEXCEPT
      ^

{
MessageLayoutElement *element = this->elements_.at(i).get();

bool isCompactEmote =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'isCompactEmote' of type 'bool' can be declared 'const' [misc-const-correctness]

Suggested change
bool isCompactEmote =
bool const isCompactEmote =

int yExtra = 0;
if (isCompactEmote)
{
yExtra = (COMPACT_EMOTES_OFFSET / 2) * this->scale_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: narrowing conversion from 'float' to 'int' [bugprone-narrowing-conversions]

            yExtra = (COMPACT_EMOTES_OFFSET / 2) * this->scale_;
                     ^

int yExtra = 0;
if (isCompactEmote)
{
yExtra = (COMPACT_EMOTES_OFFSET / 2) * this->scale_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: result of integer division used in a floating point context; possible loss of precision [bugprone-integer-division]

            yExtra = (COMPACT_EMOTES_OFFSET / 2) * this->scale_;
                      ^
Additional context

src/messages/layouts/MessageLayoutContainer.cpp:19: expanded from macro 'COMPACT_EMOTES_OFFSET'

#define COMPACT_EMOTES_OFFSET 4
                              ^

@@ -180,7 +595,7 @@ void MessageLayoutContainer::_addElement(MessageLayoutElement *element,
element->getCreator().getFlags().hasNone(
{MessageElementFlag::TwitchEmoteImage}))
{
yOffset -= (this->margin.top * this->scale_);
yOffset -= (MARGIN.top() * this->scale_);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: narrowing conversion from 'float' to 'int' [bugprone-narrowing-conversions]

        yOffset -= (MARGIN.top() * this->scale_);
                   ^

{
int c = this->elements_[i]->getSelectionIndexCount();
size_t c = this->elements_[i]->getSelectionIndexCount();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: variable 'c' of type 'size_t' (aka 'unsigned long') can be declared 'const' [misc-const-correctness]

Suggested change
size_t c = this->elements_[i]->getSelectionIndexCount();
size_t const c = this->elements_[i]->getSelectionIndexCount();

Nerixyz added a commit to SevenTV/chatterino7 that referenced this pull request Sep 28, 2023
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>
@Nerixyz Nerixyz deleted the fix/selection branch October 6, 2023 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants