Skip to content

Commit

Permalink
Retain window size across sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Aug 25, 2024
1 parent 5a09e03 commit aab4a30
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Nerdbank.Zcash.App/Nerdbank.Zcash.App/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ public override void OnFrameworkInitializationCompleted()
}

this.ViewModel.TopLevel = TopLevel.GetTopLevel(desktop.MainWindow);

// Remember the size of the window.
if (desktop.MainWindow.CanResize && this.settings is not null)
{
if (this.settings?.WindowSize is Size size)
{
desktop.MainWindow.Width = size.Width;
desktop.MainWindow.Height = size.Height;
}

desktop.MainWindow.Resized += (s, e) =>
{
if (this.settings is not null)
{
this.settings.WindowSize = new(desktop.MainWindow.Width, desktop.MainWindow.Height);
}
};
}
}
else if (this.ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
{
Expand Down
19 changes: 19 additions & 0 deletions src/Nerdbank.Zcash.App/Nerdbank.Zcash.App/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ComponentModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using Avalonia;
using Nerdbank.Cryptocurrencies;

namespace Nerdbank.Zcash.App;
Expand Down Expand Up @@ -84,6 +85,18 @@ public string? LastUsedAccountName
set => this.RaiseAndSetIfChanged(ref this.lastUsedAccountName, value);
}

[JsonIgnore]
public Size? WindowSize
{
get => this.WindowWidth is double w && this.WindowHeight is double h ? new(w, h) : null;
set
{
this.WindowWidth = value?.Width;
this.WindowHeight = value?.Height;
this.RaisePropertyChanged();
}
}

/// <summary>
/// Gets or sets a value indicating whether exchange rates will be automatically downloaded.
/// </summary>
Expand Down Expand Up @@ -112,6 +125,12 @@ public bool IsDirty
set => this.RaiseAndSetIfChanged(ref this.isDirty, value);
}

[JsonInclude]
internal double? WindowWidth { get; set; }

[JsonInclude]
internal double? WindowHeight { get; set; }

public static AppSettings Load(Stream stream) => JsonSerializer.Deserialize(stream, JsonSourceGenerationContext.Default.AppSettings)!;

public Task SaveAsync(Stream stream, CancellationToken cancellationToken) => JsonSerializer.SerializeAsync(stream, this, JsonSourceGenerationContext.Default.AppSettings, cancellationToken);
Expand Down

0 comments on commit aab4a30

Please sign in to comment.