From 2e9a81251375377d228920e7d1320ca649122f96 Mon Sep 17 00:00:00 2001 From: LemmyCook Date: Wed, 17 Sep 2025 09:30:23 -0400 Subject: [PATCH] PowerProfile: Standardization + Factorisation. Fix #307 --- Modules/Bar/Widgets/PowerProfile.qml | 37 ++---------------- Modules/SidePanel/Cards/PowerProfilesCard.qml | 30 +++++--------- Services/PowerProfileService.qml | 39 +++++++++++++++---- 3 files changed, 44 insertions(+), 62 deletions(-) diff --git a/Modules/Bar/Widgets/PowerProfile.qml b/Modules/Bar/Widgets/PowerProfile.qml index 2ddda3f7..529cba54 100644 --- a/Modules/Bar/Widgets/PowerProfile.qml +++ b/Modules/Bar/Widgets/PowerProfile.qml @@ -11,45 +11,16 @@ NIconButton { property ShellScreen screen property real scaling: 1.0 - readonly property bool hasPP: PowerProfileService.available baseSize: Style.capsuleHeight - visible: hasPP + visible: PowerProfileService.available - function profileIcon() { - if (!hasPP) - return "balanced" - if (PowerProfileService.profile === PowerProfile.Performance) - return "performance" - if (PowerProfileService.profile === PowerProfile.Balanced) - return "balanced" - if (PowerProfileService.profile === PowerProfile.PowerSaver) - return "powersaver" - } - - function profileName() { - if (!hasPP) - return "Unknown" - if (PowerProfileService.profile === PowerProfile.Performance) - return "Performance" - if (PowerProfileService.profile === PowerProfile.Balanced) - return "Balanced" - if (PowerProfileService.profile === PowerProfile.PowerSaver) - return "Power Saver" - } - - function changeProfile() { - if (!hasPP) - return - PowerProfileService.cycleProfile() - } - - icon: root.profileIcon() - tooltipText: root.profileName() + icon: PowerProfileService.getIcon() + tooltipText: `Current power profile is "${PowerProfileService.getName()}".` compact: (Settings.data.bar.density === "compact") colorBg: (PowerProfileService.profile === PowerProfile.Balanced) ? (Settings.data.bar.showCapsule ? Color.mSurfaceVariant : Color.transparent) : Color.mPrimary colorFg: (PowerProfileService.profile === PowerProfile.Balanced) ? Color.mOnSurface : Color.mOnPrimary colorBorder: Color.transparent colorBorderHover: Color.transparent - onClicked: root.changeProfile() + onClicked: PowerProfileService.cycleProfile() } diff --git a/Modules/SidePanel/Cards/PowerProfilesCard.qml b/Modules/SidePanel/Cards/PowerProfilesCard.qml index bbfe0704..9baf745e 100644 --- a/Modules/SidePanel/Cards/PowerProfilesCard.qml +++ b/Modules/SidePanel/Cards/PowerProfilesCard.qml @@ -25,45 +25,33 @@ NBox { } // Performance NIconButton { - icon: "performance" - tooltipText: "Set performance power profile." + icon: PowerProfileService.getIcon(PowerProfile.Performance) + tooltipText: `Set "${PowerProfileService.getName(PowerProfile.Performance)}" power profile.` enabled: hasPP opacity: enabled ? Style.opacityFull : Style.opacityMedium colorBg: (enabled && PowerProfileService.profile === PowerProfile.Performance) ? Color.mPrimary : Color.mSurfaceVariant colorFg: (enabled && PowerProfileService.profile === PowerProfile.Performance) ? Color.mOnPrimary : Color.mPrimary - onClicked: { - if (enabled) { - PowerProfileService.setProfile(PowerProfile.Performance) - } - } + onClicked: PowerProfileService.setProfile(PowerProfile.Performance) } // Balanced NIconButton { - icon: "balanced" - tooltipText: "Set balanced power profile." + icon: PowerProfileService.getIcon(PowerProfile.Balanced) + tooltipText: `Set "${PowerProfileService.getName(PowerProfile.Balanced)}" power profile.` enabled: hasPP opacity: enabled ? Style.opacityFull : Style.opacityMedium colorBg: (enabled && PowerProfileService.profile === PowerProfile.Balanced) ? Color.mPrimary : Color.mSurfaceVariant colorFg: (enabled && PowerProfileService.profile === PowerProfile.Balanced) ? Color.mOnPrimary : Color.mPrimary - onClicked: { - if (enabled) { - PowerProfileService.setProfile(PowerProfile.Balanced) - } - } + onClicked: PowerProfileService.setProfile(PowerProfile.Balanced) } // Eco NIconButton { - icon: "powersaver" - tooltipText: "Set eco power profile." + icon: PowerProfileService.getIcon(PowerProfile.PowerSaver) + tooltipText: `Set "${PowerProfileService.getName(PowerProfile.PowerSaver)}" power profile.` enabled: hasPP opacity: enabled ? Style.opacityFull : Style.opacityMedium colorBg: (enabled && PowerProfileService.profile === PowerProfile.PowerSaver) ? Color.mPrimary : Color.mSurfaceVariant colorFg: (enabled && PowerProfileService.profile === PowerProfile.PowerSaver) ? Color.mOnPrimary : Color.mPrimary - onClicked: { - if (enabled) { - PowerProfileService.setProfile(PowerProfile.PowerSaver) - } - } + onClicked: PowerProfileService.setProfile(PowerProfile.PowerSaver) } Item { Layout.fillWidth: true diff --git a/Services/PowerProfileService.qml b/Services/PowerProfileService.qml index 1447227b..64f1e860 100644 --- a/Services/PowerProfileService.qml +++ b/Services/PowerProfileService.qml @@ -13,17 +13,40 @@ Singleton { readonly property bool available: powerProfiles && powerProfiles.hasPerformanceProfile property int profile: powerProfiles ? powerProfiles.profile : PowerProfile.Balanced - function profileName(p) { - const prof = (p !== undefined) ? p : profile + function getName(p) { if (!available) return "Unknown" - if (prof === PowerProfile.Performance) + + const prof = (p !== undefined) ? p : profile + + switch (prof) { + case PowerProfile.Performance: return "Performance" - if (prof === PowerProfile.Balanced) + case PowerProfile.Balanced: return "Balanced" - if (prof === PowerProfile.PowerSaver) + case PowerProfile.PowerSaver: return "Power Saver" - return "Unknown" + default: + return "Unknown" + } + } + + function getIcon(p) { + if (!available) + return "balanced" + + const prof = (p !== undefined) ? p : profile + + switch (prof) { + case PowerProfile.Performance: + return "performance" + case PowerProfile.Balanced: + return "balanced" + case PowerProfile.PowerSaver: + return "powersaver" + default: + return "balanced" + } } function setProfile(p) { @@ -53,9 +76,9 @@ Singleton { function onProfileChanged() { root.profile = powerProfiles.profile // Only show toast if we have a valid profile name (not "Unknown") - const profileName = root.profileName() + const profileName = root.getName() if (profileName !== "Unknown") { - ToastService.showNotice("Power Profile", profileName) + ToastService.showNotice("Power Profile Changed", `"${profileName}"`) } } }