Skip to content

Commit

Permalink
添加新控件,优化部分功能组件性能
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Jul 27, 2024
1 parent f06c893 commit f650be2
Show file tree
Hide file tree
Showing 28 changed files with 375 additions and 194 deletions.
1 change: 1 addition & 0 deletions WonderLab/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<ResourceInclude Source="/Views/Controls/Themes/Controls/ComboBoxTheme.axaml"/>
<ResourceInclude Source="/Views/Controls/Themes/Controls/FontIconTheme.axaml"/>
<ResourceInclude Source="/Views/Controls/Themes/Controls/ImageBoxTheme.axaml"/>
<ResourceInclude Source="/Views/Controls/Themes/Controls/ImageCardTheme.axaml"/>
<ResourceInclude Source="/Views/Controls/Themes/Controls/PopupRootTheme.axaml"/>
<ResourceInclude Source="/Views/Controls/Themes/Controls/SettingCardTheme.axaml"/>
<ResourceInclude Source="/Views/Controls/Themes/Controls/ProgressBarTheme.axaml"/>
Expand Down
7 changes: 5 additions & 2 deletions WonderLab/Classes/Datas/TaskData/PreLaunchCheckTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using WonderLab.ViewModels.Dialogs.Setting;
using CommunityToolkit.Mvvm.Messaging;
using WonderLab.Classes.Datas.MessageData;
using Avalonia.Threading;

namespace WonderLab.Classes.Datas.TaskData;

Expand Down Expand Up @@ -78,6 +79,8 @@ public PreLaunchCheckTask(
public override async ValueTask BuildWorkItemAsync(CancellationToken token) {
try {
_isReturnTrue = true;

var res = Dispatcher.UIThread.CheckAccess();
await Task.Run(CheckJavaAndExecuteAsync, token);
await Task.Run(CheckResourcesAndExecuteAsync, token);
await Task.Run(CheckAccountAndExecuteAsync, token);
Expand Down Expand Up @@ -132,8 +135,8 @@ async Task CheckResourcesAndExecuteAsync() {
: null;

var resultComplete = await _resourceChecker.MissingResources.DownloadResourceEntrysAsync(downloadSource, args => {
var percentage = args.ToPercentage() * 100;
ReportProgress(percentage, $"{args.CompletedCount}/{args.TotalCount} - {percentage:0.00}%");
//var percentage = args.ToPercentage() * 100;
//(percentage, $"{args.CompletedCount}/{args.TotalCount} - {percentage:0.00}%");
}, _downloadService.MainDownloadRequest);

if (!resultComplete) {
Expand Down
8 changes: 4 additions & 4 deletions WonderLab/Classes/Datas/TaskData/TaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ protected void DebounceUIUpdate() {
ProgressDetail = _insideProgressDetail;
}

protected void ReportProgress(string detail) {
Dispatcher.UIThread.Post(() => {
protected async void ReportProgress(string detail) {
await Dispatcher.UIThread.InvokeAsync(() => {
if (!string.IsNullOrEmpty(detail)) {
ProgressDetail = detail;
}
});
}

protected void ReportProgress(double progress) {
Dispatcher.UIThread.Post(() => {
protected async void ReportProgress(double progress) {
await Dispatcher.UIThread.InvokeAsync(() => {
if (progress < 0.0) {
IsIndeterminate = true;
}
Expand Down
3 changes: 1 addition & 2 deletions WonderLab/Services/QueuedHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public override Task StartAsync(CancellationToken cancellationToken) {
}

protected override Task ExecuteAsync(CancellationToken stoppingToken) {
return Task.WhenAll(ProcessTaskQueueAsync(stoppingToken), ProcessTaskQueueAsync(stoppingToken),
ProcessTaskQueueAsync(stoppingToken));
return ProcessTaskQueueAsync(stoppingToken);
}

private async Task ProcessTaskQueueAsync(CancellationToken stoppingToken) {
Expand Down
2 changes: 1 addition & 1 deletion WonderLab/Services/TaskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public BackgroundTaskQueue(int queueLength) {

public async ValueTask QueueBackgroundWorkItemAsync(ITaskJob job) {
if (job == null) {
throw new ArgumentNullException(nameof(job));
ArgumentNullException.ThrowIfNull(job);
}

await _queue.Writer.WriteAsync(job);
Expand Down
2 changes: 1 addition & 1 deletion WonderLab/Services/UI/DialogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public async void ShowContentDialog<TViewModel>(object parameter) where TViewMod
var viewName = typeof(TViewModel).Name.Replace("ViewModel", "");

if (_dialogs.TryGetValue(viewName, out var contentFunc)) {
_dispatcher.Post(async () => {
await _dispatcher.InvokeAsync(async () => {
var dialogObject = contentFunc() as UserControl;
dialogObject!.DataContext = App.ServiceProvider!.GetRequiredService<TViewModel>();
(dialogObject.DataContext as DialogViewModelBase).Initialize(parameter);
Expand Down
6 changes: 3 additions & 3 deletions WonderLab/Services/UI/WindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void ChangeToOobe() {
}

public void Close() {
if (_wrapService.Client is { IsConnected:true }) {
if (_wrapService.Client is { IsConnected: true }) {
_wrapService.Close();
}

Expand Down Expand Up @@ -105,9 +105,9 @@ public async void SetBackground(int type) {
if (string.IsNullOrEmpty(path)) {
_dialogService = App.ServiceProvider.GetService<DialogService>();

var result = await _dialogService.OpenFilePickerAsync([
var result = await Task.Run(async () => await _dialogService.OpenFilePickerAsync([
new FilePickerFileType("图像文件") { Patterns = new List<string>() { "*.png", "*.jpg", "*.jpeg", "*.tif", "*.tiff" } }
], "打开文件");
], "打开文件"));

if (result is null) {
return;
Expand Down
4 changes: 2 additions & 2 deletions WonderLab/ViewModels/Pages/HomePageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public HomePageViewModel(

GameEntries = _gameService.GameEntries.ToObservableList();

_ = Task.Run(async () => {
RunBackgroundWork(async() => {
await Task.Delay(250);
ActiveGameEntry = _gameService.ActiveGameEntry;
});
Expand All @@ -66,7 +66,7 @@ private void Launch() {

var preCheckTask = new PreLaunchCheckTask(App.GetService<JavaFetcher>(),
_gameService,
App.GetService<DialogService>(),
App.GetService<DialogService>(),
_settingService, App.GetService<AccountService>(),
App.GetService<DownloadService>(),
_notificationService,
Expand Down
6 changes: 3 additions & 3 deletions WonderLab/ViewModels/Pages/MultiplayerPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public MultiplayerPageViewModel(
_timeOutSpan = TimeSpan.FromSeconds(10);

MinecraftPort = "25565";
_ = InitializeAsync();
Initialize();
}

[RelayCommand]
Expand All @@ -79,12 +79,12 @@ private void CopyToken() {
_windowService.CopyText(UserToken);
}

private async ValueTask InitializeAsync() {
private void Initialize() {
_wrapService.NewRequest += OnNewRequest;
_wrapService.LoginedSuccessfully += OnLoginedSuccessfully;
_wrapService.ConnectPeerSuccessfully += OnConnectPeerSuccessfully;

await Task.Run(async () => {
RunBackgroundWork(async () => {
try {
_upnPService.Init();
_upnPService.Search();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace WonderLab.ViewModels.Pages.Navigation;

public sealed partial class DownloadNavigationPageViewModel : ViewModelBase {
private readonly INavigationService _navigationService;
private readonly DownloadNavigationService _navigationService;

[ObservableProperty] private NavigationPageData _activePage;

Expand All @@ -19,7 +19,7 @@ await dispatcher.InvokeAsync(() => {
if (ActivePage?.PageKey != p.PageKey) {
ActivePage = p;
}
});
}, DispatcherPriority.ApplicationIdle);
};

_navigationService = navigationService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace WonderLab.ViewModels.Pages.Navigation;

public sealed partial class SettingNavigationPageViewModel : ViewModelBase {
private readonly INavigationService _navigationService;
private readonly SettingNavigationService _navigationService;

[ObservableProperty] private object _activeItem;
[ObservableProperty] private NavigationPageData _activePage;
Expand Down
8 changes: 5 additions & 3 deletions WonderLab/ViewModels/Pages/Setting/AboutPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ namespace WonderLab.ViewModels.Pages.Setting;
public sealed partial class AboutPageViewModel : ViewModelBase {
[RelayCommand]
private void JumpToLink(string url) {
using var process = Process.Start(new ProcessStartInfo(url) {
UseShellExecute = true,
Verb = "open"
RunBackgroundWork(() => {
using var _ = Process.Start(new ProcessStartInfo(url) {
UseShellExecute = true,
Verb = "open"
});
});
}
}
20 changes: 11 additions & 9 deletions WonderLab/ViewModels/Pages/Setting/AccountSettingPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public sealed partial class AccountSettingPageViewModel : ViewModelBase {

public AccountSettingPageViewModel(
DialogService dialogService,
SettingService settingService,
SettingService settingService,
NotificationService notificationService,
TaskService taskService) {
_dialogService = dialogService;
_settingService = settingService;
_notificationService = notificationService;

if (_settingService.Data.Accounts.Count != 0) {
taskService.QueueJob(new AccountLoadTask(_settingService.Data.Accounts));
RunBackgroundWork(() => taskService.QueueJob(new AccountLoadTask(_settingService.Data.Accounts)));
}

WeakReferenceMessenger.Default.Register<AccountMessage>(this, AccountHandle);
Expand All @@ -49,20 +49,22 @@ partial void OnActiveAccountChanged(AccountViewData value) {
_settingService.Data.ActiveAccount = value?.Account;
}

private async void AccountHandle(object obj, AccountMessage accountMessage) {
await Task.Run(async () => {
private void AccountHandle(object obj, AccountMessage accountMessage) {
RunBackgroundWork(async () => {
foreach (var item in accountMessage.Accounts.Select(x => new AccountViewData(x))) {
Accounts.Add(item);
await Task.Delay(5);
}
});
}

private async void AccountViewHandle(object obj, AccountViewMessage accountMessage) {
foreach (var item in accountMessage.Accounts) {
Accounts.Add(item);
await Task.Delay(5);
}
private void AccountViewHandle(object obj, AccountViewMessage accountMessage) {
RunBackgroundWork(async () => {
foreach (var item in accountMessage.Accounts) {
Accounts.Add(item);
await Task.Delay(5);
}
});
}

private void AccountChangeHandle(object obj, AccountChangeNotificationMessage accountMessage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private void Search() {
private void PressOobe() {
_dialogService.ShowContentDialog<RecheckToOobeDialogViewModel>();
}

protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
base.OnPropertyChanged(e);

Expand Down
65 changes: 33 additions & 32 deletions WonderLab/ViewModels/Pages/Setting/LaunchSettingPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public sealed partial class LaunchSettingPageViewModel : ViewModelBase {
private readonly JavaFetcher _javaFetcher;
private readonly DialogService _dialogService;
private readonly SettingService _settingService;
private readonly ILogger<LaunchSettingPageViewModel> _logger;
private readonly ILogger<LaunchSettingPageViewModel> _logger;

[ObservableProperty] private string _maxMemory;
[ObservableProperty] private string _activeGameFolder;
Expand All @@ -42,8 +42,8 @@ public sealed partial class LaunchSettingPageViewModel : ViewModelBase {

public LaunchSettingPageViewModel(
JavaFetcher javaFetcher,
DialogService dialogService,
SettingService settingService,
DialogService dialogService,
SettingService settingService,
ILogger<LaunchSettingPageViewModel> logger) {
_dialogService = dialogService;
_settingService = settingService;
Expand All @@ -65,42 +65,42 @@ public LaunchSettingPageViewModel(
}

[RelayCommand]
private async Task Search(string key) {
if (key is "Folder") {
var folder = await _dialogService.OpenFolderPickerAsync("Select Folder");
if (folder is null) {
return;
}
private void Search(string key) {
RunBackgroundWork(async () => {
if (key is "Folder") {
var folder = await _dialogService.OpenFolderPickerAsync("Select Folder");
if (folder is null) {
return;
}

if (GameFolders.Any(x => x == folder.FullName)) {
return;
}
if (GameFolders.Any(x => x == folder.FullName)) {
return;
}

GameFolders = GameFolders.Union(Enumerable.Repeat(folder.FullName, 1)).ToObservableList();
ActiveGameFolder = GameFolders.Last();
} else {
var java = await _dialogService.OpenFilePickerAsync(new List<FilePickerFileType> {
GameFolders = GameFolders.Union(Enumerable.Repeat(folder.FullName, 1)).ToObservableList();
ActiveGameFolder = GameFolders.Last();
} else {
var java = await _dialogService.OpenFilePickerAsync(new List<FilePickerFileType> {
new("Java文件") { Patterns = [EnvironmentUtil.IsWindow ? "javaw.exe" : "java"] }
}, "Select Java");

if (java is null) {
return;
}
if (java is null) {
return;
}

RunBackgroundWork(() => {
string javaPath = java.Name is "jre.bundle"
string javaPath = java.Name is "jre.bundle"
? Path.Combine(java.FullName, "Contents", "Home", "bin", "java")
: java.FullName;

var javaInfo = JavaUtil.GetJavaInfo(javaPath);
if (Javas.Count > 0 && Javas.Any(x => x?.JavaPath == javaInfo.JavaPath)) {
return;
}

Javas = Javas.Union(Enumerable.Repeat(javaInfo, 1)).ToObservableList();
ActiveJava = Javas.Last();
});
}
}
});
}

[RelayCommand]
Expand All @@ -118,16 +118,17 @@ private void Remove(string key) {
}

[RelayCommand]
private async Task AutoSearch() {
var javas = await _javaFetcher.FetchAsync();
Javas ??= [];
Javas.Clear();
private void AutoSearch() {
RunBackgroundWork(async () => {
var javas = await _javaFetcher.FetchAsync();
Javas ??= [];
Javas.Clear();

var javasList = Javas?.Union(javas);
Javas = javasList.ToObservableList();
var javasList = Javas?.Union(javas);
Javas = javasList.ToObservableList();

ActiveJava = Javas.LastOrDefault();
_logger.LogInformation("共存在 {JavaCount} 个 Java", Javas.Count);
ActiveJava = Javas.LastOrDefault();
}, () => _logger.LogInformation("共存在 {JavaCount} 个 Java", Javas.Count));
}

protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public sealed partial class NetworkSettingPageViewModel : ViewModelBase {
private readonly SettingService _settingService;
private readonly DownloadService _downloadService;

[ObservableProperty] private bool _isUseMirrorDownloadSource;

[ObservableProperty] private int _multiPartsCount;
[ObservableProperty] private int _multiThreadsCount;

[ObservableProperty] private bool _isUseMirrorDownloadSource;

public NetworkSettingPageViewModel(SettingService settingService, DownloadService downloadService) {
_settingService = settingService;
_downloadService = downloadService;
Expand Down
Loading

0 comments on commit f650be2

Please sign in to comment.