Skip to content
Permalink
Browse files

Add UiConfig. Remember windows state and size at shutdown.

  • Loading branch information...
nopara73 committed Nov 12, 2018
1 parent e3ecb62 commit 5d98c4fc170ac466e2aed128b600a5c094511f14
@@ -74,12 +74,18 @@ public static string TorLogsFile
public static TorProcessManager TorManager { get; private set; }

public static Config Config { get; private set; }
public static UiConfig UiConfig { get; private set; }

public static void InitializeConfig(Config config)
public static void InitializeConfigs(Config config)
{
Config = Guard.NotNull(nameof(config), config);
}

public static void InitializeUiConfigs(UiConfig uiConfig)
{
UiConfig = Guard.NotNull(nameof(uiConfig), uiConfig);
}

private static long _triedDesperateDequeuing = 0;

private static async Task TryDesperateDequeueAllCoinsAsync()
@@ -14,7 +14,9 @@
Title="{Binding Title}" BorderThickness="1" BorderBrush="{DynamicResource AvalonBorderBrush}"
MinWidth="1100" MinHeight="530"
FontFamily="{DynamicResource UiFont}" FontSize="14"
Foreground="{DynamicResource ThemeForegroundBrush}" WindowState="Maximized"
Foreground="{DynamicResource ThemeForegroundBrush}"
Width ="{Binding Width}" Height ="{Binding Height}"
WindowState ="{Binding WindowState}"
UseLayoutRounding="True" RenderOptions.BitmapInterpolationMode="HighQuality"
id:DragBehavior.IsEnabled="False" id:DropBehavior.IsEnabled="False">
<i:Interaction.Behaviors>
@@ -25,7 +27,7 @@
<Grid>
<DockPanel LastChildFill="True">
<wasabi:StatusBar DockPanel.Dock="Bottom" DataContext="{Binding StatusBar}" />
<shell:ShellView DataContext="{Binding Shell}"/>
<shell:ShellView DataContext="{Binding Shell}" />
</DockPanel>
<wasabi:ModalDialog DataContext="{Binding ModalDialog}" />
</Grid>
@@ -1,4 +1,5 @@
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
@@ -10,6 +11,7 @@
using AvalonStudio.Shell;
using AvalonStudio.Shell.Controls;
using WalletWasabi.Gui.Tabs.WalletManager;
using WalletWasabi.Gui.ViewModels;

namespace WalletWasabi.Gui
{
@@ -35,13 +37,25 @@ public MainWindow()
private void InitializeComponent()
{
Activated += OnActivated;
Closing += MainWindow_ClosingAsync;
AvaloniaXamlLoader.Load(this);
}

private async void MainWindow_ClosingAsync(object sender, CancelEventArgs e)
{
UiConfig conf = Global.UiConfig;
conf.WindowState = WindowState;
conf.Width = Width;
conf.Height = Height;

await conf.ToFileAsync();
}

private void OnActivated(object sender, EventArgs e)
{
Activated -= OnActivated;
DisplayWalletManager();
MainWindowViewModel.Instance.RefreshUiFromConfig(Global.UiConfig);
}

private void DisplayWalletManager()
@@ -22,6 +22,13 @@ private static async Task Main(string[] args)
try
{
Platform.BaseDirectory = Path.Combine(Global.DataDir, "Gui");

var uiConfigFilePath = Path.Combine(Global.DataDir, "UiConfig.json");
var uiConfig = new UiConfig(uiConfigFilePath);
await uiConfig.LoadOrCreateDefaultFileAsync();
Logger.LogInfo<UiConfig>("UiConfig is successfully initialized.");
Global.InitializeUiConfigs(uiConfig);

BuildAvaloniaApp().BeforeStarting(async builder =>
{
MainWindowViewModel.Instance = new MainWindowViewModel();
@@ -31,7 +38,8 @@ private static async Task Main(string[] args)
await config.LoadOrCreateDefaultFileAsync();
Logger.LogInfo<Config>("Config is successfully initialized.");

Global.InitializeConfig(config);
Global.InitializeConfigs(config);
MainWindowViewModel.Instance.RefreshUiFromConfig(uiConfig);

if (!File.Exists(Global.IndexFilePath)) // Load the index file from working folder if we have it.
{
@@ -47,6 +55,12 @@ private static async Task Main(string[] args)

MainWindowViewModel.Instance.StatusBar = statusBar;

//UiConfig conf = Global.UiConfig;
//MainWindowViewModel.Instance..WindowState = (Avalonia.Controls.WindowState)conf.WindowState;
//MainWindowViewModel.Instance.Width = (double)conf.Width;
//MainWindowViewModel.Instance.Height = (double)conf.Height;
//MainWindowViewModel.Instance.Position = new Point((double)conf.Left, (double)conf.Top);

if (Global.IndexDownloader.Network != Network.Main)
{
MainWindowViewModel.Instance.Title += $" - {Global.IndexDownloader.Network}";
@@ -0,0 +1,129 @@
using Avalonia.Controls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using WalletWasabi.Helpers;
using WalletWasabi.Interfaces;

namespace WalletWasabi.Gui
{
[JsonObject(MemberSerialization.OptIn)]
public class UiConfig : IConfig
{
/// <inheritdoc />
public string FilePath { get; private set; }

[JsonProperty(PropertyName = "WindowState")]
public WindowState? WindowState { get; internal set; }

[JsonProperty(PropertyName = "Height")]
public double? Height { get; internal set; }

[JsonProperty(PropertyName = "Width")]
public double? Width { get; internal set; }

public UiConfig()
{
}

public UiConfig(string filePath)
{
SetFilePath(filePath);
}

public UiConfig(WindowState windowState, double height, double width)
{
WindowState = Guard.NotNull(nameof(windowState), windowState);
Height = Guard.NotNull(nameof(height), height);
Width = Guard.NotNull(nameof(width), width);
}

/// <inheritdoc />
public async Task ToFileAsync()
{
AssertFilePathSet();

string jsonString = JsonConvert.SerializeObject(this, Formatting.Indented);
await File.WriteAllTextAsync(FilePath,
jsonString,
Encoding.UTF8);
}

/// <inheritdoc />
public async Task LoadOrCreateDefaultFileAsync()
{
AssertFilePathSet();

WindowState = Avalonia.Controls.WindowState.Maximized;
Height = 530;
Width = 1100;

if (!File.Exists(FilePath))
{
Logging.Logger.LogInfo<Config>($"{nameof(Config)} file did not exist. Created at path: `{FilePath}`.");
}
else
{
await LoadFileAsync();
}

await ToFileAsync();
}

public async Task LoadFileAsync()
{
string jsonString = await File.ReadAllTextAsync(FilePath, Encoding.UTF8);
var config = JsonConvert.DeserializeObject<UiConfig>(jsonString);

WindowState = config.WindowState ?? WindowState;
Height = config.Height ?? Height;
Width = config.Width ?? Width;
}

/// <inheritdoc />
public async Task<bool> CheckFileChangeAsync()
{
AssertFilePathSet();

if (!File.Exists(FilePath))
{
throw new FileNotFoundException($"{nameof(Config)} file did not exist at path: `{FilePath}`.");
}

string jsonString = await File.ReadAllTextAsync(FilePath, Encoding.UTF8);
var config = JsonConvert.DeserializeObject<UiConfig>(jsonString);

if (WindowState != config.WindowState)
{
return true;
}

if (Height != config.Height)
{
return true;
}

if (Width != config.Width)
{
return true;
}

return false;
}

/// <inheritdoc />
public void SetFilePath(string path)
{
FilePath = Guard.NotNullOrEmptyOrWhitespace(nameof(path), path, trim: true);
}

/// <inheritdoc />
public void AssertFilePathSet()
{
if (FilePath is null) throw new NotSupportedException($"{nameof(FilePath)} is not set. Use {nameof(SetFilePath)} to set it.");
}
}
}
@@ -1,4 +1,6 @@
using AvalonStudio.Extensibility;
using Avalonia;
using Avalonia.Controls;
using AvalonStudio.Extensibility;
using AvalonStudio.Extensibility.Dialogs;
using AvalonStudio.Shell;
using NBitcoin;
@@ -23,6 +25,30 @@ public string Title
internal set { this.RaiseAndSetIfChanged(ref _title, value); }
}

private double _height;

public double Height
{
get { return _height; }
internal set { this.RaiseAndSetIfChanged(ref _height, value); }
}

private double _width;

public double Width
{
get { return _width; }
internal set { this.RaiseAndSetIfChanged(ref _width, value); }
}

private WindowState _windowState;

public WindowState WindowState
{
get { return _windowState; }
internal set { this.RaiseAndSetIfChanged(ref _windowState, value); }
}

private StatusBarViewModel _statusBar;

public StatusBarViewModel StatusBar
@@ -62,5 +88,12 @@ public bool CanClose
get { return _canClose; }
set { this.RaiseAndSetIfChanged(ref _canClose, value); }
}

public void RefreshUiFromConfig(UiConfig config)
{
Width = (double)config.Width;
Height = (double)config.Height;
WindowState = (WindowState)config.WindowState;
}
}
}

0 comments on commit 5d98c4f

Please sign in to comment.
You can’t perform that action at this time.