summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell/Services/Worktime.qml
blob: fdb45aa0975cabcdc1015f71f220cb1e2645b644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
pragma Singleton

import QtQuick
import Quickshell
import Quickshell.Io
import QtQml

Singleton {
  id: root

  property alias time: timeState
  property alias today: todayState

  CommandState {
    id: timeState
    command: "time"
  }
  CommandState {
    id: todayState
    command: "today"
  }

  component CommandState : Scope {
    id: commandState

    required property string command
    property var state: null

    property bool strikeout: !strikeoutTimer.running
    property alias running: process.running
    property alias updating: updateTimer.running

    Process {
      id: process
      running: true
      command: [ @worktime@, commandState.command, "--waybar" ]
      stdout: StdioCollector {
	id: processCollector
	onStreamFinished: {
	  try {
	    commandState.state = JSON.parse(processCollector.text);
	    strikeoutTimer.restart();
	  } catch (e) {
	    console.warn("Worktime: Failed to parse output:", processCollector.text, e);
	  }
	}
      }
    }

    Timer {
      id: updateTimer
      running: commandState.state?.class == "running" || commandState.state?.class == "over"
      interval: 60000
      repeat: true
      onTriggered: process.running = true
    }

    Timer {
      id: strikeoutTimer
      running: false
      interval: 5 * updateTimer.interval
      repeat: false
    }
  }

  Timer {
    running: Boolean(timeState.state) && Boolean(todayState.state) && timeState.strikeout && todayState.strikeout
    interval: 1000
    repeat: false
    onTriggered: {
      timeState.state = null;
      todayState.state = null;
    }
  }
}