aboutsummaryrefslogtreecommitdiff
path: root/linux/home/.config/ags/widget/osd/OSD.ts
blob: 8239a08a3d4b19e4d8ffed9205234e2f35de3aed (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { icon } from "lib/utils"
import icons from "lib/icons"
import Progress from "./Progress"
import brightness from "service/brightness"
import options from "options"

const audio = await Service.import("audio")
const { progress, microphone } = options.osd

const DELAY = 2500

function OnScreenProgress(vertical: boolean) {
  const indicator = Widget.Icon({
    size: 42,
    vpack: "start",
  })
  const progress = Progress({
    vertical,
    width: vertical ? 42 : 300,
    height: vertical ? 300 : 42,
    child: indicator,
  })

  const revealer = Widget.Revealer({
    transition: "slide_left",
    child: progress,
  })

  let count = 0
  function show(value: number, icon: string) {
    revealer.reveal_child = true
    indicator.icon = icon
    progress.setValue(value)
    count++
    Utils.timeout(DELAY, () => {
      count--

      if (count === 0)
        revealer.reveal_child = false
    })
  }

  return revealer
    .hook(brightness, () => show(
      brightness.screen,
      icons.brightness.screen,
    ), "notify::screen")
    .hook(brightness, () => show(
      brightness.kbd,
      icons.brightness.keyboard,
    ), "notify::kbd")
    .hook(audio.speaker, () => show(
      audio.speaker.volume,
      icon(audio.speaker.icon_name || "", icons.audio.type.speaker),
    ), "notify::volume")
}

function MicrophoneMute() {
  const icon = Widget.Icon({
    class_name: "microphone",
  })

  const revealer = Widget.Revealer({
    transition: "slide_up",
    child: icon,
  })

  let count = 0
  let mute = audio.microphone.stream?.is_muted ?? false

  return revealer.hook(audio.microphone, () => Utils.idle(() => {
    if (mute !== audio.microphone.stream?.is_muted) {
      mute = audio.microphone.stream!.is_muted
      icon.icon = icons.audio.mic[mute ? "muted" : "high"]
      revealer.reveal_child = true
      count++

      Utils.timeout(DELAY, () => {
        count--
        if (count === 0)
          revealer.reveal_child = false
      })
    }
  }))
}

export default (monitor: number) => Widget.Window({
  monitor,
  name: `indicator${monitor}`,
  class_name: "indicator",
  layer: "overlay",
  click_through: true,
  anchor: ["right", "left", "top", "bottom"],
  child: Widget.Box({
    css: "padding: 2px;",
    expand: true,
    child: Widget.Overlay(
      { child: Widget.Box({ expand: true }) },
      Widget.Box({
        hpack: progress.pack.h.bind(),
        vpack: progress.pack.v.bind(),
        child: progress.vertical.bind().as(OnScreenProgress),
      }),
      Widget.Box({
        hpack: microphone.pack.h.bind(),
        vpack: microphone.pack.v.bind(),
        child: MicrophoneMute(),
      }),
    ),
  }),
})