Skip to content

Commit

Permalink
Removed QGraphicsOpacityEffect usage from media view overlay widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
23rd authored and john-preston committed Sep 4, 2024
1 parent b20e2c3 commit a60385f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
85 changes: 73 additions & 12 deletions Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ For license and copyright information please follow this link:
#include "ui/text/format_values.h"
#include "ui/item_text_options.h"
#include "ui/painter.h"
#include "ui/rect.h"
#include "ui/power_saving.h"
#include "ui/cached_round_corners.h"
#include "ui/gl/gl_window.h"
Expand Down Expand Up @@ -100,7 +101,6 @@ For license and copyright information please follow this link:
#include <QtGui/QGuiApplication>
#include <QtGui/QWindow>
#include <QtGui/QScreen>
#include <QGraphicsOpacityEffect>

#include <kurlmimedata.h>

Expand Down Expand Up @@ -228,6 +228,73 @@ QWidget *PipDelegate::pipParentWidget() {

} // namespace

class OverlayWidget::SponsoredButton : public Ui::RippleButton {
public:
SponsoredButton(QWidget *parent)
: Ui::RippleButton(parent, st::mediaviewSponsoredButton.ripple) {
}

void setText(QString text) {
_text = Ui::Text::String(
st::mediaviewSponsoredButton.style,
std::move(text),
kDefaultTextOptions,
width());
resize(width(), _text.minHeight() * 2);
}
void setOpacity(float opacity) {
_opacity = opacity;
}

protected:
void paintEvent(QPaintEvent *e) override {
auto p = QPainter(this);
const auto &st = st::mediaviewSponsoredButton;

p.setOpacity(_opacity);

const auto over = Ui::AbstractButton::isOver();
const auto down = Ui::AbstractButton::isDown();
{
auto hq = PainterHighQualityEnabler(p);
p.setPen(Qt::NoPen);
p.setBrush((over || down) ? st.textBgOver : st.textBg);
p.drawRoundedRect(
rect(),
st::mediaviewCaptionRadius,
st::mediaviewCaptionRadius);
}

Ui::RippleButton::paintRipple(p, 0, 0);

p.setPen(st.textFg);
p.setBrush(Qt::NoBrush);
_text.draw(p, {
.position = QPoint(
(width() - _text.maxWidth()) / 2,
(height() - _text.minHeight()) / 2),
.outerWidth = width(),
.availableWidth = width(),
});
}

QImage prepareRippleMask() const override {
return Ui::RippleAnimation::RoundRectMask(
size(),
st::mediaviewCaptionRadius);
}
QPoint prepareRippleStartPosition() const override {
return mapFromGlobal(QCursor::pos())
- rect::m::pos::tl(st::mediaviewSponsoredButton.padding);
}

private:
Ui::Text::String _text;
float64 _opacity = 1.;

};


struct OverlayWidget::SharedMedia {
SharedMedia(SharedMediaKey key) : key(key) {
}
Expand Down Expand Up @@ -1812,9 +1879,9 @@ bool OverlayWidget::updateControlsAnimation(crl::time now) {
} else {
_controlsOpacity.update(dt, anim::linear);
}
if (_sponsoredButtonOpacity && _sponsoredButton) {
if (_sponsoredButton) {
const auto value = _controlsOpacity.current();
_sponsoredButtonOpacity->setOpacity(value);
_sponsoredButton->setOpacity(value);
_sponsoredButton->setAttribute(
Qt::WA_TransparentForMouseEvents,
value < 1);
Expand Down Expand Up @@ -3678,19 +3745,14 @@ void OverlayWidget::initSponsoredButton() {
}
const auto &component = _session->sponsoredMessages();
const auto details = component.lookupDetails(_message->fullId());
_sponsoredButton = base::make_unique_q<Ui::RoundButton>(
_body,
rpl::single(details.buttonText),
st::mediaviewSponsoredButton);
_sponsoredButton = base::make_unique_q<SponsoredButton>(_body);
_sponsoredButton->setText(details.buttonText);
_sponsoredButton->setOpacity(1.0);

_sponsoredButton->setClickedCallback([=, link = details.link] {
UrlClickHandler::Open(link);
hide();
});
_sponsoredButtonOpacity = base::make_unique_q<QGraphicsOpacityEffect>(
_sponsoredButton.get());
_sponsoredButtonOpacity->setOpacity(1.0);
_sponsoredButton->setGraphicsEffect(_sponsoredButtonOpacity.get());
}

void OverlayWidget::updateThemePreviewGeometry() {
Expand Down Expand Up @@ -6299,7 +6361,6 @@ void OverlayWidget::clearBeforeHide() {
_helper->setControlsOpacity(1.);
_groupThumbs = nullptr;
_groupThumbsRect = QRect();
_sponsoredButtonOpacity = nullptr;
_sponsoredButton = nullptr;
}

Expand Down
6 changes: 2 additions & 4 deletions Telegram/SourceFiles/media/view/media_view_overlay_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ For license and copyright information please follow this link:
#include "media/view/media_view_open_common.h"
#include "media/stories/media_stories_delegate.h"

class QGraphicsOpacityEffect;

class History;

namespace anim {
Expand Down Expand Up @@ -143,6 +141,7 @@ class OverlayWidget final
class Renderer;
class RendererSW;
class RendererGL;
class SponsoredButton;

// If changing, see paintControls()!
enum class Over {
Expand Down Expand Up @@ -700,8 +699,7 @@ class OverlayWidget final
object_ptr<Ui::DropdownMenu> _dropdown;
base::Timer _dropdownShowTimer;

base::unique_qptr<Ui::RoundButton> _sponsoredButton;
base::unique_qptr<QGraphicsOpacityEffect> _sponsoredButtonOpacity;
base::unique_qptr<SponsoredButton> _sponsoredButton;

bool _receiveMouse = true;
bool _processingKeyPress = false;
Expand Down

0 comments on commit a60385f

Please sign in to comment.