-
Notifications
You must be signed in to change notification settings - Fork 99
/
PaneLayoutEngine.cs
146 lines (116 loc) · 4.23 KB
/
PaneLayoutEngine.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Linq;
namespace workspacer
{
public class VertLayoutEngine : PaneLayoutEngine
{
public VertLayoutEngine() : base(true) { }
public override string Name { get; set; } = "rows";
}
public class HorzLayoutEngine : PaneLayoutEngine
{
public HorzLayoutEngine() : base(false) { }
public override string Name { get; set; } = "columns";
}
public abstract class PaneLayoutEngine : ILayoutEngine
{
private readonly bool _vertical;
private readonly int _numInPrimary;
private readonly double _primaryPercentIncrement;
private int _numInPrimaryOffset = 0;
private double _primaryPercentOffset = 0;
public abstract string Name { get; set; }
public PaneLayoutEngine(bool vertical) : this(vertical, 1, 0.03) { }
public PaneLayoutEngine(bool vertical, int numInPrimary, double primaryPercentIncrement)
{
_vertical = vertical;
_numInPrimary = numInPrimary;
_primaryPercentIncrement = primaryPercentIncrement;
}
public IEnumerable<IWindowLocation> CalcLayout(IEnumerable<IWindow> windows, int spaceWidth, int spaceHeight)
{
var list = new List<IWindowLocation>();
var numWindows = windows.Count();
if (numWindows == 0)
return list;
double primaryPercent = (1.0D / numWindows) + _primaryPercentOffset;
int numInPrimary = Math.Min(GetNumInPrimary(), numWindows);
int numInSecondary = numWindows - numInPrimary;
if (_vertical)
{
int width = spaceWidth;
int height = 0;
int primaryHeight = 0;
if (numInSecondary == 0)
{
// All the windows are in the primary area
primaryHeight = spaceHeight / numWindows;
}
else
{
primaryHeight = (int)(spaceHeight * primaryPercent / numInPrimary);
height = (spaceHeight - primaryHeight * numInPrimary) / numInSecondary;
}
int startY = 0;
for (var i = 0; i < numWindows; i++)
{
int currentHeight = i < numInPrimary ? primaryHeight : height;
list.Add(new WindowLocation(0, startY, width, currentHeight, WindowState.Normal));
startY += currentHeight;
}
}
else
{
int width = 0;
int primaryWidth = 0;
if (numInSecondary == 0)
{
// All the windows are in the primary area
primaryWidth = spaceWidth / numWindows;
}
else
{
primaryWidth = (int)(spaceWidth * primaryPercent / numInPrimary);
width = (spaceWidth - primaryWidth * numInPrimary) / numInSecondary;
}
int height = spaceHeight;
int startX = 0;
for (var i = 0; i < numWindows; i++)
{
int currentWidth = i < numInPrimary ? primaryWidth : width;
list.Add(new WindowLocation(startX, 0, currentWidth, height, WindowState.Normal));
startX += currentWidth;
}
}
return list;
}
public void IncrementNumInPrimary()
{
_numInPrimaryOffset++;
}
public void DecrementNumInPrimary()
{
if (GetNumInPrimary() > 1)
{
_numInPrimaryOffset--;
}
}
public void ExpandPrimaryArea()
{
_primaryPercentOffset += _primaryPercentIncrement;
}
public void ResetPrimaryArea()
{
_primaryPercentOffset = 0;
}
public void ShrinkPrimaryArea()
{
_primaryPercentOffset -= _primaryPercentIncrement;
}
private int GetNumInPrimary()
{
return _numInPrimary + _numInPrimaryOffset;
}
}
}