-
-
Notifications
You must be signed in to change notification settings - Fork 458
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display message being replied to above input box (#4350)
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
- Loading branch information
Showing
7 changed files
with
353 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include "widgets/helper/MessageView.hpp" | ||
|
||
#include "Application.hpp" | ||
#include "messages/layouts/MessageLayout.hpp" | ||
#include "messages/MessageElement.hpp" | ||
#include "messages/Selection.hpp" | ||
#include "providers/colors/ColorProvider.hpp" | ||
#include "singletons/Theme.hpp" | ||
#include "singletons/WindowManager.hpp" | ||
|
||
#include <QApplication> | ||
#include <QPainter> | ||
|
||
namespace { | ||
|
||
using namespace chatterino; | ||
|
||
const Selection EMPTY_SELECTION; | ||
|
||
const MessageElementFlags MESSAGE_FLAGS{ | ||
MessageElementFlag::Text, | ||
MessageElementFlag::EmojiAll, | ||
MessageElementFlag::EmoteText, | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace chatterino { | ||
|
||
MessageView::MessageView() = default; | ||
MessageView::~MessageView() = default; | ||
|
||
void MessageView::createMessageLayout() | ||
{ | ||
if (this->message_ == nullptr) | ||
{ | ||
this->messageLayout_.reset(); | ||
return; | ||
} | ||
|
||
this->messageLayout_ = std::make_unique<MessageLayout>(this->message_); | ||
} | ||
|
||
void MessageView::setMessage(const MessagePtr &message) | ||
{ | ||
if (!message) | ||
{ | ||
return; | ||
} | ||
|
||
auto singleLineMessage = std::make_shared<Message>(); | ||
singleLineMessage->elements.emplace_back( | ||
std::make_unique<SingleLineTextElement>( | ||
message->messageText, MESSAGE_FLAGS, MessageColor::Type::System, | ||
FontStyle::ChatMediumSmall)); | ||
this->message_ = std::move(singleLineMessage); | ||
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 = EMPTY_SELECTION, | ||
.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->messageColors_.applyTheme(getTheme()); | ||
this->messageColors_.regular = getTheme()->splits.input.background; | ||
if (this->messageLayout_) | ||
{ | ||
this->messageLayout_->invalidateBuffer(); | ||
} | ||
} | ||
|
||
void MessageView::scaleChangedEvent(float newScale) | ||
{ | ||
(void)newScale; | ||
|
||
this->layoutMessage(); | ||
} | ||
|
||
void MessageView::layoutMessage() | ||
{ | ||
if (this->messageLayout_ == nullptr) | ||
{ | ||
return; | ||
} | ||
|
||
bool updateRequired = this->messageLayout_->layout( | ||
this->width_, this->scale(), | ||
this->scale() * static_cast<float>(this->devicePixelRatio()), | ||
MESSAGE_FLAGS, false); | ||
|
||
if (updateRequired) | ||
{ | ||
this->setFixedSize(this->width_, this->messageLayout_->getHeight()); | ||
this->update(); | ||
} | ||
} | ||
|
||
} // namespace chatterino |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
|
||
#include "messages/layouts/MessageLayoutContext.hpp" | ||
#include "messages/Message.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 | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
MessageView(); | ||
~MessageView() override; | ||
MessageView(const MessageView &) = delete; | ||
MessageView(MessageView &&) = delete; | ||
MessageView &operator=(const MessageView &) = delete; | ||
MessageView &operator=(MessageView &&) = delete; | ||
|
||
void setMessage(const 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 layoutMessage(); | ||
|
||
MessagePtr message_; | ||
std::unique_ptr<MessageLayout> messageLayout_; | ||
|
||
MessageColors messageColors_; | ||
MessagePreferences messagePreferences_; | ||
|
||
int width_{}; | ||
}; | ||
|
||
} // namespace chatterino |
Oops, something went wrong.