-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<UserControl | ||
x:Class="GlazeWM.Bar.Components.MusicComponent" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:components="clr-namespace:GlazeWM.Bar.Components" | ||
mc:Ignorable="d"> | ||
<components:LabelComponent DataContext="{Binding Label}" /> | ||
</UserControl> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Windows.Controls; | ||
|
||
namespace GlazeWM.Bar.Components | ||
{ | ||
/// <summary> | ||
/// Interaction logic for MusicComponent.xaml | ||
/// </summary> | ||
public partial class MusicComponent : UserControl | ||
{ | ||
public MusicComponent() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using GlazeWM.Domain.UserConfigs; | ||
using Windows.Media.Control; | ||
using WindowsMediaController; | ||
|
||
namespace GlazeWM.Bar.Components | ||
{ | ||
public class MusicComponentViewModel : ComponentViewModel | ||
{ | ||
private readonly MusicComponentConfig _config; | ||
private static MediaManager mediaManager; | ||
private LabelViewModel _label; | ||
public LabelViewModel Label | ||
{ | ||
get => _label; | ||
protected set => SetField(ref _label, value); | ||
} | ||
public struct SongSession | ||
{ | ||
public SongSession(GlobalSystemMediaTransportControlsSessionPlaybackStatus musicStatus, string songTitle, string artistName) | ||
{ | ||
SongTitle = songTitle; | ||
ArtistName = artistName; | ||
MusicStatus = musicStatus; | ||
} | ||
public string SongTitle { get; set; } | ||
public string ArtistName { get; set; } | ||
public GlobalSystemMediaTransportControlsSessionPlaybackStatus MusicStatus { get; set; } | ||
} | ||
private readonly Dictionary<string, SongSession> songSessionDict; | ||
public MusicComponentViewModel( | ||
BarViewModel parentViewModel, | ||
MusicComponentConfig config) : base(parentViewModel, config) | ||
{ | ||
songSessionDict = new Dictionary<string, SongSession>(); | ||
_config = config; | ||
mediaManager = new MediaManager(); | ||
mediaManager.OnAnyMediaPropertyChanged += (session, args) => MusicTitleChanged(session.Id, args.Artist, args.Title); | ||
mediaManager.OnAnyPlaybackStateChanged += (session, args) => PlaybackStateChanged(session.Id, args.PlaybackStatus); | ||
mediaManager.OnAnySessionOpened += (session) => OpenedSession(session.Id); | ||
mediaManager.OnAnySessionClosed += (session) => ClosedSession(session.Id); | ||
mediaManager.Start(); | ||
} | ||
private string GetLabel(GlobalSystemMediaTransportControlsSessionPlaybackStatus musicStatus) | ||
{ | ||
return musicStatus switch | ||
{ | ||
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing => _config.LabelPlaying, | ||
GlobalSystemMediaTransportControlsSessionPlaybackStatus.Paused => _config.LabelPaused, | ||
_ => _config.LabelNotPlaying, | ||
}; | ||
} | ||
private void MusicTitleChanged(string sessionId, string artistName, string songTitle) | ||
{ | ||
var session = songSessionDict[sessionId]; | ||
session.ArtistName = artistName; | ||
session.SongTitle = songTitle; | ||
songSessionDict[sessionId] = session; | ||
Label = CreateLabel(); | ||
} | ||
private void PlaybackStateChanged(string sessionId, GlobalSystemMediaTransportControlsSessionPlaybackStatus status) | ||
{ | ||
var session = songSessionDict[sessionId]; | ||
session.MusicStatus = status; | ||
songSessionDict[sessionId] = session; | ||
Label = CreateLabel(); | ||
} | ||
private void OpenedSession(string sessionId) | ||
{ | ||
var session = new SongSession(GlobalSystemMediaTransportControlsSessionPlaybackStatus.Playing, "", ""); | ||
songSessionDict.Add(sessionId, session); | ||
} | ||
private void ClosedSession(string sessionId) | ||
{ | ||
songSessionDict.Remove(sessionId); | ||
Label = CreateLabel(); | ||
} | ||
private LabelViewModel CreateLabel() | ||
{ | ||
var label = _config.LabelNotPlaying; | ||
var variableDictionary = new Dictionary<string, Func<string>>() | ||
{ | ||
{ | ||
"song_title", () => "" | ||
}, | ||
{ | ||
"artist_name", () => "" | ||
} | ||
}; | ||
|
||
foreach (var session in songSessionDict) | ||
{ | ||
if(GetLabel(session.Value.MusicStatus) == _config.LabelPaused && label != _config.LabelPlaying) | ||
{ | ||
label = _config.LabelPaused; | ||
variableDictionary["song_title"] = () => session.Value.SongTitle; | ||
variableDictionary["artist_name"] = () => session.Value.ArtistName; | ||
} | ||
else if (GetLabel(session.Value.MusicStatus) == _config.LabelPlaying) | ||
{ | ||
label = _config.LabelPlaying; | ||
variableDictionary["song_title"] = () => session.Value.SongTitle; | ||
variableDictionary["artist_name"] = () => session.Value.ArtistName; | ||
} | ||
} | ||
return XamlHelper.ParseLabel( | ||
label, | ||
variableDictionary, | ||
this | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace GlazeWM.Domain.UserConfigs | ||
{ | ||
public class MusicComponentConfig : BarComponentConfig | ||
{ | ||
/// <summary> | ||
/// Formatted text to display when music is not playing. | ||
/// </summary> | ||
public string LabelNotPlaying { get; set; } = ""; | ||
/// <summary> | ||
/// Formatted text to display when music is not paused. | ||
/// </summary> | ||
public string LabelPaused { get; set; } = ""; | ||
/// <summary> | ||
/// Formatted text to display when music is playing. | ||
/// </summary> | ||
public string LabelPlaying { get; set; } = "{song_title} - {artist_name}"; | ||
} | ||
} |