bar: custom module (#220)

This commit is contained in:
end-4
2024-02-27 12:03:48 +07:00
parent f4401abac2
commit d028fd162b
+61 -33
View File
@@ -1,12 +1,17 @@
const { GLib } = imports.gi;
import Widget from 'resource:///com/github/Aylur/ags/widget.js'; import Widget from 'resource:///com/github/Aylur/ags/widget.js';
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js'; import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
import Mpris from 'resource:///com/github/Aylur/ags/service/mpris.js'; import Mpris from 'resource:///com/github/Aylur/ags/service/mpris.js';
const { Box, Label, Overlay, Revealer } = Widget; const { Box, Button, EventBox, Label, Overlay, Revealer, Scrollable } = Widget;
const { execAsync, exec } = Utils; const { execAsync, exec } = Utils;
import { AnimatedCircProg } from "../.commonwidgets/cairo_circularprogress.js"; import { AnimatedCircProg } from "../.commonwidgets/cairo_circularprogress.js";
import { MaterialIcon } from '../.commonwidgets/materialicon.js'; import { MaterialIcon } from '../.commonwidgets/materialicon.js';
import { showMusicControls } from '../../variables.js'; import { showMusicControls } from '../../variables.js';
const CUSTOM_MODULE_CONTENT_SCRIPT = `${GLib.get_home_dir()}/.cache/ags/user/scripts/custom-module-poll.sh`;
const CUSTOM_MODULE_LEFTCLICK_SCRIPT = `${GLib.get_home_dir()}/.cache/ags/user/scripts/custom-module-leftclick.sh`;
const CUSTOM_MODULE_RIGHTCLICK_SCRIPT = `${GLib.get_home_dir()}/.cache/ags/user/scripts/custom-module-rightclick.sh`;
function trimTrackTitle(title) { function trimTrackTitle(title) {
if (!title) return ''; if (!title) return '';
const cleanPatterns = [ const cleanPatterns = [
@@ -17,10 +22,10 @@ function trimTrackTitle(title) {
return title; return title;
} }
const BarGroup = ({ child }) => Widget.Box({ const BarGroup = ({ child }) => Box({
className: 'bar-group-margin bar-sides', className: 'bar-group-margin bar-sides',
children: [ children: [
Widget.Box({ Box({
className: 'bar-group bar-group-standalone bar-group-pad-system', className: 'bar-group bar-group-standalone bar-group-pad-system',
children: [child], children: [child],
}), }),
@@ -34,7 +39,7 @@ const BarResource = (name, icon, command) => {
hpack: 'center', hpack: 'center',
}); });
const resourceProgress = Overlay({ const resourceProgress = Overlay({
child: Widget.Box({ child: Box({
vpack: 'center', vpack: 'center',
className: 'bar-batt', className: 'bar-batt',
homogeneous: true, homogeneous: true,
@@ -93,14 +98,14 @@ const switchToRelativeWorkspace = async (self, num) => {
export default () => { export default () => {
// TODO: use cairo to make button bounce smaller on click, if that's possible // TODO: use cairo to make button bounce smaller on click, if that's possible
const playingState = Widget.Box({ // Wrap a box cuz overlay can't have margins itself const playingState = Box({ // Wrap a box cuz overlay can't have margins itself
homogeneous: true, homogeneous: true,
children: [Widget.Overlay({ children: [Overlay({
child: Widget.Box({ child: Box({
vpack: 'center', vpack: 'center',
className: 'bar-music-playstate', className: 'bar-music-playstate',
homogeneous: true, homogeneous: true,
children: [Widget.Label({ children: [Label({
vpack: 'center', vpack: 'center',
className: 'bar-music-playstate-txt', className: 'bar-music-playstate-txt',
justification: 'center', justification: 'center',
@@ -121,9 +126,9 @@ export default () => {
] ]
})] })]
}); });
const trackTitle = Widget.Scrollable({ const trackTitle = Scrollable({
hexpand: true, hexpand: true,
child: Widget.Label({ child: Label({
className: 'txt-smallie txt-onSurfaceVariant', className: 'txt-smallie txt-onSurfaceVariant',
setup: (self) => self.hook(Mpris, label => { setup: (self) => self.hook(Mpris, label => {
const mpris = Mpris.getPlayer(''); const mpris = Mpris.getPlayer('');
@@ -142,30 +147,53 @@ export default () => {
trackTitle, trackTitle,
] ]
}) })
const systemResources = BarGroup({ const SystemResourcesOrCustomModule = () => {
child: Box({ // Check if ~/.cache/ags/user/scripts/custom-module-poll.sh exists
children: [ if (GLib.file_test(CUSTOM_MODULE_CONTENT_SCRIPT, GLib.FileTest.EXISTS)) {
BarResource('RAM Usage', 'memory', `LANG=C free | awk '/^Mem/ {printf("%.2f\\n", ($3/$2) * 100)}'`), return BarGroup({
Revealer({ child: Button({
revealChild: true, child: Label({
transition: 'slide_left', className: 'txt-smallie txt-onSurfaceVariant',
transitionDuration: 200, useMarkup: true,
child: Box({ setup: (self) => Utils.timeout(1, () => {
className: 'spacing-h-10 margin-left-10', self.label = exec(CUSTOM_MODULE_CONTENT_SCRIPT);
children: [ self.poll(5000, (self) => {
BarResource('Swap Usage', 'swap_horiz', `LANG=C free | awk '/^Swap/ {if ($2 > 0) printf("%.2f\\n", ($3/$2) * 100); else print "0";}'`), const content = exec(CUSTOM_MODULE_CONTENT_SCRIPT);
BarResource('CPU Usage', 'settings_motion_mode', `LANG=C top -bn1 | grep Cpu | sed 's/\\,/\\./g' | awk '{print $2}'`), self.label = content;
] })
}), })
setup: (self) => self.hook(Mpris, label => {
const mpris = Mpris.getPlayer('');
self.revealChild = (!mpris);
}), }),
onPrimaryClickRelease: () => execAsync(CUSTOM_MODULE_LEFTCLICK_SCRIPT).catch(print),
onSecondaryClickRelease: () => execAsync(CUSTOM_MODULE_RIGHTCLICK_SCRIPT).catch(print),
}) })
], });
}) } else {
}); return BarGroup({
return Widget.EventBox({ child: Box({
children: [
BarResource('RAM Usage', 'memory', `LANG=C free | awk '/^Mem/ {printf("%.2f\\n", ($3/$2) * 100)}'`),
Revealer({
revealChild: true,
transition: 'slide_left',
transitionDuration: 200,
child: Box({
className: 'spacing-h-10 margin-left-10',
children: [
BarResource('Swap Usage', 'swap_horiz', `LANG=C free | awk '/^Swap/ {if ($2 > 0) printf("%.2f\\n", ($3/$2) * 100); else print "0";}'`),
BarResource('CPU Usage', 'settings_motion_mode', `LANG=C top -bn1 | grep Cpu | sed 's/\\,/\\./g' | awk '{print $2}'`),
]
}),
setup: (self) => self.hook(Mpris, label => {
const mpris = Mpris.getPlayer('');
self.revealChild = (!mpris);
}),
})
],
})
});
}
}
return EventBox({
onScrollUp: (self) => switchToRelativeWorkspace(self, -1), onScrollUp: (self) => switchToRelativeWorkspace(self, -1),
onScrollDown: (self) => switchToRelativeWorkspace(self, +1), onScrollDown: (self) => switchToRelativeWorkspace(self, +1),
onPrimaryClickRelease: () => showMusicControls.setValue(!showMusicControls.value), onPrimaryClickRelease: () => showMusicControls.setValue(!showMusicControls.value),
@@ -175,7 +203,7 @@ export default () => {
className: 'spacing-h-5', className: 'spacing-h-5',
children: [ children: [
BarGroup({ child: musicStuff }), BarGroup({ child: musicStuff }),
systemResources, SystemResourcesOrCustomModule(),
] ]
}) })
}); });