Skip to content

Commit

Permalink
delete static reference of main page
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Jan 16, 2024
1 parent fc7539d commit 53ad273
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 23 deletions.
7 changes: 3 additions & 4 deletions src/Starward/Controls/SystemTrayControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Starward.Core;
using Starward.Pages;
using Starward.Messages;
using Starward.Services;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -113,9 +114,7 @@ private void Button_StartGame_Click(object sender, RoutedEventArgs e)
var process1 = _gameService.StartGame(game.GameBiz, AppConfig.IgnoreRunningGame);
if (process1 != null)
{
// todo
MainPage.Current.PauseVideo();
//User32.ShowWindow(MainWindow.Current.WindowHandle, ShowWindowCommand.SW_SHOWMINIMIZED);
WeakReferenceMessenger.Default.Send(new GameStartMessage(game.GameBiz));
_logger.LogInformation("Game started ({name}, {pid})", process1.ProcessName, process1.Id);
_ = _playTimeService.StartProcessToLogAsync(game.GameBiz);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Starward/Messages/GameStartMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Starward.Core;

namespace Starward.Messages;

public record GameStartMessage(GameBiz GameBiz);

7 changes: 7 additions & 0 deletions src/Starward/Messages/MainPageNavigateMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Microsoft.UI.Xaml.Media.Animation;
using System;

namespace Starward.Messages;

public record MainPageNavigateMessage(Type Page, object? Param = null, NavigationTransitionInfo? TransitionInfo = null);

3 changes: 3 additions & 0 deletions src/Starward/Messages/UpdateBackgroundImageMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Starward.Messages;

public record UpdateBackgroundImageMessage(bool Force);
2 changes: 1 addition & 1 deletion src/Starward/MyWindows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<sp:MainPage x:Name="mainPage" />
<sp:MainPage x:Name="mainPage" x:FieldModifier="public" />

<Frame Name="Frame_Overlay"
IsNavigationStackEnabled="False"
Expand Down
5 changes: 2 additions & 3 deletions src/Starward/MyWindows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.UI.Xaml.Media.Animation;
using Starward.Controls;
using Starward.Models;
using Starward.Pages;
using System;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -169,7 +168,7 @@ public void OverlayFrameNavigateTo(Type page, object? parameter)
{
overlayCancelTokenSource?.Cancel();
overlayCancelTokenSource = new CancellationTokenSource();
MainPage.Current?.PauseVideo();
mainPage.PauseVideo();
Frame_Overlay.Visibility = Visibility.Visible;
Frame_Overlay.Navigate(page, parameter!, new SuppressNavigationTransitionInfo());
if (Frame_Overlay.Content is UIElement element)
Expand Down Expand Up @@ -209,7 +208,7 @@ public void CloseOverlayPage()
}
Frame_Overlay.Visibility = Visibility.Collapsed;
Frame_Overlay.Content = null;
MainPage.Current?.UpdateDragRectangles();
mainPage.UpdateDragRectangles();
};
sb.Begin();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Starward/Pages/GameNoticesPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Imaging;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.Web.WebView2.Core;
using Starward.Core;
using Starward.Core.Launcher;
using Starward.Messages;
using Starward.Models;
using Starward.Services;
using System;
Expand Down Expand Up @@ -118,7 +120,7 @@ private async Task InitializeWebAsync()
string? param = node?["param"]?.ToString();
if (action is "close")
{
MainPage.Current.NavigateTo(typeof(LauncherPage));
WeakReferenceMessenger.Default.Send(new MainPageNavigateMessage(typeof(LauncherPage)));
}
if (action is "url")
{
Expand Down
4 changes: 2 additions & 2 deletions src/Starward/Pages/HoyolabToolbox/HoyolabToolboxPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private async Task CheckAgreementAsync()
sw.Stop();
if (result is ContentDialogResult.Secondary)
{
MainPage.Current.NavigateTo(typeof(LauncherPage));
WeakReferenceMessenger.Default.Send(new MainPageNavigateMessage(typeof(LauncherPage)));
return;
}
if (sw.ElapsedMilliseconds < 5000)
Expand All @@ -129,7 +129,7 @@ private async Task CheckAgreementAsync()
result = await dialog.ShowAsync();
if (result is ContentDialogResult.Secondary)
{
MainPage.Current.NavigateTo(typeof(LauncherPage));
WeakReferenceMessenger.Default.Send(new MainPageNavigateMessage(typeof(LauncherPage)));
return;
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/Starward/Pages/LauncherPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private async Task UpdateGameNoticesAlertAsync()
[RelayCommand]
private void NavigateToGameNoticesPage()
{
MainPage.Current.NavigateTo(typeof(GameNoticesPage));
WeakReferenceMessenger.Default.Send(new MainPageNavigateMessage(typeof(GameNoticesPage)));
}


Expand Down Expand Up @@ -613,7 +613,7 @@ private async Task StartGameAsync()
}
else
{
MainPage.Current.PauseVideo();
WeakReferenceMessenger.Default.Send(new GameStartMessage(CurrentGameBiz));
var action = AppConfig.AfterStartGameAction;
if (action is AfterStartGameAction.Minimize)
{
Expand Down Expand Up @@ -1497,9 +1497,9 @@ private void TextBlock_IsTextTrimmedChanged(TextBlock sender, IsTextTrimmedChang
private int videoBgVolume = AppConfig.VideoBgVolume;
partial void OnVideoBgVolumeChanged(int value)
{
if (MainPage.Current is not null)
if (MainWindow.Current?.mainPage is not null)
{
MainPage.Current.VideoBgVolume = value;
MainWindow.Current.mainPage.VideoBgVolume = value;
}
}

Expand Down Expand Up @@ -1546,7 +1546,7 @@ private void Mute()
partial void OnEnableCustomBgChanged(bool value)
{
AppConfig.SetEnableCustomBg(CurrentGameBiz, value);
_ = MainPage.Current.UpdateBackgroundImageAsync(true);
WeakReferenceMessenger.Default.Send(new UpdateBackgroundImageMessage(true));
UpdateGameButtonStyle();
}

Expand All @@ -1563,7 +1563,7 @@ private async Task ChangeCustomBgAsync()
{
CustomBg = file;
AppConfig.SetCustomBg(CurrentGameBiz, file);
_ = MainPage.Current.UpdateBackgroundImageAsync(true);
WeakReferenceMessenger.Default.Send(new UpdateBackgroundImageMessage(true));
}
}

Expand All @@ -1580,7 +1580,7 @@ private void DeleteCustomBg()
{
AppConfig.SetCustomBg(CurrentGameBiz, null);
CustomBg = null;
_ = MainPage.Current.UpdateBackgroundImageAsync(true);
WeakReferenceMessenger.Default.Send(new UpdateBackgroundImageMessage(true));
}


Expand Down
7 changes: 3 additions & 4 deletions src/Starward/Pages/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ namespace Starward.Pages;
public sealed partial class MainPage : PageBase
{

public static MainPage Current { get; private set; }


private readonly ILogger<MainPage> _logger = AppConfig.GetLogger<MainPage>();

Expand All @@ -67,7 +65,6 @@ public sealed partial class MainPage : PageBase

public MainPage()
{
Current = this;
this.InitializeComponent();
compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
InitializeGameBiz();
Expand All @@ -92,7 +89,6 @@ protected override async void OnLoaded()

protected override void OnUnloaded()
{
Current = null!;
mediaPlayer?.Dispose();
softwareBitmap?.Dispose();
WeakReferenceMessenger.Default.UnregisterAll(this);
Expand All @@ -104,6 +100,9 @@ private void RegisterMessage()
{
WeakReferenceMessenger.Default.Register<LanguageChangedMessage>(this, OnLanguageChanged);
WeakReferenceMessenger.Default.Register<NavigationViewCompactChangedMessage>(this, InitializeNavigationViewPaneDisplayMode);
WeakReferenceMessenger.Default.Register<GameStartMessage>(this, (_, _) => PauseVideo());
WeakReferenceMessenger.Default.Register<UpdateBackgroundImageMessage>(this, (_, m) => _ = UpdateBackgroundImageAsync(m.Force));
WeakReferenceMessenger.Default.Register<MainPageNavigateMessage>(this, (_, m) => NavigateTo(m.Page, m.Param, m.TransitionInfo));
}


Expand Down
2 changes: 1 addition & 1 deletion src/Starward/Pages/Setting/AppearanceSettingPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void RadioButton_WindowSize_Click(object sender, RoutedEventArgs e)
partial void OnUseSystemThemeColorChanged(bool value)
{
AppConfig.UseSystemThemeColor = value;
_ = MainPage.Current.UpdateBackgroundImageAsync(true);
WeakReferenceMessenger.Default.Send(new UpdateBackgroundImageMessage(true));
}


Expand Down

0 comments on commit 53ad273

Please sign in to comment.