Skip to content

Commit

Permalink
Fixed non-media-changing functions using fallbacks after no media cha…
Browse files Browse the repository at this point in the history
…nge was detected.
  • Loading branch information
Neonalig committed Apr 14, 2021
1 parent 09c5b00 commit e72266d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 25 deletions.
66 changes: 58 additions & 8 deletions Commands/CommandsGlobal.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -52,16 +53,20 @@ static void TC_CSC( GSMTCSessionManager Sender, CurrentSessionChangedEventArgs A

public static async Task<FuzzyMediaInfo> GetCurrentMediaDetailsAsync() => await FuzzyMediaInfo.GetAsync(TransportControls);

internal static async Task CurrentSessionControlAsync(QMediaVSIXPackage? Package, Func<GSMTCSession, Task<bool>> SessionCommand, VirtualKey? FallbackKey, string FallbackMessage) {
(FuzzyMediaInfo Old, GSMTCSession? Session, _) = await FuzzyMediaInfo.GetAsyncEx(TransportControls);
internal static async Task CurrentSessionControlAsync(QMediaVSIXPackage? Package, Func<GSMTCSession, Task<bool>> SessionCommand, ITwoStageCheck FallbackCheck, VirtualKey? FallbackKey, string FallbackMessage) {
(FuzzyMediaInfo Old, GSMTCSession? Session, GSMTCMediaProperties? Props) = await FuzzyMediaInfo.GetAsyncEx(TransportControls);
await FallbackCheck.StageOneAsync(Old, Session, Props);
int FallbackDelay = Package?.FallbackDelay ?? -1;
Debug.WriteLine("Got Delay: " + FallbackDelay);
if (FallbackDelay == 0) {
Debug.WriteLine("Zero Delay. Fallback Immediately.");
SimulateKeypress(FallbackKey);
} else if (Session != null && await SessionCommand.Invoke(Session)) {
Debug.WriteLine("Successfully invoked command. No fallback required." + (FallbackDelay > 0 ? $" Fuzzy equivalence checks will follow in {FallbackDelay} ms" : " Additionally, delay < 0, therefore no extra fuzzy equivalence checks will be made."));
if (FallbackDelay > 0 && await DelayedCheckAsync(async () => Old.Equals(await FuzzyMediaInfo.GetAsync(TransportControls)), FallbackDelay)) {
if (FallbackDelay > 0 && await DelayedCheckAsync(async () => {
(FuzzyMediaInfo New, GSMTCSession? NewSession, GSMTCMediaProperties? NewProps) = await FuzzyMediaInfo.GetAsyncEx(TransportControls);
return await FallbackCheck.StageTwoAsync(New, NewSession, NewProps);
}, FallbackDelay)) {
Debug.WriteLine("Session was not null, and the command completed successfully, however the media stayed the since. Since delay > 0, using fallback...");
Debug.WriteLine(FallbackMessage);
SimulateKeypress(FallbackKey);
Expand All @@ -74,15 +79,60 @@ internal static async Task CurrentSessionControlAsync(QMediaVSIXPackage? Package
}
}

public static async Task CurrentSessionPlayPauseAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TryTogglePlayPauseAsync(), VirtualKey.VK_MEDIA_PLAY_PAUSE, "Unable to attach to / toggle playback of media within current session.");
public interface ITwoStageCheck {
Task StageOneAsync(FuzzyMediaInfo Old, GSMTCSession? Session, GSMTCMediaProperties? Props);
Task<bool> StageTwoAsync(FuzzyMediaInfo New, GSMTCSession? Session, GSMTCMediaProperties? Props);
}

public struct TwoStageCheck_MediaChange : ITwoStageCheck {
FuzzyMediaInfo _OldMedia;
public Task StageOneAsync(FuzzyMediaInfo Old, GSMTCSession? Session, GSMTCMediaProperties? Props) {
_OldMedia = Old;
return Task.CompletedTask;
}

public Task<bool> StageTwoAsync(FuzzyMediaInfo New, GSMTCSession? Session, GSMTCMediaProperties? Props) => Task.FromResult(_OldMedia.Equals(New));
}

public struct TwoStageCheck_PlaybackStatusChange : ITwoStageCheck {
GlobalSystemMediaTransportControlsSessionPlaybackStatus? _OldStatus;
public Task StageOneAsync(FuzzyMediaInfo Old, GSMTCSession? Session, GSMTCMediaProperties? Props) {
_OldStatus = Old.Status;
return Task.CompletedTask;
}

public Task<bool> StageTwoAsync( FuzzyMediaInfo New, GSMTCSession? Session, GSMTCMediaProperties? Props ) => Task.FromResult(EqualityComparer<GlobalSystemMediaTransportControlsSessionPlaybackStatus?>.Default.Equals(_OldStatus, New.Status));
}

public struct TwoStageCheck_RepeatModeChange : ITwoStageCheck {
MediaPlaybackAutoRepeatMode? _OldRepeatMode;
public Task StageOneAsync(FuzzyMediaInfo Old, GSMTCSession? Session, GSMTCMediaProperties? Props) {
_OldRepeatMode = Session?.GetPlaybackInfo()?.AutoRepeatMode;
return Task.CompletedTask;
}

public Task<bool> StageTwoAsync(FuzzyMediaInfo New, GSMTCSession? Session, GSMTCMediaProperties? Props) => Task.FromResult(EqualityComparer<MediaPlaybackAutoRepeatMode?>.Default.Equals(_OldRepeatMode, Session?.GetPlaybackInfo()?.AutoRepeatMode));
}

public struct TwoStageCheck_ShuffleModeChange : ITwoStageCheck {
bool? _OldShuffleActive;
public Task StageOneAsync(FuzzyMediaInfo Old, GSMTCSession? Session, GSMTCMediaProperties? Props) {
_OldShuffleActive = Session?.GetPlaybackInfo()?.IsShuffleActive;
return Task.CompletedTask;
}

public Task<bool> StageTwoAsync(FuzzyMediaInfo New, GSMTCSession? Session, GSMTCMediaProperties? Props) => Task.FromResult(EqualityComparer<bool?>.Default.Equals(_OldShuffleActive, Session?.GetPlaybackInfo()?.IsShuffleActive));
}

public static async Task CurrentSessionPlayPauseAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TryTogglePlayPauseAsync(), new TwoStageCheck_PlaybackStatusChange(), VirtualKey.VK_MEDIA_PLAY_PAUSE, "Unable to attach to / toggle playback of media within current session.");

public static async Task CurrentSessionSkipPrevAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TrySkipPreviousAsync(), VirtualKey.VK_MEDIA_PREV_TRACK, "Unable to attach to / skip to previous media within current session.");
public static async Task CurrentSessionSkipPrevAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TrySkipPreviousAsync(), new TwoStageCheck_MediaChange(), VirtualKey.VK_MEDIA_PREV_TRACK, "Unable to attach to / skip to previous media within current session.");

public static async Task CurrentSessionSkipNextAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TrySkipNextAsync(), VirtualKey.VK_MEDIA_NEXT_TRACK, "Unable to attach to / skip to next media within current session.");
public static async Task CurrentSessionSkipNextAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TrySkipNextAsync(), new TwoStageCheck_MediaChange(), VirtualKey.VK_MEDIA_NEXT_TRACK, "Unable to attach to / skip to next media within current session.");

public static async Task CurrentSessionShuffleAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TryChangeShuffleActiveAsync(InvertShuffle(Session.GetPlaybackInfo().IsShuffleActive)), null, "Unable to attach to / toggle media shuffling within current session.");
public static async Task CurrentSessionShuffleAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TryChangeShuffleActiveAsync(InvertShuffle(Session.GetPlaybackInfo().IsShuffleActive)), new TwoStageCheck_ShuffleModeChange(), null, "Unable to attach to / toggle media shuffling within current session.");

public static async Task CurrentSessionRepeatAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TryChangeAutoRepeatModeAsync(InvertRepeatMode(Session.GetPlaybackInfo().AutoRepeatMode)), null, "Unable to attach to / toggle repeat (list mode) of media within current session.");
public static async Task CurrentSessionRepeatAsync(QMediaVSIXPackage? Package) => await CurrentSessionControlAsync(Package, async Session => await Session.TryChangeAutoRepeatModeAsync(InvertRepeatMode(Session.GetPlaybackInfo().AutoRepeatMode)), new TwoStageCheck_RepeatModeChange(), null, "Unable to attach to / toggle repeat (list mode) of media within current session.");

internal static bool InvertShuffle(bool? Orig) => Orig switch {
true => false,
Expand Down
32 changes: 16 additions & 16 deletions Commands/CommandsMediaInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ internal sealed class CommandsMediaInfo {

CommandID MenuCommandID = new CommandID(CommandSet, CommandId);
OleMenuCommand MenuItem = new OleMenuCommand(Execute, MenuCommandID);
MenuItem.BeforeQueryStatus += MenuItem_BeforeQueryStatus;
//MenuItem.BeforeQueryStatus += MenuItem_BeforeQueryStatus;

CommandService.AddCommand(MenuItem);

//CommandsGlobal.OnSessionChange += OnMediaChanged;
}

static void MenuItem_BeforeQueryStatus(object Sender, EventArgs E) {
if (Sender is OleMenuCommand OMC) {
ThreadHelper.JoinableTaskFactory.RunAsync(async () => {
FuzzyMediaInfo FMI = await FuzzyMediaInfo.GetAsync(CommandsGlobal.TransportControls);
string Display = FMI.ToString();
Debug.WriteLine($"Will display: {Display} ({FMI})");
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
OMC.Text = Display;
foreach (object OMCProp in OMC.Properties) {
Debug.WriteLine($"Got property: {OMCProp} ({OMCProp.GetType()})");
}
Debug.WriteLine("Done Display.");
});
}
}
//static void MenuItem_BeforeQueryStatus(object Sender, EventArgs E) {
// if (Sender is OleMenuCommand OMC) {
// ThreadHelper.JoinableTaskFactory.RunAsync(async () => {
// FuzzyMediaInfo FMI = await FuzzyMediaInfo.GetAsync(CommandsGlobal.TransportControls);
// string Display = FMI.ToString();
// //Debug.WriteLine($"Will display: {Display} ({FMI})");
// await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// OMC.Text = Display;
// //foreach (object OMCProp in OMC.Properties) {
// // Debug.WriteLine($"Got property: {OMCProp} ({OMCProp.GetType()})");
// //}
// //Debug.WriteLine("Done Display.");
// });
// }
//}

//~CommandsMediaInfo() {
// CommandsGlobal.OnSessionChange -= OnMediaChanged;
Expand Down
2 changes: 1 addition & 1 deletion source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="QMediaVSIX.51b65c0a-fbc0-4cee-8587-b7324a1a0028" Version="0.1.0.0" Language="en-US" Publisher="Starflash Studios" />
<Identity Id="QMediaVSIX.51b65c0a-fbc0-4cee-8587-b7324a1a0028" Version="0.1.1.0" Language="en-US" Publisher="Starflash Studios" />
<DisplayName>QMediaVSIX</DisplayName>
<Description xml:space="preserve">Quickly play, pause and skip your currently playing media. </Description>
<MoreInfo>https://github.com/starflash-studios/QMediaVSIX</MoreInfo>
Expand Down

0 comments on commit e72266d

Please sign in to comment.