From 6d6378bc3dcd7b9f8f6a59c7a92d8a25cc1d02b9 Mon Sep 17 00:00:00 2001 From: Kemal Setya Adhi Date: Sun, 27 Oct 2024 20:26:46 +0700 Subject: [PATCH] More queue on BG Image actions --- .../Background/BackgroundMediaUtility.cs | 81 ++++++++++++++----- .../XAMLs/MainApp/MainPage.xaml.cs | 2 - 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/CollapseLauncher/Classes/Helper/Background/BackgroundMediaUtility.cs b/CollapseLauncher/Classes/Helper/Background/BackgroundMediaUtility.cs index f5fcc253d..e7890c201 100644 --- a/CollapseLauncher/Classes/Helper/Background/BackgroundMediaUtility.cs +++ b/CollapseLauncher/Classes/Helper/Background/BackgroundMediaUtility.cs @@ -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; @@ -72,6 +72,15 @@ internal enum MediaType BoundedCapacity = 1, TaskScheduler = TaskScheduler.Current }); + internal ActionBlock SharedActionBlockQueueChange = new ActionBlock(static (action) => + _parentUI?.DispatcherQueue.TryEnqueue(() => action.Invoke()), + new ExecutionDataflowBlockOptions + { + EnsureOrdered = true, + MaxMessagesPerTask = 1, + MaxDegreeOfParallelism = 1, + BoundedCapacity = 1 + }); /// /// Attach and register the of the page to be assigned with background utility. @@ -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, @@ -393,8 +407,11 @@ private async Task LoadBackgroundInner(string mediaPath, bool /// internal void Dimm() { - _loaderMediaPlayer?.Dimm(); - _loaderStillImage?.Dimm(); + SharedActionBlockQueueChange.Post(() => + { + _loaderMediaPlayer?.Dimm(); + _loaderStillImage?.Dimm(); + }); } /// @@ -402,8 +419,11 @@ internal void Dimm() /// internal void Undimm() { - _loaderMediaPlayer?.Undimm(); - _loaderStillImage?.Undimm(); + SharedActionBlockQueueChange.Post(() => + { + _loaderMediaPlayer?.Undimm(); + _loaderStillImage?.Undimm(); + }); } /// @@ -411,8 +431,11 @@ internal void Undimm() /// internal void Mute() { - _loaderMediaPlayer?.Mute(); - _loaderStillImage?.Mute(); + SharedActionBlockQueueChange.Post(() => + { + _loaderMediaPlayer?.Mute(); + _loaderStillImage?.Mute(); + }); } /// @@ -420,8 +443,11 @@ internal void Mute() /// internal void Unmute() { - _loaderMediaPlayer?.Unmute(); - _loaderStillImage?.Unmute(); + SharedActionBlockQueueChange.Post(() => + { + _loaderMediaPlayer?.Unmute(); + _loaderStillImage?.Unmute(); + }); } /// @@ -429,8 +455,11 @@ internal void Unmute() /// internal void SetVolume(double value) { - _loaderMediaPlayer?.SetVolume(value); - _loaderStillImage?.SetVolume(value); + SharedActionBlockQueueChange.Post(() => + { + _loaderMediaPlayer?.SetVolume(value); + _loaderStillImage?.SetVolume(value); + }); } /// @@ -438,8 +467,11 @@ internal void SetVolume(double value) /// internal void WindowUnfocused() { - _loaderMediaPlayer?.WindowUnfocused(); - _loaderStillImage?.WindowUnfocused(); + SharedActionBlockQueueChange.Post(() => + { + _loaderMediaPlayer?.WindowUnfocused(); + _loaderStillImage?.WindowUnfocused(); + }); } /// @@ -447,8 +479,11 @@ internal void WindowUnfocused() /// internal void WindowFocused() { - _loaderMediaPlayer?.WindowFocused(); - _loaderStillImage?.WindowFocused(); + SharedActionBlockQueueChange.Post(() => + { + _loaderMediaPlayer?.WindowFocused(); + _loaderStillImage?.WindowFocused(); + }); } /// @@ -456,8 +491,11 @@ internal void WindowFocused() /// internal void Play() { - _loaderMediaPlayer?.Play(); - _loaderStillImage?.Play(); + SharedActionBlockQueueChange.Post(() => + { + _loaderMediaPlayer?.Play(); + _loaderStillImage?.Play(); + }); } /// @@ -465,8 +503,11 @@ internal void Play() /// internal void Pause() { - _loaderMediaPlayer?.Pause(); - _loaderStillImage?.Pause(); + SharedActionBlockQueueChange.Post(() => + { + _loaderMediaPlayer?.Pause(); + _loaderStillImage?.Pause(); + }); } public static FileStream? GetAlternativeFileStream() diff --git a/CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs b/CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs index 3d9cb937b..d50a26ef8 100644 --- a/CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs +++ b/CollapseLauncher/XAMLs/MainApp/MainPage.xaml.cs @@ -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(); }