Skip to content

Commit b7d8fbf

Browse files
wjyrich18202781743
authored andcommitted
feat: improve battery icon loading strategy
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
1 parent 1e14dbd commit b7d8fbf

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

plugins/dde-dock/power/powerapplet.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,29 @@ void PowerApplet::onHighPerformanceSupportChanged(const bool isSupport)
232232

233233
void PowerApplet::refreshBatteryIcon(const QString &icon)
234234
{
235-
QString path = ":/batteryicons/batteryicons/" + icon;
235+
QString iconName = icon;
236+
QString fallbackPath = ":/batteryicons/batteryicons/" + icon + ".svg";
237+
236238
if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) {
237-
path.append("-dark");
239+
iconName.append("-dark");
240+
fallbackPath = ":/batteryicons/batteryicons/" + icon + "-dark.svg";
241+
}
242+
243+
// 首先尝试从主题加载图标
244+
QIcon themeIcon = QIcon::fromTheme(iconName);
245+
if (!themeIcon.isNull()) {
246+
// 如果主题图标存在,使用主题图标
247+
QPixmap pixmap = themeIcon.pixmap(m_batteryIcon->size());
248+
m_batteryIcon->setPixmap(pixmap);
249+
return;
250+
}
251+
252+
// fallback使用资源文件
253+
QSvgRenderer renderer(fallbackPath);
254+
if (!renderer.isValid()) {
255+
QString normalPath = ":/batteryicons/batteryicons/" + icon + ".svg";
256+
renderer.load(normalPath);
238257
}
239-
path += ".svg";
240-
241-
QSvgRenderer renderer(path);
242258

243259
QSize size = m_batteryIcon->size();
244260
QImage image(QSize(size * devicePixelRatioF()), QImage::Format_ARGB32_Premultiplied);

plugins/dde-dock/power/powerstatuswidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void PowerStatusWidget::refreshIcon()
8282
.arg(percentageStr, plugged ? "plugged-symbolic" : "symbolic");
8383
}
8484

85-
m_iconWidget->setIcon(iconStr, ":/batteryicons/resources/batteryicons/" + iconStr + ".svg");
85+
m_iconWidget->setIcon(iconStr, ":/batteryicons/batteryicons/" + iconStr + ".svg");
8686
m_applet->refreshBatteryIcon(iconStr);
8787
}
8888

0 commit comments

Comments
 (0)