[AGS] Possibly fixed
This commit is contained in:
		| @@ -1,23 +1,63 @@ | ||||
| import { bind, exec, interval, readFile, timeout, writeFile } from "astal"; | ||||
| import { bind, interval, readFile, timeout, writeFile } from "astal"; | ||||
| import { Gtk } from "astal/gtk4"; | ||||
| import AstalBluetooth from "gi://AstalBluetooth"; | ||||
| import BTDevice from "./Device"; | ||||
| const ALIGN = Gtk.Align; | ||||
| const MAX_RETRY = 20; | ||||
|  | ||||
| const bt = AstalBluetooth.get_default(); | ||||
|  | ||||
| // const LaunchBluetoothModule = () => { | ||||
| //     const box = new Gtk.Box(); | ||||
| // | ||||
| //     const placeholder = () => { | ||||
| //         return <button cssClasses={["toggle-button"]} child={ | ||||
| //             <box> | ||||
| //                 <label | ||||
| //                     cssClasses={['title-2']} | ||||
| //                     label={"Bluetooth"}></label> | ||||
| //                 <label label={"Backend missing"}></label> | ||||
| //             </box> | ||||
| //         }> | ||||
| //         </button> | ||||
| //     } | ||||
| // | ||||
| //     if (bt.adapter) { | ||||
| //         box.append(BluetoothModule()); | ||||
| //     } else { | ||||
| //         const p = placeholder(); | ||||
| //         box.append(p); | ||||
| // | ||||
| //         let count = 0; | ||||
| //         const i = interval(1000, () => { | ||||
| //             if (bt.adapter !== null) { | ||||
| //                 box.remove(p); | ||||
| //                 box.append(BluetoothModule()); | ||||
| //                 i.cancel(); | ||||
| //             } else if (count >= MAX_RETRY) { | ||||
| //                 i.cancel(); | ||||
| //             } | ||||
| //             count++; | ||||
| //         }); | ||||
| //     } | ||||
| // | ||||
| //     return box; | ||||
| // } | ||||
|  | ||||
| const BluetoothModule = () => { | ||||
|     return ( | ||||
|         <box> | ||||
|             <button | ||||
|                 cssClasses={bind(bt.adapter, "powered").as(powered => | ||||
|                 cssClasses={bind(bt, "isPowered").as(powered => | ||||
|                     powered | ||||
|                         ? ["toggle-button", "toggle-on"] | ||||
|                         : ["toggle-button"], | ||||
|                 )} | ||||
|                 onClicked={() => | ||||
|                     bt.adapter.set_powered(!bt.adapter.get_powered()) | ||||
|                 } | ||||
|                 onClicked={() => { | ||||
|                     try { | ||||
|                         bt.adapter.set_powered(!bt.adapter.get_powered()) | ||||
|                     } catch (_) { } | ||||
|                 }} | ||||
|                 child={ | ||||
|                     <box vertical> | ||||
|                         <label | ||||
| @@ -28,13 +68,13 @@ const BluetoothModule = () => { | ||||
|                         ></label> | ||||
|                         <box halign={ALIGN.CENTER} valign={ALIGN.CENTER}> | ||||
|                             <label | ||||
|                                 visible={bind(bt.adapter, "powered").as( | ||||
|                                 visible={bind(bt, "isPowered").as( | ||||
|                                     p => !p, | ||||
|                                 )} | ||||
|                                 label="Disabled" | ||||
|                             ></label> | ||||
|                             <label | ||||
|                                 visible={bind(bt.adapter, "powered")} | ||||
|                                 visible={bind(bt, "isPowered")} | ||||
|                                 label={bind(bt, "devices").as(devices => { | ||||
|                                     let count = 0; | ||||
|                                     devices.forEach(device => { | ||||
| @@ -52,7 +92,7 @@ const BluetoothModule = () => { | ||||
|             ></button> | ||||
|             <button | ||||
|                 cssClasses={["actions-button"]} | ||||
|                 visible={bind(bt.adapter, "powered")} | ||||
|                 visible={bind(bt, "isPowered")} | ||||
|                 child={ | ||||
|                     <box> | ||||
|                         <image iconName={"arrow-right-symbolic"}></image> | ||||
| @@ -75,28 +115,16 @@ const openBTPicker = () => { | ||||
|  | ||||
| const BluetoothPickerList = () => { | ||||
|     let btEnableState = readFile("./btconf") === "true" ? true : false; | ||||
|     if (bt.adapter) { | ||||
|     if (bt.get_adapter()) { | ||||
|         print( 'Setting BT state to ' + btEnableState ); | ||||
|         bt.adapter.set_powered(btEnableState); | ||||
|     } else { | ||||
|         timeout( 1000, () => { | ||||
|             if (bt.adapter) { | ||||
|         timeout(5000, () => { | ||||
|             if (bt.get_adapter()) { | ||||
|                 print( 'Setting BT state to ' + btEnableState ); | ||||
|                 bt.adapter.set_powered(btEnableState); | ||||
|             } else { | ||||
|                 timeout( 2000, () => { | ||||
|                     if ( bt.adapter ) { | ||||
|                         bt.adapter.set_powered(btEnableState); | ||||
|                     } else { | ||||
|                         timeout( 10000, () => { | ||||
|                             if ( bt.adapter ) { | ||||
|                                 bt.adapter.set_powered(btEnableState); | ||||
|                             } else { | ||||
|                                 exec( `/bin/bash -c "notify-send 'Failed to connect to bluetooth adapter'"` ); | ||||
|                             } | ||||
|                         } ) | ||||
|                     } | ||||
|                 } ) | ||||
|             } | ||||
|         } ); | ||||
|         }) | ||||
|     } | ||||
|  | ||||
|     const updateState = () => { | ||||
| @@ -107,7 +135,11 @@ const BluetoothPickerList = () => { | ||||
|     return ( | ||||
|         <box | ||||
|             vertical | ||||
|             onDestroy={() => bt.adapter.stop_discovery()} | ||||
|             onDestroy={() => { | ||||
|                 try { | ||||
|                     bt.adapter.stop_discovery() | ||||
|                 } catch (_) { } | ||||
|             }} | ||||
|             cssClasses={["popover-box"]} | ||||
|         > | ||||
|             <label cssClasses={["title"]} label={"Bluetooth"}></label> | ||||
|   | ||||
| @@ -77,7 +77,7 @@ const NetworkWidget = () => { | ||||
|  | ||||
| const BluetoothWidget = () => { | ||||
|     const bluetooth = AstalBluetooth.get_default(); | ||||
|     const enabled = bind(bluetooth.adapter, "powered"); | ||||
|     const enabled = bind(bluetooth, "isPowered"); | ||||
|     const connected = bind(bluetooth, "isConnected"); | ||||
|  | ||||
|     // For each connected BT device, render status | ||||
|   | ||||
		Reference in New Issue
	
	Block a user