Skip to content

Commit

Permalink
[desktop] Restore window position
Browse files Browse the repository at this point in the history
  • Loading branch information
Banyc committed Dec 6, 2020
1 parent 739892e commit 524107e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/UnforgettableMemo.WinDesktop/MainWindow.xaml.Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.IO;
using System.Text.Json;

namespace UnforgettableMemo.WinDesktop
{
public partial class MainWindow
{
public class MainWindowSettings
{
public double? TopLocation { get; set; }
public double? LeftLocation { get; set; }
}

private MainWindowSettings LoadSettings()
{
string filePath = System.IO.Path.Combine(this.settingsDirectory, this.settingsFilename);
if (!File.Exists(filePath))
{
return new MainWindowSettings();
}
string fileText = File.ReadAllText(filePath);
MainWindowSettings settings = JsonSerializer.Deserialize<MainWindowSettings>(fileText);
return settings;
}

public void SaveSettings(MainWindowSettings settings)
{
Directory.CreateDirectory(this.settingsDirectory);
string fileText = JsonSerializer.Serialize(settings, new JsonSerializerOptions()
{
WriteIndented = true
});
File.WriteAllText(System.IO.Path.Combine(this.settingsDirectory, this.settingsFilename), fileText);
}
}
}
17 changes: 17 additions & 0 deletions src/UnforgettableMemo.WinDesktop/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ public class MainWindowViewModel
public Memo DisplayingMemo { get; set; }
}

private readonly string settingsDirectory = "desktop";
private readonly string settingsFilename = "mainWindowSettings.json";

private readonly MainWindowViewModel viewModel = new MainWindowViewModel();

private MemoScheduler memoScheduler;
public MainWindow()
{
MainWindowSettings settings = LoadSettings();
if (settings.LeftLocation != null)
{
this.Left = settings.LeftLocation.Value;
}
if (settings.TopLocation != null)
{
this.Top = settings.TopLocation.Value;
}
InitializeComponent();
}

Expand Down Expand Up @@ -84,6 +96,11 @@ private void btnReview_Click(object sender, RoutedEventArgs e)

private void btnExit_Click(object sender, RoutedEventArgs e)
{
SaveSettings(new MainWindowSettings()
{
LeftLocation = this.Left,
TopLocation = this.Top
});
this.Close();
}

Expand Down

0 comments on commit 524107e

Please sign in to comment.