frobar-ng: Fix Glib preventing closing
This commit is contained in:
parent
f22b42a711
commit
9916cb0170
|
@ -13,6 +13,7 @@ import time
|
|||
import typing
|
||||
|
||||
import gi
|
||||
import gi.events
|
||||
import i3ipc
|
||||
import i3ipc.aio
|
||||
import psutil
|
||||
|
@ -756,7 +757,9 @@ 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:
|
||||
|
@ -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())
|
||||
|
|
Loading…
Reference in a new issue