| 1 | #include "./desktoputils.h" |
| 2 | |
| 3 | #include <QDesktopServices> |
| 4 | #include <QUrl> |
| 5 | |
| 6 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) |
| 7 | #include <QGuiApplication> |
| 8 | #include <QStyleHints> |
| 9 | #endif |
| 10 | |
| 11 | #ifdef Q_OS_WIN32 |
| 12 | #include <QFileInfo> |
| 13 | #endif |
| 14 | |
| 15 | #if defined(Q_OS_ANDROID) && (QT_VERSION >= QT_VERSION_CHECK(6, 4, 0)) |
| 16 | #define QT_UTILITIES_OPEN_VIA_ACTIVITY |
| 17 | #include <QCoreApplication> |
| 18 | #include <QJniEnvironment> |
| 19 | #include <QJniObject> |
| 20 | #endif |
| 21 | |
| 22 | namespace QtUtilities { |
| 23 | |
| 24 | /*! |
| 25 | * \brief Shows the specified file or directory using the default file browser. |
| 26 | * \remarks |
| 27 | * - The specified \a path must *not* be specified as URL. (The conversion to a URL suitable for |
| 28 | * QDesktopServices::openUrl() is the main purpose of this function). |
| 29 | * - The Qt documentation suggests to use |
| 30 | * `QDesktopServices::openUrl(QUrl("file:///C:/Documents and Settings/All Users/Desktop", QUrl::TolerantMode));` |
| 31 | * under Windows. However, that does not work if the path contains a '#'. It is also better to use |
| 32 | * QUrl::DecodedMode to prevent QUrl from interpreting any of the paths characters in a special way. |
| 33 | * - Tries to open the path via the activity method `openPath()` under Android so that method can be implemented |
| 34 | * to override the behavior if needed. The method must take a string and return a boolean. If it does not exist |
| 35 | * the usual QDesktopServices::openUrl() is done. |
| 36 | */ |
| 37 | bool openLocalFileOrDir(const QString &path) |
| 38 | { |
| 39 | #ifdef QT_UTILITIES_OPEN_VIA_ACTIVITY |
| 40 | if (const auto context = QNativeInterface::QAndroidApplication::context(); context.isValid()) { |
| 41 | auto env = QJniEnvironment(); |
| 42 | if (auto openPathMethod = env.findMethod(context.objectClass(), "openPath", "(Ljava/lang/String;)Z")) { |
| 43 | return env->CallBooleanMethod(context.object(), openPathMethod, QJniObject::fromString(path).object()) == JNI_TRUE; |
| 44 | } |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | QUrl url(QStringLiteral("file://")); |
| 49 | |
| 50 | #ifdef Q_OS_WIN32 |
| 51 | // replace backslashes with regular slashes |
| 52 | QString tmp(path); |
| 53 | tmp.replace(QChar('\\'), QChar('/')); |
| 54 | |
| 55 | // add a slash before the drive letter of an absolute path |
| 56 | if (QFileInfo(path).isAbsolute()) { |
| 57 | tmp = QStringLiteral("/") + tmp; |
| 58 | } |
| 59 | url.setPath(tmp, QUrl::DecodedMode); |
| 60 | |
| 61 | #else |
| 62 | url.setPath(path, QUrl::DecodedMode); |
| 63 | #endif |
| 64 | return QDesktopServices::openUrl(url); |
| 65 | } |
| 66 | |
| 67 | /*! |
| 68 | * \brief Returns whether \a palette is dark. |
| 69 | */ |
| 70 | bool isPaletteDark(const QPalette &palette) |
| 71 | { |
| 72 | return palette.color(QPalette::WindowText).lightness() > palette.color(QPalette::Window).lightness(); |
| 73 | } |
| 74 | |
| 75 | /*! |
| 76 | * \brief Returns whether dark mode is enabled. |
| 77 | * \remarks Whether dark mode is enabled can only be determined on Qt 6.5 or newer and only on certain |
| 78 | * platforms. If it cannot be determined, std::nullopt is returned. |
| 79 | */ |
| 80 | std::optional<bool> isDarkModeEnabled() |
| 81 | { |
| 82 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) |
| 83 | if (const auto *const styleHints = QGuiApplication::styleHints()) { |
| 84 | const auto colorScheme = styleHints->colorScheme(); |
| 85 | if (colorScheme != Qt::ColorScheme::Unknown) { |
| 86 | return colorScheme == Qt::ColorScheme::Dark; |
| 87 | } |
| 88 | } |
| 89 | #endif |
| 90 | return std::nullopt; |
| 91 | } |
| 92 | |
| 93 | /*! |
| 94 | * \brief Invokes the specified callback when the color scheme changed. |
| 95 | * |
| 96 | * The first argument of the callback will be whether the color scheme is dark now (Qt::ColorScheme::Dark). |
| 97 | * The callback is invoked immediately by this function unless \a invokeImmediately is set to false. |
| 98 | * |
| 99 | * \remarks Whether dark mode is enabled can only be determined on Qt 6.5 or newer and only on certain |
| 100 | * platforms. If it cannot be determined the \a darkModeChangedCallback is never invoked. |
| 101 | */ |
| 102 | QMetaObject::Connection onDarkModeChanged(std::function<void(bool)> &&darkModeChangedCallback, QObject *context, bool invokeImmediately) |
| 103 | { |
| 104 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)) |
| 105 | if (const auto *const styleHints = QGuiApplication::styleHints()) { |
| 106 | if (invokeImmediately) { |
| 107 | darkModeChangedCallback(styleHints->colorScheme() == Qt::ColorScheme::Dark); |
| 108 | } |
| 109 | return QObject::connect(styleHints, &QStyleHints::colorSchemeChanged, context, |
| 110 | [handler = std::move(darkModeChangedCallback)](Qt::ColorScheme colorScheme) { return handler(colorScheme == Qt::ColorScheme::Dark); }); |
| 111 | } |
| 112 | #else |
| 113 | Q_UNUSED(darkModeChangedCallback) |
| 114 | Q_UNUSED(context) |
| 115 | Q_UNUSED(invokeImmediately) |
| 116 | #endif |
| 117 | return QMetaObject::Connection(); |
| 118 | } |
| 119 | |
| 120 | } // namespace QtUtilities |