Files
home/.local/share/plasma/plasmoids/KdeControlStation/contents/ui/js/funcs.js

169 lines
4.0 KiB
JavaScript
Raw Normal View History

2025-10-08 10:35:48 +02:00
function getBtDevice() {
2025-10-10 16:43:21 +02:00
var connectedDevices = [];
var status = {
active: false,
message: "",
};
for (var i = 0; i < btManager.devices.length; ++i) {
var device = btManager.devices[i];
if (device.connected) {
connectedDevices.push(device);
}
}
if (btManager.bluetoothBlocked) {
status.active = false;
status.message = "Disabled";
} else if (!btManager.bluetoothOperational) {
if (!btManager.adapters.length) {
status.active = false;
status.message = "Unavailable";
} else {
status.active = false;
status.message = "Offline";
}
} else if (connectedDevices.length >= 1) {
status.active = true;
status.message = connectedDevices[0].name;
} else {
status.active = true;
status.message = "Not Connected";
}
return status;
2025-10-08 10:35:48 +02:00
}
2025-10-10 16:43:21 +02:00
function toggleBluetooth() {
var enable = !btManager.bluetoothOperational;
btManager.bluetoothBlocked = !enable;
2025-10-08 10:35:48 +02:00
2025-10-10 16:43:21 +02:00
for (var i = 0; i < btManager.adapters.length; ++i) {
var adapter = btManager.adapters[i];
adapter.powered = enable;
}
2025-10-08 10:35:48 +02:00
}
function checkInhibition() {
2025-10-10 16:43:21 +02:00
var inhibited = false;
if (!NotificationManager.Server.valid) {
return false;
}
var inhibitedUntil = notificationSettings.notificationsInhibitedUntil;
if (!isNaN(inhibitedUntil.getTime())) {
inhibited |= Date.now() < inhibitedUntil.getTime();
}
if (notificationSettings.notificationsInhibitedByApplication) {
inhibited |= true;
}
if (notificationSettings.inhibitNotificationsWhenScreensMirrored) {
inhibited |= notificationSettings.screensMirrored;
}
return inhibited;
2025-10-08 10:35:48 +02:00
}
function toggleDnd() {
2025-10-10 16:43:21 +02:00
if (Funcs.checkInhibition()) {
notificationSettings.notificationsInhibitedUntil = undefined;
notificationSettings.revokeApplicationInhibitions();
2025-10-08 10:35:48 +02:00
2025-10-10 16:43:21 +02:00
// overrules current mirrored screen setup, updates again when screen configuration
notificationSettings.screensMirrored = false;
notificationSettings.save();
2025-10-08 10:35:48 +02:00
2025-10-10 16:43:21 +02:00
return;
}
2025-10-08 10:35:48 +02:00
2025-10-10 16:43:21 +02:00
var d = new Date();
d.setYear(d.getFullYear() + 1);
2025-10-08 10:35:48 +02:00
2025-10-10 16:43:21 +02:00
notificationSettings.notificationsInhibitedUntil = d;
notificationSettings.save();
2025-10-08 10:35:48 +02:00
}
function revokeInhibitions() {
2025-10-10 16:43:21 +02:00
notificationSettings.notificationsInhibitedUntil = undefined;
notificationSettings.revokeApplicationInhibitions();
// overrules current mirrored screen setup, updates again when screen configuration changes
notificationSettings.screensMirrored = false;
2025-10-08 10:35:48 +02:00
2025-10-10 16:43:21 +02:00
notificationSettings.save();
2025-10-08 10:35:48 +02:00
}
function toggleRedshiftInhibition() {
2025-10-10 16:43:21 +02:00
if (!monitor.available) {
return;
}
switch (inhibitor.state) {
case Redshift.Inhibitor.Inhibiting:
case Redshift.Inhibitor.Inhibited:
inhibitor.uninhibit();
break;
case Redshift.Inhibitor.Uninhibiting:
case Redshift.Inhibitor.Uninhibited:
inhibitor.inhibit();
break;
}
2025-10-08 10:35:48 +02:00
}
function volumePercent(volume) {
2025-10-10 16:43:21 +02:00
return (volume / Vol.PulseAudio.NormalVolume) * 100;
2025-10-08 10:35:48 +02:00
}
function boundVolume(volume) {
2025-10-10 16:43:21 +02:00
return Math.max(
Vol.PulseAudio.MinimalVolume,
Math.min(volume, Vol.PulseAudio.NormalVolume),
);
2025-10-08 10:35:48 +02:00
}
function changeVolumeByPercent(volumeObject, deltaPercent) {
2025-10-10 16:43:21 +02:00
const oldVolume = volumeObject.volume;
const oldPercent = volumePercent(oldVolume);
const targetPercent = oldPercent + deltaPercent;
const newVolume = boundVolume(
Math.round(Vol.PulseAudio.NormalVolume * (targetPercent / 100)),
);
const newPercent = volumePercent(newVolume);
volumeObject.muted = newPercent == 0;
volumeObject.volume = newVolume;
return newPercent;
2025-10-08 10:35:48 +02:00
}
function volIconName(volume, muted, prefix) {
2025-10-10 16:43:21 +02:00
if (!prefix) {
prefix = "audio-volume";
}
var icon = null;
var percent = volume / Vol.PulseAudio.NormalVolume;
if (percent <= 0.0 || muted) {
icon = prefix + "-muted";
} else if (percent <= 0.25) {
icon = prefix + "-low";
} else if (percent <= 0.75) {
icon = prefix + "-medium";
} else {
icon = prefix + "-high";
}
return icon;
2025-10-08 10:35:48 +02:00
}
function getNetworkConnectionName() {
2025-10-10 16:43:21 +02:00
var status = network.networkStatus.activeConnections;
var statusParts;
2025-10-08 10:35:48 +02:00
2025-10-10 16:43:21 +02:00
if (isAirplane) {
return "On";
}
2025-10-08 10:35:48 +02:00
2025-10-10 16:43:21 +02:00
if (status && status !== "Disconnected") {
statusParts = status.split(":");
var connectionName = statusParts[1]?.trim().split(" ").slice(2).join(" ");
return connectionName || "Connected";
}
2025-10-08 10:35:48 +02:00
2025-10-10 16:43:21 +02:00
return "Disconnected";
}