Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 54 additions & 11 deletions Trdo/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.UI.Dispatching;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Win32;
Expand Down Expand Up @@ -29,13 +29,19 @@ public partial class App : Application
private Mutex? _singleInstanceMutex;
private EventWaitHandle? _trayIconRestoreEvent;
private TaskbarCreatedMonitor? _taskbarCreatedMonitor;
private DispatcherQueue? _uiDispatcherQueue;
private DispatcherQueueTimer? _restoreEventMonitorTimer;

/// <summary>
/// Maximum length for the now playing text in the tooltip before truncation.
/// </summary>
private const int MaxTooltipNowPlayingLength = 60;

/// <summary>
/// Maximum length for the full tray icon tooltip (NOTIFYICONDATA limit).
/// </summary>
private const int MaxTrayTooltipLength = 128;

public App()
{
InitializeComponent();
Expand Down Expand Up @@ -129,9 +135,11 @@ protected override async void OnLaunched(LaunchActivatedEventArgs args)
Debug.WriteLine($"[App] Failed to register TaskbarCreated monitor: {ex.Message}");
}

_uiDispatcherQueue = DispatcherQueue.GetForCurrentThread();

InitializeTrayIcon();
await UpdateTrayIconAsync();
UpdatePlayPauseCommandText();
UpdatePlayPauseCommandText(forceTooltip: true);
StartRestoreEventMonitor();
}

Expand Down Expand Up @@ -290,6 +298,9 @@ private async Task UpdateTrayIconAsync()
_trayIcon.SetIcon("Assets/Radio.ico");
}

// SetIcon can clear the native tooltip even when the text is unchanged.
UpdatePlayPauseCommandText(forceTooltip: true);

await Task.CompletedTask;
}

Expand All @@ -314,41 +325,73 @@ private static bool IsSystemInDarkMode()
}
}

private void UpdatePlayPauseCommandText()
private void UpdatePlayPauseCommandText(bool forceTooltip = false)
{
if (_trayIcon is null)
return;

string station = _playerVm.SelectedStation?.Name ?? string.Empty;
station = station.Split(" ", StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() ?? string.Empty;

string tooltip;
if (!_playerVm.CanPlay)
{
_trayIcon.Tooltip = "Trdo - Add a station to start listening";
tooltip = "Trdo - Add a station to start listening";
}
else if (_playerVm.IsPlaying)
{

// Include now playing info if available
if (_playerVm.HasNowPlaying)
{
// Truncate long now playing text to keep tooltip readable
string nowPlaying = _playerVm.NowPlaying;
if (nowPlaying.Length > MaxTooltipNowPlayingLength)
{
nowPlaying = string.Concat(nowPlaying.AsSpan(0, MaxTooltipNowPlayingLength - 3), "...");
}
_trayIcon.Tooltip = $"Trdo {station} (Playing)\n{nowPlaying}";

tooltip = $"Trdo {station} (Playing)\n{nowPlaying}";
}
else
{
_trayIcon.Tooltip = $"Trdo {station} (Playing)";
tooltip = $"Trdo {station} (Playing)";
}
}
else
{
_trayIcon.Tooltip = $"Trdo {station} (Paused)";
tooltip = $"Trdo {station} (Paused)";
}

SetTrayTooltip(tooltip, forceTooltip);
}

private void SetTrayTooltip(string? text, bool force = false)
{
if (_trayIcon is null)
return;

string tooltip = string.IsNullOrWhiteSpace(text) ? "Trdo" : text.Trim();
if (tooltip.Length > MaxTrayTooltipLength)
{
tooltip = string.Concat(tooltip.AsSpan(0, MaxTrayTooltipLength - 3), "...");
}

void Apply()
{
if (force)
{
// WinUIEx only sends a native tooltip update when the value changes.
_trayIcon.Tooltip = "\u200B";
}

_trayIcon.Tooltip = tooltip;
}

if (_uiDispatcherQueue is not null && !_uiDispatcherQueue.HasThreadAccess)
{
_uiDispatcherQueue.TryEnqueue(Apply);
return;
}

Apply();
}

private void StartRestoreEventMonitor()
Expand Down Expand Up @@ -405,7 +448,7 @@ private async Task EnsureTrayIconVisibleAsync()
}

await UpdateTrayIconAsync();
UpdatePlayPauseCommandText();
UpdatePlayPauseCommandText(forceTooltip: true);
}
catch (Exception ex)
{
Expand Down
Loading