From 782f473f86a0acaee093363d67fa290332e3e013 Mon Sep 17 00:00:00 2001 From: Kemal Setya Adhi Date: Mon, 12 Sep 2022 15:53:38 +0700 Subject: [PATCH] Add an ability to pause Image Carousel when hovered --- .../XAMLs/MainApp/Pages/HomePage.xaml | 31 +++++----- .../XAMLs/MainApp/Pages/HomePage.xaml.cs | 60 ++++++++++--------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml b/CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml index b8908b7da..57b7bfb7f 100644 --- a/CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml +++ b/CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml @@ -183,8 +183,11 @@ Margin="0,148,0,0" Orientation="Vertical" HorizontalAlignment="Left"> + MaxWidth="414" Height="192" + CornerRadius="8" ItemsSource="{x:Bind MenuPanels.imageCarouselPanel}" + HorizontalAlignment="Center" + PointerEntered="CarouselStopScroll" + PointerExited="CarouselRestartScroll"> @@ -192,19 +195,19 @@ + Padding="4,4" Margin="10,-14,10,0" CornerRadius="8" + Shadow="{StaticResource SharedShadow}" + Background="{StaticResource CarouselPipsAcrylicBrush}" + HorizontalAlignment="Right" Visibility="Collapsed" + VerticalAlignment="Center"> + Margin="0,0,0,0" + VerticalAlignment="Center" + PreviousButtonVisibility="Visible" + NextButtonVisibility="Visible" + NumberOfPages="{x:Bind MenuPanels.imageCarouselPanel.Count}" + SelectedPageIndex="{x:Bind Path=ImageCarousel.SelectedIndex, Mode=TwoWay}" + Orientation="Horizontal"/> StartCarouselAutoScroll(PageToken.Token)); } if (!GetAppConfigValue("ShowEventsPanel").ToBool()) @@ -80,6 +78,7 @@ public HomePage() CheckFailedDeltaPatchState(); CheckRunningGameInstance(); + StartCarouselAutoScroll(PageToken.Token); } catch (Exception ex) { @@ -88,43 +87,46 @@ public HomePage() } } - private async void TryLoadEventPanelImage() + private void TryLoadEventPanelImage() { if (regionNewsProp.eventPanel == null) return; - await Task.Run(() => - { - DispatcherQueue.TryEnqueue(() => - { - ImageEventImgGrid.Visibility = Visibility.Visible; - ImageEventImg.Source = new BitmapImage(new Uri(regionNewsProp.eventPanel.icon)); - ImageEventImg.Tag = regionNewsProp.eventPanel.url; - }); - }); + ImageEventImgGrid.Visibility = Visibility.Visible; + ImageEventImg.Source = new BitmapImage(new Uri(regionNewsProp.eventPanel.icon)); + ImageEventImg.Tag = regionNewsProp.eventPanel.url; } public async void ResetLastTimeSpan() => await Task.Run(() => LastTimeSpan = Stopwatch.StartNew()); - private void StartCarouselAutoScroll(CancellationToken token = new CancellationToken(), int delay = 5) + private async void StartCarouselAutoScroll(CancellationToken token = new CancellationToken(), int delay = 5) { - DispatcherQueue.TryEnqueue(async () => + if (regionNewsProp.eventPanel == null) return; + try { - try + while (true) { - while (true) - { - await Task.Delay(delay * 1000, token); - if (ImageCarousel.SelectedIndex != MenuPanels.imageCarouselPanel.Count - 1) - ImageCarousel.SelectedIndex++; - else - for (int i = MenuPanels.imageCarouselPanel.Count; i > 0; i--) - { - ImageCarousel.SelectedIndex = i - 1; - await Task.Delay(100, token); - } - } + await Task.Delay(delay * 1000, token); + if (ImageCarousel.SelectedIndex != MenuPanels.imageCarouselPanel.Count - 1) + ImageCarousel.SelectedIndex++; + else + for (int i = MenuPanels.imageCarouselPanel.Count; i > 0; i--) + { + ImageCarousel.SelectedIndex = i - 1; + await Task.Delay(100, token); + } } - catch (Exception) { } - }); + } + catch (Exception) { } + } + + private void CarouselStopScroll(object sender, PointerRoutedEventArgs e) + { + PageToken.Cancel(); + } + + private void CarouselRestartScroll(object sender, PointerRoutedEventArgs e) + { + PageToken = new CancellationTokenSource(); + StartCarouselAutoScroll(PageToken.Token); } private void FadeInSocMedButton(object sender, PointerRoutedEventArgs e)