Skip to content

Commit 5f8433c

Browse files
committed
Fix app volume tooltip and control in taskbar
1 parent e9fede5 commit 5f8433c

4 files changed

Lines changed: 56 additions & 14 deletions

File tree

‎FluentFlyoutWPF/Classes/Utils/MediaPlayerData.cs‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private class CachedMediaPlayerInfo
1919
public required string Title { get; set; }
2020
public ImageSource? Icon { get; set; }
2121
public int ProcessId { get; set; }
22+
public required DateTime ProcessIdCheckedAt { get; set; }
2223
}
2324
// cache for media player info to avoid redundant process lookups
2425
private static readonly ConcurrentDictionary<string, CachedMediaPlayerInfo> mediaPlayerCache = [];
@@ -119,7 +120,8 @@ public static (string, ImageSource?) GetAndCacheMediaPlayerData(string mediaPlay
119120
{
120121
Title = shellTitle,
121122
Icon = shellIcon,
122-
ProcessId = -1 // no process match, -1 avoids colliding with real PIDs
123+
ProcessId = -1, // no process match, -1 avoids colliding with real PIDs
124+
ProcessIdCheckedAt = DateTime.UtcNow
123125
};
124126

125127
return (shellTitle, shellIcon);
@@ -141,7 +143,8 @@ public static (string, ImageSource?) GetAndCacheMediaPlayerData(string mediaPlay
141143
{
142144
Title = mediaTitle,
143145
Icon = mediaIcon,
144-
ProcessId = processData.ProcessId
146+
ProcessId = processData.ProcessId,
147+
ProcessIdCheckedAt = DateTime.UtcNow
145148
};
146149

147150
return (mediaTitle, mediaIcon);
@@ -151,9 +154,35 @@ public static (string, ImageSource?) GetAndCacheMediaPlayerData(string mediaPlay
151154
{
152155
GetAndCacheMediaPlayerData(mediaPlayerId);
153156

154-
if (!mediaPlayerCache.TryGetValue(mediaPlayerId, out var cachedInfo)
155-
&& (!mediaPlayerIdVariants.TryGetValue(mediaPlayerId, out var variantKey)
156-
|| !mediaPlayerCache.TryGetValue(variantKey, out cachedInfo))) return null;
157+
string cacheKey;
158+
if (mediaPlayerCache.TryGetValue(mediaPlayerId, out var cachedInfo))
159+
{
160+
cacheKey = mediaPlayerId;
161+
}
162+
else if (mediaPlayerIdVariants.TryGetValue(mediaPlayerId, out var variantKey)
163+
&& mediaPlayerCache.TryGetValue(variantKey, out cachedInfo))
164+
{
165+
cacheKey = variantKey;
166+
}
167+
else
168+
{
169+
return null;
170+
}
171+
172+
if (cachedInfo.ProcessId > 0)
173+
return cachedInfo.ProcessId;
174+
175+
if (DateTime.UtcNow - cachedInfo.ProcessIdCheckedAt < TimeSpan.FromSeconds(CACHE_DURATION_SECONDS))
176+
return null;
177+
178+
mediaPlayerCache.TryRemove(cacheKey, out _);
179+
mediaPlayerIdVariants.TryRemove(mediaPlayerId, out _);
180+
GetAndCacheMediaPlayerData(mediaPlayerId);
181+
182+
if (!mediaPlayerCache.TryGetValue(mediaPlayerId, out cachedInfo)
183+
&& (!mediaPlayerIdVariants.TryGetValue(mediaPlayerId, out var refreshedVariantKey)
184+
|| !mediaPlayerCache.TryGetValue(refreshedVariantKey, out cachedInfo)))
185+
return null;
157186

158187
return cachedInfo.ProcessId > 0 ? cachedInfo.ProcessId : null;
159188
}
@@ -286,7 +315,8 @@ private static bool IsBrowser(string processName) =>
286315
{
287316
Title = title,
288317
Icon = icon,
289-
ProcessId = processId
318+
ProcessId = processId,
319+
ProcessIdCheckedAt = DateTime.UtcNow
290320
};
291321
}
292322

‎FluentFlyoutWPF/Controls/TaskbarWidgetControl.xaml.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ private void Grid_MouseEnter(object sender, MouseEventArgs e)
179179
{
180180
if (string.IsNullOrEmpty(SongTitle.Text + SongArtist.Text)) return;
181181

182+
RefreshAppVolumeTooltip();
183+
182184
SolidColorBrush targetBackgroundBrush;
183185
// hover effects with animations, hard-coded colors because I can't find the resource brushes
184186
WindowsThemeDetector.GetWindowsTheme(out _, out var systemTheme);

‎FluentFlyoutWPF/MainWindow.xaml.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ private static bool MatchesFilterEntry(string entry, string appName, string appI
338338

339339
int? processId = MediaPlayerData.GetAndCacheProcessId(activeSession.Id);
340340
return processId.HasValue
341-
? volumeMixerViewModel.Sessions.FirstOrDefault(session => session.ProcessId == processId.Value)?.Volume
341+
? volumeMixerViewModel.GetSessionVolume(processId.Value)
342342
: null;
343343
}
344344

‎FluentFlyoutWPF/ViewModels/VolumeMixerViewModel.cs‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,30 @@ public bool TryAdjustMasterVolume(float delta)
135135

136136
public bool TryAdjustSessionVolume(int processId, float delta)
137137
{
138-
var session = Sessions.FirstOrDefault(session => session.ProcessId == processId);
138+
var session = FindSession(processId);
139139

140140
if (session == null)
141-
{
142-
RefreshSessions();
143-
session = Sessions.FirstOrDefault(session => session.ProcessId == processId);
144-
}
145-
146-
if (session == null) return false;
141+
return false;
147142

148143
session.AdjustVolume(delta);
149144
return true;
150145
}
151146

147+
public float? GetSessionVolume(int processId)
148+
{
149+
return FindSession(processId)?.Volume;
150+
}
151+
152+
private AudioSessionModel? FindSession(int processId)
153+
{
154+
var session = Sessions.FirstOrDefault(session => session.ProcessId == processId);
155+
if (session != null)
156+
return session;
157+
158+
RefreshSessions();
159+
return Sessions.FirstOrDefault(session => session.ProcessId == processId);
160+
}
161+
152162

153163
public void SyncMasterFromDevice()
154164
{

0 commit comments

Comments
 (0)