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

0001 /*
0002  *   SPDX-FileCopyrightText: 2016 Michael Abrahams <miabraha@gmail.com>
0003  *
0004  *   SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 import QtQuick 2.15
0008 import org.kde.plasma.plasmoid 2.0
0009 import org.kde.plasma.core as PlasmaCore
0010 import org.kde.kirigami 2.20 as Kirigami
0011 import org.kde.plasma.plasma5support 2.0 as P5Support
0012 import org.kde.kquickcontrolsaddons 2.0 as QtExtra
0013 import org.kde.plasma.private.timer 0.1 as TimerPlasmoid
0014 import org.kde.notification 1.0
0015 
0016 PlasmoidItem {
0017     id: root;
0018 
0019     switchWidth: Kirigami.Units.gridUnit * 8
0020     switchHeight: Kirigami.Units.gridUnit * 4
0021 
0022     readonly property bool inPanel: [PlasmaCore.Types.TopEdge, PlasmaCore.Types.RightEdge, PlasmaCore.Types.BottomEdge, PlasmaCore.Types.LeftEdge]
0023         .includes(Plasmoid.location)
0024     readonly property bool isVertical: Plasmoid.formFactor === PlasmaCore.Types.Vertical
0025 
0026     readonly property variant predefinedTimers: plasmoid.configuration.predefinedTimers;
0027 
0028     Plasmoid.backgroundHints: PlasmaCore.Types.ShadowBackground | PlasmaCore.Types.ConfigurableBackground
0029 
0030     // Display remaining time (hours and minutes) (default: enabled)
0031     readonly property bool showRemainingTime: Plasmoid.configuration.showRemainingTime
0032 
0033     // Display seconds in addition to hours and minutes (default: enabled)
0034     readonly property bool showSeconds: Plasmoid.configuration.showSeconds
0035     property int seconds : restoreToSeconds(plasmoid.configuration.running, plasmoid.configuration.savedAt, plasmoid.configuration.seconds);
0036 
0037     // Display timer toggle control (default: enabled)
0038     readonly property bool showTimerToggle: Plasmoid.configuration.showTimerToggle
0039 
0040     // Display progress bar (default: disabled)
0041     readonly property bool showProgressBar: Plasmoid.configuration.showProgressBar
0042 
0043     // show notification on timer completion (default: enabled)
0044     property bool showNotification: plasmoid.configuration.showNotification;
0045     // run custom command on timer completion (default: disabled)
0046     property bool runCommand: plasmoid.configuration.runCommand;
0047     property string command: plasmoid.configuration.command;
0048 
0049     // show title (can be customized in the settings dialog, default: disabled)
0050     readonly property bool showTitle: plasmoid.configuration.showTitle;
0051     readonly property string title: plasmoid.configuration.title;
0052     readonly property bool alertMode: root.running && root.seconds < 60
0053     property bool running: plasmoid.configuration.running > 0
0054     property bool suspended: false;
0055 
0056     readonly property string notificationText: plasmoid.configuration.notificationText;
0057 
0058     toolTipMainText: {
0059         var timerName = "";
0060         if (showTitle && title != "") {
0061             timerName = title;
0062         } else {
0063             timerName = Plasmoid.title;
0064         }
0065 
0066         if (running) {
0067             return i18n("%1 is running", timerName);
0068         } else {
0069             return i18n("%1 not running", timerName);
0070         }
0071     }
0072     toolTipSubText: running ? i18np("Remaining time left: %1 second", "Remaining time left: %1 seconds", seconds) : i18n("Use mouse wheel to change digits or choose from predefined timers in the context menu");
0073 
0074     compactRepresentation: CompactRepresentation { }
0075     fullRepresentation: TimerView { }
0076 
0077     function toggleTimer() {
0078         if (root.running) {
0079             root.stopTimer();
0080         } else {
0081             root.startTimer();
0082         }
0083     }
0084 
0085     Notification {
0086         id: timerNotification
0087         componentName: "plasma_applet_timer"
0088         eventId: "timerFinished"
0089         title: root.title || i18n("Timer")
0090         text: notificationText || i18n("Timer finished")
0091     }
0092 
0093     Timer {
0094         id: t;
0095         interval: 1000;
0096         onTriggered: {
0097             if (root.seconds != 0) {
0098                 root.seconds--;
0099             }
0100             if (root.seconds == 0) {
0101                 root.running = false;
0102 
0103                 if (showNotification) {
0104                     timerNotification.sendEvent();
0105                 }
0106                 if (runCommand) {
0107                     TimerPlasmoid.Timer.runCommand(command);
0108                 }
0109                 saveTimer();
0110             }
0111         }
0112         repeat: true;
0113         running: root.running;
0114     }
0115 
0116     Timer {
0117         id: delayedSaveTimer;
0118         interval: 3000;
0119         onTriggered: saveTimer();
0120     }
0121 
0122     function onDigitHasChanged() {
0123         delayedSaveTimer.stop();
0124         delayedSaveTimer.start();
0125     }
0126 
0127     Plasmoid.contextualActions: [
0128         PlasmaCore.Action {
0129             id: startAction
0130             text: i18nc("@action", "&Start")
0131             onTriggered: startTimer()
0132         },
0133         PlasmaCore.Action {
0134             id: stopAction
0135             text: i18nc("@action", "S&top")
0136             onTriggered: stopTimer()
0137         },
0138         PlasmaCore.Action {
0139             id: resetAction
0140             text: i18nc("@action", "&Reset")
0141             onTriggered: resetTimer()
0142         },
0143         PlasmaCore.Action {
0144             id: separator1
0145             isSeparator: true
0146         },
0147         PlasmaCore.Action {
0148             id: separator2
0149             isSeparator: true
0150         }
0151     ]
0152 
0153     Instantiator {
0154         model: plasmoid.configuration.predefinedTimers
0155         delegate: PlasmaCore.Action {
0156             text: TimerPlasmoid.Timer.secondsToString(modelData, "hh:mm:ss")
0157             onTriggered: {
0158                 seconds = modelData
0159                 startTimer();
0160             }
0161         }
0162         onObjectAdded: (index, object) => {
0163             Plasmoid.contextualActions.splice(Plasmoid.contextualActions.indexOf(separator2), 0, object)
0164         }
0165         onObjectRemoved: (index, object) => {
0166             Plasmoid.contextualActions.splice(Plasmoid.contextualActions.indexOf(object), 1)
0167         }
0168     }
0169 
0170     function startTimer() {
0171         running = true;
0172         suspended = false;
0173         opacityNeedsReset();
0174         saveTimer();
0175     }
0176 
0177     function stopTimer() {
0178         running = false;
0179         suspended = true;
0180         saveTimer();
0181     }
0182 
0183     function resetTimer() {
0184         running = false;
0185         suspended = false;
0186         seconds = 0;
0187         opacityNeedsReset();
0188         saveTimer();
0189     }
0190 
0191     signal opacityNeedsReset()
0192     signal digitHasChanged()
0193 
0194     function saveTimer() {
0195         plasmoid.configuration.running = running ? seconds : 0;
0196         plasmoid.configuration.savedAt = new Date();
0197         plasmoid.configuration.seconds = seconds
0198     }
0199 
0200     function restoreToSeconds(cRunning, cSavedAt, cSeconds) {
0201         if (cRunning > 0) {
0202             var elapsedSeconds = cRunning - ~~(~~(((new Date()).getTime() - cSavedAt.getTime()) / 1000));
0203             if (elapsedSeconds >= 0) {
0204                 return elapsedSeconds;
0205             } else {
0206                 return 0;
0207             }
0208         } else {
0209             return cSeconds;
0210         }
0211     }
0212 }
0213