refact: add environ and remove scaleFactor property

- remove scaleFactor property !
- add environ property to set scaleFactor easily
This commit is contained in:
ck 2024-04-02 11:28:33 +08:00 committed by Comix
parent 02bebd4ef0
commit 144461c379
4 changed files with 30 additions and 95 deletions

View File

@ -76,11 +76,12 @@
/>
</property>
<property name="ScaleFactor" type="d" access="readwrite">
<property name="Environ" type="s" access="readwrite">
<annotation
name="org.freedesktop.DBus.Description"
value="Indicate the scale factor of application's instance.
Note: Set `0` to use global scaleFactor"
value="Indicate the environment of application's instance.
passing some specific environment variables to Launch
this application, eg. 'LANG=en_US;PATH=xxx:yyy;'"
/>
</property>

View File

@ -57,7 +57,7 @@ constexpr auto AppExecOption = u8"appExec";
constexpr auto STORAGE_VERSION = 0;
constexpr auto ApplicationPropertiesGroup = u8"Application Properties";
constexpr auto LastLaunchedTime = u8"LastLaunchedTime";
constexpr auto ScaleFactor = u8"ScaleFactor";
constexpr auto Environ = u8"Environ";
constexpr auto LaunchedTimes = u8"LaunchedTimes";
constexpr auto InstalledTime = u8"InstalledTime";

View File

@ -32,51 +32,16 @@
#include <utility>
#include <wordexp.h>
double getScaleFactor() noexcept
{
auto sessionBus = QDBusConnection::sessionBus();
QDBusMessage reply1 = sessionBus.call(QDBusMessage::createMethodCall(
"org.deepin.dde.XSettings1", "/org/deepin/dde/XSettings1", "org.deepin.dde.XSettings1", "GetScaleFactor"));
if (reply1.type() != QDBusMessage::ReplyMessage) {
qWarning() << "call GetScaleFactor Failed:" << reply1.errorMessage();
return 1.0;
}
QDBusReply<double> ret1(reply1);
double scale = ret1.isValid() ? ret1.value() : 1.0;
scale = scale > 0 ? scale : 1;
return scale;
}
void ApplicationService::appendExtraEnvironments(QVariantMap &runtimeOptions) const noexcept
{
QString oldEnv;
// scale factor
static QStringList scaleEnvs{"DEEPIN_WINE_SCALE=%1;", "QT_SCALE_FACTOR=%1;"};
auto factor = scaleFactor();
if (auto it = runtimeOptions.find("env"); it != runtimeOptions.cend()) {
oldEnv = it->value<QString>();
}
for (const auto &env : scaleEnvs) {
oldEnv.append(env.arg(factor));
}
// FIX #7528 GTK app only scale the UI, not scaling of text.
// GDK_SCALE Must be set to an integer, see https://docs.gtk.org/gtk3/x11.html
// 1.25 ==> 1 , 1.75 ==>2
int scale = qRound(factor);
oldEnv.append(QString("GDK_SCALE=%1;").arg(scale));
if (scale > 0) { // avoid division by 0
oldEnv.append(QString("GDK_DPI_SCALE=%1;").arg(qreal(1.0 / scale)));
}
// GIO
oldEnv.append(QString{"GIO_LAUNCHED_DESKTOP_FILE=%1;"}.arg(m_desktopSource.sourcePath()));
// set for dxcb platform plugin, which should use scale factor directly instead of processing double times.
oldEnv.append(QString{"D_DXCB_FORCE_OVERRIDE_HIDPI=1"});
const QString &env = environ();
if (!env.isEmpty())
oldEnv.prepend(env);
runtimeOptions.insert("env", oldEnv);
}
@ -85,7 +50,6 @@ ApplicationService::ApplicationService(DesktopFile source,
ApplicationManager1Service *parent,
std::weak_ptr<ApplicationManager1Storage> storage)
: QObject(parent)
, m_scaleFactor(getScaleFactor())
, m_storage(std::move(storage))
, m_desktopSource(std::move(source))
{
@ -139,30 +103,9 @@ ApplicationService::ApplicationService(DesktopFile source,
m_launchedTimes = value.toLongLong();
}
value = storagePtr->readApplicationValue(appId, ApplicationPropertiesGroup, ScaleFactor);
value = storagePtr->readApplicationValue(appId, ApplicationPropertiesGroup, ::Environ);
if (!value.isNull()) {
bool ok{false};
auto tmp = value.toDouble(&ok);
if (ok) {
m_scaleFactor = tmp;
m_customScale = true;
}
}
if (!QDBusConnection::sessionBus().connect("org.deepin.dde.XSettings1",
"/org/deepin/dde/XSettings1",
"org.deepin.dde.XSettings1",
"SetScaleFactorDone",
this,
SLOT(onGlobalScaleFactorChanged()))) {
qWarning() << "connect to org.deepin.dde.XSettings1 failed, scaleFactor is invalid.";
}
}
void ApplicationService::onGlobalScaleFactorChanged() noexcept
{
if (!m_customScale) {
m_scaleFactor = getScaleFactor();
m_environ = value.toString();
}
}
@ -606,13 +549,17 @@ qint64 ApplicationService::launchedTimes() const noexcept
return m_launchedTimes;
}
double ApplicationService::scaleFactor() const noexcept
QString ApplicationService::environ() const noexcept
{
return m_scaleFactor;
return m_environ;
}
void ApplicationService::setScaleFactor(double value) noexcept
void ApplicationService::setEnviron(const QString &value) noexcept
{
if (environ().trimmed() == value.trimmed()) {
return;
}
auto storagePtr = m_storage.lock();
if (!storagePtr) {
qCritical() << "broken storage.";
@ -621,28 +568,19 @@ void ApplicationService::setScaleFactor(double value) noexcept
}
auto appId = id();
if (value == 0) {
m_customScale = false;
m_scaleFactor = getScaleFactor();
if (!storagePtr->deleteApplicationValue(appId, ApplicationPropertiesGroup, ScaleFactor)) {
qCritical() << "failed to delete app's property:" << appId;
}
return;
}
if (m_customScale) {
if (!storagePtr->updateApplicationValue(appId, ApplicationPropertiesGroup, ScaleFactor, value)) {
sendErrorReply(QDBusError::Failed, "update scaleFactor failed.");
if (!m_environ.isEmpty()) {
if (!storagePtr->updateApplicationValue(appId, ApplicationPropertiesGroup, Environ, value)) {
sendErrorReply(QDBusError::Failed, "update environ failed.");
return;
}
} else {
if (!storagePtr->createApplicationValue(appId, ApplicationPropertiesGroup, ScaleFactor, value)) {
sendErrorReply(QDBusError::Failed, "set scaleFactor failed.");
if (!storagePtr->createApplicationValue(appId, ApplicationPropertiesGroup, Environ, value)) {
sendErrorReply(QDBusError::Failed, "set environ failed.");
}
}
m_scaleFactor = value;
emit scaleFactorChanged();
m_environ = value;
emit environChanged();
}
bool ApplicationService::autostartCheck(const QString &filePath) const noexcept
@ -907,7 +845,7 @@ void ApplicationService::resetEntry(DesktopEntry *newEntry) noexcept
emit categoriesChanged();
emit MimeTypesChanged();
emit terminalChanged();
emit scaleFactorChanged();
emit environChanged();
emit launchedTimesChanged();
}

View File

@ -67,9 +67,9 @@ public:
Q_PROPERTY(qint64 LaunchedTimes READ launchedTimes NOTIFY launchedTimesChanged)
[[nodiscard]] qint64 launchedTimes() const noexcept;
Q_PROPERTY(double ScaleFactor READ scaleFactor WRITE setScaleFactor NOTIFY scaleFactorChanged)
[[nodiscard]] double scaleFactor() const noexcept;
void setScaleFactor(double value) noexcept;
Q_PROPERTY(QString Environ READ environ WRITE setEnviron NOTIFY environChanged)
[[nodiscard]] QString environ() const noexcept;
void setEnviron(const QString &value) noexcept;
Q_PROPERTY(bool Terminal READ terminal NOTIFY terminalChanged)
[[nodiscard]] bool terminal() const noexcept;
@ -134,9 +134,6 @@ public:
[[nodiscard]] LaunchTask unescapeExec(const QString &str, const QStringList &fields) noexcept;
[[nodiscard]] static std::optional<QStringList> unescapeExecArgs(const QString &str) noexcept;
private Q_SLOTS:
void onGlobalScaleFactorChanged() noexcept;
public Q_SLOTS:
// NOTE: 'realExec' only for internal implementation
QDBusObjectPath
@ -164,7 +161,7 @@ Q_SIGNALS:
void categoriesChanged();
void MimeTypesChanged();
void terminalChanged();
void scaleFactorChanged();
void environChanged();
void launchedTimesChanged();
private:
@ -174,11 +171,10 @@ private:
std::weak_ptr<ApplicationManager1Storage> storage);
static QSharedPointer<ApplicationService> createApplicationService(
DesktopFile source, ApplicationManager1Service *parent, std::weak_ptr<ApplicationManager1Storage> storage) noexcept;
bool m_customScale{false};
qint64 m_lastLaunch{0};
qint64 m_installedTime{0};
qint64 m_launchedTimes{0};
double m_scaleFactor;
QString m_environ;
std::weak_ptr<ApplicationManager1Storage> m_storage;
AutostartSource m_autostartSource;
QDBusObjectPath m_applicationPath;