mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-06-19 09:35:22 -04:00
feat(ipc): add powerprofile status & shared profile helpers
- Follow-up to PR #2515
This commit is contained in:
+24
-37
@@ -1894,7 +1894,7 @@ Item {
|
||||
|
||||
IpcHandler {
|
||||
function open(): string {
|
||||
if (typeof PowerProfiles === "undefined")
|
||||
if (!PowerProfileWatcher.available)
|
||||
return "ERROR: power-profiles-daemon not available";
|
||||
|
||||
PopoutService.openPowerProfileModal();
|
||||
@@ -1907,7 +1907,7 @@ Item {
|
||||
}
|
||||
|
||||
function toggle(): string {
|
||||
if (typeof PowerProfiles === "undefined")
|
||||
if (!PowerProfileWatcher.available)
|
||||
return "ERROR: power-profiles-daemon not available";
|
||||
|
||||
PopoutService.togglePowerProfileModal();
|
||||
@@ -1915,59 +1915,46 @@ Item {
|
||||
}
|
||||
|
||||
function list(): string {
|
||||
if (typeof PowerProfiles === "undefined")
|
||||
if (!PowerProfileWatcher.available)
|
||||
return "ERROR: power-profiles-daemon not available";
|
||||
|
||||
const profiles = ["power-saver", "balanced"];
|
||||
if (PowerProfiles.hasPerformanceProfile)
|
||||
profiles.push("performance");
|
||||
return PowerProfileWatcher.availableProfiles.map(profile => PowerProfileWatcher.profileSlug(profile)).join("\n");
|
||||
}
|
||||
|
||||
return profiles.join("\n");
|
||||
function status(): string {
|
||||
if (!PowerProfileWatcher.available)
|
||||
return "ERROR: power-profiles-daemon not available";
|
||||
|
||||
return PowerProfileWatcher.profileSlug(PowerProfiles.profile);
|
||||
}
|
||||
|
||||
function set(profile: string): string {
|
||||
if (typeof PowerProfiles === "undefined")
|
||||
if (!PowerProfileWatcher.available)
|
||||
return "ERROR: power-profiles-daemon not available";
|
||||
|
||||
if (!profile)
|
||||
return "ERROR: No profile specified";
|
||||
|
||||
const lower = profile.toLowerCase().trim();
|
||||
if (lower === "power-saver" || lower === "powersaver" || lower === "saver" || lower === "0") {
|
||||
PowerProfiles.profile = PowerProfile.PowerSaver;
|
||||
return "POWERPROFILE_SET_SUCCESS";
|
||||
} else if (lower === "balanced" || lower === "1") {
|
||||
PowerProfiles.profile = PowerProfile.Balanced;
|
||||
return "POWERPROFILE_SET_SUCCESS";
|
||||
} else if (lower === "performance" || lower === "2") {
|
||||
if (PowerProfiles.hasPerformanceProfile) {
|
||||
PowerProfiles.profile = PowerProfile.Performance;
|
||||
return "POWERPROFILE_SET_SUCCESS";
|
||||
} else {
|
||||
return "ERROR: Performance profile not supported by hardware";
|
||||
}
|
||||
} else {
|
||||
const parsed = PowerProfileWatcher.parseProfileSlug(profile);
|
||||
if (parsed === -1)
|
||||
return "ERROR: Unknown power profile. Supported options: power-saver, balanced, performance";
|
||||
}
|
||||
|
||||
if (parsed === PowerProfile.Performance && !PowerProfiles.hasPerformanceProfile)
|
||||
return "ERROR: Performance profile not supported by hardware";
|
||||
|
||||
if (!PowerProfileWatcher.applyProfile(parsed))
|
||||
return "ERROR: Failed to set power profile";
|
||||
|
||||
return "POWERPROFILE_SET_SUCCESS";
|
||||
}
|
||||
|
||||
function cycle(): string {
|
||||
if (typeof PowerProfiles === "undefined")
|
||||
if (!PowerProfileWatcher.available)
|
||||
return "ERROR: power-profiles-daemon not available";
|
||||
|
||||
const current = PowerProfiles.profile;
|
||||
const profiles = [PowerProfile.PowerSaver, PowerProfile.Balanced];
|
||||
if (PowerProfiles.hasPerformanceProfile)
|
||||
profiles.push(PowerProfile.Performance);
|
||||
if (!PowerProfileWatcher.cycleProfile())
|
||||
return "ERROR: Failed to set power profile";
|
||||
|
||||
const index = profiles.indexOf(current);
|
||||
if (index === -1) {
|
||||
PowerProfiles.profile = PowerProfile.Balanced;
|
||||
return "POWERPROFILE_CYCLE_SUCCESS";
|
||||
}
|
||||
|
||||
const nextIndex = (index + 1) % profiles.length;
|
||||
PowerProfiles.profile = profiles[nextIndex];
|
||||
return "POWERPROFILE_CYCLE_SUCCESS";
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ DankModal {
|
||||
keepPopoutsOpen: true
|
||||
|
||||
property int selectedIndex: 0
|
||||
property var profileModel: (typeof PowerProfiles !== "undefined") ? [PowerProfile.PowerSaver, PowerProfile.Balanced].concat(PowerProfiles.hasPerformanceProfile ? [PowerProfile.Performance] : []) : [PowerProfile.PowerSaver, PowerProfile.Balanced, PowerProfile.Performance]
|
||||
property var profileModel: PowerProfileWatcher.availableProfiles
|
||||
|
||||
function openCentered() {
|
||||
open();
|
||||
@@ -100,10 +100,15 @@ DankModal {
|
||||
}
|
||||
|
||||
function setProfile(profile) {
|
||||
if (typeof PowerProfiles !== "undefined") {
|
||||
PowerProfiles.profile = profile;
|
||||
if (PowerProfileWatcher.applyProfile(profile)) {
|
||||
hideDialog();
|
||||
return;
|
||||
}
|
||||
hideDialog();
|
||||
|
||||
if (!PowerProfileWatcher.available)
|
||||
ToastService.showError(I18n.tr("power-profiles-daemon not available"));
|
||||
else
|
||||
ToastService.showError(I18n.tr("Failed to set power profile"));
|
||||
}
|
||||
|
||||
content: Component {
|
||||
|
||||
@@ -24,14 +24,13 @@ Rectangle {
|
||||
}
|
||||
|
||||
function setProfile(profile) {
|
||||
if (typeof PowerProfiles === "undefined") {
|
||||
ToastService.showError(I18n.tr("power-profiles-daemon not available"));
|
||||
if (PowerProfileWatcher.applyProfile(profile))
|
||||
return;
|
||||
}
|
||||
PowerProfiles.profile = profile;
|
||||
if (PowerProfiles.profile !== profile) {
|
||||
|
||||
if (!PowerProfileWatcher.available)
|
||||
ToastService.showError(I18n.tr("power-profiles-daemon not available"));
|
||||
else
|
||||
ToastService.showError(I18n.tr("Failed to set power profile"));
|
||||
}
|
||||
}
|
||||
|
||||
Column {
|
||||
@@ -193,7 +192,7 @@ Rectangle {
|
||||
}
|
||||
|
||||
DankButtonGroup {
|
||||
property var profileModel: (typeof PowerProfiles !== "undefined") ? [PowerProfile.PowerSaver, PowerProfile.Balanced].concat(PowerProfiles.hasPerformanceProfile ? [PowerProfile.Performance] : []) : [PowerProfile.PowerSaver, PowerProfile.Balanced, PowerProfile.Performance]
|
||||
property var profileModel: PowerProfileWatcher.availableProfiles
|
||||
property int currentProfileIndex: {
|
||||
if (typeof PowerProfiles === "undefined")
|
||||
return 1;
|
||||
|
||||
@@ -21,14 +21,13 @@ DankPopout {
|
||||
}
|
||||
|
||||
function setProfile(profile) {
|
||||
if (typeof PowerProfiles === "undefined") {
|
||||
ToastService.showError(I18n.tr("power-profiles-daemon not available"));
|
||||
if (PowerProfileWatcher.applyProfile(profile))
|
||||
return;
|
||||
}
|
||||
PowerProfiles.profile = profile;
|
||||
if (PowerProfiles.profile !== profile) {
|
||||
|
||||
if (!PowerProfileWatcher.available)
|
||||
ToastService.showError(I18n.tr("power-profiles-daemon not available"));
|
||||
else
|
||||
ToastService.showError(I18n.tr("Failed to set power profile"));
|
||||
}
|
||||
}
|
||||
|
||||
popupWidth: 400
|
||||
@@ -555,7 +554,7 @@ DankPopout {
|
||||
DankButtonGroup {
|
||||
id: profileButtonGroup
|
||||
|
||||
property var profileModel: (typeof PowerProfiles !== "undefined") ? [PowerProfile.PowerSaver, PowerProfile.Balanced].concat(PowerProfiles.hasPerformanceProfile ? [PowerProfile.Performance] : []) : [PowerProfile.PowerSaver, PowerProfile.Balanced, PowerProfile.Performance]
|
||||
property var profileModel: PowerProfileWatcher.availableProfiles
|
||||
property int currentProfileIndex: {
|
||||
if (typeof PowerProfiles === "undefined")
|
||||
return 1;
|
||||
|
||||
@@ -140,30 +140,24 @@ BasePill {
|
||||
log.info("Trigger! Delta: " + delta);
|
||||
|
||||
// This is after the other delta checks so it only shows on valid Y scroll
|
||||
if (typeof PowerProfiles === "undefined") {
|
||||
if (!PowerProfileWatcher.available) {
|
||||
ToastService.showError(I18n.tr("power-profiles-daemon not available"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Get list of profiles, and current index
|
||||
const profiles = [PowerProfile.PowerSaver, PowerProfile.Balanced].concat(PowerProfiles.hasPerformanceProfile ? [PowerProfile.Performance] : []);
|
||||
const profiles = PowerProfileWatcher.availableProfiles;
|
||||
var index = profiles.findIndex(profile => PowerProfiles.profile === profile);
|
||||
|
||||
// Step once based on mouse wheel direction
|
||||
if (delta > 0)
|
||||
index += 1;
|
||||
else
|
||||
index -= 1;
|
||||
|
||||
// Already at end of list, can't go further
|
||||
if (index < 0 || index >= profiles.length)
|
||||
return;
|
||||
|
||||
// Set new profile
|
||||
PowerProfiles.profile = profiles[index];
|
||||
if (PowerProfiles.profile !== profiles[index]) {
|
||||
if (!PowerProfileWatcher.applyProfile(profiles[index]))
|
||||
ToastService.showError(I18n.tr("Failed to set power profile"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,68 @@ Singleton {
|
||||
property int currentProfile: -1
|
||||
property int previousProfile: -1
|
||||
|
||||
readonly property bool available: typeof PowerProfiles !== "undefined"
|
||||
|
||||
readonly property var availableProfiles: {
|
||||
if (!available)
|
||||
return [PowerProfile.PowerSaver, PowerProfile.Balanced, PowerProfile.Performance];
|
||||
|
||||
return [PowerProfile.PowerSaver, PowerProfile.Balanced].concat(PowerProfiles.hasPerformanceProfile ? [PowerProfile.Performance] : []);
|
||||
}
|
||||
|
||||
signal profileChanged(int profile)
|
||||
|
||||
function profileSlug(profile: int): string {
|
||||
switch (profile) {
|
||||
case PowerProfile.PowerSaver:
|
||||
return "power-saver";
|
||||
case PowerProfile.Balanced:
|
||||
return "balanced";
|
||||
case PowerProfile.Performance:
|
||||
return "performance";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
function parseProfileSlug(slug: string): int {
|
||||
if (!slug)
|
||||
return -1;
|
||||
|
||||
const lower = slug.toLowerCase().trim();
|
||||
if (lower === "power-saver" || lower === "powersaver" || lower === "saver" || lower === "0")
|
||||
return PowerProfile.PowerSaver;
|
||||
if (lower === "balanced" || lower === "1")
|
||||
return PowerProfile.Balanced;
|
||||
if (lower === "performance" || lower === "2")
|
||||
return PowerProfile.Performance;
|
||||
return -1;
|
||||
}
|
||||
|
||||
function applyProfile(profile: int): bool {
|
||||
if (!available)
|
||||
return false;
|
||||
|
||||
if (profile === PowerProfile.Performance && !PowerProfiles.hasPerformanceProfile)
|
||||
return false;
|
||||
|
||||
if (availableProfiles.indexOf(profile) === -1)
|
||||
return false;
|
||||
|
||||
PowerProfiles.profile = profile;
|
||||
return PowerProfiles.profile === profile;
|
||||
}
|
||||
|
||||
function cycleProfile(): bool {
|
||||
if (!available)
|
||||
return false;
|
||||
|
||||
const profiles = availableProfiles;
|
||||
const index = profiles.indexOf(PowerProfiles.profile);
|
||||
const nextProfile = index === -1 ? PowerProfile.Balanced : profiles[(index + 1) % profiles.length];
|
||||
return applyProfile(nextProfile);
|
||||
}
|
||||
|
||||
Connections {
|
||||
target: typeof PowerProfiles !== "undefined" ? PowerProfiles : null
|
||||
|
||||
|
||||
Reference in New Issue
Block a user