Skip to content

Commit

Permalink
fix: [extension] auto zoom web view
Browse files Browse the repository at this point in the history
Log:
Change-Id: Ib1755c34dfd394675631d76371c663697ba709d7
  • Loading branch information
deepin-mozart committed Apr 3, 2024
1 parent 54e629a commit 8270604
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
35 changes: 34 additions & 1 deletion src/plugins/core/gui/plugindetailsview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,43 @@
#include <QDesktopServices>
#include <QWebEngineView>
#include <QDir>
#include <QApplication>
#include <QMainWindow>

DWIDGET_USE_NAMESPACE
using namespace dpfservice;

class AutoZoomWebEngineView : public QWebEngineView {
public:
explicit AutoZoomWebEngineView(QWidget *parent = nullptr)
: QWebEngineView(parent) {}

protected:
void resizeEvent(QResizeEvent *event) override {
QWebEngineView::resizeEvent(event);

// first resize is full screen
if (isFirstResize) {
isFirstResize = false;
return;
}

QSize newSize = event->size();
qreal zoomFactor = calculateZoomFactor(newSize);
setZoomFactor(zoomFactor);
}

qreal calculateZoomFactor(const QSize &size) {
if (size.width() > maxWidth)
maxWidth = size.width();
qreal zoomFactor = static_cast<qreal>(size.width()) / maxWidth;
return zoomFactor;
}
private:
int maxWidth = 0;
bool isFirstResize = true;
};

DetailsView::DetailsView(QWidget *parent)
: DWidget(parent)
{
Expand Down Expand Up @@ -135,7 +168,7 @@ void DetailsView::setupUi()
logoLabel->setPixmap(logo.pixmap(QSize(96, 96)));

auto webViewLayout = new QHBoxLayout();
webView = new QWebEngineView(this);
webView = new AutoZoomWebEngineView(this);
webView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
webViewLayout->addWidget(webView);

Expand Down
5 changes: 3 additions & 2 deletions src/plugins/core/gui/plugindetailsview.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ QT_BEGIN_NAMESPACE
class QGridLayout;
class QVBoxLayout;
class QSpacerItem;
class QWebEngineView;
class QLabel;
QT_END_NAMESPACE

DWIDGET_BEGIN_NAMESPACE
class DTextEdit;
class DLabel;
DWIDGET_END_NAMESPACE

class AutoZoomWebEngineView;
class DetailsView : public DTK_WIDGET_NAMESPACE::DWidget
{
Q_OBJECT
Expand Down Expand Up @@ -50,7 +51,7 @@ private slots:

DTK_WIDGET_NAMESPACE::DPushButton *loadBtn {nullptr};
dpf::PluginMetaObjectPointer pluginMetaInfo;
QWebEngineView *webView {nullptr};
AutoZoomWebEngineView *webView {nullptr};
QLabel *logoLabel {nullptr};
};

Expand Down

0 comments on commit 8270604

Please sign in to comment.