bar: network and bluetooth actions

Do stuff on click
This commit is contained in:
2 * r + 2 * t 2025-01-15 21:47:51 +11:00
parent ef9485dc3f
commit 0592075b4e

View file

@ -1,7 +1,7 @@
import { execAsync, GLib, register, Variable } from "astal"; import { execAsync, GLib, register, Variable } from "astal";
import { bind, kebabify } from "astal/binding"; import { bind, kebabify } from "astal/binding";
import { App, Astal, astalify, Gdk, Gtk, type ConstructProps } from "astal/gtk3"; import { App, Astal, astalify, Gdk, Gtk, type ConstructProps } from "astal/gtk3";
import AstalBluetooth01 from "gi://AstalBluetooth"; import AstalBluetooth from "gi://AstalBluetooth";
import AstalHyprland from "gi://AstalHyprland"; import AstalHyprland from "gi://AstalHyprland";
import AstalNetwork from "gi://AstalNetwork"; import AstalNetwork from "gi://AstalNetwork";
import AstalNotifd from "gi://AstalNotifd"; import AstalNotifd from "gi://AstalNotifd";
@ -189,12 +189,27 @@ const TrayItem = (item: AstalTray.TrayItem) => {
const Tray = () => <box className="module tray">{bind(AstalTray.get_default(), "items").as(i => i.map(TrayItem))}</box>; const Tray = () => <box className="module tray">{bind(AstalTray.get_default(), "items").as(i => i.map(TrayItem))}</box>;
const Network = () => ( const Network = () => (
<stack <button
transitionType={Gtk.StackTransitionType.SLIDE_UP_DOWN} onClick={(_, event) => {
transitionDuration={120} const network = AstalNetwork.get_default();
shown={bind(AstalNetwork.get_default(), "primary").as(p => if (event.button === Astal.MouseButton.PRIMARY) {
p === AstalNetwork.Primary.WIFI ? "wifi" : "wired" // TODO: networks panel
)} } else if (event.button === Astal.MouseButton.SECONDARY) network.wifi.enabled = !network.wifi.enabled;
else if (event.button === Astal.MouseButton.MIDDLE)
execAsync("uwsm app -- gnome-control-center wifi").catch(() => {
network.wifi.scan();
execAsync(
"uwsm app -- foot -T nmtui fish -c 'sleep .1; set -e COLORTERM; TERM=xterm-old nmtui connect'"
).catch(err => {
// Idk why but foot always throws this error when it opens
if (
err.message !==
"warn: wayland.c:1619: compositor does not implement the XDG toplevel icon protocol\nwarn: terminal.c:1973: slave exited with signal 1 (Hangup)"
)
console.error(err);
});
});
}}
setup={self => { setup={self => {
const network = AstalNetwork.get_default(); const network = AstalNetwork.get_default();
const tooltipText = Variable(""); const tooltipText = Variable("");
@ -225,6 +240,13 @@ const Network = () => (
update(); update();
setupCustomTooltip(self, bind(tooltipText)); setupCustomTooltip(self, bind(tooltipText));
}} }}
>
<stack
transitionType={Gtk.StackTransitionType.SLIDE_UP_DOWN}
transitionDuration={120}
shown={bind(AstalNetwork.get_default(), "primary").as(p =>
p === AstalNetwork.Primary.WIFI ? "wifi" : "wired"
)}
> >
<stack <stack
name="wifi" name="wifi"
@ -274,17 +296,22 @@ const Network = () => (
<label className="icon" label="lan" name="connected" /> <label className="icon" label="lan" name="connected" />
</stack> </stack>
</stack> </stack>
</button>
); );
const Bluetooth = () => ( const Bluetooth = () => (
<stack <button
transitionType={Gtk.StackTransitionType.SLIDE_UP_DOWN} onClick={(_, event) => {
transitionDuration={120} if (event.button === Astal.MouseButton.PRIMARY) {
shown={bind(AstalBluetooth01.get_default(), "isPowered").as(p => (p ? "enabled" : "disabled"))} // TODO: bluetooth panel
} else if (event.button === Astal.MouseButton.SECONDARY) AstalBluetooth.get_default().toggle();
else if (event.button === Astal.MouseButton.MIDDLE)
execAsync("uwsm app -- blueman-manager").catch(console.error);
}}
setup={self => { setup={self => {
const tooltipText = Variable(""); const tooltipText = Variable("");
const update = () => { const update = () => {
const devices = AstalBluetooth01.get_default() const devices = AstalBluetooth.get_default()
.get_devices() .get_devices()
.filter(d => d.connected); .filter(d => d.connected);
tooltipText.set( tooltipText.set(
@ -293,14 +320,20 @@ const Bluetooth = () => (
: "No connected devices" : "No connected devices"
); );
}; };
self.hook(AstalBluetooth01.get_default(), "notify", update); self.hook(AstalBluetooth.get_default(), "notify", update); // TODO: fix not updating
update(); update();
setupCustomTooltip(self, bind(tooltipText)); setupCustomTooltip(self, bind(tooltipText));
}} }}
>
<stack
transitionType={Gtk.StackTransitionType.SLIDE_UP_DOWN}
transitionDuration={120}
shown={bind(AstalBluetooth.get_default(), "isPowered").as(p => (p ? "enabled" : "disabled"))}
> >
<label className="icon" label="bluetooth" name="enabled" /> <label className="icon" label="bluetooth" name="enabled" />
<label className="icon" label="bluetooth_disabled" name="disabled" /> <label className="icon" label="bluetooth_disabled" name="disabled" />
</stack> </stack>
</button>
); );
const StatusIcons = () => ( const StatusIcons = () => (