Skip to content

Commit

Permalink
Set default zoom of webview based on DPI.
Browse files Browse the repository at this point in the history
Control zoom factor directly rather than using hardcoded zoom levels.

In order to ensure that the webview starts out scaled appropriately to the
current font DPI, we set default zoom level based on current DPI, e.g.:

  96 DPI - 100% zoom
  120 DPI - 125% zoom

Since this will allow zoom levels beyond the predefined set, I also change the
logic for zooming to simply add or remove 10% to the current scaling.
  • Loading branch information
yiding committed Feb 9, 2019
1 parent 4fd2723 commit a6191fa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 58 deletions.
10 changes: 0 additions & 10 deletions src/libs/browser/webcontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ WebControl::WebControl(QWidget *parent)

}

int WebControl::zoomLevel() const
{
return m_webView->zoomLevel();
}

void WebControl::setZoomLevel(int level)
{
m_webView->setZoomLevel(level);
}

void WebControl::zoomIn()
{
m_webView->zoomIn();
Expand Down
3 changes: 0 additions & 3 deletions src/libs/browser/webcontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ class WebControl : public QWidget
QWebHistory *history() const;
void restoreHistory(const QByteArray &array);
QByteArray saveHistory() const;

int zoomLevel() const;
void setZoomLevel(int level);
void setJavaScriptEnabled(bool enabled);

void setWebBridgeObject(const QString &name, QObject *object);
Expand Down
51 changes: 13 additions & 38 deletions src/libs/browser/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,59 +38,33 @@

using namespace Zeal::Browser;

namespace {
const qreal MIN_ZOOM_FACTOR = 0.3;
}

WebView::WebView(QWidget *parent)
: QWebView(parent)
{
page()->setNetworkAccessManager(Core::Application::instance()->networkManager());
}

int WebView::zoomLevel() const
{
return m_zoomLevel;
}

void WebView::setZoomLevel(int level)
{
if (level == m_zoomLevel) {
return;
}

level = qMax(0, level);
level = qMin(level, availableZoomLevels().size() - 1);

m_zoomLevel = level;

setZoomFactor(availableZoomLevels().at(level) / 100.0);
emit zoomLevelChanged();
}

const QVector<int> &WebView::availableZoomLevels()
{
static const QVector<int> zoomLevels = {30, 40, 50, 67, 80, 90, 100,
110, 120, 133, 150, 170, 200,
220, 233, 250, 270, 285, 300};
return zoomLevels;
}

const int &WebView::defaultZoomLevel()
{
static const int level = availableZoomLevels().indexOf(100);
return level;
resetZoom();
}

void WebView::zoomIn()
{
setZoomLevel(m_zoomLevel + 1);
setZoomFactor(zoomFactor() + 0.1);
}

void WebView::zoomOut()
{
setZoomLevel(m_zoomLevel - 1);
if (zoomFactor() <= MIN_ZOOM_FACTOR) {
return;
}
setZoomFactor(zoomFactor() - 0.1);
}

void WebView::resetZoom()
{
setZoomLevel(defaultZoomLevel());
setZoomFactor(logicalDpiY() / 96.0f);
}

QWebView *WebView::createWindow(QWebPage::WebWindowType type)
Expand Down Expand Up @@ -291,7 +265,8 @@ void WebView::wheelEvent(QWheelEvent *event)
delta -= 120 * direction;
}

setZoomLevel(m_zoomLevel + levelDelta);
qreal newZoomFactor = zoomFactor() + levelDelta * 0.1f;
setZoomFactor(qMax(newZoomFactor, MIN_ZOOM_FACTOR));
event->accept();
return;
}
Expand Down
7 changes: 0 additions & 7 deletions src/libs/browser/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ class WebView : public QWebView
public:
explicit WebView(QWidget *parent = nullptr);

int zoomLevel() const;
void setZoomLevel(int level);

static const QVector<int> &availableZoomLevels();
static const int &defaultZoomLevel();

public slots:
void zoomIn();
void zoomOut();
Expand All @@ -65,7 +59,6 @@ public slots:

QMenu *m_contextMenu = nullptr;
QUrl m_clickedLink;
int m_zoomLevel = defaultZoomLevel();
};

} // namespace Browser
Expand Down

0 comments on commit a6191fa

Please sign in to comment.