diff --git a/hm/desktop/frobar/.dev/new.py b/hm/desktop/frobar/.dev/new.py index 8acecc0..6233509 100644 --- a/hm/desktop/frobar/.dev/new.py +++ b/hm/desktop/frobar/.dev/new.py @@ -13,6 +13,7 @@ import time import typing import gi +import gi.events import i3ipc import i3ipc.aio import psutil @@ -756,11 +757,13 @@ class MprisProvider(MirrorProvider): text += f" {clip(title)}" self.title.setText(text) - def show_players(self, manager: gi.repository.Playerctl.PlayerManager, exclude: str | None = None) -> None: + def show_players( + self, manager: gi.repository.Playerctl.PlayerManager, exclude: str | None = None + ) -> None: # FIXME Opening and closing a chromium player, it will stay there (as default), # which is not the behaviour of playerctl somehow for name in manager.props.player_names: - if name == exclude: # DEBUG + if name == exclude: # DEBUG continue player = gi.repository.Playerctl.Player.new_from_name(name) self.show_player(player) @@ -824,7 +827,13 @@ class MprisProvider(MirrorProvider): log.debug(f"Player name vanished: {name}") self.show_players(manager, exclude=name) - def blocking(self) -> None: + async def run(self) -> None: + await super().run() + self.status = self.sectionType(parent=self.module, color=self.color) + self.album = self.sectionType(parent=self.module, color=self.color) + self.artist = self.sectionType(parent=self.module, color=self.color) + self.title = self.sectionType(parent=self.module, color=self.color) + manager = gi.repository.Playerctl.PlayerManager() manager.connect("name-appeared", self.on_name_appeared) manager.connect("player-appeared", self.on_player_appear) @@ -836,22 +845,6 @@ class MprisProvider(MirrorProvider): self.show_players(manager) - # FIXME Prevents graceful shutdown - main = gi.repository.GLib.MainLoop() - main.run() - - async def run(self) -> None: - await super().run() - self.status = self.sectionType(parent=self.module, color=self.color) - self.album = self.sectionType(parent=self.module, color=self.color) - self.artist = self.sectionType(parent=self.module, color=self.color) - self.title = self.sectionType(parent=self.module, color=self.color) - - # PyGObject can work with async code, but is experimental, - # also it running in its own event loop makes things tricky - loop = asyncio.get_running_loop() - await loop.run_in_executor(None, self.blocking) - class AlertingProvider(Provider): COLOR_NORMAL = rich.color.Color.parse("green") @@ -1293,4 +1286,9 @@ async def main() -> None: await bar.run() -asyncio.run(main()) +if __name__ == "__main__": + # Using GLib's event loop so we can run GLib's code + policy = gi.events.GLibEventLoopPolicy() + asyncio.set_event_loop_policy(policy) + loop = policy.get_event_loop() + loop.run_until_complete(main())