Skip to content

Commit

Permalink
handle window state & user session & storage device changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jan 2, 2025
1 parent b33a8c5 commit 74d7db1
Show file tree
Hide file tree
Showing 17 changed files with 462 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/Starward/Controls/GameBannerAndPost.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public bool ShowBannerAndPost

private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
WeakReferenceMessenger.Default.RegisterAll(this);
WeakReferenceMessenger.Default.UnregisterAll(this);
_bannerTimer.Stop();
}

Expand Down
30 changes: 28 additions & 2 deletions src/Starward/Features/Background/AppBackground.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Starward.Core.HoYoPlay;
using Starward.Features.ViewHost;
using Starward.Frameworks;
using Starward.Helpers;
using System;
Expand Down Expand Up @@ -41,6 +42,7 @@ public AppBackground()
{
this.InitializeComponent();
WeakReferenceMessenger.Default.Register<BackgroundChangedMessage>(this, OnBackgroundChanged);
WeakReferenceMessenger.Default.Register<MainWindowStateChangedMessage>(this, OnMainWindowStateChanged);
Unloaded += (_, _) => { DisposeVideoResource(); WeakReferenceMessenger.Default.UnregisterAll(this); };
}

Expand Down Expand Up @@ -110,7 +112,7 @@ private void InitializeBackgroundImage()
BackgroundImageSource = new BitmapImage(new Uri(file));
try
{
string? hex = AppConfig.AccentColor;
string? hex = AppSetting.AccentColor;
if (!string.IsNullOrWhiteSpace(hex))
{
Color color = ColorHelper.ToColor(hex);
Expand Down Expand Up @@ -142,7 +144,7 @@ public async Task UpdateBackgroundAsync()
try
{
cancelSource?.Cancel();
cancelSource = new();
cancelSource = new(TimeSpan.FromSeconds(5));

if (CurrentGameId is null)
{
Expand Down Expand Up @@ -207,6 +209,7 @@ private async Task ChangeBackgroundImageAsync(string file, CancellationToken can
}

Color? color = AccentColorHelper.GetAccentColor(writeableBitmap.PixelBuffer, decodeWidth, decodeHeight);
AppSetting.AccentColor = color?.ToHex() ?? null;
AccentColorHelper.ChangeAppAccentColor(color);
BackgroundImageSource = writeableBitmap;
}
Expand Down Expand Up @@ -244,6 +247,7 @@ private async Task ChangeBackgroundImageAsync(string file, CancellationToken can
using IMemoryBufferReference memoryBufferReference = bitmapBuffer.CreateReference();
memoryBufferReference.As<AccentColorHelper.IMemoryBufferByteAccess>().GetBuffer(out nint bufferPtr, out uint capacity);
Color? color = AccentColorHelper.GetAccentColor(bufferPtr, capacity, decodeWidth, decodeHeight);
AppSetting.AccentColor = color?.ToHex() ?? null;
AccentColorHelper.ChangeAppAccentColor(color);
BackgroundImageSource = softwareBitmapSource;
}
Expand Down Expand Up @@ -302,4 +306,26 @@ private async void OnBackgroundChanged(object _, BackgroundChangedMessage messag



private void OnMainWindowStateChanged(object _, MainWindowStateChangedMessage message)
{
try
{
if (mediaPlayer is not null)
{
var state = mediaPlayer.PlaybackSession.PlaybackState;
if (message.Activate && state is not MediaPlaybackState.Playing)
{
mediaPlayer.Play();
}
else if (message.Hide || message.SessionLock)
{
mediaPlayer.Pause();
}
}
}
catch { }
}



}
15 changes: 13 additions & 2 deletions src/Starward/Features/GameLauncher/GameBannerAndPost.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,26 @@ private async void GameBannerAndPost_Loaded(object sender, RoutedEventArgs e)

private void GameBannerAndPost_Unloaded(object sender, RoutedEventArgs e)
{
WeakReferenceMessenger.Default.RegisterAll(this);
WeakReferenceMessenger.Default.UnregisterAll(this);
_bannerTimer.Stop();
}



private void OnMainWindowStateChanged(object _, MainWindowStateChangedMessage message)
{
// todo
try
{
if (message.Activate)
{
_bannerTimer.Start();
}
else if (message.Hide || message.SessionLock)
{
_bannerTimer.Stop();
}
}
catch { }
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Starward.Features.GameLauncher;

/// <summary>
/// 游戏安装路径变更
/// </summary>
internal class GameInstallPathChangedMessage
{

}
7 changes: 4 additions & 3 deletions src/Starward/Features/GameLauncher/GameLauncherPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@
VerticalAlignment="Bottom">
<Grid.Resources>
<cu:AttachedDropShadow x:Key="TextShadow"
BlurRadius="8"
CastTo="{x:Bind Border_TextShadowTarget}"
Offset="2" />
Offset="0,2" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="44" />
Expand All @@ -84,7 +85,7 @@
FontSize="12"
Visibility="{x:Bind IsInstallPathRemovableTipEnabled}">
<Run Foreground="{ThemeResource TextFillColorSecondaryBrush}" Text="{x:Bind lang:Lang.GameLauncherPage_RemovableStorageDeviceNotConnected}" />
<Hyperlink UnderlineStyle="None">
<Hyperlink Click="Hyperlink_LocateGame_Click" UnderlineStyle="None">
<Run Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}" Text="{x:Bind lang:Lang.GameLauncherPage_Relocate}" />
</Hyperlink>
</TextBlock>
Expand All @@ -97,7 +98,7 @@
FontSize="12"
Visibility="{x:Bind InstalledLocateGameEnabled}">
<Run Foreground="{ThemeResource TextFillColorSecondaryBrush}" Text="{x:Bind lang:Lang.InstallGameDialog_Installed}" />
<Hyperlink UnderlineStyle="None">
<Hyperlink Click="Hyperlink_LocateGame_Click" UnderlineStyle="None">
<Run Foreground="{ThemeResource AccentTextFillColorPrimaryBrush}" Text="{x:Bind lang:Lang.InstallGameDialog_LocateGame}" />
</Hyperlink>
</TextBlock>
Expand Down
93 changes: 89 additions & 4 deletions src/Starward/Features/GameLauncher/GameLauncherPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Logging;
using Starward.Features.Background;
using Starward.Features.ViewHost;
using Starward.Frameworks;
using Starward.Helpers;
using System;
Expand Down Expand Up @@ -34,8 +35,8 @@ public GameLauncherPage()



[NotifyPropertyChangedFor(nameof(InstalledLocateGameEnabled))]
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(InstalledLocateGameEnabled))]
public partial GameState GameState { get; set; }


Expand All @@ -46,6 +47,16 @@ public GameLauncherPage()
protected override void OnLoaded()
{
CheckGameVersion();
WeakReferenceMessenger.Default.Register<GameInstallPathChangedMessage>(this, OnGameInstallPathChanged);
WeakReferenceMessenger.Default.Register<MainWindowStateChangedMessage>(this, OnMainWindowStateChanged);
WeakReferenceMessenger.Default.Register<RemovableStorageDeviceChangedMessage>(this, OnRemovableStorageDeviceChanged);
}



protected override void OnUnloaded()
{
WeakReferenceMessenger.Default.UnregisterAll(this);
}


Expand Down Expand Up @@ -148,6 +159,79 @@ private async void CheckGameVersion()




/// <summary>
/// 定位游戏路径
/// </summary>
/// <returns></returns>
private async Task LocateGameAsync()
{
try
{
string? folder = await _gameLauncherService.LocateGameInstallFolderAsync(this.XamlRoot);
if (string.IsNullOrWhiteSpace(folder))
{
return;
}
AppSetting.SetGameInstallPath(CurrentGameBiz, folder);
CheckGameVersion();
}
catch (Exception ex)
{

}
}



/// <summary>
/// 定位游戏路径
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private async void Hyperlink_LocateGame_Click(Microsoft.UI.Xaml.Documents.Hyperlink sender, Microsoft.UI.Xaml.Documents.HyperlinkClickEventArgs args)
{
await LocateGameAsync();
}




private void OnGameInstallPathChanged(object _, GameInstallPathChangedMessage message)
{
CheckGameVersion();
}




private void OnMainWindowStateChanged(object _, MainWindowStateChangedMessage message)
{
try
{
if (message.Activate && (message.ElapsedOver(TimeSpan.FromMinutes(10)) || message.IsCrossingHour))
{
CheckGameVersion();
}
}
catch { }
}




private void OnRemovableStorageDeviceChanged(object _, RemovableStorageDeviceChangedMessage message)
{
try
{
CheckGameVersion();
}
catch { }
}




#endregion


Expand Down Expand Up @@ -210,6 +294,7 @@ private async Task StartGameAsync()




#region Drop Background File


Expand All @@ -219,9 +304,9 @@ private void RootGrid_DragOver(object sender, Microsoft.UI.Xaml.DragEventArgs e)
{
if (e.DataView.Contains(StandardDataFormats.StorageItems))
{
e.AcceptedOperation = DataPackageOperation.Copy;
Border_BackgroundDragIn.Opacity = 1;
}
e.AcceptedOperation = DataPackageOperation.Copy;
Border_BackgroundDragIn.Opacity = 1;
}
}


Expand Down
19 changes: 19 additions & 0 deletions src/Starward/Features/GameLauncher/GameLauncherService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml;
using Starward.Core;
using Starward.Core.HoYoPlay;
using Starward.Features.HoYoPlay;
using Starward.Frameworks;
using Starward.Helpers;
using System;
using System.ComponentModel;
using System.Diagnostics;
Expand Down Expand Up @@ -256,4 +258,21 @@ public async Task<bool> IsGameExeExistsAsync(GameId gameId, string? installPath
}



/// <summary>
/// 选择游戏安装目录
/// </summary>
/// <param name="xamlRoot"></param>
/// <returns></returns>
public async Task<string?> LocateGameInstallFolderAsync(XamlRoot xamlRoot)
{
// 判断是否为可移动存储设备,使用 DriveHelper.IsDeviceRemovableOrOnUSB
string? folder = await FileDialogHelper.PickFolderAsync(xamlRoot);
return Directory.Exists(folder) ? folder : null;
}





}
Loading

0 comments on commit 74d7db1

Please sign in to comment.