From 45b05b64adb3dae396814925bbfa78de6b3f45c2 Mon Sep 17 00:00:00 2001 From: wjyrich Date: Thu, 7 Aug 2025 15:47:40 +0800 Subject: [PATCH] feat: improve battery icon loading strategy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Modified battery icon loading to first try system theme icons before falling back to built-in resources 2. Added proper handling for dark theme variants by appending "-dark" suffix 3. Improved SVG renderer fallback logic when theme icons are not available 4. Fixed resource path inconsistency in PowerStatusWidget 5. Added proper error handling for invalid SVG files The changes improve icon loading reliability and maintainability by: - Supporting system theme icons which allows for better customization - Providing proper fallback mechanisms when theme icons are missing - Ensuring consistent behavior across light/dark themes - Fixing resource path inconsistencies between components feat: 改进电池图标加载策略 1. 修改电池图标加载逻辑,优先尝试系统主题图标,再回退到内置资源 2. 添加了对暗色主题变体的正确处理,通过添加"-dark"后缀 3. 改进了当主题图标不可用时的SVG渲染器回退逻辑 4. 修复了PowerStatusWidget中的资源路径不一致问题 5. 为无效SVG文件添加了适当的错误处理 这些改进通过以下方式提高了图标加载的可靠性和可维护性: - 支持系统主题图标,允许更好的自定义 - 当主题图标缺失时提供适当的回退机制 - 确保在亮/暗主题下行为一致 - 修复组件间的资源路径不一致问题 Pms: BUG-323767 --- plugins/dde-dock/power/powerapplet.cpp | 26 ++++++++++++++++---- plugins/dde-dock/power/powerstatuswidget.cpp | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/plugins/dde-dock/power/powerapplet.cpp b/plugins/dde-dock/power/powerapplet.cpp index 731028c65..15e3ff3b0 100644 --- a/plugins/dde-dock/power/powerapplet.cpp +++ b/plugins/dde-dock/power/powerapplet.cpp @@ -232,13 +232,29 @@ void PowerApplet::onHighPerformanceSupportChanged(const bool isSupport) void PowerApplet::refreshBatteryIcon(const QString &icon) { - QString path = ":/batteryicons/batteryicons/" + icon; + QString iconName = icon; + QString fallbackPath = ":/batteryicons/batteryicons/" + icon + ".svg"; + if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) { - path.append("-dark"); + iconName.append("-dark"); + fallbackPath = ":/batteryicons/batteryicons/" + icon + "-dark.svg"; + } + + // 首先尝试从主题加载图标 + QIcon themeIcon = QIcon::fromTheme(iconName); + if (!themeIcon.isNull()) { + // 如果主题图标存在,使用主题图标 + QPixmap pixmap = themeIcon.pixmap(m_batteryIcon->size()); + m_batteryIcon->setPixmap(pixmap); + return; + } + + // fallback使用资源文件 + QSvgRenderer renderer(fallbackPath); + if (!renderer.isValid()) { + QString normalPath = ":/batteryicons/batteryicons/" + icon + ".svg"; + renderer.load(normalPath); } - path += ".svg"; - - QSvgRenderer renderer(path); QSize size = m_batteryIcon->size(); QImage image(QSize(size * devicePixelRatioF()), QImage::Format_ARGB32_Premultiplied); diff --git a/plugins/dde-dock/power/powerstatuswidget.cpp b/plugins/dde-dock/power/powerstatuswidget.cpp index 56ad831e2..c766b40a6 100644 --- a/plugins/dde-dock/power/powerstatuswidget.cpp +++ b/plugins/dde-dock/power/powerstatuswidget.cpp @@ -82,7 +82,7 @@ void PowerStatusWidget::refreshIcon() .arg(percentageStr, plugged ? "plugged-symbolic" : "symbolic"); } - m_iconWidget->setIcon(iconStr, ":/batteryicons/resources/batteryicons/" + iconStr + ".svg"); + m_iconWidget->setIcon(iconStr, ":/batteryicons/batteryicons/" + iconStr + ".svg"); m_applet->refreshBatteryIcon(iconStr); }