Skip to content

Commit 0cbfe18

Browse files
b0bbywanclaude
andcommitted
refactor(mpris): route capability properties through Capabilities.setFromProp
The six identical Can* arms in UpdatePlayerProperties and the reflection loop in loadCapabilitiesFromProps both mapped dbus-tagged bool fields; setFromProp is now the single tag-to-field mapping. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AKi5652rX9cdUDycdU1btS
1 parent c414d82 commit 0cbfe18

2 files changed

Lines changed: 23 additions & 49 deletions

File tree

backend/mpris/mpris.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -260,30 +260,8 @@ func (m *MPRISBackend) UpdatePlayerProperties(busName string, changed map[string
260260
players[i].Position = val
261261
players[i].PositionUpdatedAt = time.Now()
262262
}
263-
case "CanPlay":
264-
if val, ok := extract[bool](variant); ok {
265-
players[i].Capabilities.CanPlay = val
266-
}
267-
case "CanPause":
268-
if val, ok := extract[bool](variant); ok {
269-
players[i].Capabilities.CanPause = val
270-
}
271-
case "CanGoNext":
272-
if val, ok := extract[bool](variant); ok {
273-
players[i].Capabilities.CanGoNext = val
274-
}
275-
case "CanGoPrevious":
276-
if val, ok := extract[bool](variant); ok {
277-
players[i].Capabilities.CanGoPrevious = val
278-
}
279-
case "CanSeek":
280-
if val, ok := extract[bool](variant); ok {
281-
players[i].Capabilities.CanSeek = val
282-
}
283-
case "CanControl":
284-
if val, ok := extract[bool](variant); ok {
285-
players[i].Capabilities.CanControl = val
286-
}
263+
case "CanPlay", "CanPause", "CanGoNext", "CanGoPrevious", "CanSeek", "CanControl":
264+
players[i].Capabilities.setFromProp(key, variant)
287265
}
288266
}
289267

backend/mpris/player.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -233,34 +233,30 @@ func trackFromSignalMetadata(meta map[string]dbus.Variant) Track {
233233
return track
234234
}
235235

236-
// loadCapabilitiesFromProps loads capabilities from already retrieved properties.
237-
// Used by loadFromDBus() to avoid additional D-Bus calls.
238-
// Maps D-Bus properties (CanPlay, CanPause, etc.) to the Capabilities struct
239-
// using reflection and `dbus` tags.
240-
func (p *Player) loadCapabilitiesFromProps(props map[string]dbus.Variant) Capabilities {
241-
var caps Capabilities
242-
243-
val := reflect.ValueOf(&caps).Elem()
244-
typ := val.Type()
245-
246-
for i := 0; i < val.NumField(); i++ {
247-
field := val.Field(i)
248-
fieldType := typ.Field(i)
249-
250-
// Retrieve dbus tag
251-
dbusTag := fieldType.Tag.Get("dbus")
252-
if dbusTag == "" {
253-
continue
254-
}
255-
256-
// Retrieve property from props
257-
if variant, ok := props[dbusTag]; ok {
258-
if boolVal, ok := extract[bool](variant); ok {
259-
field.SetBool(boolVal)
260-
}
236+
// setFromProp sets the Capabilities field whose `dbus` tag matches name;
237+
// no-op when no field matches or the variant does not hold a bool.
238+
func (c *Capabilities) setFromProp(name string, variant dbus.Variant) {
239+
val, ok := extract[bool](variant)
240+
if !ok {
241+
return
242+
}
243+
v := reflect.ValueOf(c).Elem()
244+
typ := v.Type()
245+
for i := 0; i < v.NumField(); i++ {
246+
if typ.Field(i).Tag.Get("dbus") == name {
247+
v.Field(i).SetBool(val)
248+
return
261249
}
262250
}
251+
}
263252

253+
// loadCapabilitiesFromProps loads capabilities from already retrieved
254+
// properties, avoiding additional D-Bus calls.
255+
func (p *Player) loadCapabilitiesFromProps(props map[string]dbus.Variant) Capabilities {
256+
var caps Capabilities
257+
for name, variant := range props {
258+
caps.setFromProp(name, variant)
259+
}
264260
return caps
265261
}
266262

0 commit comments

Comments
 (0)