PowerProfile: create PowerProfileService, use it for the BarWidget and

PowerProfilesCard
This commit is contained in:
Ly-sec 2025-09-09 17:12:56 +02:00
parent 3d51f758f8
commit 144406ae0e
3 changed files with 80 additions and 29 deletions

View file

@ -11,8 +11,7 @@ NIconButton {
property ShellScreen screen property ShellScreen screen
property real scaling: 1.0 property real scaling: 1.0
property var powerProfiles: PowerProfiles readonly property bool hasPP: PowerProfileService.available
readonly property bool hasPP: powerProfiles.hasPerformanceProfile
sizeRatio: 0.8 sizeRatio: 0.8
visible: hasPP visible: hasPP
@ -20,34 +19,29 @@ NIconButton {
function profileIcon() { function profileIcon() {
if (!hasPP) if (!hasPP)
return "yin-yang" return "yin-yang"
if (powerProfiles.profile === PowerProfile.Performance) if (PowerProfileService.profile === PowerProfile.Performance)
return "speedometer2" return "speedometer2"
if (powerProfiles.profile === PowerProfile.Balanced) if (PowerProfileService.profile === PowerProfile.Balanced)
return "yin-yang" return "yin-yang"
if (powerProfiles.profile === PowerProfile.PowerSaver) if (PowerProfileService.profile === PowerProfile.PowerSaver)
return "leaf" return "leaf"
} }
function profileName() { function profileName() {
if (!hasPP) if (!hasPP)
return "Unknown" return "Unknown"
if (powerProfiles.profile === PowerProfile.Performance) if (PowerProfileService.profile === PowerProfile.Performance)
return "Performance" return "Performance"
if (powerProfiles.profile === PowerProfile.Balanced) if (PowerProfileService.profile === PowerProfile.Balanced)
return "Balanced" return "Balanced"
if (powerProfiles.profile === PowerProfile.PowerSaver) if (PowerProfileService.profile === PowerProfile.PowerSaver)
return "Power Saver" return "Power Saver"
} }
function changeProfile() { function changeProfile() {
if (!hasPP) if (!hasPP)
return return
if (powerProfiles.profile === PowerProfile.Performance) PowerProfileService.cycleProfile()
powerProfiles.profile = PowerProfile.PowerSaver
else if (powerProfiles.profile === PowerProfile.Balanced)
powerProfiles.profile = PowerProfile.Performance
else if (powerProfiles.profile === PowerProfile.PowerSaver)
powerProfiles.profile = PowerProfile.Balanced
} }
icon: root.profileIcon() icon: root.profileIcon()

View file

@ -13,9 +13,8 @@ NBox {
Layout.preferredWidth: 1 Layout.preferredWidth: 1
implicitHeight: powerRow.implicitHeight + Style.marginM * 2 * scaling implicitHeight: powerRow.implicitHeight + Style.marginM * 2 * scaling
// PowerProfiles service // Centralized service
property var powerProfiles: PowerProfiles readonly property bool hasPP: PowerProfileService.available
readonly property bool hasPP: powerProfiles.hasPerformanceProfile
property real spacing: 0 property real spacing: 0
RowLayout { RowLayout {
@ -32,12 +31,12 @@ NBox {
tooltipText: "Set performance power profile." tooltipText: "Set performance power profile."
enabled: hasPP enabled: hasPP
opacity: enabled ? Style.opacityFull : Style.opacityMedium opacity: enabled ? Style.opacityFull : Style.opacityMedium
colorBg: (enabled && powerProfiles.profile === PowerProfile.Performance) ? Color.mPrimary : Color.mSurfaceVariant colorBg: (enabled
colorFg: (enabled && powerProfiles.profile === PowerProfile.Performance) ? Color.mOnPrimary : Color.mPrimary && PowerProfileService.profile === PowerProfile.Performance) ? Color.mPrimary : Color.mSurfaceVariant
colorFg: (enabled && PowerProfileService.profile === PowerProfile.Performance) ? Color.mOnPrimary : Color.mPrimary
onClicked: { onClicked: {
if (enabled) { if (enabled) {
powerProfiles.profile = PowerProfile.Performance PowerProfileService.setProfile(PowerProfile.Performance)
ToastService.showNotice("Power Profile", "Performance")
} }
} }
} }
@ -47,12 +46,12 @@ NBox {
tooltipText: "Set balanced power profile." tooltipText: "Set balanced power profile."
enabled: hasPP enabled: hasPP
opacity: enabled ? Style.opacityFull : Style.opacityMedium opacity: enabled ? Style.opacityFull : Style.opacityMedium
colorBg: (enabled && powerProfiles.profile === PowerProfile.Balanced) ? Color.mPrimary : Color.mSurfaceVariant colorBg: (enabled
colorFg: (enabled && powerProfiles.profile === PowerProfile.Balanced) ? Color.mOnPrimary : Color.mPrimary && PowerProfileService.profile === PowerProfile.Balanced) ? Color.mPrimary : Color.mSurfaceVariant
colorFg: (enabled && PowerProfileService.profile === PowerProfile.Balanced) ? Color.mOnPrimary : Color.mPrimary
onClicked: { onClicked: {
if (enabled) { if (enabled) {
powerProfiles.profile = PowerProfile.Balanced PowerProfileService.setProfile(PowerProfile.Balanced)
ToastService.showNotice("Power Profile", "Balanced")
} }
} }
} }
@ -62,12 +61,12 @@ NBox {
tooltipText: "Set eco power profile." tooltipText: "Set eco power profile."
enabled: hasPP enabled: hasPP
opacity: enabled ? Style.opacityFull : Style.opacityMedium opacity: enabled ? Style.opacityFull : Style.opacityMedium
colorBg: (enabled && powerProfiles.profile === PowerProfile.PowerSaver) ? Color.mPrimary : Color.mSurfaceVariant colorBg: (enabled
colorFg: (enabled && powerProfiles.profile === PowerProfile.PowerSaver) ? Color.mOnPrimary : Color.mPrimary && PowerProfileService.profile === PowerProfile.PowerSaver) ? Color.mPrimary : Color.mSurfaceVariant
colorFg: (enabled && PowerProfileService.profile === PowerProfile.PowerSaver) ? Color.mOnPrimary : Color.mPrimary
onClicked: { onClicked: {
if (enabled) { if (enabled) {
powerProfiles.profile = PowerProfile.PowerSaver PowerProfileService.setProfile(PowerProfile.PowerSaver)
ToastService.showNotice("Power Profile", "Power Saver")
} }
} }
} }

View file

@ -0,0 +1,58 @@
pragma Singleton
import QtQuick
import Quickshell
import Quickshell.Services.UPower
import qs.Commons
import qs.Services
Singleton {
id: root
readonly property var powerProfiles: PowerProfiles
readonly property bool available: powerProfiles && powerProfiles.hasPerformanceProfile
property int profile: powerProfiles ? powerProfiles.profile : PowerProfile.Balanced
function profileName(p) {
const prof = (p !== undefined) ? p : profile
if (!available)
return "Unknown"
if (prof === PowerProfile.Performance)
return "Performance"
if (prof === PowerProfile.Balanced)
return "Balanced"
if (prof === PowerProfile.PowerSaver)
return "Power Saver"
return "Unknown"
}
function setProfile(p) {
if (!available)
return
try {
powerProfiles.profile = p
} catch (e) {
Logger.error("PowerProfileService", "Failed to set profile:", e)
}
}
function cycleProfile() {
if (!available)
return
const current = powerProfiles.profile
if (current === PowerProfile.Performance)
setProfile(PowerProfile.PowerSaver)
else if (current === PowerProfile.Balanced)
setProfile(PowerProfile.Performance)
else if (current === PowerProfile.PowerSaver)
setProfile(PowerProfile.Balanced)
}
Connections {
target: powerProfiles
function onProfileChanged() {
root.profile = powerProfiles.profile
ToastService.showNotice("Power Profile", root.profileName())
}
}
}