diff options
author | Hannah von Reth <hannah.vonreth@owncloud.com> | 2021-01-21 19:57:14 +0300 |
---|---|---|
committer | Hannah von Reth <vonreth@kde.org> | 2021-01-25 17:51:45 +0300 |
commit | d6dc2abef6e90449ead6d37a872a620e90d54298 (patch) | |
tree | 6b822efa25a9b55778020b1fe9f6f7dfd604f878 /src | |
parent | cf9322c2bd4cf5637e2a6d300170014a7704b229 (diff) |
Fix branding fallback
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/guiutility.cpp | 2 | ||||
-rw-r--r-- | src/libsync/theme.cpp | 72 | ||||
-rw-r--r-- | src/libsync/theme.h | 15 |
3 files changed, 67 insertions, 22 deletions
diff --git a/src/gui/guiutility.cpp b/src/gui/guiutility.cpp index ead4a78ad..c5a94724e 100644 --- a/src/gui/guiutility.cpp +++ b/src/gui/guiutility.cpp @@ -105,7 +105,7 @@ QIcon Utility::getCoreIcon(const QString &icon_name) if (icon_name.isEmpty()) { return {}; } - const QString path = Theme::instance()->isUsingDarkTheme() ? QStringLiteral("dark") : QStringLiteral("light"); + const QString path = (Theme::instance()->isUsingDarkTheme() && Theme::instance()->hasTheme(Theme::IconType::VanillaIcon, QStringLiteral("dark"))) ? QStringLiteral("dark") : QStringLiteral("light"); const QIcon icon(QStringLiteral(":/client/resources/%1/%2").arg(path, icon_name)); Q_ASSERT(!icon.isNull()); return icon; diff --git a/src/libsync/theme.cpp b/src/libsync/theme.cpp index 949571c25..c7353c00f 100644 --- a/src/libsync/theme.cpp +++ b/src/libsync/theme.cpp @@ -38,10 +38,41 @@ #endif namespace { +QString vanillaThemePath() +{ + return QStringLiteral(":/client/ownCloud/theme"); +} + +QString brandThemePath() +{ + return QStringLiteral(":/client/" APPLICATION_SHORTNAME "/theme"); +} + +QString darkTheme() +{ + return QStringLiteral("dark"); +} + +QString coloredTheme() +{ + return QStringLiteral("colored"); +} + +QString whiteTheme() +{ + return QStringLiteral("white"); +} + +QString blackTheme() +{ + return QStringLiteral("black"); +} + constexpr bool strings_equal(char const *a, char const *b) { return *a == *b && (*a == '\0' || strings_equal(a + 1, b + 1)); } + constexpr bool isVanilla() { // TODO: c++17 stringview @@ -137,12 +168,11 @@ QIcon Theme::aboutIcon() const return applicationIcon(); } -bool Theme::isUsingDarkTheme(IconType type) const +bool Theme::isUsingDarkTheme() const { - if (!_hasDarkColoredTheme && type != IconType::VanillaIcon) { - return false; - } - return QPalette().base().color().lightnessF() <= 0.5; + //TODO: replace by a command line switch + static bool forceDark = qEnvironmentVariableIsSet("OWNCLOUD_FORCE_DARK_MODE"); + return forceDark || QPalette().base().color().lightnessF() <= 0.5; } /* * helper to load a icon from either the icon theme the desktop provides or from @@ -150,18 +180,19 @@ bool Theme::isUsingDarkTheme(IconType type) const */ QIcon Theme::themeIcon(const QString &name, bool sysTray, bool sysTrayMenuVisible, IconType iconType) const { + // prevent recusion const bool useCoreIcon = (iconType == IconType::VanillaIcon) || isVanilla(); QString flavor; if (sysTray) { - flavor = systrayIconFlavor(_mono, sysTrayMenuVisible); + flavor = systrayIconFlavor(iconType, _mono, sysTrayMenuVisible); } else { - if (isUsingDarkTheme(iconType)) { - flavor = QStringLiteral("dark"); + if (isUsingDarkTheme() && hasTheme(iconType, darkTheme())) { + flavor = darkTheme(); } else { - flavor = QStringLiteral("colored"); + flavor = coloredTheme(); } } - const QString path = useCoreIcon ? QStringLiteral(":/client/ownCloud/theme") : QStringLiteral(":/client/%1/theme").arg(appName()); + const QString path = useCoreIcon ? vanillaThemePath() : brandThemePath(); const QString key = name + QLatin1Char(',') + flavor; QIcon &cached = _iconCache[key]; // Take reference, this will also "set" the cache entry if (cached.isNull()) { @@ -210,17 +241,26 @@ QIcon Theme::themeIcon(const QString &name, bool sysTray, bool sysTrayMenuVisibl return cached; } -bool Theme::hasTheme(const QString &theme) +bool Theme::hasTheme(IconType type, const QString &theme) const { - return QFileInfo(QStringLiteral(":/client/" APPLICATION_SHORTNAME "/theme/%1/").arg(theme)).isDir(); + const auto key = qMakePair(type != IconType::VanillaIcon, theme); + auto it = _themeCache.find(key); + if (it == _themeCache.end()) { + bool found = QFileInfo(QStringLiteral("%1/%2/").arg(type == IconType::VanillaIcon ? vanillaThemePath() : brandThemePath(), theme)).isDir(); + if (!found && type == IconType::BrandedIconWithFallbackToVanillaIcon) { + found = hasTheme(IconType::VanillaIcon, theme); + } + return _themeCache[key] = found; + } + return it.value(); } -QString Theme::systrayIconFlavor(bool mono, bool sysTrayMenuVisible) const +QString Theme::systrayIconFlavor(IconType type, bool mono, bool sysTrayMenuVisible) const { Q_UNUSED(sysTrayMenuVisible) QString flavor; if (mono) { - flavor = Utility::hasDarkSystray() ? QStringLiteral("white") : QStringLiteral("black"); + flavor = Utility::hasDarkSystray() ? whiteTheme() : blackTheme(); #ifdef Q_OS_MAC if (sysTrayMenuVisible) { @@ -229,7 +269,7 @@ QString Theme::systrayIconFlavor(bool mono, bool sysTrayMenuVisible) const #endif } else { // we have a dark sys tray and the theme has support for that - flavor = (Utility::hasDarkSystray() && _hasDarkColoredTheme) ? QStringLiteral("dark") : QStringLiteral("colored"); + flavor = (Utility::hasDarkSystray() && hasTheme(type, darkTheme())) ? darkTheme() : coloredTheme(); } return flavor; } @@ -303,7 +343,7 @@ bool Theme::systrayUseMonoIcons() const bool Theme::monoIconsAvailable() const { - return hasTheme(systrayIconFlavor(true)); + return hasTheme(IconType::BrandedIcon, systrayIconFlavor(IconType::BrandedIcon, true)); } QString Theme::updateCheckUrl() const diff --git a/src/libsync/theme.h b/src/libsync/theme.h index 843f7a4bb..ad8feb3fd 100644 --- a/src/libsync/theme.h +++ b/src/libsync/theme.h @@ -104,6 +104,7 @@ public: BrandedIconWithFallbackToVanillaIcon, VanillaIcon }; + /** * get an sync state icon */ @@ -118,7 +119,7 @@ public: * Whether use the dark icon theme * The function also ensures the theme supports the dark theme */ - bool isUsingDarkTheme(IconType fallbackType = IconType::BrandedIcon) const; + bool isUsingDarkTheme() const; #endif virtual QString statusHeaderText(SyncResult::Status) const; @@ -188,7 +189,7 @@ public: #ifndef TOKEN_AUTH_ONLY /** colored, white or black */ - QString systrayIconFlavor(bool mono, bool sysTrayMenuVisible = false) const; + QString systrayIconFlavor(IconType type, bool mono, bool sysTrayMenuVisible = false) const; /** * Override to use a string or a custom image name. @@ -406,10 +407,12 @@ public: */ virtual bool enableExperimentalFeatures() const; + // whether or not a theme is available + bool hasTheme(IconType type, const QString &theme) const; + protected: #ifndef TOKEN_AUTH_ONLY QIcon themeIcon(const QString &name, bool sysTray = false, bool sysTrayMenuVisible = false, IconType iconType = IconType::BrandedIconWithFallbackToVanillaIcon) const; - static bool hasTheme(const QString &theme); #endif Theme(); @@ -423,8 +426,10 @@ private: static Theme *_instance; bool _mono; #ifndef TOKEN_AUTH_ONLY - bool _hasDarkColoredTheme = hasTheme(QStringLiteral("dark")); - mutable QHash<QString, QIcon> _iconCache; + // <is vanilla, theme name> + mutable QMap<QPair<bool, QString>, bool> _themeCache; + + mutable QMap<QString, QIcon> _iconCache; #endif }; } |