aboutsummaryrefslogtreecommitdiff
path: root/.config/ags/widget/bar/buttons/SystemIndicators.ts
blob: cc98548f796344cdb4d69af953974b69a675d499 (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
import PanelButton from '../PanelButton';
import icons from 'lib/icons';
import asusctl from 'service/asusctl';

const notifications = await Service.import('notifications');
const bluetooth = await Service.import('bluetooth');
const audio = await Service.import('audio');
const network = await Service.import('network');
const powerprof = await Service.import('powerprofiles');

const ProfileIndicator = () => {
  const visible = asusctl.available ? asusctl.bind('profile').as(p => p !== 'Balanced') : powerprof.bind('active_profile').as(p => p !== 'balanced');

  const icon = asusctl.available ? asusctl.bind('profile').as(p => icons.asusctl.profile[p]) : powerprof.bind('active_profile').as(p => icons.powerprofile[p]);

  return Widget.Icon({ visible, icon });
};

const ModeIndicator = () => {
  if (!asusctl.available) {
    return Widget.Icon({
      setup(self) {
        Utils.idle(() => (self.visible = false));
      },
    });
  }

  return Widget.Icon({
    visible: asusctl.bind('mode').as(m => m !== 'Hybrid'),
    icon: asusctl.bind('mode').as(m => icons.asusctl.mode[m]),
  });
};

const MicrophoneIndicator = () =>
  Widget.Icon()
    .hook(audio, self => (self.visible = audio.recorders.length > 0 || audio.microphone.is_muted || false))
    .hook(audio.microphone, self => {
      const vol = audio.microphone.is_muted ? 0 : audio.microphone.volume;
      const { muted, low, medium, high } = icons.audio.mic;
      const cons = [
        [67, high],
        [34, medium],
        [1, low],
        [0, muted],
      ] as const;
      self.icon = cons.find(([n]) => n <= vol * 100)?.[1] || '';
    });

const DNDIndicator = () =>
  Widget.Icon({
    visible: notifications.bind('dnd'),
    icon: icons.notifications.silent,
  });

const BluetoothIndicator = () =>
  Widget.Overlay({
    class_name: 'bluetooth',
    passThrough: true,
    child: Widget.Icon({
      icon: icons.bluetooth.enabled,
      visible: bluetooth.bind('enabled'),
    }),
    overlay: Widget.Label({
      hpack: 'end',
      vpack: 'start',
      label: bluetooth.bind('connected_devices').as(c => `${c.length}`),
      visible: bluetooth.bind('connected_devices').as(c => c.length > 0),
    }),
  });

const NetworkIndicator = () =>
  Widget.Icon().hook(network, self => {
    const icon = network[network.primary || 'wifi']?.icon_name;
    self.icon = icon || '';
    self.visible = !!icon;
  });

const AudioIndicator = () =>
  Widget.Icon().hook(audio.speaker, self => {
    const vol = audio.speaker.is_muted ? 0 : audio.speaker.volume;
    const { muted, low, medium, high, overamplified } = icons.audio.volume;
    const cons = [
      [101, overamplified],
      [67, high],
      [34, medium],
      [1, low],
      [0, muted],
    ] as const;
    self.icon = cons.find(([n]) => n <= vol * 100)?.[1] || '';
  });

export default () =>
  PanelButton({
    window: 'quicksettings',
    on_clicked: () => App.toggleWindow('quicksettings'),
    on_scroll_up: () => (audio.speaker.volume += 0.02),
    on_scroll_down: () => (audio.speaker.volume -= 0.02),
    child: Widget.Box([
      //ProfileIndicator(),
      ModeIndicator(),
      DNDIndicator(),
      BluetoothIndicator(),
      MicrophoneIndicator(),
      AudioIndicator(),
      NetworkIndicator(),
    ]),
  });