forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowPositionList.cs
88 lines (77 loc) · 2.46 KB
/
WindowPositionList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
using GitCommands;
namespace GitUI
{
/// <summary>
/// Stores the state and position of a single window.
/// </summary>
[DebuggerDisplay("Name={Name} Rect={Rect} DeviceDpi={DeviceDpi} State={State}")]
[Serializable]
public class WindowPosition
{
protected WindowPosition()
{
DeviceDpi = 96;
}
public WindowPosition(Rectangle rect, int deviceDpi, FormWindowState state, string name)
{
Rect = rect;
DeviceDpi = deviceDpi;
State = state;
Name = name;
}
public Rectangle Rect { get; set; }
[DefaultValue(96)]
public int DeviceDpi { get; set; }
public FormWindowState State { get; set; }
public string? Name { get; set; }
}
[Serializable]
public class WindowPositionList
{
private static readonly string ConfigFilePath = Path.Combine(AppSettings.LocalApplicationDataPath.Value, "WindowPositions.xml");
private static readonly XmlSerializer _serializer = new(typeof(WindowPositionList));
public List<WindowPosition> WindowPositions { get; set; } = new List<WindowPosition>();
protected WindowPositionList()
{
}
public WindowPosition? Get(string name)
{
return WindowPositions.FirstOrDefault(r => r.Name == name);
}
public void AddOrUpdate(WindowPosition pos)
{
WindowPositions.RemoveAll(r => r.Name == pos.Name);
WindowPositions.Add(pos);
}
public static WindowPositionList? Load()
{
if (!File.Exists(ConfigFilePath))
{
return new WindowPositionList();
}
try
{
using var stream = File.Open(ConfigFilePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
return (WindowPositionList)_serializer.Deserialize(stream);
}
catch
{
return new WindowPositionList();
}
}
public void Save()
{
using var stream = File.Open(ConfigFilePath, FileMode.Create, FileAccess.Write);
_serializer.Serialize(stream, this);
}
}
}