Skip to content

Commit

Permalink
Add UiConfig. Remember windows state and size at shutdown.
Browse files Browse the repository at this point in the history
  • Loading branch information
nopara73 committed Nov 12, 2018
1 parent e3ecb62 commit 5d98c4f
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 5 deletions.
8 changes: 7 additions & 1 deletion WalletWasabi.Gui/Global.cs
Expand Up @@ -74,12 +74,18 @@ public static string TorLogsFile
public static TorProcessManager TorManager { get; private set; } public static TorProcessManager TorManager { get; private set; }


public static Config Config { 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); 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 long _triedDesperateDequeuing = 0;


private static async Task TryDesperateDequeueAllCoinsAsync() private static async Task TryDesperateDequeueAllCoinsAsync()
Expand Down
6 changes: 4 additions & 2 deletions WalletWasabi.Gui/MainWindow.xaml
Expand Up @@ -14,7 +14,9 @@
Title="{Binding Title}" BorderThickness="1" BorderBrush="{DynamicResource AvalonBorderBrush}" Title="{Binding Title}" BorderThickness="1" BorderBrush="{DynamicResource AvalonBorderBrush}"
MinWidth="1100" MinHeight="530" MinWidth="1100" MinHeight="530"
FontFamily="{DynamicResource UiFont}" FontSize="14" 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" UseLayoutRounding="True" RenderOptions.BitmapInterpolationMode="HighQuality"
id:DragBehavior.IsEnabled="False" id:DropBehavior.IsEnabled="False"> id:DragBehavior.IsEnabled="False" id:DropBehavior.IsEnabled="False">
<i:Interaction.Behaviors> <i:Interaction.Behaviors>
Expand All @@ -25,7 +27,7 @@
<Grid> <Grid>
<DockPanel LastChildFill="True"> <DockPanel LastChildFill="True">
<wasabi:StatusBar DockPanel.Dock="Bottom" DataContext="{Binding StatusBar}" /> <wasabi:StatusBar DockPanel.Dock="Bottom" DataContext="{Binding StatusBar}" />
<shell:ShellView DataContext="{Binding Shell}"/> <shell:ShellView DataContext="{Binding Shell}" />
</DockPanel> </DockPanel>
<wasabi:ModalDialog DataContext="{Binding ModalDialog}" /> <wasabi:ModalDialog DataContext="{Binding ModalDialog}" />
</Grid> </Grid>
Expand Down
14 changes: 14 additions & 0 deletions WalletWasabi.Gui/MainWindow.xaml.cs
@@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
Expand All @@ -10,6 +11,7 @@
using AvalonStudio.Shell; using AvalonStudio.Shell;
using AvalonStudio.Shell.Controls; using AvalonStudio.Shell.Controls;
using WalletWasabi.Gui.Tabs.WalletManager; using WalletWasabi.Gui.Tabs.WalletManager;
using WalletWasabi.Gui.ViewModels;


namespace WalletWasabi.Gui namespace WalletWasabi.Gui
{ {
Expand All @@ -35,13 +37,25 @@ public MainWindow()
private void InitializeComponent() private void InitializeComponent()
{ {
Activated += OnActivated; Activated += OnActivated;
Closing += MainWindow_ClosingAsync;
AvaloniaXamlLoader.Load(this); 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) private void OnActivated(object sender, EventArgs e)
{ {
Activated -= OnActivated; Activated -= OnActivated;
DisplayWalletManager(); DisplayWalletManager();
MainWindowViewModel.Instance.RefreshUiFromConfig(Global.UiConfig);
} }


private void DisplayWalletManager() private void DisplayWalletManager()
Expand Down
16 changes: 15 additions & 1 deletion WalletWasabi.Gui/Program.cs
Expand Up @@ -22,6 +22,13 @@ private static async Task Main(string[] args)
try try
{ {
Platform.BaseDirectory = Path.Combine(Global.DataDir, "Gui"); 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 => BuildAvaloniaApp().BeforeStarting(async builder =>
{ {
MainWindowViewModel.Instance = new MainWindowViewModel(); MainWindowViewModel.Instance = new MainWindowViewModel();
Expand All @@ -31,7 +38,8 @@ private static async Task Main(string[] args)
await config.LoadOrCreateDefaultFileAsync(); await config.LoadOrCreateDefaultFileAsync();
Logger.LogInfo<Config>("Config is successfully initialized."); 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. if (!File.Exists(Global.IndexFilePath)) // Load the index file from working folder if we have it.
{ {
Expand All @@ -47,6 +55,12 @@ private static async Task Main(string[] args)
MainWindowViewModel.Instance.StatusBar = statusBar; 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) if (Global.IndexDownloader.Network != Network.Main)
{ {
MainWindowViewModel.Instance.Title += $" - {Global.IndexDownloader.Network}"; MainWindowViewModel.Instance.Title += $" - {Global.IndexDownloader.Network}";
Expand Down
129 changes: 129 additions & 0 deletions WalletWasabi.Gui/UiConfig.cs
@@ -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.");
}
}
}
35 changes: 34 additions & 1 deletion WalletWasabi.Gui/ViewModels/MainWindowViewModel.cs
@@ -1,4 +1,6 @@
using AvalonStudio.Extensibility; using Avalonia;
using Avalonia.Controls;
using AvalonStudio.Extensibility;
using AvalonStudio.Extensibility.Dialogs; using AvalonStudio.Extensibility.Dialogs;
using AvalonStudio.Shell; using AvalonStudio.Shell;
using NBitcoin; using NBitcoin;
Expand All @@ -23,6 +25,30 @@ public string Title
internal set { this.RaiseAndSetIfChanged(ref _title, value); } 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; private StatusBarViewModel _statusBar;


public StatusBarViewModel StatusBar public StatusBarViewModel StatusBar
Expand Down Expand Up @@ -62,5 +88,12 @@ public bool CanClose
get { return _canClose; } get { return _canClose; }
set { this.RaiseAndSetIfChanged(ref _canClose, value); } 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.