[modules/spotify] improve update mechanism
instead of updating the widget list during each update, create the list of widgets during initialization, and later only update the widget states. see #702
This commit is contained in:
parent
7a4d4d5ab6
commit
965e7b2453
1 changed files with 36 additions and 34 deletions
|
@ -25,7 +25,6 @@ import core.input
|
||||||
import core.decorators
|
import core.decorators
|
||||||
import util.format
|
import util.format
|
||||||
|
|
||||||
|
|
||||||
class Module(core.module.Module):
|
class Module(core.module.Module):
|
||||||
def __init__(self, config, theme):
|
def __init__(self, config, theme):
|
||||||
super().__init__(config, theme, [])
|
super().__init__(config, theme, [])
|
||||||
|
@ -43,6 +42,39 @@ class Module(core.module.Module):
|
||||||
self.__cmd = "dbus-send --session --type=method_call --dest=org.mpris.MediaPlayer2.spotify \
|
self.__cmd = "dbus-send --session --type=method_call --dest=org.mpris.MediaPlayer2.spotify \
|
||||||
/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player."
|
/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player."
|
||||||
|
|
||||||
|
widget_map = {}
|
||||||
|
for widget_name in self.__layout:
|
||||||
|
widget = self.add_widget(name=widget_name)
|
||||||
|
if widget_name == "spotify.prev":
|
||||||
|
widget_map[widget] = {
|
||||||
|
"button": core.input.LEFT_MOUSE,
|
||||||
|
"cmd": self.__cmd + "Previous",
|
||||||
|
}
|
||||||
|
widget.set("state", "prev")
|
||||||
|
elif widget_name == "spotify.pause":
|
||||||
|
widget_map[widget] = {
|
||||||
|
"button": core.input.LEFT_MOUSE,
|
||||||
|
"cmd": self.__cmd + "PlayPause",
|
||||||
|
}
|
||||||
|
elif widget_name == "spotify.next":
|
||||||
|
widget_map[widget] = {
|
||||||
|
"button": core.input.LEFT_MOUSE,
|
||||||
|
"cmd": self.__cmd + "Next",
|
||||||
|
}
|
||||||
|
widget.set("state", "next")
|
||||||
|
elif widget_name == "spotify.song":
|
||||||
|
widget.set("state", "song")
|
||||||
|
widget.full_text(self.__song)
|
||||||
|
else:
|
||||||
|
raise KeyError(
|
||||||
|
"The spotify module does not have a {widget_name!r} widget".format(
|
||||||
|
widget_name=widget_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
for widget, callback_options in widget_map.items():
|
||||||
|
core.input.register(widget, **callback_options)
|
||||||
|
|
||||||
|
|
||||||
def hidden(self):
|
def hidden(self):
|
||||||
return self.string_song == ""
|
return self.string_song == ""
|
||||||
|
|
||||||
|
@ -62,23 +94,10 @@ class Module(core.module.Module):
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
try:
|
try:
|
||||||
self.clear_widgets()
|
|
||||||
self.__get_song()
|
self.__get_song()
|
||||||
|
|
||||||
widget_map = {}
|
for widget in self.widgets():
|
||||||
for widget_name in self.__layout:
|
if widget.name == "spotify.pause":
|
||||||
widget = self.add_widget(name=widget_name)
|
|
||||||
if widget_name == "spotify.prev":
|
|
||||||
widget_map[widget] = {
|
|
||||||
"button": core.input.LEFT_MOUSE,
|
|
||||||
"cmd": self.__cmd + "Previous",
|
|
||||||
}
|
|
||||||
widget.set("state", "prev")
|
|
||||||
elif widget_name == "spotify.pause":
|
|
||||||
widget_map[widget] = {
|
|
||||||
"button": core.input.LEFT_MOUSE,
|
|
||||||
"cmd": self.__cmd + "PlayPause",
|
|
||||||
}
|
|
||||||
playback_status = str(
|
playback_status = str(
|
||||||
dbus.Interface(
|
dbus.Interface(
|
||||||
dbus.SessionBus().get_object(
|
dbus.SessionBus().get_object(
|
||||||
|
@ -92,25 +111,8 @@ class Module(core.module.Module):
|
||||||
widget.set("state", "playing")
|
widget.set("state", "playing")
|
||||||
else:
|
else:
|
||||||
widget.set("state", "paused")
|
widget.set("state", "paused")
|
||||||
elif widget_name == "spotify.next":
|
|
||||||
widget_map[widget] = {
|
|
||||||
"button": core.input.LEFT_MOUSE,
|
|
||||||
"cmd": self.__cmd + "Next",
|
|
||||||
}
|
|
||||||
widget.set("state", "next")
|
|
||||||
elif widget_name == "spotify.song":
|
|
||||||
widget.set("state", "song")
|
|
||||||
widget.full_text(self.__song)
|
|
||||||
else:
|
|
||||||
raise KeyError(
|
|
||||||
"The spotify module does not have a {widget_name!r} widget".format(
|
|
||||||
widget_name=widget_name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
for widget, callback_options in widget_map.items():
|
|
||||||
core.input.register(widget, **callback_options)
|
|
||||||
|
|
||||||
except Exception:
|
except Exception as e:
|
||||||
self.__song = ""
|
self.__song = ""
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
Loading…
Reference in a new issue