Clean up media module logic and code. (#380)

* Organized media menu code

* More consolidation
This commit is contained in:
Jas Singh
2024-10-27 00:17:51 -07:00
committed by GitHub
parent 86ff27fd3e
commit 14654998ea
23 changed files with 511 additions and 427 deletions

View File

@@ -0,0 +1,39 @@
import { Attribute } from 'lib/types/widget';
import { MprisPlayer } from 'types/service/mpris';
import Slider from 'types/widgets/slider';
export const updateTooltip = (self: Slider<Attribute>, foundPlayer: MprisPlayer): void => {
if (foundPlayer === undefined) {
self.tooltip_text = '00:00';
return;
}
const playerPosition = foundPlayer.position;
const curHour = Math.floor(playerPosition / 3600);
const curMin = Math.floor((playerPosition % 3600) / 60);
const curSec = Math.floor(playerPosition % 60);
if (typeof foundPlayer.position === 'number' && foundPlayer.position >= 0) {
const formatTime = (time: number): string => {
return time.toString().padStart(2, '0');
};
const formatHour = (hour: number): string => {
return hour > 0 ? formatTime(hour) + ':' : '';
};
self.tooltip_text = `${formatHour(curHour)}${formatTime(curMin)}:${formatTime(curSec)}`;
} else {
self.tooltip_text = `00:00`;
}
};
export const update = (self: Slider<Attribute>, foundPlayer: MprisPlayer): void => {
if (foundPlayer !== undefined) {
const value = foundPlayer.length ? foundPlayer.position / foundPlayer.length : 0;
self.value = value > 0 ? value : 0;
} else {
self.value = 0;
}
};

View File

@@ -0,0 +1,47 @@
const media = await Service.import('mpris');
import { BoxWidget } from 'lib/types/widget';
import { getPlayerInfo } from '../helpers';
import { update, updateTooltip } from './helpers';
const Bar = (): BoxWidget => {
return Widget.Box({
class_name: 'media-indicator-current-progress-bar',
hexpand: true,
children: [
Widget.Box({
hexpand: true,
child: Widget.Slider({
hexpand: true,
tooltip_text: '--',
class_name: 'menu-slider media progress',
draw_value: false,
on_change: ({ value }) => {
const foundPlayer = getPlayerInfo();
if (foundPlayer === undefined) {
return;
}
return (foundPlayer.position = value * foundPlayer.length);
},
setup: (self) => {
self.poll(1000, () => {
const foundPlayer = getPlayerInfo();
if (foundPlayer.play_back_status === 'Playing') {
update(self, foundPlayer);
updateTooltip(self, foundPlayer);
}
});
self.hook(media, () => {
const foundPlayer = getPlayerInfo();
update(self, foundPlayer);
updateTooltip(self, foundPlayer);
});
},
}),
}),
],
});
};
export { Bar };