Kylin/Fluent/Utilities.cpp
2024-08-31 04:13:21 +08:00

134 lines
3.7 KiB
C++

#include "Utilities.h"
#include <QSettings>
#include <qt_windows.h>
Utilities *Utilities::instance() {
static Utilities *self = nullptr;
if (self == nullptr) {
self = new Utilities();
}
return self;
}
Utilities *Utilities::create(QQmlEngine *, QJSEngine *) {
auto ret = instance();
QJSEngine::setObjectOwnership(ret, QJSEngine::CppOwnership);
return ret;
}
Utilities::Utilities(QObject *parent) : QObject{parent} {
}
bool Utilities::isSoftware() {
return QQuickWindow::sceneGraphBackend() == "software";
}
void Utilities::deleteLater(QObject *p) {
if (p) {
p->deleteLater();
}
}
QColor Utilities::withOpacity(const QColor &color, qreal opacity) {
int alpha = qRound(opacity * 255) & 0xff;
return QColor::fromRgba((alpha << 24) | (color.rgba() & 0xffffff));
}
QRect Utilities::desktopAvailableGeometry(QQuickWindow *window) {
return window->screen()->availableGeometry();
}
QString Utilities::getWallpaperFilePath() {
#if defined(Q_OS_WIN)
wchar_t path[MAX_PATH] = {};
if (::SystemParametersInfoW(SPI_GETDESKWALLPAPER, MAX_PATH, path, FALSE) == FALSE) {
return {};
}
return QString::fromWCharArray(path);
#elif defined(Q_OS_LINUX)
auto type = QSysInfo::productType();
if (type == "uos") {
QProcess process;
QStringList args;
args << "--session";
args << "--type=method_call";
args << "--print-reply";
args << "--dest=com.deepin.wm";
args << "/com/deepin/wm";
args << "com.deepin.wm.GetCurrentWorkspaceBackgroundForMonitor";
args << QString("string:'%1'").arg(currentTimestamp());
process.start("dbus-send", args);
process.waitForFinished();
QByteArray result = process.readAllStandardOutput().trimmed();
int startIndex = result.indexOf("file:///");
if (startIndex != -1) {
auto path = result.mid(startIndex + 7, result.length() - startIndex - 8);
return path;
}
}
#elif defined(Q_OS_MACOS)
QProcess process;
QStringList args;
args << "-e";
args << R"(tell application "Finder" to get POSIX path of (desktop picture as alias))";
process.start("osascript", args);
process.waitForFinished();
QByteArray result = process.readAllStandardOutput().trimmed();
if (result.isEmpty()) {
return "/System/Library/CoreServices/DefaultDesktop.heic";
}
return result;
#else
return {};
#endif
}
QUrl Utilities::getUrlByFilePath(const QString &path) {
return QUrl::fromLocalFile(path);
}
bool Utilities::isMacos() {
#if defined(Q_OS_MACOS)
return true;
#else
return false;
#endif
}
bool Utilities::isWin() {
#if defined(Q_OS_WIN)
return true;
#else
return false;
#endif
}
int Utilities::windowBuildNumber() {
#if defined(Q_OS_WIN)
QSettings regKey{QString::fromUtf8(R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion)"),
QSettings::NativeFormat};
if (regKey.contains(QString::fromUtf8("CurrentBuildNumber"))) {
auto buildNumber = regKey.value(QString::fromUtf8("CurrentBuildNumber")).toInt();
return buildNumber;
}
#endif
return -1;
}
bool Utilities::isWindows11OrGreater() {
static QVariant var;
if (var.isNull()) {
#if defined(Q_OS_WIN)
auto buildNumber = windowBuildNumber();
if (buildNumber >= 22000) {
var = QVariant::fromValue(true);
return true;
}
#endif
var = QVariant::fromValue(false);
return false;
} else {
return var.toBool();
}
}