Skip to content

Commit

Permalink
Add option to toggle displaying MediaSession source
Browse files Browse the repository at this point in the history
  • Loading branch information
x-sheep committed Sep 17, 2023
1 parent ab39228 commit d4cba89
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# IDE0008: Use explicit type
dotnet_diagnostic.IDE0008.severity = none
16 changes: 16 additions & 0 deletions StreamThing/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,21 @@ private void Application_Startup(object sender, StartupEventArgs e)

Settings.Width = parsed.GetValueForOption(widthOption) ?? Settings.Width;
}

public async Task SaveSettings()
{
try
{
var settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StreamThing");
Directory.CreateDirectory(settingsFolder);
var settingsPath = Path.Combine(settingsFolder, "settings.json");
using var stream = new FileStream(settingsPath, FileMode.Create);
await JsonSerializer.SerializeAsync(stream, Settings);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
}
9 changes: 9 additions & 0 deletions StreamThing/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
Background="Black"
Loaded="Window_Loaded"
xmlns:ui="http://schemas.modernwpf.com/2019">
<Window.ContextMenu>
<ContextMenu>
<MenuItem Header="Display media source" Name="DisplayMediaSourceOption">
<MenuItem Tag="{x:Static local:MediaSourceVisibilty.Always}" Header="Always" Click="DisplayMediaSourceOption_Click"/>
<MenuItem Tag="{x:Static local:MediaSourceVisibilty.WhenPlaying}" Header="When playing" Click="DisplayMediaSourceOption_Click"/>
<MenuItem Tag="{x:Static local:MediaSourceVisibilty.Never}" Header="Never" Click="DisplayMediaSourceOption_Click"/>
</MenuItem>
</ContextMenu>
</Window.ContextMenu>
<Canvas>
<Label x:Name="StatusText" Foreground="White" Content="No session opened" Canvas.Left="16" Canvas.Top="8" FontSize="16" FontFamily="Segoe UI"/>
<Label x:Name="SongTitle" Foreground="White" Content="TITLE" Canvas.Left="16" Canvas.Top="42" FontSize="36" FontFamily="Segoe UI BOLD"/>
Expand Down
60 changes: 42 additions & 18 deletions StreamThing/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace StreamThing
/// </summary>
public partial class MainWindow : Window
{
private static App App => (App)Application.Current;

private static readonly MediaManager mediaManager = new MediaManager();
private string? lastMediaSession = null;

Expand All @@ -38,6 +40,8 @@ public MainWindow()
mediaManager.OnAnySessionOpened += MediaManager_OnAnySessionOpened;
mediaManager.OnAnySessionClosed += MediaManager_OnAnySessionClosed;

UpdatePopupMenu();

mediaManager.Start();

Application.Current.Dispatcher.Invoke(async () =>
Expand Down Expand Up @@ -94,6 +98,7 @@ private async Task LoadRandomSession()
StatusText.Content = "No session opened";
SongTitle.Content = "";
SongAuthor.Content = "";
StatusText.Visibility = App.Settings.MediaSourceVisibilty != MediaSourceVisibilty.Always ? Visibility.Hidden : Visibility.Visible;
}

private void MediaManager_OnAnyPlaybackStateChanged(MediaSession mediaSession, GlobalSystemMediaTransportControlsSessionPlaybackInfo playbackInfo)
Expand Down Expand Up @@ -121,34 +126,53 @@ private async Task UpdateUI(MediaSession mediaSession)
StatusText.Content = "Playing from " + lastMediaSession;
SongTitle.Content = songInfo.Title.ToUpper();
SongAuthor.Content = songInfo.Artist.Replace(" - Topic", "", StringComparison.InvariantCultureIgnoreCase);
StatusText.Visibility = App.Settings.MediaSourceVisibilty == MediaSourceVisibilty.Never ? Visibility.Hidden : Visibility.Visible;
}
}

private void UpdatePopupMenu()
{
var mediaSourceVisibilty = App.Settings.MediaSourceVisibilty;
foreach (var subview in DisplayMediaSourceOption.Items)
{
var option = (MenuItem)subview!;
var tag = (MediaSourceVisibilty)option.Tag;
option.IsChecked = mediaSourceVisibilty == tag;
}

if (mediaSourceVisibilty == MediaSourceVisibilty.Never ||
(mediaSourceVisibilty == MediaSourceVisibilty.WhenPlaying && !mediaManager.CurrentMediaSessions.Any())
)
{
StatusText.Visibility = Visibility.Hidden;
}
else
{
StatusText.Visibility = Visibility.Visible;
}
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
var app = (App)Application.Current;
if (app.Settings.Width.HasValue)
app.MainWindow.Width = Math.Max(100, app.Settings.Width.Value);
if (App.Settings.Width.HasValue)
App.MainWindow.Width = Math.Max(100, App.Settings.Width.Value);

app.MainWindow.SizeChanged += MainWindow_SizeChanged;
App.MainWindow.SizeChanged += MainWindow_SizeChanged;
}

private async void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
var app = (App)Application.Current;
app.Settings.Width = (int)e.NewSize.Width;
try
{
var settingsFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "StreamThing");
Directory.CreateDirectory(settingsFolder);
var settingsPath = Path.Combine(settingsFolder, "settings.json");
using var stream = new FileStream(settingsPath, FileMode.Create);
await JsonSerializer.SerializeAsync(stream, app.Settings);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
App.Settings.Width = (int)e.NewSize.Width;
await App.SaveSettings();
}

private async void DisplayMediaSourceOption_Click(object sender, RoutedEventArgs e)
{
var newValue = (MediaSourceVisibilty)((MenuItem)sender).Tag;
App.Settings.MediaSourceVisibilty = newValue;
UpdatePopupMenu();

await App.SaveSettings();
}
}
}
9 changes: 9 additions & 0 deletions StreamThing/SettingsObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,14 @@ namespace StreamThing
public class SettingsObject
{
public int? Width { get; set; } = null;

public MediaSourceVisibilty MediaSourceVisibilty { get; set; } = MediaSourceVisibilty.Always;
}

public enum MediaSourceVisibilty
{
Always,
WhenPlaying,
Never
}
}
4 changes: 4 additions & 0 deletions StreamThing/StreamThing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<Version>1.0.3</Version>
</PropertyGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dubya.WindowsMediaController" Version="2.2.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
Expand Down
5 changes: 5 additions & 0 deletions WindowsMediaController.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ VisualStudioVersion = 16.0.32228.343
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StreamThing", "StreamThing\StreamThing.csproj", "{0454D061-7BAB-4A24-995C-6FC5DC90CC55}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{94C39E39-01A3-463B-B907-29315981F007}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down

0 comments on commit d4cba89

Please sign in to comment.