2024-12-04 00:18:11 -05:00
|
|
|
import { bind } from 'astal';
|
|
|
|
import { Gtk } from 'astal/gtk3';
|
|
|
|
|
|
|
|
import AstalBluetooth from 'gi://AstalBluetooth';
|
|
|
|
|
2024-12-04 23:29:00 -05:00
|
|
|
import { ListBox, ToggleButton } from '../misc/subclasses';
|
2024-12-04 00:18:11 -05:00
|
|
|
import Separator from '../misc/separator';
|
|
|
|
|
|
|
|
import DeviceWidget from './device';
|
|
|
|
|
|
|
|
|
2024-12-04 23:29:00 -05:00
|
|
|
const calculateDevSort = (dev: AstalBluetooth.Device) => {
|
|
|
|
let value = 0;
|
|
|
|
|
|
|
|
if (dev.connected) {
|
|
|
|
value += 1000;
|
|
|
|
}
|
|
|
|
if (dev.paired) {
|
|
|
|
value += 100;
|
|
|
|
}
|
|
|
|
if (dev.blocked) {
|
|
|
|
value += 10;
|
|
|
|
}
|
|
|
|
if (dev.icon) {
|
|
|
|
if (dev.icon === 'audio-headset') {
|
|
|
|
value += 9;
|
|
|
|
}
|
|
|
|
if (dev.icon === 'audio-headphones') {
|
|
|
|
value += 8;
|
|
|
|
}
|
|
|
|
if (dev.icon === 'audio-card') {
|
|
|
|
value += 7;
|
|
|
|
}
|
|
|
|
if (dev.icon === 'computer') {
|
|
|
|
value += 6;
|
|
|
|
}
|
|
|
|
if (dev.icon === 'phone') {
|
|
|
|
value += 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
|
2024-12-04 00:18:11 -05:00
|
|
|
export default () => {
|
|
|
|
const bluetooth = AstalBluetooth.get_default();
|
|
|
|
|
2024-12-04 22:21:52 -05:00
|
|
|
const deviceList = (
|
|
|
|
<scrollable
|
|
|
|
className="list"
|
|
|
|
|
2024-12-04 23:29:00 -05:00
|
|
|
css="min-height: 300px;"
|
2024-12-04 22:21:52 -05:00
|
|
|
hscroll={Gtk.PolicyType.NEVER}
|
|
|
|
vscroll={Gtk.PolicyType.AUTOMATIC}
|
|
|
|
>
|
2024-12-04 23:29:00 -05:00
|
|
|
<ListBox
|
|
|
|
selectionMode={Gtk.SelectionMode.SINGLE}
|
|
|
|
|
2024-12-04 22:21:52 -05:00
|
|
|
setup={(self) => {
|
2024-12-04 23:29:00 -05:00
|
|
|
bluetooth.devices
|
2024-12-04 22:21:52 -05:00
|
|
|
.filter((dev) => dev.name)
|
2024-12-04 23:29:00 -05:00
|
|
|
.forEach((dev) => {
|
|
|
|
self.add(<DeviceWidget dev={dev} />);
|
|
|
|
});
|
2024-12-04 22:21:52 -05:00
|
|
|
|
|
|
|
self.hook(bluetooth, 'device-added', (_, dev) => {
|
|
|
|
if (dev.name) {
|
|
|
|
self.add(<DeviceWidget dev={dev} />);
|
2024-12-04 23:29:00 -05:00
|
|
|
self.invalidate_sort();
|
2024-12-04 22:21:52 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.hook(bluetooth, 'device-removed', (_, dev) => {
|
2024-12-04 23:29:00 -05:00
|
|
|
const children = self
|
|
|
|
.get_children()
|
|
|
|
.map((ch) => ch.get_child()) as DeviceWidget[];
|
|
|
|
|
2024-12-04 22:21:52 -05:00
|
|
|
const devWidget = children.find((ch) => ch.dev === dev);
|
|
|
|
|
|
|
|
if (devWidget) {
|
|
|
|
devWidget.revealChild = false;
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2024-12-04 23:29:00 -05:00
|
|
|
devWidget.get_parent()?.destroy();
|
2024-12-04 22:21:52 -05:00
|
|
|
}, devWidget.transitionDuration + 100);
|
|
|
|
}
|
|
|
|
});
|
2024-12-04 23:29:00 -05:00
|
|
|
|
|
|
|
self.set_sort_func((a, b) => {
|
|
|
|
const devA = (a.get_child() as DeviceWidget).dev;
|
|
|
|
const devB = (b.get_child() as DeviceWidget).dev;
|
|
|
|
|
|
|
|
const sort = calculateDevSort(devB) - calculateDevSort(devA);
|
|
|
|
|
|
|
|
return sort !== 0 ? sort : devA.name.localeCompare(devB.name);
|
|
|
|
});
|
2024-12-04 22:21:52 -05:00
|
|
|
}}
|
2024-12-04 23:29:00 -05:00
|
|
|
/>
|
2024-12-04 22:21:52 -05:00
|
|
|
</scrollable>
|
|
|
|
);
|
|
|
|
|
2024-12-04 00:18:11 -05:00
|
|
|
return (
|
|
|
|
<box
|
|
|
|
className="bluetooth widget"
|
|
|
|
vertical
|
|
|
|
>
|
|
|
|
<centerbox homogeneous>
|
|
|
|
<switch
|
|
|
|
cursor="pointer"
|
|
|
|
valign={Gtk.Align.CENTER}
|
|
|
|
halign={Gtk.Align.START}
|
|
|
|
|
|
|
|
active={bind(bluetooth, 'isPowered')}
|
|
|
|
|
|
|
|
setup={(self) => {
|
|
|
|
self.connect('notify::active', () => {
|
|
|
|
bluetooth.adapter?.set_powered(self.active);
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<box />
|
|
|
|
|
|
|
|
<ToggleButton
|
|
|
|
cursor="pointer"
|
|
|
|
halign={Gtk.Align.END}
|
|
|
|
|
|
|
|
className="toggle-button"
|
|
|
|
|
|
|
|
sensitive={bind(bluetooth, 'isPowered')}
|
|
|
|
|
|
|
|
onToggled={(self) => {
|
|
|
|
self.toggleClassName('active', self.active);
|
|
|
|
|
|
|
|
if (self.active) {
|
|
|
|
bluetooth.adapter?.start_discovery();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bluetooth.adapter?.stop_discovery();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
2024-12-04 22:21:52 -05:00
|
|
|
<icon icon="emblem-synchronizing-symbolic" css="font-size: 30px;" />
|
2024-12-04 00:18:11 -05:00
|
|
|
</ToggleButton>
|
|
|
|
</centerbox>
|
|
|
|
|
|
|
|
<Separator size={8} vertical />
|
|
|
|
|
2024-12-04 22:21:52 -05:00
|
|
|
{deviceList}
|
2024-12-04 00:18:11 -05:00
|
|
|
</box>
|
|
|
|
);
|
|
|
|
};
|