Skip to content

Commit

Permalink
src/gui/widgets/mpv/mpvwidget: implement dpi scale override
Browse files Browse the repository at this point in the history
Implement DPI scaling override on Linux. This is useful for people using
fractional DPI scaling on Wayland since Wayland stores DPI scale factor
as an integer. This causes devicePixelRatio() to return the wrong value
on Wayland.

Closes #221
  • Loading branch information
ripose-jp committed Oct 14, 2024
1 parent f3084f1 commit b060a0e
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 37 deletions.
44 changes: 42 additions & 2 deletions src/gui/widgets/mpv/mpvwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,23 @@ MpvWidget::MpvWidget(QWidget *parent)
this, &MpvWidget::initTimer,
Qt::QueuedConnection
);
#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
connect(
mediator, &GlobalMediator::interfaceSettingsChanged,
this, &MpvWidget::initDimensions,
Qt::QueuedConnection
);
#endif

/* Run initialization tasks */
#if defined(Q_OS_MACOS)
m_sleepAssertIDValid = false;
#endif
initPropertyMap();
initSubtitleRegex();
#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
initDimensions();
#endif
}

MpvWidget::~MpvWidget()
Expand Down Expand Up @@ -411,6 +421,33 @@ void MpvWidget::initSubtitleRegex()
settings.endGroup();
}

#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
void MpvWidget::initDimensions()
{
QSettings settings;
settings.beginGroup(Constants::Settings::Interface::GROUP);

m_overrideDevicePixelRatio = settings.value(
Constants::Settings::Interface::DPI_SCALE_OVERRIDE,
Constants::Settings::Interface::DPI_SCALE_OVERRIDE_DEFAULT
).toBool();
if (m_overrideDevicePixelRatio)
{
m_devicePixelRatio = settings.value(
Constants::Settings::Interface::DPI_SCALE_FACTOR,
Constants::Settings::Interface::DPI_SCALE_FACTOR_DEFAULT
).toDouble();
}
else
{
m_devicePixelRatio = screen()->devicePixelRatio();
}

m_height = height() * m_devicePixelRatio;
m_width = width() * m_devicePixelRatio;
}
#endif

void MpvWidget::initTimer()
{
{
Expand Down Expand Up @@ -643,8 +680,11 @@ void MpvWidget::resizeGL(int width, int height)

void MpvWidget::screenChanged(QScreen *screen)
{
m_devicePixelRatio = screen ? screen->devicePixelRatio() : 1.0;
resizeGL(width(), height());
if (!m_overrideDevicePixelRatio)
{
m_devicePixelRatio = screen ? screen->devicePixelRatio() : 1.0;
resizeGL(width(), height());
}
}

void MpvWidget::reportFrameSwap()
Expand Down
10 changes: 10 additions & 0 deletions src/gui/widgets/mpv/mpvwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,13 @@ private Q_SLOTS:
*/
void initSubtitleRegex();

#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
/**
* Initializes screen dimensions with regards to DPI scaling.
*/
void initDimensions();
#endif

/**
* Initializes the cursor timer.
*/
Expand Down Expand Up @@ -384,6 +391,9 @@ private Q_SLOTS:
/* The DPI ratio. */
qreal m_devicePixelRatio = 1.0;

/* Override default device pixel ratio */
bool m_overrideDevicePixelRatio = false;

/* The height of the player adjusted for DPI. */
int m_height;

Expand Down
39 changes: 39 additions & 0 deletions src/gui/widgets/settings/interfacesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ InterfaceSettings::InterfaceSettings(QWidget *parent)

#if !defined(Q_OS_UNIX) || defined(Q_OS_DARWIN)
m_ui->checkSystemIcons->hide();
m_ui->widgetDpiScaling->hide();
#endif

#if !defined(Q_OS_WIN)
Expand Down Expand Up @@ -176,6 +177,16 @@ void InterfaceSettings::restoreDefaults()
);
#endif

/* DPI Scaling */
#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
m_ui->checkDpiScaleOverride->setChecked(
Constants::Settings::Interface::DPI_SCALE_OVERRIDE_DEFAULT
);
m_ui->spinDpiScaleFactor->setValue(
Constants::Settings::Interface::DPI_SCALE_FACTOR_DEFAULT
);
#endif

/* Style Sheets */
m_ui->checkStyleSheets->setChecked(
Constants::Settings::Interface::STYLESHEETS_DEFAULT
Expand Down Expand Up @@ -296,6 +307,22 @@ void InterfaceSettings::restoreSaved()
);
#endif

/* DPI Scaling */
#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
m_ui->checkDpiScaleOverride->setChecked(
settings.value(
Constants::Settings::Interface::DPI_SCALE_OVERRIDE,
Constants::Settings::Interface::DPI_SCALE_OVERRIDE_DEFAULT
).toBool()
);
m_ui->spinDpiScaleFactor->setValue(
settings.value(
Constants::Settings::Interface::DPI_SCALE_FACTOR,
Constants::Settings::Interface::DPI_SCALE_FACTOR_DEFAULT
).toDouble()
);
#endif

/* Style Sheets */
m_ui->checkStyleSheets->setChecked(
settings.value(
Expand Down Expand Up @@ -404,6 +431,18 @@ void InterfaceSettings::applyChanges()
);
#endif

/* DPI Scaling */
#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
settings.setValue(
Constants::Settings::Interface::DPI_SCALE_OVERRIDE,
m_ui->checkDpiScaleOverride->isChecked()
);
settings.setValue(
Constants::Settings::Interface::DPI_SCALE_FACTOR,
m_ui->spinDpiScaleFactor->value()
);
#endif

/* Style Sheets */
settings.setValue(
Constants::Settings::Interface::STYLESHEETS,
Expand Down
Loading

0 comments on commit b060a0e

Please sign in to comment.