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

107 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using Windows.Data.Json;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
namespace CrossSoundWidget.Services
{
/// <summary>
/// The RedWhite brushes live once in App resources (Styles/RedWhite.xaml, keys "Rw" + token).
/// Everything references them with StaticResource, so changing a brush's Color here re-colors the
/// whole widget live. Colors come from CrossSound's /appearance when it answers, else from the
/// fixed RedWhite palettes below, picked by Game Bar's light/dark.
/// </summary>
public static class ThemeManager
{
// token name in the API → resource key suffix. Unknown API names are ignored.
private static readonly string[] Tokens =
{
"red", "redHover", "redTint", "redSoft", "redBorder", "onRed", "bg", "bgSoft", "bg2", "text",
"textMuted", "textSubtle", "iconInactive", "hoverColor", "field", "fieldFocus", "line", "hair",
};
private static readonly Dictionary<string, string> Light = new Dictionary<string, string>
{
{ "red", "#FF0000" }, { "redHover", "#FF6459" }, { "redTint", "#FF00000F" }, { "redSoft", "#FF00001F" },
{ "redBorder", "#FF000038" }, { "onRed", "#FFFFFF" }, { "bg", "#FFFFFF" }, { "bgSoft", "#FFF5F5" },
{ "bg2", "#FFEBEB" }, { "text", "#FF0000" }, { "textMuted", "#DA4545" }, { "textSubtle", "#FF746C" },
{ "iconInactive", "#E58B8B" }, { "hoverColor", "#FF7373" }, { "field", "#FF000012" },
{ "fieldFocus", "#FF00001F" }, { "line", "#FF0000" }, { "hair", "#DD2D4A" },
};
private static readonly Dictionary<string, string> Dark = new Dictionary<string, string>
{
{ "red", "#FF0000" }, { "redHover", "#FF6459" }, { "redTint", "#FF000014" }, { "redSoft", "#FF000024" },
{ "redBorder", "#FF000047" }, { "onRed", "#000000" }, { "bg", "#000000" }, { "bgSoft", "#0F0000" },
{ "bg2", "#260000" }, { "text", "#FF0000" }, { "textMuted", "#DA4545" }, { "textSubtle", "#FF746C" },
{ "iconInactive", "#9E5555" }, { "hoverColor", "#FF7373" }, { "field", "#FF00001A" },
{ "fieldFocus", "#FF000029" }, { "line", "#FF0000" }, { "hair", "#DD2D4A" },
};
public static bool IsDark { get; private set; }
/// <summary>Apply CrossSound's /appearance answer. False if it had no usable palette.</summary>
public static bool ApplyAppearance(JsonObject appearance)
{
var palette = Json.Obj(appearance, "palette");
if (palette == null) return false;
var dark = Json.Bool(appearance, "isDark") ?? false;
var fallback = dark ? Dark : Light;
foreach (var token in Tokens)
{
Color c;
if (!TryParse(Json.Str(palette, token), out c)) TryParse(fallback[token], out c);
SetBrush(token, c);
}
IsDark = dark;
return true;
}
/// <summary>The fixed RedWhite palette (no /appearance: the Electron server, or not running).</summary>
public static void ApplyFallback(bool dark)
{
foreach (var pair in dark ? Dark : Light)
{
Color c;
if (TryParse(pair.Value, out c)) SetBrush(pair.Key, c);
}
IsDark = dark;
}
public static ElementTheme ElementTheme { get { return IsDark ? ElementTheme.Dark : ElementTheme.Light; } }
public static Brush Brush(string token)
{
return (Brush)Application.Current.Resources["Rw" + char.ToUpperInvariant(token[0]) + token.Substring(1)];
}
private static void SetBrush(string token, Color color)
{
var brush = Brush(token) as SolidColorBrush;
if (brush != null && brush.Color != color) brush.Color = color;
}
/// <summary>
/// The API sends #RRGGBB or #RRGGBBAA (alpha last); XAML wants alpha first. Parse it here,
/// never hand the string to XAML.
/// </summary>
public static bool TryParse(string hex, out Color color)
{
color = Colors.Transparent;
if (string.IsNullOrEmpty(hex)) return false;
var s = hex.Trim().TrimStart('#');
if (s.Length != 6 && s.Length != 8) return false;
uint v;
if (!uint.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out v)) return false;
if (s.Length == 6)
color = Color.FromArgb(255, (byte)(v >> 16), (byte)(v >> 8), (byte)v);
else
color = Color.FromArgb((byte)v, (byte)(v >> 24), (byte)(v >> 16), (byte)(v >> 8));
return true;
}
}
}