Warning, /plasma/plasma-workspace/applets/brightness/package/contents/ui/main.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2011 Sebastian Kügler <sebas@kde.org>
0003     SPDX-FileCopyrightText: 2011 Viranch Mehta <viranch.mehta@gmail.com>
0004     SPDX-FileCopyrightText: 2013-2015 Kai Uwe Broulik <kde@privat.broulik.de>
0005     SPDX-FileCopyrightText: 2021-2022 ivan tkachenko <me@ratijas.tk>
0006     SPDX-FileCopyrightText: 2023 Natalie Clarius <natalie.clarius@kde.org
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 import QtQuick
0012 import QtQuick.Layouts
0013 
0014 import org.kde.coreaddons as KCoreAddons
0015 import org.kde.kcmutils // KCMLauncher
0016 import org.kde.config // KAuthorized
0017 import org.kde.notification
0018 import org.kde.plasma.core as PlasmaCore
0019 import org.kde.plasma.plasma5support as P5Support
0020 import org.kde.plasma.plasmoid
0021 import org.kde.kirigami as Kirigami
0022 import org.kde.kitemmodels as KItemModels
0023 
0024 import org.kde.plasma.private.brightnesscontrolplugin
0025 
0026 import "logic.js" as Logic
0027 
0028 PlasmoidItem {
0029     id: brightnesscontrol
0030 
0031     property QtObject pmSource: P5Support.DataSource {
0032         id: pmSource
0033         engine: "powermanagement"
0034         connectedSources: sources
0035         onSourceAdded: source => {
0036             disconnectSource(source);
0037             connectSource(source);
0038         }
0039         onSourceRemoved: source => {
0040             disconnectSource(source);
0041         }
0042         onDataChanged: {
0043             Logic.updateBrightness(brightnesscontrol, pmSource);
0044         }
0045     }
0046     property QtObject updateScreenBrightnessJob
0047     property QtObject updateKeyboardBrightnessJob
0048 
0049     readonly property bool isScreenBrightnessAvailable: pmSource.data["PowerDevil"] && pmSource.data["PowerDevil"]["Screen Brightness Available"] ? true : false
0050     readonly property bool isKeyboardBrightnessAvailable: pmSource.data["PowerDevil"] && pmSource.data["PowerDevil"]["Keyboard Brightness Available"] ? true : false
0051     readonly property bool isBrightnessAvailable: isScreenBrightnessAvailable || isKeyboardBrightnessAvailable
0052     readonly property int maximumScreenBrightness: pmSource.data["PowerDevil"] ? pmSource.data["PowerDevil"]["Maximum Screen Brightness"] || 0 : 0
0053     readonly property int maximumKeyboardBrightness: pmSource.data["PowerDevil"] ? pmSource.data["PowerDevil"]["Maximum Keyboard Brightness"] || 0 : 0
0054 
0055     readonly property bool inPanel: (Plasmoid.location === PlasmaCore.Types.TopEdge
0056         || Plasmoid.location === PlasmaCore.Types.RightEdge
0057         || Plasmoid.location === PlasmaCore.Types.BottomEdge
0058         || Plasmoid.location === PlasmaCore.Types.LeftEdge)
0059 
0060     property bool disableBrightnessUpdate: true
0061     property int screenBrightness
0062     property int keyboardBrightness
0063     property int screenBrightnessPercent: maximumScreenBrightness ? Math.round(100 * screenBrightness / maximumScreenBrightness) : 0
0064     property int keyboardBrightnessPercent: maximumKeyboardBrightness ? Math.round(100 * keyboardBrightness / maximumKeyboardBrightness) : 0
0065 
0066     NightColorMonitor {
0067         id: nightColorMonitor
0068     }
0069     NightColorInhibitor {
0070         id: nightColorInhibitor
0071     }
0072     property bool isNightColorActive: nightColorMonitor.running && nightColorMonitor.currentTemperature != 6500
0073 
0074     function symbolicizeIconName(iconName) {
0075         const symbolicSuffix = "-symbolic";
0076         if (iconName.endsWith(symbolicSuffix)) {
0077             return iconName;
0078         }
0079 
0080         return iconName + symbolicSuffix;
0081     }
0082 
0083     switchWidth: Kirigami.Units.gridUnit * 10
0084     switchHeight: Kirigami.Units.gridUnit * 10
0085 
0086     Plasmoid.title: i18n("Brightness and Color")
0087 
0088     LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft
0089     LayoutMirroring.childrenInherit: true
0090 
0091     Plasmoid.status: {
0092         return isScreenBrightnessAvailable || isKeyboardBrightnessAvailable || isNightColorActive ? PlasmaCore.Types.ActiveStatus : PlasmaCore.Types.PassiveStatus;
0093     }
0094 
0095     toolTipMainText: {
0096         const parts = [];
0097         if (isScreenBrightnessAvailable) {
0098             parts.push(i18n("Screen brightness at %1%", screenBrightnessPercent));
0099         }
0100         if (isKeyboardBrightnessAvailable) {
0101             parts.push(i18n("Keyboard brightness at %1%", keyboardBrightnessPercent));
0102         }
0103         if (nightColorMonitor.enabled) {
0104             if (!nightColorMonitor.running) {
0105                 parts.push(i18nc("Status", "Night Light off"));
0106             } else if (nightColorMonitor.currentTemperature != 6500) {
0107                 parts.push(i18nc("Status; placeholder is a temperature", "Night Light at %1K", nightColorMonitor.currentTemperature));
0108             }
0109         }
0110 
0111         return parts.join("\n");
0112     }
0113 
0114     toolTipSubText: {
0115         const parts = [];
0116         if (isScreenBrightnessAvailable) {
0117             parts.push(i18n("Scroll to adjust screen brightness"));
0118         }
0119         if (nightColorMonitor.enabled) {
0120             parts.push(i18n("Middle-click to toggle Night Light"));
0121         }
0122         return parts.join("\n");
0123     }
0124 
0125     Plasmoid.icon: {
0126         let iconName = "brightness-high";
0127 
0128         if (nightColorMonitor.enabled) {
0129             if (!nightColorMonitor.running) {
0130                 iconName = "redshift-status-off";
0131             } else if (nightColorMonitor.currentTemperature != 6500) {
0132                 if (nightColorMonitor.daylight) {
0133                     iconName = "redshift-status-day";
0134                 } else {
0135                     iconName = "redshift-status-on";
0136                 }
0137             }
0138         }
0139 
0140         if (inPanel) {
0141             return symbolicizeIconName(iconName);
0142         }
0143 
0144         return iconName;
0145     }
0146 
0147     onScreenBrightnessChanged: {
0148         if (disableBrightnessUpdate) {
0149             return;
0150         }
0151         const service = pmSource.serviceForSource("PowerDevil");
0152         const operation = service.operationDescription("setBrightness");
0153         operation.brightness = screenBrightness;
0154         // show OSD only when the plasmoid isn't expanded since the moving slider is feedback enough
0155         operation.silent = brightnesscontrol.expanded;
0156         updateScreenBrightnessJob = service.startOperationCall(operation);
0157         updateScreenBrightnessJob.finished.connect(job => {
0158             Logic.updateBrightness(brightnesscontrol, pmSource);
0159         });
0160     }
0161 
0162     onKeyboardBrightnessChanged: {
0163         if (disableBrightnessUpdate) {
0164             return;
0165         }
0166         var service = pmSource.serviceForSource("PowerDevil");
0167         var operation = service.operationDescription("setKeyboardBrightness");
0168         operation.brightness = keyboardBrightness;
0169         // show OSD only when the plasmoid isn't expanded since the moving slider is feedback enough
0170         operation.silent = brightnesscontrol.expanded;
0171         updateKeyboardBrightnessJob = service.startOperationCall(operation);
0172         updateKeyboardBrightnessJob.finished.connect(job => {
0173             Logic.updateBrightness(brightnesscontrol, pmSource);
0174         });
0175     }
0176 
0177     compactRepresentation: CompactRepresentation {
0178         isBrightnessAvailable: brightnesscontrol.isScreenBrightnessAvailable
0179 
0180         onWheel: wheel => {
0181             if (!brightnesscontrol.isScreenBrightnessAvailable) {
0182                 return;
0183             }
0184             const delta = (wheel.inverted ? -1 : 1) * (wheel.angleDelta.y ? wheel.angleDelta.y : -wheel.angleDelta.x);
0185 
0186             const maximumBrightness = brightnesscontrol.maximumScreenBrightness
0187             // Don't allow the UI to turn off the screen
0188             // Please see https://git.reviewboard.kde.org/r/122505/ for more information
0189             const minimumBrightness = (maximumBrightness > 100 ? 1 : 0)
0190             const stepSize = Math.max(1, maximumBrightness / 20)
0191 
0192             let newBrightness;
0193             if (Math.abs(delta) < 120) {
0194                 // Touchpad scrolling
0195                 brightnessError += delta * stepSize / 120;
0196                 const change = Math.round(brightnessError);
0197                 brightnessError -= change;
0198                 newBrightness = brightnesscontrol.screenBrightness + change;
0199             } else if (wheel.modifiers & Qt.ShiftModifier) {
0200                 newBrightness = Math.round((Math.round(brightnesscontrol.screenBrightness * 100 / maximumBrightness) + delta/120) / 100 * maximumBrightness)
0201             } else {
0202                 // Discrete/wheel scrolling
0203                 newBrightness = Math.round(brightnesscontrol.screenBrightness/stepSize + delta/120) * stepSize;
0204             }
0205             brightnesscontrol.screenBrightness = Math.max(minimumBrightness, Math.min(maximumBrightness, newBrightness));
0206         }
0207 
0208         acceptedButtons: Qt.LeftButton | Qt.MiddleButton
0209         property bool wasExpanded: false
0210         onPressed: wasExpanded = brightnesscontrol.expanded
0211         onClicked: mouse => {
0212             if (mouse.button == Qt.MiddleButton) {
0213                 toggleNightColorInhibition();
0214             } else {
0215                 brightnesscontrol.expanded = !wasExpanded;
0216             }
0217         }
0218 
0219         function toggleNightColorInhibition() {
0220             if (!nightColorMonitor.available) {
0221                 return;
0222             }
0223             switch (nightColorInhibitor.state) {
0224             case NightColorInhibitor.Inhibiting:
0225             case NightColorInhibitor.Inhibited:
0226                 nightColorInhibitor.uninhibit();
0227                 break;
0228             case NightColorInhibitor.Uninhibiting:
0229             case NightColorInhibitor.Uninhibited:
0230                 nightColorInhibitor.inhibit();
0231                 break;
0232             }
0233         }
0234     }
0235 
0236     fullRepresentation: PopupDialog {
0237         id: dialogItem
0238 
0239         readonly property var appletInterface: brightnesscontrol
0240 
0241         Layout.minimumWidth: Kirigami.Units.gridUnit * 10
0242         Layout.maximumWidth: Kirigami.Units.gridUnit * 80
0243         Layout.preferredWidth: Kirigami.Units.gridUnit * 20
0244 
0245         Layout.minimumHeight: Kirigami.Units.gridUnit * 10
0246         Layout.maximumHeight: Kirigami.Units.gridUnit * 40
0247         Layout.preferredHeight: implicitHeight
0248 
0249         isBrightnessAvailable: brightnesscontrol.isBrightnessAvailable
0250         isScreenBrightnessAvailable: brightnesscontrol.isScreenBrightnessAvailable
0251         isKeyboardBrightnessAvailable: brightnesscontrol.isKeyboardBrightnessAvailable
0252     } // todo
0253 
0254     Plasmoid.contextualActions: [
0255         PlasmaCore.Action {
0256             id: configureNightLight
0257             icon.name: "configure"
0258             text: i18nc("@action:inmenu", "Configure Night Light…")
0259             visible: KAuthorized.authorize("kcm_nightcolor")
0260             priority: PlasmaCore.Action.LowPriority
0261             onTriggered: KCMLauncher.openSystemSettings("kcm_nightcolor")
0262         }
0263     ]
0264 
0265     Component.onCompleted: {
0266         Logic.updateBrightness(brightnesscontrol, pmSource);
0267         Plasmoid.removeInternalAction("configure");
0268     }
0269 }