-
Notifications
You must be signed in to change notification settings - Fork 0
/
FenceWindow.cs
559 lines (462 loc) · 18.1 KB
/
FenceWindow.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
using NoFences.Model;
using NoFences.Util;
using NoFences.Win32;
using Peter;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using static NoFences.Win32.WindowUtil;
namespace NoFences
{
public partial class FenceWindow : Form
{
private int titleHeight;
private const int titleOffset = 3;
private const int itemWidth = 75;
private const int itemHeight = 32 + itemPadding + textHeight;
private const int textHeight = 35;
private const int itemPadding = 15;
private const float shadowDist = 1.5f;
private readonly FenceInfo fenceInfo;
private Font titleFont;
private Font iconFont;
private string selectedItem;
private string hoveringItem;
private bool shouldUpdateSelection;
private bool shouldRunDoubleClick;
private bool hasSelectionUpdated;
private bool hasHoverUpdated;
private bool isMinified;
private int prevHeight;
private int scrollHeight;
private int scrollOffset;
private readonly ThrottledExecution throttledMove = new ThrottledExecution(TimeSpan.FromSeconds(4));
private readonly ThrottledExecution throttledResize = new ThrottledExecution(TimeSpan.FromSeconds(4));
private readonly ShellContextMenu shellContextMenu = new ShellContextMenu();
private readonly ThumbnailProvider thumbnailProvider = new ThumbnailProvider();
private void ReloadFonts()
{
var family = new FontFamily("Segoe UI");
titleFont = new Font(family, (int)Math.Floor(titleHeight / 2.0));
iconFont = new Font(family, 9);
}
public FenceWindow(FenceInfo fenceInfo)
{
InitializeComponent();
Titulo();
DropShadow.ApplyShadows(this);
BlurUtil.EnableBlur(Handle);
WindowUtil.HideFromAltTab(Handle);
this.titleHeight = fenceInfo.TitleHeight;
this.MouseWheel += FenceWindow_MouseWheel;
thumbnailProvider.IconThumbnailLoaded += ThumbnailProvider_IconThumbnailLoaded;
if (titleHeight < 16 || titleHeight > 100)
titleHeight = 35;
ReloadFonts();
AllowDrop = true;
this.fenceInfo = fenceInfo;
Text = fenceInfo.Name;
Location = new Point(fenceInfo.PosX, fenceInfo.PosY);
Width = fenceInfo.Width;
Height = fenceInfo.Height;
//Chama função para tratar bordas dos docks
this.Region = GetRoundedRegion(this.Width, this.Height);
prevHeight = Height;
lockedToolStripMenuItem.Checked = fenceInfo.Locked;
minifyToolStripMenuItem.Checked = fenceInfo.CanMinify;
Minify();
}
public void Titulo()
{
txtTitu.Visible = true;
txtTitu.Focus();
txtTitu.SelectAll();
}
protected override void WndProc(ref Message m)
{
//Console.WriteLine(m.Msg.ToString("X4"));
// Remove border
if (m.Msg == 0x0083)
{
m.Result = IntPtr.Zero;
return;
}
// Mouse leave
var myrect = new Rectangle(Location, Size);
if (m.Msg == 0x02a2 && !myrect.IntersectsWith(new Rectangle(MousePosition, new Size(1, 1))))
{
Minify();
}
// Prevent maximize
if ((m.Msg == WM_SYSCOMMAND) && m.WParam.ToInt32() == 0xF032)
{
m.Result = IntPtr.Zero;
return;
}
// Prevent foreground
if (m.Msg == WM_SETFOCUS)
{
SetWindowPos(Handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
return;
}
// Other messages
base.WndProc(ref m);
// Se não estiver bloqueado e usando o botão esquerdo do mouse
if (MouseButtons == MouseButtons.Right || lockedToolStripMenuItem.Checked)
return;
// Então, permita arrastar e redimensionar
if (m.Msg == WM_NCHITTEST)
{
var pt = PointToClient(new Point(m.LParam.ToInt32()));
// Arrasta o dock
if ((int)m.Result <= HTCLIENT && pt.Y < titleHeight)
{
m.Result = new IntPtr(HTTOPLEFT);
// MessageBox.Show(m.Result.ToString());
}
// Ajustar o posicionamento do dock corretamente de acordo com a atela
m.Result = (IntPtr)HTCAPTION;
FenceWindow_MouseEnter(null, null);
if (Screen.PrimaryScreen.Bounds.X >= fenceInfo.PosX)
{
m.Result = new IntPtr(HTTOPLEFT);
MessageBox.Show(m.Result.ToString());
}
else if (pt.X > (Width - 10) && pt.Y < 10)
{
m.Result = new IntPtr(HTTOPRIGHT);
}
else if (pt.X < 10 && pt.Y > (Height - 10))
{
m.Result = new IntPtr(HTBOTTOMLEFT);
}
else if (pt.X > (Width - 10) && pt.Y > (Height - 10))
{
m.Result = new IntPtr(HTBOTTOMRIGHT);
}
else if (pt.Y > (Height - 10))
{
m.Result = new IntPtr(HTBOTTOM);
}
else if (pt.X < 10)
{
m.Result = new IntPtr(HTLEFT);
}
else if (pt.X > (Width - 10))
{
m.Result = new IntPtr(HTRIGHT);
}
}
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show(this, "Deseja realmente remover esta cerca?", "Remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
FenceManager.Instance.RemoveFence(fenceInfo);
Close();
}
}
private void DeleteItemToolStripMenuItem_Click(object sender, EventArgs e)
{
fenceInfo.Files.Remove(hoveringItem);
hoveringItem = null;
Save();
Refresh();
}
private void ContextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
deleteItemToolStripMenuItem.Visible = hoveringItem != null;
}
//Verificar o codigo abaixo, esta com problemas
private void FenceWindow_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) && !lockedToolStripMenuItem.Checked)
e.Effect = DragDropEffects.Move;
}
// verificar o codigo abaixo, esta com problemas
private void FenceWindow_DragDrop(object sender, DragEventArgs e)
{
var dropped = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (var file in dropped)
if (!fenceInfo.Files.Contains(file) && ItemExists(file))
fenceInfo.Files.Add(file);
Save();
Refresh();
}
//codigo abaixo com problemas, redimensiona ate certo tamanho depois da erro no dock
//redimensiona só a sombra apos um tamanho
private void FenceWindow_Resize(object sender, EventArgs e)
{
throttledResize.Run(() =>
{
fenceInfo.Width = Width;
fenceInfo.Height = isMinified ? prevHeight : Height;
Save();
});
Refresh();
}
private void FenceWindow_MouseMove(object sender, MouseEventArgs e)
{
Refresh();
}
private void FenceWindow_MouseEnter(object sender, EventArgs e)
{
if (minifyToolStripMenuItem.Checked && isMinified)
{
isMinified = false;
Height = prevHeight;
}
}
private void FenceWindow_MouseLeave(object sender, EventArgs e)
{
Minify();
selectedItem = null;
Refresh();
}
//Função para auto esconder o corpo do dock
private void Minify()
{
if (minifyToolStripMenuItem.Checked && !isMinified)
{
isMinified = true;
prevHeight = Height;
Height = titleHeight;
Refresh();
}
}
private void MinifyToolStripMenuItem_Click(object sender, EventArgs e)
{
if (isMinified)
{
Height = prevHeight;
isMinified = false;
}
fenceInfo.CanMinify = minifyToolStripMenuItem.Checked;
Save();
}
private void FenceWindow_Click(object sender, EventArgs e)
{
shouldUpdateSelection = true;
Refresh();
}
private void FenceWindow_DoubleClick(object sender, EventArgs e)
{
shouldRunDoubleClick = true;
Refresh();
}
private void FenceWindow_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clip = new Region(ClientRectangle);
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// Background
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(100, Color.Blue)), ClientRectangle);
// Title
e.Graphics.DrawString(Text, titleFont, Brushes.White, new PointF(Width / 2, titleOffset), new StringFormat { Alignment = StringAlignment.Center });
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(50, Color.Black)), new RectangleF(0, 0, Width, titleHeight));
// Items
var x = itemPadding;
var y = itemPadding;
scrollHeight = 0;
e.Graphics.Clip = new Region(new Rectangle(0, titleHeight, Width, Height - titleHeight));
foreach (var file in fenceInfo.Files)
{
var entry = FenceEntry.FromPath(file);
if (entry == null)
continue;
RenderEntry(e.Graphics, entry, x, y + titleHeight - scrollOffset);
var itemBottom = y + itemHeight;
if (itemBottom > scrollHeight)
scrollHeight = itemBottom;
x += itemWidth + itemPadding;
if (x + itemWidth > Width)
{
x = itemPadding;
y += itemHeight + itemPadding;
}
}
scrollHeight -= (ClientRectangle.Height - titleHeight);
// Scroll bars
if (scrollHeight > 0)
{
var contentHeight = Height - titleHeight;
var scrollbarHeight = contentHeight - scrollHeight;
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(150, Color.Black)), new Rectangle(Width - 5, titleHeight + scrollOffset, 5, scrollbarHeight));
scrollOffset = Math.Min(scrollOffset, scrollHeight);
}
// Manipuladores de clique
if (shouldUpdateSelection && !hasSelectionUpdated)
selectedItem = null;
if (!hasHoverUpdated)
hoveringItem = null;
shouldRunDoubleClick = false;
shouldUpdateSelection = false;
hasSelectionUpdated = false;
hasHoverUpdated = false;
}
private void RenderEntry(Graphics g, FenceEntry entry, int x, int y)
{
var icon = entry.ExtractIcon(thumbnailProvider);
var name = entry.Name;
var textPosition = new PointF(x, y + icon.Height + 5);
var textMaxSize = new SizeF(itemWidth, textHeight);
var stringFormat = new StringFormat { Alignment = StringAlignment.Center, Trimming = StringTrimming.EllipsisCharacter };
var textSize = g.MeasureString(name, iconFont, textMaxSize, stringFormat);
var outlineRect = new Rectangle(x - 2, y - 2, itemWidth + 2, icon.Height + (int)textSize.Height + 5 + 2);
var outlineRectInner = outlineRect.Shrink(1);
var mousePos = PointToClient(MousePosition);
var mouseOver = mousePos.X >= x && mousePos.Y >= y && mousePos.X < x + outlineRect.Width && mousePos.Y < y + outlineRect.Height;
if (mouseOver)
{
hoveringItem = entry.Path;
hasHoverUpdated = true;
}
if (mouseOver && shouldUpdateSelection)
{
selectedItem = entry.Path;
shouldUpdateSelection = false;
hasSelectionUpdated = true;
}
if (mouseOver && shouldRunDoubleClick)
{
shouldRunDoubleClick = false;
entry.Open();
}
if (selectedItem == entry.Path)
{
if (mouseOver)
{
g.DrawRectangle(new Pen(Color.FromArgb(120, SystemColors.ActiveBorder)), outlineRectInner);
g.FillRectangle(new SolidBrush(Color.FromArgb(100, SystemColors.GradientActiveCaption)), outlineRect);
}
else
{
g.DrawRectangle(new Pen(Color.FromArgb(120, SystemColors.ActiveBorder)), outlineRectInner);
g.FillRectangle(new SolidBrush(Color.FromArgb(80, SystemColors.GradientInactiveCaption)), outlineRect);
}
}
else
{
if (mouseOver)
{
g.DrawRectangle(new Pen(Color.FromArgb(120, SystemColors.ActiveBorder)), outlineRectInner);
g.FillRectangle(new SolidBrush(Color.FromArgb(80, SystemColors.ActiveCaption)), outlineRect);
}
}
g.DrawIcon(icon, x + itemWidth / 2 - icon.Width / 2, y);
g.DrawString(name, iconFont, new SolidBrush(Color.FromArgb(180, 15, 15, 15)), new RectangleF(textPosition.Move(shadowDist, shadowDist), textMaxSize), stringFormat);
g.DrawString(name, iconFont, Brushes.White, new RectangleF(textPosition, textMaxSize), stringFormat);
}
private void RenameToolStripMenuItem_Click(object sender, EventArgs e)
{
Titulo();
}
private void NewFenceToolStripMenuItem_Click(object sender, EventArgs e)
{
FenceManager.Instance.CreateFence("Novo Dock");
}
private void FenceWindow_FormClosed(object sender, FormClosedEventArgs e)
{
if (Application.OpenForms.Count == 0)
Application.Exit();
}
private readonly object saveLock = new object();
private void Save()
{
lock (saveLock)
{
FenceManager.Instance.UpdateFence(fenceInfo);
}
}
private void FenceWindow_LocationChanged(object sender, EventArgs e)
{
throttledMove.Run(() =>
{
fenceInfo.PosX = Location.X;
fenceInfo.PosY = Location.Y;
Save();
});
}
private void LockedToolStripMenuItem_Click(object sender, EventArgs e)
{
fenceInfo.Locked = lockedToolStripMenuItem.Checked;
Save();
}
private void TitleSizeToolStripMenuItem_Click(object sender, EventArgs e)
{
var dialog = new HeightDialog(fenceInfo.TitleHeight);
if (dialog.ShowDialog(this) == DialogResult.OK)
{
fenceInfo.TitleHeight = dialog.TitleHeight;
titleHeight = dialog.TitleHeight;
ReloadFonts();
Minify();
if (isMinified)
{
Height = titleHeight;
}
Refresh();
Save();
}
}
private void FenceWindow_MouseWheel(object sender, MouseEventArgs e)
{
if (scrollHeight < 1)
return;
scrollOffset -= Math.Sign(e.Delta) * 10;
if (scrollOffset < 0)
scrollOffset = 0;
if (scrollOffset > scrollHeight)
scrollOffset = scrollHeight;
Invalidate();
}
private void ThumbnailProvider_IconThumbnailLoaded(object sender, EventArgs e)
{
Invalidate();
}
private bool ItemExists(string path)
{
return File.Exists(path) || Directory.Exists(path);
}
private void PictureBox1_Click(object sender, EventArgs e)
{
if (MessageBox.Show(this, "Deseja realmente fechar este dock?", "Fechar", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
FenceManager.Instance.RemoveFence(fenceInfo);
Close();
}
}
private void PicMenu_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
return;
if (hoveringItem != null && !ModifierKeys.HasFlag(Keys.Shift))
{
shellContextMenu.ShowContextMenu(new[] { new FileInfo(hoveringItem) }, MousePosition);
}
else
{
appContextMenu.Show(this, e.Location);
}
}
private void TxtTitu_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
txtTitu.Visible = false;
RenomeTitu();
}
}
public void RenomeTitu()
{
if (txtTitu.Text != string.Empty)
{
Text = txtTitu.Text;
fenceInfo.Name = Text;
Refresh();
Save();
}
}
}
}