Skip to content

Commit

Permalink
[components-webview] Create screen resolution to layout.css.devPixels…
Browse files Browse the repository at this point in the history
…PerPx mapping. JB#62599

This is rather simple way to map these. At the same, there ain't
that many screen resolution that we'd like to map. Thus, this
approach is reasoble for our needs.

For testing this, if you have ran browser at least once, you need to
remove __PREFS_WRITTEN__ flag file from your device.

> rm $HOME/.local/share/org.sailfishos/browser/__PREFS_WRITTEN__

GitHub issue:
sailfishos/sailfish-browser#1085
  • Loading branch information
rainemak committed Oct 2, 2024
1 parent 1df9060 commit b94cf08
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions lib/webenginesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <QtCore/QFile>
#include <QtCore/QLocale>
#include <QtCore/QMap>
#include <QtCore/QSettings>
#include <QtCore/QSize>
#include <QtCore/QStandardPaths>
Expand All @@ -42,6 +43,11 @@ Q_GLOBAL_STATIC(SailfishOS::WebEngineSettingsPrivate, webEngineSettingsPrivateIn
Singleton class which provides access to the global Web engine settings.
*/

// Let's gather here resolution to layout.css.devPixelsPerPx mappings.
static QMap<QString, qreal> fixedDevPxMap {
{ QStringLiteral("1080x2520"), 2.608695652173913 }
};

static bool testScreenDimensions(qreal pixelRatio)
{
QScreen *screen = QGuiApplication::primaryScreen();
Expand All @@ -50,6 +56,13 @@ static bool testScreenDimensions(qreal pixelRatio)
return fmod(w, 1.0) == 0 && fmod(h, 1.0) == 0;
}

static qreal mappedPixelRatio(const QSize &screenResolution)
{
return fixedDevPxMap.value(QStringLiteral("%1x%2")
.arg(screenResolution.width())
.arg(screenResolution.height()), -1.0);
}

static quint64 getTotalMemory() {
struct sysinfo info;
sysinfo(&info);
Expand Down Expand Up @@ -191,20 +204,27 @@ void SailfishOS::WebEngineSettings::initialize()
qreal touchStartTolerance = dragThreshold / QGuiApplication::primaryScreen()->physicalDotsPerInch();
engineSettings->setPreference(QString("apz.touch_start_tolerance"), QString("%1f").arg(touchStartTolerance));

qreal pixelRatio = SAILFISH_WEBENGINE_DEFAULT_PIXEL_RATIO * silicaTheme->pixelRatio();
// Round to nearest even rounding factor
pixelRatio = qRound(pixelRatio / 0.5) * 0.5;
QSize screenResolution = QGuiApplication::primaryScreen()->size();
int screenWidth = screenResolution.width();

qreal pixelRatio = mappedPixelRatio(screenResolution);

// Try to calculate pixelRatio only if not mapped.
if (pixelRatio == -1.0) {
pixelRatio = SAILFISH_WEBENGINE_DEFAULT_PIXEL_RATIO * silicaTheme->pixelRatio();
// Round to nearest even rounding factor
pixelRatio = qRound(pixelRatio / 0.5) * 0.5;

int screenWidth = QGuiApplication::primaryScreen()->size().width();

// Do not floor the pixel ratio if the pixel ratio less than 2.0 (1.5 is minimum).
if (pixelRatio >= 2.0 && !testScreenDimensions(pixelRatio)) {
qreal tempPixelRatio = qFloor(pixelRatio);
if (testScreenDimensions(tempPixelRatio)) {
pixelRatio = tempPixelRatio;
// Do not floor the pixel ratio if the pixel ratio less than 2.0 (1.5 is minimum).
if (pixelRatio >= 2.0 && !testScreenDimensions(pixelRatio)) {
qreal tempPixelRatio = qFloor(pixelRatio);
if (testScreenDimensions(tempPixelRatio)) {
pixelRatio = tempPixelRatio;
}
} else if (screenWidth >= 1080) {
pixelRatio = qRound(pixelRatio);
}
} else if (screenWidth >= 1080) {
pixelRatio = qRound(pixelRatio);
}

engineSettings->setPixelRatio(pixelRatio);
Expand Down

0 comments on commit b94cf08

Please sign in to comment.