nixos-configs/devices/wim/config/ags/js/quick-settings/button-grid.js

356 lines
10 KiB
JavaScript
Raw Normal View History

import App from 'resource:///com/github/Aylur/ags/app.js';
import Bluetooth from 'resource:///com/github/Aylur/ags/service/bluetooth.js';
import Network from 'resource:///com/github/Aylur/ags/service/network.js';
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
import { Box, Icon, Label, Revealer } from 'resource:///com/github/Aylur/ags/widget.js';
import { execAsync } from 'resource:///com/github/Aylur/ags/utils.js';
import { SpeakerIcon, MicIcon } from '../misc/audio-icons.js';
import CursorBox from '../misc/cursorbox.js';
import Separator from '../misc/separator.js';
2023-12-04 15:39:12 -05:00
import { NetworkMenu } from './network.js';
import { BluetoothMenu } from './bluetooth.js';
2023-12-04 15:39:12 -05:00
const SPACING = 28;
2023-12-20 03:45:05 -05:00
const ButtonStates = [];
2023-12-23 01:14:21 -05:00
/**
* @typedef {import('types/widgets/widget').default} Widget
* @typedef {import('types/widgets/box').default} Box
* @typedef {import('types/widgets/icon').default} Icon
* @typedef {import('types/widgets/label').default} Label
* @typedef {import('types/widgets/revealer').default} Revealer
* @typedef {[any, function, (string|undefined)?]} BindTuple
*/
2023-12-20 03:45:05 -05:00
/**
* @param {{
* command?: function
* secondary_command?: function
* on_open?: function(Revealer):void
2023-12-23 01:14:21 -05:00
* icon: string|BindTuple
* indicator?: BindTuple
2023-12-20 03:45:05 -05:00
* menu?: any
* }} o
*/
const GridButton = ({
2023-12-20 03:45:05 -05:00
command = () => {/**/},
secondary_command = () => {/**/},
on_open = () => {/**/},
icon,
indicator,
menu,
2023-12-20 03:45:05 -05:00
}) => {
const Activated = Variable(false);
ButtonStates.push(Activated);
2023-12-23 01:14:21 -05:00
let iconWidget;
/** @type Label */
let indicatorWidget = Label();
// Allow setting icon dynamically or statically
if (typeof icon === 'string') {
2023-12-23 01:14:21 -05:00
iconWidget = Icon({
2023-12-20 03:45:05 -05:00
class_name: 'grid-label',
icon,
setup: (self) => {
self.hook(Activated, () => {
self.setCss(`color: ${Activated.value ?
'rgba(189, 147, 249, 0.8)' :
'unset'};`);
});
},
});
}
2023-12-23 01:14:21 -05:00
else if (Array.isArray(icon)) {
iconWidget = Icon({
2023-12-20 03:45:05 -05:00
class_name: 'grid-label',
setup: (self) => {
self
2023-12-23 01:14:21 -05:00
// @ts-expect-error
.hook(...icon)
.hook(Activated, () => {
self.setCss(`color: ${Activated.value ?
'rgba(189, 147, 249, 0.8)' :
'unset'};`);
});
},
});
}
if (indicator) {
2023-12-23 01:14:21 -05:00
indicatorWidget = Label({
2023-12-20 03:45:05 -05:00
class_name: 'sub-label',
justification: 'left',
truncate: 'end',
2023-12-23 01:14:21 -05:00
max_width_chars: 12,
setup: (self) => {
2023-12-23 01:14:21 -05:00
// @ts-expect-error
self.hook(...indicator);
},
});
}
if (menu) {
menu = Revealer({
transition: 'slide_down',
child: menu,
2023-12-23 01:14:21 -05:00
reveal_child: Activated.bind(),
});
}
const widget = Box({
vertical: true,
children: [
Box({
2023-12-20 03:45:05 -05:00
class_name: 'grid-button',
children: [
CursorBox({
2023-12-20 03:45:05 -05:00
class_name: 'left-part',
on_primary_click_release: () => {
if (Activated.value) {
2023-12-20 03:45:05 -05:00
secondary_command();
}
else {
command();
}
},
2023-12-23 01:14:21 -05:00
child: iconWidget,
}),
CursorBox({
2023-12-20 03:45:05 -05:00
class_name: 'right-part',
on_primary_click_release: () => {
ButtonStates.forEach((state) => {
if (state !== Activated) {
state.value = false;
}
});
Activated.value = !Activated.value;
},
2023-12-20 03:45:05 -05:00
on_hover: (self) => {
if (menu) {
2023-12-20 03:45:05 -05:00
const rowMenu =
self.get_parent()
?.get_parent()
?.get_parent()
?.get_parent()
2023-12-23 01:14:21 -05:00
// @ts-expect-error
2023-12-20 03:45:05 -05:00
?.children[1];
2023-12-23 01:14:21 -05:00
const isSetup = rowMenu.get_children().find(
/** @param {Box} ch */
(ch) => ch === menu,
);
if (!isSetup) {
rowMenu.add(menu);
rowMenu.show_all();
}
}
},
child: Icon({
icon: `${App.configDir }/icons/down-large.svg`,
2023-12-20 03:45:05 -05:00
class_name: 'grid-chev',
setup: (self) => {
self.hook(Activated, () => {
let deg = 270;
if (Activated.value) {
deg = menu ? 360 : 450;
on_open(menu);
}
self.setCss(`
-gtk-icon-transform: rotate(${deg}deg);
`);
});
},
}),
}),
],
}),
2023-12-23 01:14:21 -05:00
indicatorWidget,
],
});
return widget;
};
2023-12-23 01:14:21 -05:00
const Row = ({ buttons }) => {
const widget = Box({
vertical: true,
children: [
Box({
2023-12-20 03:45:05 -05:00
class_name: 'button-row',
hpack: 'center',
}),
Box({ vertical: true }),
],
});
for (let i = 0; i < buttons.length; ++i) {
if (i === buttons.length - 1) {
2023-12-23 01:14:21 -05:00
// @ts-expect-error
widget.children[0].add(buttons[i]);
}
else {
2023-12-23 01:14:21 -05:00
// @ts-expect-error
widget.children[0].add(buttons[i]);
2023-12-23 01:14:21 -05:00
// @ts-expect-error
widget.children[0].add(Separator(SPACING));
}
}
return widget;
};
const FirstRow = () => Row({
buttons: [
GridButton({
command: () => Network.toggleWifi(),
2023-12-20 03:45:05 -05:00
secondary_command: () => {
// TODO: connection editor
},
2023-12-23 01:14:21 -05:00
icon: [Network,
/** @param {Icon} self */
(self) => {
self.icon = Network.wifi?.icon_name;
}],
2023-12-23 01:14:21 -05:00
indicator: [Network,
/** @param {Label} self */
(self) => {
self.label = Network.wifi?.ssid || Network.wired?.internet;
}],
2023-12-04 15:39:12 -05:00
menu: NetworkMenu(),
on_open: () => Network.wifi.scan(),
}),
// TODO: do vpn
GridButton({
command: () => {
//
},
2023-12-20 03:45:05 -05:00
secondary_command: () => {
//
},
icon: 'airplane-mode-disabled-symbolic',
}),
GridButton({
command: () => Bluetooth.toggle(),
2023-12-20 03:45:05 -05:00
secondary_command: () => {
// TODO: bluetooth connection editor
},
2023-12-23 01:14:21 -05:00
icon: [Bluetooth,
/** @param {Icon} self */
(self) => {
if (Bluetooth.enabled) {
self.icon = Bluetooth.connected_devices[0] ?
Bluetooth.connected_devices[0].icon_name :
'bluetooth-active-symbolic';
}
else {
self.icon = 'bluetooth-disabled-symbolic';
}
}],
indicator: [Bluetooth,
/** @param {Label} self */
(self) => {
self.label = Bluetooth.connected_devices[0] ?
`${Bluetooth.connected_devices[0]}` :
'Disconnected';
}, 'notify::connected-devices'],
menu: BluetoothMenu(),
on_open: (menu) => {
2023-12-23 01:14:21 -05:00
execAsync(`bluetoothctl scan ${menu.reveal_child ?
'on' :
2023-12-08 00:01:43 -05:00
'off'}`).catch(print);
},
}),
],
});
const SecondRow = () => Row({
buttons: [
GridButton({
command: () => {
execAsync(['pactl', 'set-sink-mute',
'@DEFAULT_SINK@', 'toggle']).catch(print);
},
2023-12-20 03:45:05 -05:00
secondary_command: () => {
execAsync(['bash', '-c', 'pavucontrol'])
.catch(print);
},
2023-12-23 01:14:21 -05:00
icon: [SpeakerIcon,
/** @param {Icon} self */
(self) => {
self.icon = SpeakerIcon.value;
}],
}),
GridButton({
command: () => {
execAsync(['pactl', 'set-source-mute',
'@DEFAULT_SOURCE@', 'toggle']).catch(print);
},
2023-12-20 03:45:05 -05:00
secondary_command: () => {
execAsync(['bash', '-c', 'pavucontrol'])
.catch(print);
},
2023-12-23 01:14:21 -05:00
icon: [MicIcon,
/** @param {Icon} self */
(self) => {
self.icon = MicIcon.value;
}],
}),
GridButton({
command: () => {
execAsync(['lock']).catch(print);
},
2023-12-20 03:45:05 -05:00
secondary_command: () => App.openWindow('powermenu'),
icon: 'system-lock-screen-symbolic',
}),
],
});
export default () => Box({
2023-12-20 03:45:05 -05:00
class_name: 'button-grid',
vertical: true,
2023-11-06 18:37:23 -05:00
hpack: 'center',
children: [
FirstRow(),
Separator(10, { vertical: true }),
SecondRow(),
],
});