-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListForm.cs
289 lines (245 loc) · 11.7 KB
/
ListForm.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace MiLauncherFW
{
/// <summary>
/// Represents a window of the file list that has responsibilities
/// to control selected list item and display information for user.
/// </summary>
internal partial class ListForm : Form
{
//
// Properties
//
internal List<FileStats> ListViewItems { get; private set; } = new List<FileStats>();
internal SortKeyOption SortKey { get; set; } = SortKeyOption.Priority;
internal (string, string) ModeCaptions { get; set; }
private int _virtualListIndex;
internal int VirtualListIndex
{
get {
return _virtualListIndex;
}
set {
_virtualListIndex = PositiveModulo(value, listView.VirtualListSize);
// With MultiSelect false, adding a new index automatically removes old one
listView.SelectedIndices.Add(_virtualListIndex);
}
}
//
// Delegate
//
public Action<KeyEventArgs> ListViewKeyDown;
//
// Constructor
//
public ListForm()
{
InitializeComponent();
var fontName = Program.appSettings.ListViewFontName;
var fontSize = Program.appSettings.ListViewFontSize;
listView.Font = new Font(fontName, fontSize);
}
//
// Event handler
//
private void listView_DrawItem(object sender, DrawListViewItemEventArgs e)
{
if (VirtualListIndex == e.Item.Index) {
// TODO: CMIC
e.Graphics.FillRectangle(new SolidBrush(MainForm.colorPattern4), e.Bounds);
}
// e.Item.Text may contain DirectorySeparation space, but it does not affect for now
var match = Regex.Match(e.Item.Text, @"^(?<Directory>.*\\)(?<File>[^\\]+\\?)$");
var directoryName = match.Groups["Directory"].Value;
var fileName = match.Groups["File"].Value;
var fileNameColor = fileName.EndsWith("\\") ? MainForm.colorPattern3 : MainForm.colorPattern5;
Rectangle bounds = e.Bounds;
TextRenderer.DrawText(e.Graphics, directoryName, e.Item.Font, bounds, e.Item.ForeColor,
TextFormatFlags.NoPadding);
bounds.X += TextRenderer.MeasureText(e.Graphics, directoryName, e.Item.Font, Size.Empty,
TextFormatFlags.NoPadding).Width;
TextRenderer.DrawText(e.Graphics, fileName, e.Item.Font, bounds, fileNameColor, TextFormatFlags.NoPadding);
}
private void listView_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
var pathName = ListViewItems[e.ItemIndex].ShortPathName
?? ListViewItems[e.ItemIndex].FullPathName;
var itemText = Program.appSettings.DirectorySeparation
? Regex.Replace(pathName, @"\\(?=.)", "\\ ")
: pathName;
e.Item = new ListViewItem(itemText);
}
private void listView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
// TODO: CMIC
e.Graphics.FillRectangle(new SolidBrush(MainForm.colorPattern2), e.Bounds);
Rectangle bounds = e.Bounds;
// Capture SortKey name and value
var sortKeyMatch = Regex.Match(e.Header.Text, @"^(<[^>]*>)([^<]*)");
// Display SortKey name
TextRenderer.DrawText(e.Graphics, sortKeyMatch.Groups[1].Value, e.Font, bounds, Color.White,
TextFormatFlags.NoPadding);
bounds.X += TextRenderer.MeasureText(e.Graphics, sortKeyMatch.Groups[1].Value, e.Font,
Size.Empty, TextFormatFlags.NoPadding).Width;
// Display SortKey value with specified color
TextRenderer.DrawText(e.Graphics, sortKeyMatch.Groups[2].Value, e.Font, bounds,
MainForm.colorPattern5, TextFormatFlags.NoPadding);
bounds.X += TextRenderer.MeasureText(e.Graphics, sortKeyMatch.Groups[2].Value, e.Font,
Size.Empty, TextFormatFlags.NoPadding).Width;
// Capture CrawlMode name and CrawlPath
var crawlModeMatch = Regex.Match(e.Header.Text, @"(<CrawlMode> .*\\)([^\\]+\\)");
if (crawlModeMatch.Success) {
// Display SortKey name
TextRenderer.DrawText(e.Graphics, crawlModeMatch.Groups[1].Value, e.Font, bounds, Color.White,
TextFormatFlags.NoPadding);
bounds.X += TextRenderer.MeasureText(e.Graphics, crawlModeMatch.Groups[1].Value, e.Font,
Size.Empty, TextFormatFlags.NoPadding).Width;
// Display SortKey value with specified color
TextRenderer.DrawText(e.Graphics, crawlModeMatch.Groups[2].Value, e.Font, bounds,
MainForm.colorPattern5, TextFormatFlags.NoPadding);
bounds.X += TextRenderer.MeasureText(e.Graphics, crawlModeMatch.Groups[2].Value, e.Font,
Size.Empty, TextFormatFlags.NoPadding).Width;
}
}
// Key down event in listView makes focus on MainForm
private void listView_KeyDown(object sender, KeyEventArgs e)
{
ListViewKeyDown?.Invoke(e);
}
//
// Methods
//
internal void SetVirtualList(List<FileStats> sourceItems)
{
ListViewItems = sourceItems.OrderByDescending(x => x.SortValue(SortKey)).ToList();
listView.VirtualListSize = ListViewItems.Count;
}
internal FileStats CurrentItem()
{
return (Visible & listView.VirtualListSize > 0) ? ListViewItems[VirtualListIndex] : null;
}
internal void ShowAt(int? x = null, int? y = null, int index = 0)
{
Location = new Point(x ?? Location.X, y ?? Location.Y);
Visible = true;
if (ListViewItems.Any()) {
// To add() SelectedIndices, listView requires focus on, which is mentioned by MSDN
// Changing height in AdjustHeight() seems to focus on list view
AdjustHeight();
VirtualListIndex = index;
SetColumnHeader(VirtualListIndex);
listView.EnsureVisible(VirtualListIndex);
AdjustWidth();
}
else {
// TODO: CMICst for Column header height; 30
Height = 30;
SetColumnHeader();
int headerWidth = TextRenderer.MeasureText(Header.Text, listView.Font).Width;
// TODO: CMICst for allowance between ListView and ListForm; 40
Width = headerWidth + 40;
// Width might be greater than headerWidth + 40 due to OS and FormBorderStyle restriction
listView.Columns[0].Width = Width - 40;
}
listView.Refresh();
}
internal void AdjustHeight()
{
var heightPerLine = listView.GetItemRect(0).Height;
var lineCount = Math.Min(Program.appSettings.MaxListLine, listView.VirtualListSize + 1);
// TODO: CMICst, 30 is Column header height
Height = heightPerLine * lineCount + 30;
}
internal void AdjustWidth()
{
int headerWidth = TextRenderer.MeasureText(Header.Text, listView.Font).Width;
listView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
int columnContentWidth = listView.GetItemRect(0).Width;
var maxWidth = Math.Max(columnContentWidth, headerWidth);
// TODO: CMICst, adding 10 in order to have enough space for the actual width,
// which might be caused by MeasureText is not taking e.Graphics as argument
listView.Columns[0].Width = maxWidth + 10;
// TODO: CMICst
Width = maxWidth + 40;
}
private void SetColumnHeader(int? index = null)
{
var sortKeyString = (SortKey == SortKeyOption.FullPathName) ? "Path" : SortKey.ToString();
Header.Text = "<" + sortKeyString + "> ";
Header.Text += (SortKey != SortKeyOption.FullPathName && index != null)
? ListViewItems[(int)index].SortValue(SortKey)
: "--";
// If any, displays additional information defined by ModeCaptions in column header
if (ModeCaptions == (null, null)) return;
Header.Text += " <" + ModeCaptions.Item1 + "> ";
var baseWidth = TextRenderer.MeasureText(Header.Text, listView.Font).Width;
Header.Text += FileStats.GetShortenedString(ModeCaptions.Item2, baseWidth) ?? ModeCaptions.Item2;
}
private static int PositiveModulo(int x, int y)
{
int z = x % y;
return (z >= 0) ? z : z + y;
}
//
// Methods for commands
//
internal void SelectNextItem()
{
if (listView.VirtualListSize == 0) return;
VirtualListIndex++;
SetColumnHeader(VirtualListIndex);
var originalScrollPosition = listView.GetItemRect(0).Y;
listView.EnsureVisible(VirtualListIndex);
// Resize column width only if displaying initially or scrolling list in order to reduce flickers
// If GetItemRect(0).Y changes after EnsureVisible(), list view scrolls. Then, resize column
if (VirtualListIndex == 0 || originalScrollPosition != listView.GetItemRect(0).Y)
AdjustWidth();
}
internal void SelectPreviousItem()
{
if (listView.VirtualListSize == 0) return;
VirtualListIndex--;
SetColumnHeader(VirtualListIndex);
var originalScrollPosition = listView.GetItemRect(0).Y;
listView.EnsureVisible(VirtualListIndex);
// Resize column width only if displaying initially or scrolling list in order to reduce flickers
// If GetItemRect(0).Y changes after EnsureVisible(), list view scrolls. Then, resize column
if (VirtualListIndex == 0 || originalScrollPosition != listView.GetItemRect(0).Y)
AdjustWidth();
}
internal void SortByDescending(SortKeyOption? option = null)
{
if (!Visible) return;
SortKey = option ?? SortKey;
ListViewItems = ListViewItems.OrderByDescending(x => x.SortValue(SortKey)).ToList();
ShowAt();
}
internal void SortBy(SortKeyOption? option = null)
{
if (!Visible) return;
SortKey = option ?? SortKey;
ListViewItems = ListViewItems.OrderBy(x => x.SortValue(SortKey)).ToList();
ShowAt();
}
internal void CopyPath()
{
Clipboard.SetText(CurrentItem().FullPathName);
}
internal void IncrementPriority(int delta)
{
if (CurrentItem() == null) return;
var orgPathName = CurrentItem().FullPathName;
CurrentItem().Priority += delta;
ListViewItems = ListViewItems.OrderByDescending(x => x.SortValue(SortKey)).ToList();
var orgPathIndex = ListViewItems.FindIndex(x => x.FullPathName == orgPathName);
ShowAt(null, null, orgPathIndex);
}
}
}