Skip to content

Commit

Permalink
upate
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jul 27, 2024
1 parent 603f2d5 commit 1dccec2
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 106 deletions.
48 changes: 36 additions & 12 deletions src/Starward/Controls/InstallGameController.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,37 @@
<FontIcon FontSize="16"
Glyph="&#xEBD3;"
IsTextScaleFactorEnabled="False" />
<ProgressRing Width="34"
Height="34"
Background="#60000000"
IsIndeterminate="{x:Bind ProgressIsIndeterminate}"
Value="{x:Bind ProgressValue}">
<ProgressRing.Resources>
<x:Double x:Key="ProgressRingStrokeThickness">2</x:Double>
</ProgressRing.Resources>
</ProgressRing>
</Grid>

<Button.Flyout>
<Flyout x:Name="Flyout_InstallGame" Placement="RightEdgeAlignedBottom">
<Flyout x:Name="Flyout_InstallGame"
Closed="Flyout_InstallGame_Closed"
Opened="Flyout_InstallGame_Opened"
Placement="RightEdgeAlignedBottom">
<Flyout.FlyoutPresenterStyle>
<Style BasedOn="{StaticResource DefaultFlyoutPresenterStyle}" TargetType="FlyoutPresenter">
<!--<Setter Property="MaxHeight" Value="758" />-->
</Style>
</Flyout.FlyoutPresenterStyle>
<Grid Width="320">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>

<ItemsControl Grid.Row="1" ItemsSource="{x:Bind InstallServices}">
<ItemsControl ItemsSource="{x:Bind InstallServices}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Spacing="12" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>

<Grid Height="44"
ColumnSpacing="16"
RowSpacing="2">
Expand All @@ -54,6 +62,7 @@
<ColumnDefinition />
</Grid.ColumnDefinitions>


<Grid Grid.RowSpan="3"
Width="44"
Height="44"
Expand All @@ -75,6 +84,7 @@
IsTextScaleFactorEnabled="False"
Text="{Binding StateText}" />


<ProgressBar Grid.Row="1"
Grid.Column="1"
MinHeight="4"
Expand Down Expand Up @@ -108,21 +118,33 @@
Visibility="{Binding RemainingTimeText, Converter={StaticResource ObjectToVisibilityConverter}}" />
</StackPanel>


<Grid x:Name="Grid_ActionButtonOverlay"
Grid.RowSpan="3"
Grid.ColumnSpan="2"
Background="Transparent"
Opacity="0"
PointerEntered="Grid_ActionButtonOverlay_PointerEntered"
PointerExited="Grid_ActionButtonOverlay_PointerExited">
<Grid.OpacityTransition>
<ScalarTransition />
</Grid.OpacityTransition>
<StackPanel Margin="0,-6,0,0"
<TextBlock x:Name="TextBlock_ProgressValue"
HorizontalAlignment="Right"
VerticalAlignment="Top"
FontSize="14"
IsTextScaleFactorEnabled="False"
Text="{Binding ProgressValueText}">
<TextBlock.OpacityTransition>
<ScalarTransition />
</TextBlock.OpacityTransition>
</TextBlock>
<StackPanel x:Name="StackPanel_ActionButton"
Margin="0,-6,0,0"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Opacity="0"
Orientation="Horizontal"
Spacing="4">
<StackPanel.OpacityTransition>
<ScalarTransition />
</StackPanel.OpacityTransition>
<Button Width="28"
Height="28"
Padding="0"
Expand All @@ -149,7 +171,9 @@
</StackPanel>
</Grid>


</Grid>

</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Expand Down
53 changes: 51 additions & 2 deletions src/Starward/Controls/InstallGameController.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public InstallGameController()
private ObservableCollection<InstallGameStateModel> _installServices = new();


[ObservableProperty]
private bool _ProgressIsIndeterminate;


[ObservableProperty]
private double _ProgressValue;


private void _installGameManager_InstallTaskAdded(object? sender, InstallGameStateModel e)
{
Expand Down Expand Up @@ -94,9 +101,26 @@ private void _timer_Tick(DispatcherQueueTimer sender, object args)
try
{
_semaphoreSlim.Wait();
long totalBytes = 0;
long finishedBytes = 0;
_installGameManager.UpdateSpeedState();
foreach (var model in InstallServices)
{
model.UpdateState();
if (model.Service.State is InstallGameState.Download)
{
totalBytes += model.Service.TotalBytes;
finishedBytes += model.Service.FinishBytes;
}
}
if (totalBytes > 0)
{
ProgressIsIndeterminate = false;
ProgressValue = 100d * finishedBytes / totalBytes;
}
else
{
ProgressIsIndeterminate = true;
}
}
finally
Expand All @@ -111,7 +135,14 @@ private void Grid_ActionButtonOverlay_PointerEntered(object sender, Microsoft.UI
{
if (sender is Grid grid)
{
grid.Opacity = 1;
if (grid.FindName("StackPanel_ActionButton") is StackPanel stackPanel)
{
stackPanel.Opacity = 1;
}
if (grid.FindName("TextBlock_ProgressValue") is TextBlock textBlock)
{
textBlock.Opacity = 0;
}
}
}

Expand All @@ -121,9 +152,27 @@ private void Grid_ActionButtonOverlay_PointerExited(object sender, Microsoft.UI.
{
if (sender is Grid grid)
{
grid.Opacity = 0;
if (grid.FindName("StackPanel_ActionButton") is StackPanel stackPanel)
{
stackPanel.Opacity = 0;
}
if (grid.FindName("TextBlock_ProgressValue") is TextBlock textBlock)
{
textBlock.Opacity = 1;
}
}
}


private void Flyout_InstallGame_Opened(object sender, object e)
{
_timer.Interval = TimeSpan.FromSeconds(0.1);
}


private void Flyout_InstallGame_Closed(object sender, object e)
{
_timer.Interval = TimeSpan.FromSeconds(1);
}

}
26 changes: 20 additions & 6 deletions src/Starward/Services/Download/InstallGameManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Starward.Core;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Threading;

namespace Starward.Services.Download;

Expand All @@ -12,7 +14,7 @@ internal class InstallGameManager
private readonly ConcurrentDictionary<GameBiz, InstallGameStateModel> _services = new();


public InstallGameManager()
private InstallGameManager()
{
_services = new();
}
Expand All @@ -23,22 +25,34 @@ public InstallGameManager()



public static long DownloadBytesInSecond;

public double Speed { get; set; }

public static long SpeedLimitBytesPerSecond { get; set; } = long.MaxValue;


public double SpeedLimit { get; set; }
public static bool IsExceedSpeedLimit => Interlocked.Read(ref DownloadBytesInSecond) >= SpeedLimitBytesPerSecond;


private long _lastTimeStamp;

public event EventHandler<InstallGameStateModel> InstallTaskAdded;

public void UpdateSpeedState()
{
long ts = Stopwatch.GetTimestamp();
if (ts - _lastTimeStamp >= Stopwatch.Frequency)
{
DownloadBytesInSecond = 0;
}
}


public event EventHandler<InstallGameStateModel> InstallTaskRemoved;

public event EventHandler<InstallGameStateModel> InstallTaskAdded;



public event EventHandler<InstallGameStateModel> InstallTaskRemoved;



Expand All @@ -59,7 +73,6 @@ public bool TryGetInstallService(GameBiz gameBiz, [NotNullWhen(true)] out Instal




public void AddInstallService(InstallGameService service)
{
var model = new InstallGameStateModel(service);
Expand Down Expand Up @@ -99,6 +112,7 @@ private void Model_InstallFailed(object? sender, EventArgs e)
}



private void Model_InstallCanceled(object? sender, EventArgs e)
{
if (sender is InstallGameStateModel model)
Expand Down
Loading

0 comments on commit 1dccec2

Please sign in to comment.