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]>
199 lines
7.2 KiB
C#
199 lines
7.2 KiB
C#
using System;
|
|
using Windows.Foundation;
|
|
using Windows.System;
|
|
using Windows.UI.Xaml;
|
|
using Windows.UI.Xaml.Controls;
|
|
using Windows.UI.Xaml.Input;
|
|
using Windows.UI.Xaml.Media;
|
|
using Windows.UI.Xaml.Shapes;
|
|
using CrossSoundWidget.Services;
|
|
|
|
namespace CrossSoundWidget.Controls
|
|
{
|
|
/// <summary>
|
|
/// A flat RedWhite bar (square ends, square thumb) for seeking and volume. Unlike Slider it
|
|
/// separates "the user is moving it" from "the user let go": <see cref="Preview"/> fires while
|
|
/// dragging, <see cref="Committed"/> fires once on release, on a click, or after keyboard / wheel
|
|
/// steps settle. While the user is interacting, <see cref="Value"/> ignores outside updates so
|
|
/// the clock can't yank the thumb from under the pointer.
|
|
/// </summary>
|
|
public sealed class SeekBar : UserControl
|
|
{
|
|
private readonly Grid _root;
|
|
private readonly Rectangle _track;
|
|
private readonly Rectangle _fill;
|
|
private readonly Rectangle _thumb;
|
|
private readonly TranslateTransform _thumbShift = new TranslateTransform();
|
|
private readonly DispatcherTimer _settle = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(350) };
|
|
private double _value;
|
|
private bool _dragging;
|
|
private bool _hover;
|
|
|
|
/// <summary>Fires with the new value (0..1) while the user is moving the bar.</summary>
|
|
public event Action<double> Preview;
|
|
/// <summary>Fires once with the final value (0..1) when the user is done.</summary>
|
|
public event Action<double> Committed;
|
|
|
|
/// <summary>How far an arrow key or a wheel notch moves the bar (0..1).</summary>
|
|
public double Step { get; set; } = 0.05;
|
|
|
|
public bool IsInteracting { get { return _dragging || _settle.IsEnabled; } }
|
|
|
|
public double Value
|
|
{
|
|
get { return _value; }
|
|
set
|
|
{
|
|
if (IsInteracting) return;
|
|
SetFraction(value);
|
|
}
|
|
}
|
|
|
|
public SeekBar()
|
|
{
|
|
IsTabStop = true;
|
|
UseSystemFocusVisuals = true;
|
|
Height = 24;
|
|
MinWidth = 40;
|
|
|
|
_track = new Rectangle { Height = 3, VerticalAlignment = VerticalAlignment.Center, Fill = ThemeManager.Brush("redSoft") };
|
|
_fill = new Rectangle { Height = 3, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Left, Fill = ThemeManager.Brush("red"), Width = 0 };
|
|
_thumb = new Rectangle
|
|
{
|
|
Width = 10, Height = 10, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Left,
|
|
Fill = ThemeManager.Brush("red"), Opacity = 0, RenderTransform = _thumbShift,
|
|
};
|
|
_root = new Grid { Background = new SolidColorBrush(Windows.UI.Colors.Transparent) };
|
|
_root.Children.Add(_track);
|
|
_root.Children.Add(_fill);
|
|
_root.Children.Add(_thumb);
|
|
Content = _root;
|
|
|
|
_root.SizeChanged += (s, e) => Layout();
|
|
_root.PointerEntered += (s, e) => { _hover = true; UpdateThumb(); };
|
|
_root.PointerExited += (s, e) => { _hover = false; UpdateThumb(); };
|
|
_root.PointerPressed += OnPointerPressed;
|
|
_root.PointerMoved += OnPointerMoved;
|
|
_root.PointerReleased += OnPointerReleased;
|
|
_root.PointerCaptureLost += OnPointerCaptureLost;
|
|
_root.PointerWheelChanged += OnWheel;
|
|
GotFocus += (s, e) => UpdateThumb();
|
|
LostFocus += (s, e) => UpdateThumb();
|
|
IsEnabledChanged += (s, e) => { Opacity = IsEnabled ? 1 : 0.4; UpdateThumb(); };
|
|
_settle.Tick += (s, e) => { _settle.Stop(); Fire(Committed); };
|
|
}
|
|
|
|
private void SetFraction(double v)
|
|
{
|
|
if (double.IsNaN(v)) v = 0;
|
|
_value = Math.Max(0, Math.Min(1, v));
|
|
Layout();
|
|
}
|
|
|
|
private void Layout()
|
|
{
|
|
var w = _root.ActualWidth;
|
|
if (w <= 0) return;
|
|
_fill.Width = w * _value;
|
|
_thumbShift.X = Math.Max(0, Math.Min(w - _thumb.Width, w * _value - _thumb.Width / 2));
|
|
}
|
|
|
|
private void UpdateThumb()
|
|
{
|
|
var show = IsEnabled && (_hover || _dragging || FocusState != FocusState.Unfocused);
|
|
_thumb.Opacity = show ? 1 : 0;
|
|
}
|
|
|
|
private double ValueAt(Point p)
|
|
{
|
|
var w = _root.ActualWidth;
|
|
return w > 0 ? p.X / w : 0;
|
|
}
|
|
|
|
private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
if (!IsEnabled) return;
|
|
_settle.Stop();
|
|
_dragging = true;
|
|
_root.CapturePointer(e.Pointer);
|
|
SetFraction(ValueAt(e.GetCurrentPoint(_root).Position));
|
|
UpdateThumb();
|
|
Fire(Preview);
|
|
e.Handled = true;
|
|
}
|
|
|
|
private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
if (!_dragging) return;
|
|
SetFraction(ValueAt(e.GetCurrentPoint(_root).Position));
|
|
Fire(Preview);
|
|
e.Handled = true;
|
|
}
|
|
|
|
private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
if (!_dragging) return;
|
|
SetFraction(ValueAt(e.GetCurrentPoint(_root).Position));
|
|
_root.ReleasePointerCapture(e.Pointer);
|
|
Finish();
|
|
e.Handled = true;
|
|
}
|
|
|
|
private void OnPointerCaptureLost(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
if (_dragging) Finish();
|
|
}
|
|
|
|
private void Finish()
|
|
{
|
|
_dragging = false;
|
|
UpdateThumb();
|
|
Fire(Committed);
|
|
}
|
|
|
|
private void OnWheel(object sender, PointerRoutedEventArgs e)
|
|
{
|
|
if (!IsEnabled) return;
|
|
var delta = e.GetCurrentPoint(_root).Properties.MouseWheelDelta;
|
|
Nudge(delta > 0 ? Step : -Step);
|
|
e.Handled = true;
|
|
}
|
|
|
|
protected override void OnKeyDown(KeyRoutedEventArgs e)
|
|
{
|
|
if (!IsEnabled) { base.OnKeyDown(e); return; }
|
|
switch (e.Key)
|
|
{
|
|
case VirtualKey.Left:
|
|
case VirtualKey.Down:
|
|
case VirtualKey.GamepadDPadLeft:
|
|
Nudge(-Step); e.Handled = true; break;
|
|
case VirtualKey.Right:
|
|
case VirtualKey.Up:
|
|
case VirtualKey.GamepadDPadRight:
|
|
Nudge(Step); e.Handled = true; break;
|
|
case VirtualKey.Home:
|
|
Nudge(-1); e.Handled = true; break;
|
|
case VirtualKey.End:
|
|
Nudge(1); e.Handled = true; break;
|
|
default:
|
|
base.OnKeyDown(e); break;
|
|
}
|
|
}
|
|
|
|
/// <summary>Keyboard and wheel steps preview at once and commit when they stop.</summary>
|
|
private void Nudge(double delta)
|
|
{
|
|
SetFraction(_value + delta);
|
|
Fire(Preview);
|
|
_settle.Stop();
|
|
_settle.Start();
|
|
}
|
|
|
|
private void Fire(Action<double> handler)
|
|
{
|
|
if (handler != null) handler(_value);
|
|
}
|
|
}
|
|
}
|