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

223 lines
7.8 KiB
JavaScript
Raw Normal View History

2023-12-04 15:39:12 -05:00
import App from 'resource:///com/github/Aylur/ags/app.js';
import Network from 'resource:///com/github/Aylur/ags/service/network.js';
import Variable from 'resource:///com/github/Aylur/ags/variable.js';
2023-12-04 17:48:56 -05:00
import { Box, Icon, Label, ListBox, Overlay, Revealer, Scrollable } from 'resource:///com/github/Aylur/ags/widget.js';
2023-12-04 15:39:12 -05:00
import { execAsync } from 'resource:///com/github/Aylur/ags/utils.js';
import CursorBox from '../misc/cursorbox.js';
2023-12-04 15:39:12 -05:00
const SCROLL_THRESH_H = 200;
const SCROLL_THRESH_N = 7;
2023-12-04 15:39:12 -05:00
const AccessPoint = (ap) => {
const widget = Box({
className: 'menu-item',
2023-12-04 15:39:12 -05:00
});
widget.ap = Variable(ap);
const child = Box({
hexpand: true,
children: [
Icon({
setup: (self) => {
self.hook(widget.ap, () => {
self.icon = widget.ap.value.iconName;
});
},
2023-12-04 15:39:12 -05:00
}),
Label({
setup: (self) => {
self.hook(widget.ap, () => {
self.label = widget.ap.value.ssid || '';
});
},
2023-12-04 15:39:12 -05:00
}),
Icon({
icon: 'object-select-symbolic',
hexpand: true,
hpack: 'end',
setup: (self) => {
self.hook(Network, () => {
self.setCss(
`opacity: ${
widget.ap.value.ssid === Network.wifi.ssid ?
'1' :
'0'
};
`,
);
});
},
2023-12-04 15:39:12 -05:00
}),
],
});
widget.add(Revealer({
revealChild: true,
transition: 'slide_down',
child: CursorBox({
on_primary_click_release: () => {
2023-12-04 15:39:12 -05:00
execAsync(`nmcli device wifi connect
${widget.ap.value.bssid}`).catch(print);
},
child,
}),
}));
return widget;
};
export const NetworkMenu = () => {
const APList = new Map();
const topArrow = Revealer({
2023-12-04 17:48:56 -05:00
transition: 'slide_down',
2023-12-04 15:39:12 -05:00
child: Icon({
icon: `${App.configDir }/icons/down-large.svg`,
className: 'scrolled-indicator',
size: 16,
css: '-gtk-icon-transform: rotate(180deg);',
}),
});
const bottomArrow = Revealer({
2023-12-04 17:48:56 -05:00
transition: 'slide_up',
2023-12-04 15:39:12 -05:00
child: Icon({
icon: `${App.configDir }/icons/down-large.svg`,
className: 'scrolled-indicator',
size: 16,
}),
});
return Overlay({
pass_through: true,
overlays: [
Box({
vpack: 'start',
hpack: 'center',
css: 'margin-top: 12px',
children: [topArrow],
}),
Box({
vpack: 'end',
hpack: 'center',
css: 'margin-bottom: 12px',
children: [bottomArrow],
}),
],
child: Box({
className: 'menu',
child: Scrollable({
hscroll: 'never',
vscroll: 'never',
setup: (self) => {
self.on('edge-reached', (_, pos) => {
// Manage scroll indicators
if (pos === 2) {
topArrow.revealChild = false;
bottomArrow.revealChild = true;
}
else if (pos === 3) {
topArrow.revealChild = true;
bottomArrow.revealChild = false;
}
});
},
2023-12-04 15:39:12 -05:00
child: ListBox({
setup: (self) => {
self.set_sort_func((a, b) => {
return b.get_children()[0].ap.value.strength -
a.get_children()[0].ap.value.strength;
});
self.hook(Network, () => {
// Add missing APs
Network.wifi?.access_points.forEach((ap) => {
if (ap.ssid !== 'Unknown') {
if (APList.has(ap.ssid)) {
const accesPoint = APList.get(ap.ssid)
.ap.value;
if (accesPoint.strength < ap.strength) {
APList.get(ap.ssid).ap.value = ap;
}
2023-12-04 15:39:12 -05:00
}
else {
APList.set(ap.ssid, AccessPoint(ap));
2023-12-04 15:39:12 -05:00
self.add(APList.get(ap.ssid));
self.show_all();
}
2023-12-04 15:39:12 -05:00
}
});
2023-12-04 15:39:12 -05:00
// Delete ones that don't exist anymore
const difference = Array.from(APList.keys())
.filter((ssid) => !Network.wifi.access_points
.find((ap) => ap.ssid === ssid) &&
ssid !== 'Unknown');
2023-12-04 15:39:12 -05:00
difference.forEach((ssid) => {
const apWidget = APList.get(ssid);
2023-12-04 15:39:12 -05:00
if (apWidget) {
if (apWidget.toDestroy) {
apWidget.get_parent().destroy();
APList.delete(ssid);
}
else {
apWidget.children[0]
.revealChild = false;
apWidget.toDestroy = true;
}
2023-12-04 15:39:12 -05:00
}
});
// Start scrolling after a specified height
// is reached by the children
const height = Math.max(
self.get_parent().get_allocated_height(),
SCROLL_THRESH_H,
);
const scroll = self.get_parent().get_parent();
if (self.get_children().length > SCROLL_THRESH_N) {
scroll.vscroll = 'always';
scroll.setCss(`min-height: ${height}px;`);
// Make bottom scroll indicator appear only
// when first getting overflowing children
if (!(bottomArrow.revealChild === true ||
topArrow.revealChild === true)) {
bottomArrow.revealChild = true;
2023-12-04 15:39:12 -05:00
}
}
else {
scroll.vscroll = 'never';
scroll.setCss('');
topArrow.revealChild = false;
bottomArrow.revealChild = false;
2023-12-04 15:39:12 -05:00
}
// Trigger sort_func
self.get_children().forEach((ch) => {
ch.changed();
});
2023-12-04 15:39:12 -05:00
});
},
2023-12-04 15:39:12 -05:00
}),
}),
}),
});
};