diff --git a/src/LipUI/App.xaml.cs b/src/LipUI/App.xaml.cs index a225200..46e8e00 100644 --- a/src/LipUI/App.xaml.cs +++ b/src/LipUI/App.xaml.cs @@ -1,4 +1,5 @@ using Microsoft.UI.Xaml; +using System.IO; // To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. @@ -15,7 +16,7 @@ public partial class App : Application /// public App() { - this.InitializeComponent(); + InitializeComponent(); } /// @@ -24,12 +25,15 @@ public App() /// Details about the launch request and process. protected override void OnLaunched(LaunchActivatedEventArgs args) { - //Main.Initialize(); m_window = new MainWindow(); m_window.Activate(); - //ExtendAcrylicIntoTitleBar(); + UnhandledException += App_UnhandledException; + } + + private void App_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { } internal Window? m_window; diff --git a/src/LipUI/Language/zh-CN/Resources.resw b/src/LipUI/Language/zh-CN/Resources.resw index 8b485a6..c06caad 100644 --- a/src/LipUI/Language/zh-CN/Resources.resw +++ b/src/LipUI/Language/zh-CN/Resources.resw @@ -129,6 +129,9 @@ 无描述 + + 未选择服务器 + 错误 @@ -138,8 +141,8 @@ 警告 - - 未知的服务器路径! + + 未知的Lip路径! 取消 diff --git a/src/LipUI/LipUI.csproj b/src/LipUI/LipUI.csproj index a660362..0628e1e 100644 --- a/src/LipUI/LipUI.csproj +++ b/src/LipUI/LipUI.csproj @@ -70,7 +70,7 @@ - + diff --git a/src/LipUI/MainWindow.xaml.cs b/src/LipUI/MainWindow.xaml.cs index a8bc28c..dd2afb3 100644 --- a/src/LipUI/MainWindow.xaml.cs +++ b/src/LipUI/MainWindow.xaml.cs @@ -56,11 +56,14 @@ public MainWindow() Closed += MainWindow_Closed; - Helpers.MainWindow = this; + Services.MainWindow = this; } private async void MainWindow_Closed(object sender, WindowEventArgs args) - => await Main.SaveConfigAsync(); + { + Services.OnWindowClosed(); + await Main.SaveConfigAsync(); + } private NativeMethods.WinProc? newWndProc = null; private nint oldWndProc = nint.Zero; @@ -297,7 +300,7 @@ private bool TryGoBack() NavView.DisplayMode is NavigationViewDisplayMode.Minimal)) return false; - ContentFrame.GoBack(); + ContentFrame.TryGoBack(); return true; } diff --git a/src/LipUI/Models/GlobalResources.cs b/src/LipUI/Models/GlobalResources.cs index ec2dd38..c4329ad 100644 --- a/src/LipUI/Models/GlobalResources.cs +++ b/src/LipUI/Models/GlobalResources.cs @@ -1,5 +1,4 @@ using LipUI.Assets; -using LipUI.Pages; using Microsoft.UI.Xaml.Media.Imaging; @@ -7,7 +6,7 @@ namespace LipUI.Models; internal static class GlobalResources { - public static readonly BitmapImage GrassBlock = Helpers.CreateImageFromBytes(Images.grass_block); - public static readonly BitmapImage Netherrack = Helpers.CreateImageFromBytes(Images.netherrack); - public static readonly BitmapImage Glass = Helpers.CreateImageFromBytes(Images.glass); + public static readonly BitmapImage GrassBlock = Services.CreateImageFromBytes(Images.grass_block); + public static readonly BitmapImage Netherrack = Services.CreateImageFromBytes(Images.netherrack); + public static readonly BitmapImage Glass = Services.CreateImageFromBytes(Images.glass); } diff --git a/src/LipUI/Models/ILipUIModules.cs b/src/LipUI/Models/ILipUIModules.cs index d3226d6..85d2016 100644 --- a/src/LipUI/Models/ILipUIModules.cs +++ b/src/LipUI/Models/ILipUIModules.cs @@ -16,11 +16,13 @@ public interface ILipUIModulesNonGeneric public Type PageType { get; } - public FrameworkElement? IconContent { get; } + public FrameworkElement? IconContent { get => null; } - public Brush? IconBackground { get; } + public Brush? IconBackground { get => null; } public void OnIconInitialze(ModuleIcon icon) { } + + public void OnExit() { } } public interface ILipUIModules : ILipUIModulesNonGeneric diff --git a/src/LipUI/Pages/HomePage.xaml b/src/LipUI/Pages/HomePage.xaml index 03cb570..3764df1 100644 --- a/src/LipUI/Pages/HomePage.xaml +++ b/src/LipUI/Pages/HomePage.xaml @@ -8,13 +8,12 @@ xmlns:animatedvisuals="using:Microsoft.UI.Xaml.Controls.AnimatedVisuals" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - mc:Ignorable="d" - Loaded="Page_Loaded"> + mc:Ignorable="d"> - + diff --git a/src/LipUI/Pages/HomePage.xaml.cs b/src/LipUI/Pages/HomePage.xaml.cs index 0d62a59..6d6287a 100644 --- a/src/LipUI/Pages/HomePage.xaml.cs +++ b/src/LipUI/Pages/HomePage.xaml.cs @@ -33,36 +33,17 @@ private async void RefreshIcon() { ServerInstance? instance = Main.Config.SelectedServer; ServerIconImage.Source = await ServerIcon.GetIcon(instance); - ServerDesc.Text = instance is null || string.IsNullOrWhiteSpace(instance.Description) ? - "home$emptyDesc".GetLocalized() : instance.Description; + ServerDesc.Text = + instance is null ? + "home$nullServerPath".GetLocalized() : + string.IsNullOrWhiteSpace(instance.Description) ? + "home$emptyDesc".GetLocalized() : + instance.Description; } private void SelectServerButton_Click(object sender, RoutedEventArgs e) => Frame.Navigate(typeof(ServerSelectionPage), () => { DispatcherQueue.TryEnqueue(() => { RefreshIcon(); }); }); - private void Page_Loaded(object sender, RoutedEventArgs e) - => ShowLipInstallerPageIfNotExist(); - - private void ShowLipInstallerPageIfNotExist() - { - if (File.Exists(Main.Config.Settings.LipPath)) - { - return; - } - - var dialog = new ContentDialog() - { - XamlRoot = XamlRoot, - Content = new LipInstallerView() - }; - - Task.Run(() => - { - Task.Delay(100); - DispatcherQueue.TryEnqueue(async () => await dialog.ShowAsync()); - }); - } - private void StartServerButton_Click(object sender, RoutedEventArgs e) { if (Main.Config.SelectedServer is null) @@ -97,7 +78,10 @@ private void BackButton_PointerExited(object sender, PointerRoutedEventArgs e) => AnimatedIcon.SetState(BackAnimatedIcon, "Normal"); private void BackButton_Click(object sender, RoutedEventArgs e) - => ContentFrame.GoBack(); + { + ModuleIcon.OnExit(ContentFrame.Content.GetType()); + ContentFrame.TryGoBack(); + } private void ContentFrame_Navigated(object sender, NavigationEventArgs e) { @@ -113,7 +97,7 @@ private void ContentFrame_Navigated(object sender, NavigationEventArgs e) } catch (Exception ex) { - await Helpers.ShowInfoBarAsync(ex); + await Services.ShowInfoBarAsync(ex); } }); } diff --git a/src/LipUI/Pages/HomePageModules/BdsPropertiesEditorPage.xaml.cs b/src/LipUI/Pages/HomePageModules/BdsPropertiesEditorPage.xaml.cs index b6a741b..6d879f3 100644 --- a/src/LipUI/Pages/HomePageModules/BdsPropertiesEditorPage.xaml.cs +++ b/src/LipUI/Pages/HomePageModules/BdsPropertiesEditorPage.xaml.cs @@ -12,7 +12,7 @@ using System.Threading.Tasks; using Windows.System; using Windows.UI; -using static LipUI.Pages.Helpers; +using static LipUI.Services; // To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. @@ -64,7 +64,7 @@ private async void Page_Loaded(object sender, RoutedEventArgs e) { await Task.Delay(500); await ShowInfoBarAsync("propertiesEditor$nullServerPath".GetLocalized(), null, InfoBarSeverity.Error); - Frame.GoBack(); + Frame.TryGoBack(); return; }; @@ -126,7 +126,7 @@ private async void LoadPropertiesAndCreateUI() catch (Exception ex) { await ShowInfoBarAsync(ex, default, null); - Frame.GoBack(); + Frame.TryGoBack(); return; } diff --git a/src/LipUI/Pages/IndexPage.xaml.cs b/src/LipUI/Pages/IndexPage.xaml.cs index 21d14a9..4c9844b 100644 --- a/src/LipUI/Pages/IndexPage.xaml.cs +++ b/src/LipUI/Pages/IndexPage.xaml.cs @@ -111,7 +111,7 @@ where tooth.Name.ToLower().Contains(str) } catch (Exception ex) { - await Helpers.ShowInfoBarAsync(ex); + await Services.ShowInfoBarAsync(ex); } }); } @@ -143,7 +143,7 @@ where tooth.Name.ToLower().Contains(query) } catch (Exception ex) { - await Helpers.ShowInfoBarAsync(ex); + await Services.ShowInfoBarAsync(ex); } }); diff --git a/src/LipUI/Pages/LipExecutionPanelPage.xaml.cs b/src/LipUI/Pages/LipExecutionPanelPage.xaml.cs index 5f810fa..f12870d 100644 --- a/src/LipUI/Pages/LipExecutionPanelPage.xaml.cs +++ b/src/LipUI/Pages/LipExecutionPanelPage.xaml.cs @@ -84,6 +84,14 @@ public LipConsoleHandler(LipExecutionPanelPage page) public void Run() { + Action closed = null!; + closed = () => + { + process?.Kill(); + Services.WindowClosed -= closed; + }; + Services.WindowClosed += closed; + dispatcherQueue.TryEnqueue(async () => { lip = await Main.CreateLipConsole(page.XamlRoot); @@ -96,9 +104,9 @@ public void Run() page.LipWorkingInfoText.Text = string.Empty; page.ProgressRateText.Text = string.Empty; - await Helpers.ShowInfoBarAsync( + await Services.ShowInfoBarAsync( "infobar$error".GetLocalized(), - "lipExecution$nullServerPath".GetLocalized(), + "lipExecution$nullLipPath".GetLocalized(), InfoBarSeverity.Error); return; @@ -127,6 +135,7 @@ await Helpers.ShowInfoBarAsync( break; } + Services.WindowClosed -= closed; }); } diff --git a/src/LipUI/Pages/LocalPackagePage.xaml.cs b/src/LipUI/Pages/LocalPackagePage.xaml.cs index d479892..a5d59cf 100644 --- a/src/LipUI/Pages/LocalPackagePage.xaml.cs +++ b/src/LipUI/Pages/LocalPackagePage.xaml.cs @@ -59,7 +59,7 @@ private void ReloadPackage(IEnumerable? items = null) } catch (Exception ex) { - await Helpers.ShowInfoBarAsync(ex); + await Services.ShowInfoBarAsync(ex); } if (arr is not null) @@ -91,7 +91,7 @@ private void RefreshUpgradableTeeth() } catch (Exception ex) { - await Helpers.ShowInfoBarAsync(ex); + await Services.ShowInfoBarAsync(ex); return; } @@ -137,7 +137,7 @@ where tooth.Info.Name.ToLower().Contains(str) } catch (Exception ex) { - await Helpers.ShowInfoBarAsync(ex); + await Services.ShowInfoBarAsync(ex); } }); } @@ -166,7 +166,7 @@ where tooth.Info.Name.ToLower().Contains(query) } catch (Exception ex) { - await Helpers.ShowInfoBarAsync(ex); + await Services.ShowInfoBarAsync(ex); } }); } diff --git a/src/LipUI/Pages/ServerSelectionPage.xaml b/src/LipUI/Pages/ServerSelectionPage.xaml index 50824c0..fad8064 100644 --- a/src/LipUI/Pages/ServerSelectionPage.xaml +++ b/src/LipUI/Pages/ServerSelectionPage.xaml @@ -11,7 +11,7 @@ - + diff --git a/src/LipUI/Pages/ServerSelectionPage.xaml.cs b/src/LipUI/Pages/ServerSelectionPage.xaml.cs index aba96fc..32e4b03 100644 --- a/src/LipUI/Pages/ServerSelectionPage.xaml.cs +++ b/src/LipUI/Pages/ServerSelectionPage.xaml.cs @@ -191,7 +191,7 @@ private async ValueTask ItemClick_AddServerViewClicked(object sender, ItemClickE { if (instance == server) { - await Helpers.ShowInfoBarAsync( + await Services.ShowInfoBarAsync( "infoBar$error".GetLocalized(), "serverSelection$existed".GetLocalized(), InfoBarSeverity.Error); @@ -219,7 +219,7 @@ private async void ItemClick_SelectMode(object sender, ItemClickEventArgs e) Main.Config.SelectedServer = (e.ClickedItem as ServerInstanceView)!.ServerInstance; refreshIcon!(); await Main.SaveConfigAsync(); - Frame.GoBack(); + Frame.TryGoBack(); } private void ItemClick_EditMode(object sender, ItemClickEventArgs e) diff --git a/src/LipUI/Pages/Helpers.cs b/src/LipUI/Services.cs similarity index 83% rename from src/LipUI/Pages/Helpers.cs rename to src/LipUI/Services.cs index f295487..3d473f0 100644 --- a/src/LipUI/Pages/Helpers.cs +++ b/src/LipUI/Services.cs @@ -6,16 +6,29 @@ using Microsoft.Windows.ApplicationModel.Resources; using System.Threading.Tasks; -namespace LipUI.Pages; +namespace LipUI; -public static class ResourceExtensions +internal static class ResourceExtensions { private static readonly ResourceLoader _resourceLoader = new(); public static string GetLocalized(this string resourceKey) => _resourceLoader.GetString(resourceKey); } -internal static class Helpers +internal static class FrameExtensions +{ + public static bool TryGoBack(this Frame frame) + { + if (frame.CanGoBack) + { + frame.GoBack(); + return true; + } + return false; + } +} + +internal static class Services { public static BitmapImage CreateImageFromBytes(byte[] bytes) { @@ -70,4 +83,8 @@ public static async ValueTask ShowInfoBarAsync( interval = TimeSpan.FromSeconds(5); await ShowInfoBarAsync(ex.GetType().Name, ex.Message, InfoBarSeverity.Error, interval, barContent); } + + public static event Action? WindowClosed; + + internal static void OnWindowClosed() => WindowClosed?.Invoke(); } diff --git a/src/LipUI/VIews/LipInstallerView.xaml.cs b/src/LipUI/VIews/LipInstallerView.xaml.cs index f0b39d0..f9fc3c9 100644 --- a/src/LipUI/VIews/LipInstallerView.xaml.cs +++ b/src/LipUI/VIews/LipInstallerView.xaml.cs @@ -1,5 +1,4 @@ using LipUI.Models; -using LipUI.Pages; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using System; @@ -79,7 +78,7 @@ private async ValueTask DownloadLipPortable(InstallerInfo info) using var client = new HttpClient( new HttpClientHandler() { ClientCertificateOptions = ClientCertificateOption.Automatic }) { - //Timeout = TimeSpan.FromSeconds(2), + Timeout = TimeSpan.FromSeconds(5), DefaultRequestHeaders = { ExpectContinue = false } }; @@ -145,28 +144,36 @@ private bool View_DownloadProgressChanged(InstallerInfo info, int val) private async void InstallButton_Click(object sender, RoutedEventArgs e) { - var progressRing = ((ProgressRing)ProgressRingBorder.Child); - progressRing.IsActive = true; - progressRing.Visibility = Visibility.Visible; + try + { + var progressRing = ((ProgressRing)ProgressRingBorder.Child); + progressRing.IsActive = true; + progressRing.Visibility = Visibility.Visible; - CancelButton.IsEnabled = false; - InstallButton.IsEnabled = false; - //ViewGrid.Children.Remove(ButtonGrid); + CancelButton.IsEnabled = false; + InstallButton.IsEnabled = false; + //ViewGrid.Children.Remove(ButtonGrid); - var info = await RequestLipInstallerInfo(); + var info = await RequestLipInstallerInfo(); - ChangeProgressRingMode(info); + ChangeProgressRingMode(info); - var bytes = await DownloadLipPortable(info); + var bytes = await DownloadLipPortable(info); - UnzipAndCopyLipEXE(bytes); + UnzipAndCopyLipEXE(bytes); - DispatcherQueue.TryEnqueue(() => InfoText.Text = "lipInstaller$installCompleted".GetLocalized()); + DispatcherQueue.TryEnqueue(() => InfoText.Text = "lipInstaller$installCompleted".GetLocalized()); - await Task.Delay(1000); + await Task.Delay(1000); - var dialog = Parent as ContentDialog; - dialog!.Hide(); + var dialog = Parent as ContentDialog; + dialog!.Hide(); + } + catch (Exception ex) + { + await Services.ShowInfoBarAsync(ex); + (Parent as ContentDialog)!.Hide(); + } } private void CancelButton_Click(object sender, RoutedEventArgs e) diff --git a/src/LipUI/VIews/ModuleIcon.xaml.cs b/src/LipUI/VIews/ModuleIcon.xaml.cs index c9f2eb8..f4e64f2 100644 --- a/src/LipUI/VIews/ModuleIcon.xaml.cs +++ b/src/LipUI/VIews/ModuleIcon.xaml.cs @@ -1,5 +1,4 @@ using LipUI.Models; -using LipUI.Pages; using Microsoft.UI; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; @@ -56,8 +55,16 @@ public ModuleIcon(Type moduleType) } catch (Exception ex) { - Task.Run(() => Helpers.ShowInfoBarAsync(ex)); + Task.Run(() => Services.ShowInfoBarAsync(ex)); return; } } + + internal static void OnExit(Type type) + { + if (Modules.TryGetValue(type, out var module)) + { + module.OnExit(); + } + } }