Skip to content

Commit

Permalink
More queue on BG Image actions
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Oct 27, 2024
1 parent 5badadf commit 6d6378b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal enum MediaType
internal static readonly string[] SupportedMediaPlayerExt =
[".mp4", ".mov", ".mkv", ".webm", ".avi", ".gif"];

private FrameworkElement? _parentUI;
private static FrameworkElement? _parentUI;
private ImageUI? _bgImageBackground;
private ImageUI? _bgImageBackgroundLast;
private MediaPlayerElement? _bgMediaPlayerBackground;
Expand Down Expand Up @@ -72,6 +72,15 @@ internal enum MediaType
BoundedCapacity = 1,
TaskScheduler = TaskScheduler.Current
});
internal ActionBlock<Action> SharedActionBlockQueueChange = new ActionBlock<Action>(static (action) =>
_parentUI?.DispatcherQueue.TryEnqueue(() => action.Invoke()),
new ExecutionDataflowBlockOptions
{
EnsureOrdered = true,
MaxMessagesPerTask = 1,
MaxDegreeOfParallelism = 1,
BoundedCapacity = 1
});

/// <summary>
/// Attach and register the <see cref="Grid" /> of the page to be assigned with background utility.
Expand Down Expand Up @@ -305,7 +314,12 @@ internal async void LoadBackground(string mediaPath,
ThrowExceptionAction? throwAction = null,
Action? actionAfterLoaded = null)
{
await (SharedActionBlockQueue?.SendAsync(LoadBackgroundInner(mediaPath, isRequestInit, isForceRecreateCache, throwAction, actionAfterLoaded)) ?? Task.CompletedTask);
while (!await SharedActionBlockQueue?.SendAsync(LoadBackgroundInner(mediaPath, isRequestInit, isForceRecreateCache, throwAction, actionAfterLoaded))!)
{
// Delay the invoke 1/4 second and wait until the action can
// be sent again.
await Task.Delay(250);
}
}

private async Task LoadBackgroundInner(string mediaPath, bool isRequestInit = false,
Expand Down Expand Up @@ -393,80 +407,107 @@ private async Task LoadBackgroundInner(string mediaPath, bool
/// </summary>
internal void Dimm()
{
_loaderMediaPlayer?.Dimm();
_loaderStillImage?.Dimm();
SharedActionBlockQueueChange.Post(() =>
{
_loaderMediaPlayer?.Dimm();
_loaderStillImage?.Dimm();
});
}

/// <summary>
/// Undimming the current loaded background
/// </summary>
internal void Undimm()
{
_loaderMediaPlayer?.Undimm();
_loaderStillImage?.Undimm();
SharedActionBlockQueueChange.Post(() =>
{
_loaderMediaPlayer?.Undimm();
_loaderStillImage?.Undimm();
});
}

/// <summary>
/// Mute the audio of the currently loaded background
/// </summary>
internal void Mute()
{
_loaderMediaPlayer?.Mute();
_loaderStillImage?.Mute();
SharedActionBlockQueueChange.Post(() =>
{
_loaderMediaPlayer?.Mute();
_loaderStillImage?.Mute();
});
}

/// <summary>
/// Unmute the audio of the currently loaded background
/// </summary>
internal void Unmute()
{
_loaderMediaPlayer?.Unmute();
_loaderStillImage?.Unmute();
SharedActionBlockQueueChange.Post(() =>
{
_loaderMediaPlayer?.Unmute();
_loaderStillImage?.Unmute();
});
}

/// <summary>
/// Set the volume of the audio from the currently loaded background
/// </summary>
internal void SetVolume(double value)
{
_loaderMediaPlayer?.SetVolume(value);
_loaderStillImage?.SetVolume(value);
SharedActionBlockQueueChange.Post(() =>
{
_loaderMediaPlayer?.SetVolume(value);
_loaderStillImage?.SetVolume(value);
});
}

/// <summary>
/// Trigger the unfocused window event to the currently loaded background
/// </summary>
internal void WindowUnfocused()
{
_loaderMediaPlayer?.WindowUnfocused();
_loaderStillImage?.WindowUnfocused();
SharedActionBlockQueueChange.Post(() =>
{
_loaderMediaPlayer?.WindowUnfocused();
_loaderStillImage?.WindowUnfocused();
});
}

/// <summary>
/// Trigger the focused window event to the currently loaded background
/// </summary>
internal void WindowFocused()
{
_loaderMediaPlayer?.WindowFocused();
_loaderStillImage?.WindowFocused();
SharedActionBlockQueueChange.Post(() =>
{
_loaderMediaPlayer?.WindowFocused();
_loaderStillImage?.WindowFocused();
});
}

/// <summary>
/// Play/Resume the currently loaded background
/// </summary>
internal void Play()
{
_loaderMediaPlayer?.Play();
_loaderStillImage?.Play();
SharedActionBlockQueueChange.Post(() =>
{
_loaderMediaPlayer?.Play();
_loaderStillImage?.Play();
});
}

/// <summary>
/// Pause the currently loaded background
/// </summary>
internal void Pause()
{
_loaderMediaPlayer?.Pause();
_loaderStillImage?.Pause();
SharedActionBlockQueueChange.Post(() =>
{
_loaderMediaPlayer?.Pause();
_loaderStillImage?.Pause();
});
}

public static FileStream? GetAlternativeFileStream()
Expand Down
2 changes: 0 additions & 2 deletions CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,6 @@ public void SetThemeParameters()
#region Background Image
private void BackgroundImg_IsImageHideEvent(object sender, bool e)
{
if (IsFirstStartup) return;

if (e) CurrentBackgroundHandler?.Dimm();
else CurrentBackgroundHandler?.Undimm();
}
Expand Down

0 comments on commit 6d6378b

Please sign in to comment.