Files
DankMaterialShell/quickshell/Modules/DankIsland/DmsSystemActivitySource.qml
T

146 lines
5.0 KiB
QML

pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import qs.Common
import qs.Services
QtObject {
id: root
required property IslandController controller
property string kind: "volume"
property real demoRatio: 0.68
readonly property bool demoEnabled: Quickshell.env("DMS_DANKISLAND_SYSTEM_DEMO") === "1"
readonly property bool volumeActivity: kind === "volume"
readonly property bool available: demoEnabled || (volumeActivity ? !!AudioService.sink?.audio : DisplayService.brightnessAvailable)
readonly property bool muted: !demoEnabled && volumeActivity && (AudioService.sink?.audio?.muted ?? false)
readonly property real value: demoEnabled ? demoRatio * 100 : volumeActivity ? Math.min(AudioService.sinkMaxVolume, Math.round((AudioService.sink?.audio?.volume ?? 0) * 100)) : DisplayService.brightnessLevel
readonly property real maximum: {
if (demoEnabled)
return 100;
if (volumeActivity)
return AudioService.sinkMaxVolume;
const deviceInfo = DisplayService.getCurrentDeviceInfo();
if (!deviceInfo)
return 100;
if (SessionData.getBrightnessExponential(deviceInfo.id))
return 100;
return deviceInfo.displayMax || 100;
}
readonly property int minimum: {
if (volumeActivity || demoEnabled)
return 0;
const deviceInfo = DisplayService.getCurrentDeviceInfo();
if (!deviceInfo)
return 1;
if (SessionData.getBrightnessExponential(deviceInfo.id))
return 1;
return (deviceInfo.class === "backlight" || deviceInfo.class === "ddc") ? 1 : 0;
}
readonly property real ratio: maximum > 0 ? Math.max(0, Math.min(1, value / maximum)) : 0
readonly property string title: volumeActivity ? "Volume" : "Brightness"
readonly property string unit: {
if (volumeActivity)
return "%";
const deviceInfo = demoEnabled ? null : DisplayService.getCurrentDeviceInfo();
if (!deviceInfo)
return "%";
if (SessionData.getBrightnessExponential(deviceInfo.id))
return "%";
return deviceInfo.class === "ddc" ? "" : "%";
}
readonly property string displayValue: muted ? I18n.tr("Muted") : Math.round(value) + unit
readonly property string iconName: {
if (!volumeActivity) {
const deviceInfo = demoEnabled ? null : DisplayService.getCurrentDeviceInfo();
if (!demoEnabled && (!available || !deviceInfo))
return "brightness_low";
if (demoEnabled || deviceInfo.class === "backlight" || deviceInfo.class === "ddc") {
if (ratio <= 0.33)
return "brightness_low";
return ratio <= 0.66 ? "brightness_medium" : "brightness_high";
}
if (String(deviceInfo.name ?? "").includes("kbd"))
return "keyboard";
return "lightbulb";
}
if (!available || muted)
return "volume_off";
const volume = demoEnabled ? ratio : (AudioService.sink?.audio?.volume ?? 0);
if (volume === 0)
return "volume_mute";
return volume <= 0.33 ? "volume_down" : "volume_up";
}
function show(activityKind) {
if (controller.requestSystemActivity(activityKind))
kind = activityKind;
}
function setRatio(nextRatio) {
const clampedRatio = Math.max(0, Math.min(1, nextRatio));
if (demoEnabled) {
demoRatio = clampedRatio;
return;
}
SessionData.suppressOSDTemporarily();
if (volumeActivity)
AudioService.setVolume(Math.round(clampedRatio * maximum));
else
DisplayService.setBrightness(Math.round(clampedRatio * maximum), DisplayService.lastIpcDevice, true);
}
function toggleMute() {
if (!volumeActivity || !available)
return;
if (demoEnabled)
return;
SessionData.suppressOSDTemporarily();
AudioService.toggleMute();
}
property Connections audioConnection: Connections {
target: AudioService.sink?.audio ?? null
function onVolumeChanged() {
if (SettingsData.osdVolumeEnabled)
root.show("volume");
}
function onMutedChanged() {
if (SettingsData.osdVolumeEnabled)
root.show("volume");
}
}
property Connections brightnessConnection: Connections {
target: DisplayService
function onBrightnessChanged(showOsd) {
if (showOsd && SettingsData.osdBrightnessEnabled)
root.show("brightness");
}
}
property Timer demoTimer: Timer {
interval: 3200
repeat: true
running: root.demoEnabled
onTriggered: {
const nextKind = root.kind === "volume" ? "brightness" : "volume";
root.demoRatio = nextKind === "volume" ? 0.68 : 0.82;
root.show(nextKind);
}
}
Component.onCompleted: {
if (demoEnabled)
show("volume");
}
}