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

Display message being replied to above input box #4350

Merged
merged 21 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Minor: Add an option to use new experimental smarter emote completion. (#4987)
- Minor: Add `--safe-mode` command line option that can be used for troubleshooting when Chatterino is misbehaving or is misconfigured. It disables hiding the settings button & prevents plugins from loading. (#4985)
- Minor: Updated the flatpakref link included with nightly builds to point to up-to-date flathub-beta builds. (#5008)
- Minor: Replying to a message will now display the message being replied to. (#4350)
- Bugfix: Fixed an issue where certain emojis did not send to Twitch chat correctly. (#4840)
- Bugfix: Fixed capitalized channel names in log inclusion list not being logged. (#4848)
- Bugfix: Trimmed custom streamlink paths on all platforms making sure you don't accidentally add spaces at the beginning or end of its path. (#4834)
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,8 @@ set(SOURCE_FILES
widgets/helper/EffectLabel.hpp
widgets/helper/InvisibleSizeGrip.cpp
widgets/helper/InvisibleSizeGrip.hpp
widgets/helper/MessageView.cpp
widgets/helper/MessageView.hpp
widgets/helper/NotebookButton.cpp
widgets/helper/NotebookButton.hpp
widgets/helper/NotebookTab.cpp
Expand Down
159 changes: 159 additions & 0 deletions src/widgets/helper/MessageView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#include "MessageView.hpp"

#include "Application.hpp"
#include "messages/layouts/MessageLayout.hpp"
#include "messages/Selection.hpp"
#include "providers/colors/ColorProvider.hpp"
#include "singletons/Settings.hpp"
#include "singletons/Theme.hpp"
#include "singletons/WindowManager.hpp"

#include <QApplication>
#include <QPainter>

namespace chatterino {

namespace {
static const Selection emptySelection;
dnsge marked this conversation as resolved.
Show resolved Hide resolved
dnsge marked this conversation as resolved.
Show resolved Hide resolved
}
dnsge marked this conversation as resolved.
Show resolved Hide resolved
dnsge marked this conversation as resolved.
Show resolved Hide resolved

MessageView::MessageView()
: MessageView(nullptr)
{
}

MessageView::MessageView(MessagePtr message)
dnsge marked this conversation as resolved.
Show resolved Hide resolved
: message_(std::move(message))
, width_(0)
{
this->createMessageLayout();

// Configure theme and preferences for rendering message
this->messageColors_.applyTheme(getTheme());
this->messagePreferences_.connectSettings(getSettings(),
this->signalHolder_);

// Update frame for any GIFs
this->signalHolder_.managedConnect(
getIApp()->getWindows()->gifRepaintRequested, [&] {
this->maybeUpdate();
});

// Re-layout and potentially update if base flags change (e.g. settings for badges).
this->signalHolder_.managedConnect(getApp()->windows->wordFlagsChanged,
[this] {
this->layoutMessage();
});
}

MessageView::~MessageView()
dnsge marked this conversation as resolved.
Show resolved Hide resolved
dnsge marked this conversation as resolved.
Show resolved Hide resolved
{
}

void MessageView::createMessageLayout()
{
if (this->message_ == nullptr)
{
this->messageLayout_.reset();
return;
}

this->messageLayout_ = std::make_unique<MessageLayout>(this->message_);
}

void MessageView::setMessage(MessagePtr message)
dnsge marked this conversation as resolved.
Show resolved Hide resolved
{
if (this->message_ != message)
{
this->message_ = std::move(message);
this->createMessageLayout();
this->layoutMessage();
}
}

void MessageView::clearMessage()
{
this->setMessage(nullptr);
}

void MessageView::setWidth(int width)
{
if (this->width_ != width)
{
this->width_ = width;
this->layoutMessage();
}
}

void MessageView::paintEvent(QPaintEvent * /*event*/)
{
QPainter painter(this);

auto ctx = MessagePaintContext{
.painter = painter,
.selection = emptySelection,
.colorProvider = ColorProvider::instance(),
.messageColors = this->messageColors_,
.preferences = this->messagePreferences_,

.canvasWidth = this->width_,
.isWindowFocused = this->window() == QApplication::activeWindow(),
.isMentions = false,

.y = 0,
.messageIndex = 0,
.isLastReadMessage = false,
};

this->messageLayout_->paint(ctx);
}

void MessageView::themeChangedEvent()
{
this->layoutMessage();
}

void MessageView::scaleChangedEvent(float /*newScale*/)
{
this->layoutMessage();
}

void MessageView::maybeUpdate()
{
if (this->messageLayout_ != nullptr)
{
this->update();
}
}

void MessageView::layoutMessage()
{
if (this->messageLayout_ == nullptr)
{
return;
}

auto flags = getFlags();
bool updateRequired =
dnsge marked this conversation as resolved.
Show resolved Hide resolved
this->messageLayout_->layout(this->width_, this->scale(), flags);

if (updateRequired)
{
this->setFixedSize(this->width_, this->messageLayout_->getHeight());
this->update();
}
}

MessageElementFlags MessageView::getFlags() const
dnsge marked this conversation as resolved.
Show resolved Hide resolved
{
// Start with base global flags
auto flags = getIApp()->getWindows()->getWordFlags();

// Don't show inline replies or reply button
flags.unset(MessageElementFlag::RepliedMessage);
flags.unset(MessageElementFlag::ReplyButton);

return flags;
}

} // namespace chatterino
53 changes: 53 additions & 0 deletions src/widgets/helper/MessageView.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include "messages/layouts/MessageLayoutContext.hpp"
#include "messages/Message.hpp"
#include "messages/MessageElement.hpp"
#include "widgets/BaseWidget.hpp"

#include <QWidget>

namespace chatterino {

class MessageLayout;

/// MessageView is a fixed-width widget that displays a single message.
/// For the message to be rendered, you must call setWidth.
class MessageView : public BaseWidget
dnsge marked this conversation as resolved.
Show resolved Hide resolved
{
Q_OBJECT

public:
MessageView();
MessageView(MessagePtr message);

~MessageView() override;

void setMessage(MessagePtr message);
void clearMessage();

void setWidth(int width);

protected:
void paintEvent(QPaintEvent * /*event*/) override;
void themeChangedEvent() override;
void scaleChangedEvent(float /*newScale*/) override;

private:
void createMessageLayout();
void maybeUpdate();
void layoutMessage();

MessageElementFlags getFlags() const;

private:
dnsge marked this conversation as resolved.
Show resolved Hide resolved
dnsge marked this conversation as resolved.
Show resolved Hide resolved
dnsge marked this conversation as resolved.
Show resolved Hide resolved
MessagePtr message_;
std::unique_ptr<MessageLayout> messageLayout_;

MessageColors messageColors_;
MessagePreferences messagePreferences_;

int width_;
};

} // namespace chatterino
Loading
Loading