From acc9b5df43b04f6cd99012f9e02324dc1a37035b Mon Sep 17 00:00:00 2001 From: am009 Date: Fri, 22 Nov 2024 00:39:51 +0800 Subject: [PATCH] feat: (initial) save window location. --- src/TMSpeech.Core/ConfigTypes.cs | 4 +++- src/TMSpeech.GUI/App.axaml.cs | 18 ++++++++++++++++++ src/TMSpeech.GUI/Controls/TrayMenu.cs | 8 ++++++++ src/TMSpeech.GUI/ViewModels/ConfigViewModel.cs | 5 +++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/TMSpeech.Core/ConfigTypes.cs b/src/TMSpeech.Core/ConfigTypes.cs index 76a7fa4..23b59dc 100644 --- a/src/TMSpeech.Core/ConfigTypes.cs +++ b/src/TMSpeech.Core/ConfigTypes.cs @@ -9,6 +9,7 @@ public static class GeneralConfigTypes public const string StartOnLaunch = "general.StartOnLaunch"; public const string AutoUpdate = "general.AutoUpdate"; public const string ResultLogPath = "general.ResultLogPath"; + public const string MainWindowLocation = "general.MainWindowLocation"; private static Dictionary _defaultConfig => new() @@ -17,7 +18,8 @@ public static class GeneralConfigTypes { LaunchOnStartup, false }, { StartOnLaunch, true }, { AutoUpdate, true }, - { ResultLogPath, Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TMSpeechLogs") } + { ResultLogPath, Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TMSpeechLogs") }, + { MainWindowLocation, new List() } }; public static Dictionary DefaultConfig => _defaultConfig; diff --git a/src/TMSpeech.GUI/App.axaml.cs b/src/TMSpeech.GUI/App.axaml.cs index 20369ec..92eda4c 100644 --- a/src/TMSpeech.GUI/App.axaml.cs +++ b/src/TMSpeech.GUI/App.axaml.cs @@ -1,6 +1,11 @@ using System; +using System.Collections.Generic; using System.Linq; +using System.Net.Http.Json; using System.Reactive.Concurrency; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; @@ -41,6 +46,19 @@ public override void OnFrameworkInitializationCompleted() desktop.ShutdownMode = Avalonia.Controls.ShutdownMode.OnExplicitShutdown; _mainWindow = new MainWindow(); desktop.MainWindow = _mainWindow; + + // TODO better solution? + var savedLocationObj = ConfigManagerFactory.Instance.Get(GeneralConfigTypes.MainWindowLocation); + if (savedLocationObj.ValueKind == JsonValueKind.Array) + { + var savedLocation = savedLocationObj.Deserialize(); + if (savedLocation != null && savedLocation.Length == 4) + { + _mainWindow.Width = savedLocation[2]; + _mainWindow.Height = savedLocation[3]; + _mainWindow.Position = new(savedLocation[0], savedLocation[1]); + } + } } else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform) { diff --git a/src/TMSpeech.GUI/Controls/TrayMenu.cs b/src/TMSpeech.GUI/Controls/TrayMenu.cs index cc48471..e2a7595 100644 --- a/src/TMSpeech.GUI/Controls/TrayMenu.cs +++ b/src/TMSpeech.GUI/Controls/TrayMenu.cs @@ -1,9 +1,11 @@ using System; +using System.Collections.Generic; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; using ReactiveUI; +using TMSpeech.Core; using TMSpeech.GUI.Views; namespace TMSpeech.GUI.Controls; @@ -29,6 +31,12 @@ public void UpdateItems() private void Exit() { + // Save window location and size. + var left = _mainWindow.Position.X; + var top = _mainWindow.Position.Y; + var width = (int)_mainWindow.Width; + var height = (int)_mainWindow.Height; + ConfigManagerFactory.Instance.Apply>(GeneralConfigTypes.MainWindowLocation, [left, top, width, height]); Environment.Exit(0); } diff --git a/src/TMSpeech.GUI/ViewModels/ConfigViewModel.cs b/src/TMSpeech.GUI/ViewModels/ConfigViewModel.cs index 3cc9b72..32ff5e8 100644 --- a/src/TMSpeech.GUI/ViewModels/ConfigViewModel.cs +++ b/src/TMSpeech.GUI/ViewModels/ConfigViewModel.cs @@ -172,6 +172,11 @@ public class GeneralSectionConfigViewModel : SectionConfigViewModelBase [Reactive] [ConfigJsonValue] public bool AutoUpdate { get; set; } + + // Left, Top, Width, Height + [Reactive] + [ConfigJsonValue] + public List MainWindowLocation { get; set; } = []; } public class AppearanceSectionConfigViewModel : SectionConfigViewModelBase