Files
speckle.xunit.runner.wpf/xunit.runner.wpf/Storage.cs
T
Dustin Campbell 135eccb795 Save and restore the position of the Main window
It's pretty annoying that the XUnit runner always starts with the same width and height. This
change adds code to save and restore the position. It uses the Win32 `GetWindowPlacement` and
`SetWindowPlacement` APIs to ensure that the normal position is properly saved and restored
regardless of whether the window is maximized or not.
2015-12-03 08:37:59 -08:00

87 lines
2.6 KiB
C#

using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Text;
using System.Windows;
using System.Xml;
using System.Xml.Linq;
namespace xunit.runner.wpf
{
internal static partial class Storage
{
private const string WindowLayoutFileName = "window_layout.xml";
private static string GetWindowLayoutFileName(Window window) => $"{window.Name}_{WindowLayoutFileName}";
private static IsolatedStorageFile GetStorageFile() => IsolatedStorageFile.GetUserStoreForDomain();
public static XmlTextReader OpenXmlFile(string fileName)
{
var storage = GetStorageFile();
if (!storage.FileExists(fileName))
{
return null;
}
var fileStream = storage.OpenFile(fileName, FileMode.Open, FileAccess.Read);
var reader = new XmlTextReader(fileStream);
reader.WhitespaceHandling = WhitespaceHandling.None;
return reader;
}
public static XmlTextWriter CreateXmlFile(string fileName)
{
var storage = GetStorageFile();
var fileStream = storage.CreateFile(fileName);
var writer = new XmlTextWriter(fileStream, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
return writer;
}
public static void RestoreWindowLayout(Window window)
{
if (window == null)
{
throw new ArgumentNullException(nameof(window));
}
if (string.IsNullOrWhiteSpace(window.Name))
{
throw new ArgumentException("Name is not set.", nameof(window));
}
using (var windowLayoutReader = OpenXmlFile(GetWindowLayoutFileName(window)))
{
if (windowLayoutReader != null)
{
windowLayoutReader.MoveToContent();
var xml = XElement.Load(windowLayoutReader);
WindowPlacement.Restore(window, xml);
}
}
}
public static void SaveWindowLayout(Window window)
{
if (window == null)
{
throw new ArgumentNullException(nameof(window));
}
if (string.IsNullOrWhiteSpace(window.Name))
{
throw new ArgumentException("Name is not set.", nameof(window));
}
using (var windowLayoutWriter = CreateXmlFile(GetWindowLayoutFileName(window)))
{
var xml = WindowPlacement.Save(window);
xml.Save(windowLayoutWriter);
}
}
}
}