Skip to content

Commit

Permalink
feat: (initial) save window location.
Browse files Browse the repository at this point in the history
  • Loading branch information
am009 committed Nov 21, 2024
1 parent cd734dd commit acc9b5d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/TMSpeech.Core/ConfigTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> _defaultConfig => new()
Expand All @@ -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<int>() }
};

public static Dictionary<string, object> DefaultConfig => _defaultConfig;
Expand Down
18 changes: 18 additions & 0 deletions src/TMSpeech.GUI/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<JsonElement>(GeneralConfigTypes.MainWindowLocation);
if (savedLocationObj.ValueKind == JsonValueKind.Array)
{
var savedLocation = savedLocationObj.Deserialize<int[]>();
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)
{
Expand Down
8 changes: 8 additions & 0 deletions src/TMSpeech.GUI/Controls/TrayMenu.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<List<int>>(GeneralConfigTypes.MainWindowLocation, [left, top, width, height]);
Environment.Exit(0);
}

Expand Down
5 changes: 5 additions & 0 deletions src/TMSpeech.GUI/ViewModels/ConfigViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ public class GeneralSectionConfigViewModel : SectionConfigViewModelBase
[Reactive]
[ConfigJsonValue]
public bool AutoUpdate { get; set; }

// Left, Top, Width, Height
[Reactive]
[ConfigJsonValue]
public List<int> MainWindowLocation { get; set; } = [];
}

public class AppearanceSectionConfigViewModel : SectionConfigViewModelBase
Expand Down

0 comments on commit acc9b5d

Please sign in to comment.