Skip to content

Commit

Permalink
Add an ability to pause Image Carousel when hovered
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Sep 12, 2022
1 parent b91b695 commit 782f473
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 43 deletions.
31 changes: 17 additions & 14 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -183,28 +183,31 @@
Margin="0,148,0,0" Orientation="Vertical" HorizontalAlignment="Left">
<StackPanel HorizontalAlignment="Left">
<FlipView x:Name="ImageCarousel" Shadow="{StaticResource SharedShadow}" Visibility="Collapsed"
MaxWidth="414" Height="192"
CornerRadius="8" ItemsSource="{x:Bind MenuPanels.imageCarouselPanel}" HorizontalAlignment="Center">
MaxWidth="414" Height="192"
CornerRadius="8" ItemsSource="{x:Bind MenuPanels.imageCarouselPanel}"
HorizontalAlignment="Center"
PointerEntered="CarouselStopScroll"
PointerExited="CarouselRestartScroll">
<FlipView.ItemTemplate>
<DataTemplate>
<Image Source="{Binding Icon}" Tag="{Binding URL}" ToolTipService.ToolTip="{Binding Description}" PointerPressed="OpenImageLinkFromTag"/>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
<Grid x:Name="ImageCarouselPipsPager"
Padding="4,4" Margin="10,-14,10,0" CornerRadius="8"
Shadow="{StaticResource SharedShadow}"
Background="{StaticResource CarouselPipsAcrylicBrush}"
HorizontalAlignment="Right" Visibility="Collapsed"
VerticalAlignment="Center">
Padding="4,4" Margin="10,-14,10,0" CornerRadius="8"
Shadow="{StaticResource SharedShadow}"
Background="{StaticResource CarouselPipsAcrylicBrush}"
HorizontalAlignment="Right" Visibility="Collapsed"
VerticalAlignment="Center">
<PipsPager
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"/>
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"/>
</Grid>
</StackPanel>
<Grid x:Name="PostPanel" Padding="8" Width="414" Visibility="Collapsed"
Expand Down
60 changes: 31 additions & 29 deletions CollapseLauncher/XAMLs/MainApp/Pages/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public HomePage()
ImageCarousel.Translation += Shadow48;
ImageCarouselPipsPager.Translation += Shadow16;
PostPanel.Translation += Shadow16;

Task.Run(() => StartCarouselAutoScroll(PageToken.Token));
}

if (!GetAppConfigValue("ShowEventsPanel").ToBool())
Expand All @@ -80,6 +78,7 @@ public HomePage()

CheckFailedDeltaPatchState();
CheckRunningGameInstance();
StartCarouselAutoScroll(PageToken.Token);
}
catch (Exception ex)
{
Expand All @@ -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)
Expand Down

0 comments on commit 782f473

Please sign in to comment.