Skip to content

Commit

Permalink
bar: custom module (end-4#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
end-4 committed Feb 27, 2024
1 parent 7cd77e3 commit c7c692a
Showing 1 changed file with 61 additions and 33 deletions.
94 changes: 61 additions & 33 deletions .config/ags/modules/bar/music.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
const { GLib } = imports.gi;
import Widget from 'resource:///com/github/Aylur/ags/widget.js';
import * as Utils from 'resource:///com/github/Aylur/ags/utils.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;
import { AnimatedCircProg } from "../.commonwidgets/cairo_circularprogress.js";
import { MaterialIcon } from '../.commonwidgets/materialicon.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) {
if (!title) return '';
const cleanPatterns = [
Expand All @@ -17,10 +22,10 @@ function trimTrackTitle(title) {
return title;
}

const BarGroup = ({ child }) => Widget.Box({
const BarGroup = ({ child }) => Box({
className: 'bar-group-margin bar-sides',
children: [
Widget.Box({
Box({
className: 'bar-group bar-group-standalone bar-group-pad-system',
children: [child],
}),
Expand All @@ -34,7 +39,7 @@ const BarResource = (name, icon, command) => {
hpack: 'center',
});
const resourceProgress = Overlay({
child: Widget.Box({
child: Box({
vpack: 'center',
className: 'bar-batt',
homogeneous: true,
Expand Down Expand Up @@ -93,14 +98,14 @@ const switchToRelativeWorkspace = async (self, num) => {

export default () => {
// 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,
children: [Widget.Overlay({
child: Widget.Box({
children: [Overlay({
child: Box({
vpack: 'center',
className: 'bar-music-playstate',
homogeneous: true,
children: [Widget.Label({
children: [Label({
vpack: 'center',
className: 'bar-music-playstate-txt',
justification: 'center',
Expand All @@ -121,9 +126,9 @@ export default () => {
]
})]
});
const trackTitle = Widget.Scrollable({
const trackTitle = Scrollable({
hexpand: true,
child: Widget.Label({
child: Label({
className: 'txt-smallie txt-onSurfaceVariant',
setup: (self) => self.hook(Mpris, label => {
const mpris = Mpris.getPlayer('');
Expand All @@ -142,30 +147,53 @@ export default () => {
trackTitle,
]
})
const systemResources = BarGroup({
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);
const SystemResourcesOrCustomModule = () => {
// Check if ~/.cache/ags/user/scripts/custom-module-poll.sh exists
if (GLib.file_test(CUSTOM_MODULE_CONTENT_SCRIPT, GLib.FileTest.EXISTS)) {
return BarGroup({
child: Button({
child: Label({
className: 'txt-smallie txt-onSurfaceVariant',
useMarkup: true,
setup: (self) => Utils.timeout(1, () => {
self.label = exec(CUSTOM_MODULE_CONTENT_SCRIPT);
self.poll(5000, (self) => {
const content = exec(CUSTOM_MODULE_CONTENT_SCRIPT);
self.label = content;
})
})
}),
onPrimaryClickRelease: () => execAsync(CUSTOM_MODULE_LEFTCLICK_SCRIPT).catch(print),
onSecondaryClickRelease: () => execAsync(CUSTOM_MODULE_RIGHTCLICK_SCRIPT).catch(print),
})
],
})
});
return Widget.EventBox({
});
} else {
return BarGroup({
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),
onScrollDown: (self) => switchToRelativeWorkspace(self, +1),
onPrimaryClickRelease: () => showMusicControls.setValue(!showMusicControls.value),
Expand All @@ -175,7 +203,7 @@ export default () => {
className: 'spacing-h-5',
children: [
BarGroup({ child: musicStuff }),
systemResources,
SystemResourcesOrCustomModule(),
]
})
});
Expand Down

0 comments on commit c7c692a

Please sign in to comment.