dotfiles/home/ags/files/config.js

255 lines
7.5 KiB
JavaScript
Raw Normal View History

2024-07-05 14:06:21 +02:00
const hyprland = await Service.import("hyprland")
const audio = await Service.import("audio")
const systemtray = await Service.import("systemtray")
2024-07-05 15:10:53 +02:00
import { getIconName } from "./utils.js"
2024-07-05 18:07:30 +02:00
import { NotificationPopups } from "./notificationPopups.js"
2024-07-06 00:01:45 +02:00
2024-07-05 23:18:09 +02:00
const battery = await Service.import('battery')
2024-07-06 00:01:45 +02:00
const powerProfiles = await Service.import('powerprofiles')
2024-07-05 14:06:21 +02:00
2024-07-06 13:21:49 +02:00
// temperature-cmd = "bash -c 'sensors | grep Tctl | cut -c16-22'";
// temperature-cmd = "bash -c 'sensors | grep Package | cut -c17-23'";
2024-07-05 22:46:16 +02:00
2024-07-05 23:18:09 +02:00
const batteryIndicator = Widget.Box({
2024-07-06 13:21:49 +02:00
visible: battery.available,
children: battery.available? [
2024-07-06 00:01:45 +02:00
Widget.Icon().hook(battery, self => {
const thresholds = [...Array(11).keys()].map( i => i * 10);
const icon = thresholds.find(threshold => threshold >= battery.percent)
self.icon = battery.charging? `battery-level-${icon}-charging-symbolic` : `battery-level-${icon}-symbolic`
self.tooltip_text = `Battery ${battery.percent}%`
2024-07-06 01:11:33 +02:00
self.class_name = "battery-item";
2024-07-06 00:01:45 +02:00
}),
]: []
2024-07-05 23:18:09 +02:00
})
2024-07-05 15:50:08 +02:00
const volumeIndicator = Widget.Button({
on_clicked: () => audio.speaker.is_muted = !audio.speaker.is_muted,
2024-07-05 18:07:30 +02:00
class_name: "item blue",
2024-07-05 15:50:08 +02:00
child: Widget.Box({
children: [
Widget.Icon().hook(audio.speaker, self => {
const vol = audio.speaker.volume * 100
const icon = [
[101, 'overamplified'],
[67, 'high'],
[34, 'medium'],
[1, 'low'],
[0, 'muted'],
].find(([threshold]) => threshold <= vol)?.[1]
self.icon = `audio-volume-${icon}-symbolic`
2024-07-06 00:01:45 +02:00
self.tooltip_text = `Volume ${Math.floor(vol)}%`
2024-07-05 15:50:08 +02:00
}),
Widget.Label().hook(audio.speaker, self => {
2024-07-05 18:07:30 +02:00
const label = `${Math.floor(audio.speaker.volume * 100)}%`
self.label = audio.speaker.is_muted ? "" : label
self.css = audio.speaker.is_muted ? "margin-left:0;": "margin-left: 0.7em;"
2024-07-05 15:50:08 +02:00
})
]}),
});
2024-07-05 14:06:21 +02:00
function Clients() {
const activeId = hyprland.active.client.bind("address")
2024-07-05 15:50:08 +02:00
const clients = hyprland.bind("clients").as(cl =>
2024-07-05 18:07:30 +02:00
cl.filter(a => !a.title.includes("rofi")).map(({ address, title, workspace }) =>
2024-07-05 15:50:08 +02:00
{
const short_title = title.length > 40 ? title.slice(0, 20) + "..." : title
2024-07-05 18:07:30 +02:00
return Widget.Box({
2024-07-05 15:50:08 +02:00
attribute: workspace.id,
2024-07-05 18:07:30 +02:00
class_name: activeId.as(i => `${i === address ? "focused" : ""}`),
children: [
Widget.Icon({
vexpand: false,
size: 16,
className: "app-icon",
icon: getIconName(hyprland.clients.find(c => c.address === address))
}),
Widget.Label(`${short_title}`)
],
2024-07-05 15:50:08 +02:00
})
2024-07-05 18:07:30 +02:00
})
2024-07-05 15:50:08 +02:00
)
2024-07-05 14:06:21 +02:00
return Widget.Box({
class_name: "clients",
children: clients,
2024-07-05 18:07:30 +02:00
setup: self => self.hook(hyprland, () => self.children.forEach(box => {
box.visible = hyprland.active.workspace.id === box.attribute
2024-07-05 15:50:08 +02:00
})),
2024-07-05 14:06:21 +02:00
})
}
function Workspaces() {
const activeId = hyprland.active.workspace.bind("id")
2024-07-05 15:10:53 +02:00
const workspaces = hyprland.bind("workspaces").as(ws => {
2024-07-05 14:06:21 +02:00
ws.sort((a, b) => a.id - b.id)
2024-07-05 15:10:53 +02:00
return ws.map(({ id, monitorID }) =>
Widget.Button({
attribute: monitorID,
2024-07-05 15:50:08 +02:00
label: `${id}`.slice(-1),
onClicked: () => hyprland.messageAsync(`dispatch workspace ${id}`),
2024-07-05 15:10:53 +02:00
class_name: activeId.as(i => `${i === id ? "focused" : ""}`),
})
)
2024-07-05 14:06:21 +02:00
})
return Widget.Box({
children: workspaces,
2024-07-05 15:50:08 +02:00
class_name: "workspaces",
2024-07-05 15:10:53 +02:00
setup: self => self.hook(hyprland, () => self.children.forEach(btn => {
btn.visible = hyprland.active.monitor.id === btn.attribute
})),
2024-07-05 14:06:21 +02:00
})
}
function SysTray() {
const items = systemtray.bind("items")
.as(items => items.map(item => Widget.Button({
child: Widget.Icon({
icon: item.bind("icon"),
2024-07-05 22:46:16 +02:00
class_name: "systray-item",
2024-07-05 14:06:21 +02:00
}),
on_primary_click: (_, event) => item.activate(event),
on_secondary_click: (_, event) => item.openMenu(event),
tooltip_markup: item.bind("tooltip_markup"),
2024-07-05 22:46:16 +02:00
class_name: "systray",
2024-07-05 14:06:21 +02:00
})))
return Widget.Box({
children: items,
})
}
function Left() {
return Widget.Box({
spacing: 8,
children: [
Clients(),
],
})
}
function Center() {
return Widget.Box({
spacing: 8,
children: [
Workspaces(),
],
})
}
function Right() {
return Widget.Box({
hpack: "end",
spacing: 8,
2024-07-05 18:07:30 +02:00
class_name: "right",
2024-07-05 14:06:21 +02:00
children: [
SysTray(),
2024-07-05 23:18:09 +02:00
batteryIndicator,
2024-07-05 15:50:08 +02:00
volumeIndicator,
2024-07-06 00:22:01 +02:00
Widget.Box({
2024-07-05 15:50:08 +02:00
class_name: "item",
2024-07-06 00:22:01 +02:00
children: [
Widget.Icon({
vexpand: false,
size: 16,
icon: "speedometer",
2024-07-06 00:48:20 +02:00
css: "margin-right: 0.7em;",
2024-07-06 00:22:01 +02:00
}),
Widget.Label({
label: Variable("", {
2024-07-06 00:36:05 +02:00
poll: [2000, 'top -b -n 1', out => out.split('\n')
2024-07-06 00:22:01 +02:00
.find(line => line.includes('Cpu(s)'))
.split(/\s+/)[1]
.replace(',', '.').toString() + "%"],
}).bind()
})
]}),
2024-07-05 18:07:30 +02:00
Widget.Label({
class_name: "item blue",
2024-07-05 22:46:16 +02:00
label: Variable("", {
2024-07-05 23:07:29 +02:00
poll: [2000, 'free', out => (out.split('\n')
2024-07-05 22:46:16 +02:00
.find(line => line.includes('Mem:'))
2024-07-05 23:07:29 +02:00
.split(/\s+/)[2] / 1000000).toFixed(2) + "GB"],
2024-07-05 22:46:16 +02:00
}).bind(),
2024-07-05 18:07:30 +02:00
}),
2024-07-06 00:48:20 +02:00
Widget.Button({
class_name: "item",
child: Widget.Box({
children: [
Widget.Icon({
vexpand: false,
size: 16,
icon: powerProfiles.bind('active_profile').as(profile => `power-profile-${profile}-symbolic`),
css: "margin-right: 0.7em;",
}),
Widget.Label({
label: Variable("", { poll: [5000, 'bash -c "cat /proc/cpuinfo | grep \\"MHz\\" | awk \'{print \\$4}\' | sort -n | tail -1 | awk \'{printf \\"%.2fGHz\\", \\$1/1000}\'"'] }).bind(),
}),
]
}),
on_clicked: () => {
2024-07-06 01:11:18 +02:00
let next = false
for (const profile of powerProfiles.profiles) {
if (next) {
powerProfiles.active_profile = profile["Profile"]
return
} else if (profile["Profile"] === powerProfiles.active_profile) {
next = true
}
}
powerProfiles.active_profile = powerProfiles.profiles[0]["Profile"]
2024-07-06 00:48:20 +02:00
},
}),
2024-07-05 18:07:30 +02:00
Widget.Label({
class_name: "item blue",
2024-07-06 13:21:49 +02:00
label: Variable("", {
poll: [5000, 'sensors', out => out.split('\n')
.find(line => line.includes('Tctl') || line.includes('Package'))
.split(/\s+/)[1].replace("+", "")],
}).bind(),
2024-07-05 18:07:30 +02:00
}),
2024-07-06 14:52:01 +02:00
Widget.Button({
2024-07-05 18:07:30 +02:00
class_name: "item",
2024-07-06 14:52:01 +02:00
child: Widget.Label({
label: Variable("", { poll: [1000, 'date "+%Y-%m-%d"'] }).bind(),
}),
on_clicked: () => Utils.execAsync(['gnome-calendar']),
2024-07-05 18:07:30 +02:00
}),
Widget.Label({
class_name: "item blue",
2024-07-05 15:50:08 +02:00
label: Variable("", { poll: [1000, 'date "+%H:%M:%S"'] }).bind(),
})
2024-07-05 14:06:21 +02:00
],
})
}
function Bar(monitor = 0) {
return Widget.Window({
2024-07-05 18:07:30 +02:00
name: `ags-bar-${monitor}`, // name has to be unique
2024-07-05 14:06:21 +02:00
class_name: "bar",
monitor,
anchor: ["top", "left", "right"],
exclusivity: "exclusive",
2024-07-06 13:27:03 +02:00
css: "font-size: " + (hyprland.monitors[monitor]["width"] > 1920? "1.2em;": "1em;"),
2024-07-05 14:06:21 +02:00
child: Widget.CenterBox({
2024-07-05 15:50:08 +02:00
class_name: "window-box",
2024-07-05 14:06:21 +02:00
start_widget: Left(),
center_widget: Center(),
end_widget: Right(),
}),
})
}
2024-07-05 01:33:59 +02:00
App.config({
2024-07-05 14:06:21 +02:00
style: "./style.css",
2024-07-05 18:24:53 +02:00
iconTheme: "Papirus",
2024-07-05 14:06:21 +02:00
windows: [
2024-07-06 14:52:01 +02:00
Bar(0),
2024-07-05 18:07:30 +02:00
NotificationPopups(),
2024-07-05 14:06:21 +02:00
],
2024-07-05 01:33:59 +02:00
})
2024-07-05 14:06:21 +02:00
export { }