🔧 chore(modules/bluetooth): also select default adapter on bluetoothctl
This commit is contained in:
+51
-31
@@ -1,5 +1,7 @@
|
||||
import { createRoot, getScope, Scope } from "ags";
|
||||
import GObject, { getter, gtype, register, setter } from "ags/gobject";
|
||||
import GObject, { getter, gtype, property, register, setter } from "ags/gobject";
|
||||
import { execAsync } from "ags/process";
|
||||
|
||||
import AstalBluetooth from "gi://AstalBluetooth";
|
||||
|
||||
|
||||
@@ -16,6 +18,8 @@ export class Bluetooth extends GObject.Object {
|
||||
|
||||
@getter(Boolean)
|
||||
get isAvailable() { return this.#isAvailable; }
|
||||
|
||||
@property(Boolean) saveDefaultAdapter = true;
|
||||
|
||||
@getter(gtype<AstalBluetooth.Adapter|null>(AstalBluetooth.Adapter))
|
||||
get adapter() { return this.#adapter; }
|
||||
@@ -24,6 +28,19 @@ export class Bluetooth extends GObject.Object {
|
||||
set adapter(newAdapter: AstalBluetooth.Adapter|null) {
|
||||
this.#adapter = newAdapter;
|
||||
this.notify("adapter");
|
||||
|
||||
if(!newAdapter) return;
|
||||
|
||||
AstalBluetooth.get_default().adapters.filter(ad => {
|
||||
if(ad.address !== newAdapter.address)
|
||||
return true;
|
||||
|
||||
ad.set_powered(true);
|
||||
return false;
|
||||
}).forEach(ad => ad.set_powered(false));
|
||||
|
||||
execAsync(`bluetoothctl select ${newAdapter.address}`).catch(e =>
|
||||
console.error(`Bluetooth: Couldn't select adapter. Stderr: ${e}`));
|
||||
}
|
||||
|
||||
constructor() {
|
||||
@@ -38,38 +55,41 @@ export class Bluetooth extends GObject.Object {
|
||||
}
|
||||
|
||||
this.#connections.set(
|
||||
AstalBluetooth.get_default(),
|
||||
AstalBluetooth.get_default().connect("adapter-added", (self, adapter) => {
|
||||
if(self.adapters.length === 1) // adapter was just added
|
||||
this.adapter = adapter;
|
||||
})
|
||||
AstalBluetooth.get_default(), [
|
||||
AstalBluetooth.get_default().connect("adapter-added", (self, adapter) => {
|
||||
if(self.adapters.length === 1) // adapter was just added
|
||||
this.adapter = adapter;
|
||||
}),
|
||||
AstalBluetooth.get_default().connect("adapter-removed", (self, adapter) => {
|
||||
if(self.adapters.length < 1) {
|
||||
this.adapter = null;
|
||||
this.#isAvailable = false;
|
||||
this.notify("is-available");
|
||||
}
|
||||
|
||||
if(this.#adapter?.address !== adapter.address)
|
||||
return;
|
||||
|
||||
// the removed adapter was the default
|
||||
|
||||
if(self.adapters.length < 1) {
|
||||
this.adapter = null;
|
||||
this.#isAvailable = false;
|
||||
this.notify("is-available");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.#adapter = self.adapters[0];
|
||||
})
|
||||
]
|
||||
);
|
||||
|
||||
this.#connections.set(
|
||||
AstalBluetooth.get_default(),
|
||||
AstalBluetooth.get_default().connect("adapter-removed", (self, adapter) => {
|
||||
if(self.adapters.length < 1) {
|
||||
this.adapter = null;
|
||||
this.#isAvailable = false;
|
||||
this.notify("is-available");
|
||||
}
|
||||
|
||||
if(this.#adapter?.address !== adapter.address)
|
||||
return;
|
||||
|
||||
// the removed adapter was the default
|
||||
|
||||
if(self.adapters.length < 1) {
|
||||
this.adapter = null;
|
||||
this.#isAvailable = false;
|
||||
this.notify("is-available");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.#adapter = self.adapters[0];
|
||||
})
|
||||
);
|
||||
this.#scope.onCleanup(() => this.#connections.forEach((ids, gobj) =>
|
||||
Array.isArray(ids) ?
|
||||
ids.forEach(id => gobj.disconnect(id))
|
||||
: gobj.disconnect(ids)
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { createPoll } from "ags/time";
|
||||
import { exec, execAsync } from "ags/process";
|
||||
import { Accessor, For, getScope, onCleanup, With } from "ags";
|
||||
import { Accessor, For, getScope, With } from "ags";
|
||||
import { Astal, Gtk } from "ags/gtk4";
|
||||
import { getSymbolicIcon } from "./apps";
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import { tr } from "../../../../i18n/intl";
|
||||
import { Windows } from "../../../../windows";
|
||||
import { Notifications } from "../../../../modules/notifications";
|
||||
import { execApp } from "../../../../modules/apps";
|
||||
import { execAsync } from "ags/process";
|
||||
import { createBinding, createComputed, For, With } from "ags";
|
||||
import { Bluetooth } from "../../../../modules/bluetooth";
|
||||
|
||||
@@ -69,10 +68,10 @@ export const BluetoothPage = new Page({
|
||||
return <PageButton class={isSelected.as(is => is ? "selected" : "")}
|
||||
title={adapter.alias ?? "Adapter"} icon={"bluetooth-active-symbolic"}
|
||||
description={createBinding(adapter, "address")}
|
||||
actionClicked={() =>
|
||||
adapter.address !== Bluetooth.getDefault().adapter?.address &&
|
||||
selectAdapter(adapter)
|
||||
}
|
||||
actionClicked={() => {
|
||||
if(adapter.address !== Bluetooth.getDefault().adapter?.address)
|
||||
Bluetooth.getDefault().adapter = adapter;
|
||||
}}
|
||||
endWidget={
|
||||
<Gtk.Image iconName={"object-select-symbolic"} visible={isSelected} />
|
||||
}
|
||||
@@ -212,18 +211,3 @@ function DeviceWidget({ device }: { device: AstalBluetooth.Device }): Gtk.Widget
|
||||
</With>}
|
||||
/> as Gtk.Widget;
|
||||
}
|
||||
|
||||
function selectAdapter(adapter: AstalBluetooth.Adapter): void {
|
||||
AstalBluetooth.get_default().adapters.filter(ad => {
|
||||
if(ad.alias !== adapter.alias)
|
||||
return true;
|
||||
|
||||
ad.set_powered(true);
|
||||
return false;
|
||||
}).forEach(ad => ad.set_powered(false));
|
||||
|
||||
execAsync(`bluetoothctl select ${adapter.address}`).catch(e =>
|
||||
console.error(`Bluetooth: Couldn't select adapter. Stderr: ${e}`));
|
||||
|
||||
Bluetooth.getDefault().adapter = adapter;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user