Files
RedLintu16andClaude Opus 5.5 7caaccbd49 Rebuild the CrossSound Game Bar widget from scratch
A new UWP widget against CrossSound's local API (127.0.0.1:8963):
- Player with an interpolated clock, seek-on-release, play/pause, prev/next,
  like, shuffle, volume and mute
- Queue, Discover (history + shelves, refresh, play all, detail views) and
  Search (tracks, playlists, albums, artists)
- /events and /appearance/events loops with back-off and a visible
  connection status; signed-out states shown as such
- RedWhite styling with live theme from CrossSound's /appearance
- deploy.ps1 to build and register for the current user

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-28 19:58:36 -07:00

131 lines
5.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using CrossSoundWidget.Services;
using Windows.Data.Json;
namespace CrossSoundWidget.Models
{
/// <summary>One /state or /events snapshot. <c>{}</c> parses to <see cref="IsEmpty"/>.</summary>
public sealed class PlayerState
{
public static readonly PlayerState Empty = Parse(null);
public bool IsEmpty { get; private set; }
public bool IsPlaying { get; private set; }
public bool IsLiked { get; private set; }
public string Title { get; private set; }
public string Artist { get; private set; }
public string Artwork { get; private set; }
/// <summary>Seconds (the API sends whole seconds).</summary>
public double Position { get; private set; }
/// <summary>Seconds; 0 when unknown.</summary>
public double Duration { get; private set; }
public bool Shuffle { get; private set; }
public bool Autoplay { get; private set; }
/// <summary>0..1, or null when the server doesn't report it (Electron).</summary>
public double? Volume { get; private set; }
public List<QueueItem> Queue { get; private set; }
public int QueueIndex { get; private set; }
public static PlayerState Parse(JsonObject o)
{
var s = new PlayerState { Queue = new List<QueueItem>() };
if (o == null || (Json.Get(o, "title") == null && Json.Get(o, "queue") == null && Json.Get(o, "isPlaying") == null))
{
s.IsEmpty = true;
return s;
}
s.IsPlaying = Json.Bool(o, "isPlaying") ?? false;
s.IsLiked = Json.Bool(o, "isLiked") ?? false;
s.Title = Json.Str(o, "title") ?? "";
s.Artist = Json.Str(o, "artist") ?? "";
s.Artwork = Json.Str(o, "artwork") ?? "";
s.Position = Math.Max(0, Json.Num(o, "position") ?? 0);
s.Shuffle = Json.Bool(o, "shuffle") ?? false;
s.Autoplay = Json.Bool(o, "autoplay") ?? false;
var vol = Json.Num(o, "volume");
if (vol != null) s.Volume = Math.Max(0, Math.Min(1, vol.Value));
var queue = Json.Arr(o, "queue");
if (queue != null)
{
foreach (var v in queue)
{
var item = QueueItem.Parse(Json.AsObject(v));
if (item != null) s.Queue.Add(item);
}
}
s.QueueIndex = (int)(Json.Long(o, "queueIndex") ?? -1);
// The exact length is the current queue row's duration (ms); durationStr is the fallback.
double duration = 0;
if (s.QueueIndex >= 0 && s.QueueIndex < s.Queue.Count) duration = s.Queue[s.QueueIndex].DurationMs / 1000.0;
if (duration <= 0) duration = ParseClock(Json.Str(o, "durationStr"));
s.Duration = Math.Max(0, duration);
return s;
}
/// <summary>A copy with the play state flipped, for the instant feedback before /events confirms.</summary>
public PlayerState WithPlaying(bool playing, double position)
{
var c = (PlayerState)MemberwiseClone();
c.IsPlaying = playing;
c.Position = position;
return c;
}
public PlayerState WithLiked(bool liked) { var c = (PlayerState)MemberwiseClone(); c.IsLiked = liked; return c; }
public PlayerState WithShuffle(bool on) { var c = (PlayerState)MemberwiseClone(); c.Shuffle = on; return c; }
public PlayerState WithPosition(double position) { var c = (PlayerState)MemberwiseClone(); c.Position = position; return c; }
public PlayerState WithVolume(double volume) { var c = (PlayerState)MemberwiseClone(); c.Volume = volume; return c; }
/// <summary>"m:ss" or "h:mm:ss" to seconds; 0 when it can't be read.</summary>
public static double ParseClock(string text)
{
if (string.IsNullOrWhiteSpace(text)) return 0;
double total = 0;
foreach (var part in text.Trim().Split(':'))
{
int n;
if (!int.TryParse(part, NumberStyles.Integer, CultureInfo.InvariantCulture, out n)) return 0;
total = total * 60 + n;
}
return total;
}
public static string FormatClock(double seconds)
{
if (double.IsNaN(seconds) || seconds < 0) seconds = 0;
var t = (long)Math.Floor(seconds);
long h = t / 3600, m = (t % 3600) / 60, sec = t % 60;
return h > 0
? string.Format(CultureInfo.InvariantCulture, "{0}:{1:00}:{2:00}", h, m, sec)
: string.Format(CultureInfo.InvariantCulture, "{0}:{1:00}", m, sec);
}
}
public sealed class QueueItem
{
public string Id { get; private set; }
public string Title { get; private set; }
public string Artist { get; private set; }
public string Artwork { get; private set; }
public double DurationMs { get; private set; }
public static QueueItem Parse(JsonObject o)
{
if (o == null) return null;
return new QueueItem
{
Id = Json.Str(o, "id") ?? "",
Title = Json.Str(o, "title") ?? "",
Artist = Json.Str(o, "artist") ?? "",
Artwork = Json.Str(o, "artwork") ?? "",
DurationMs = Json.Num(o, "duration") ?? 0,
};
}
}
}