-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.cpp
2535 lines (2143 loc) · 79.5 KB
/
ui.cpp
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2023 Christopher Antos
// License: http://opensource.org/licenses/MIT
#include "main.h"
#include "data.h"
#include "scan.h"
#include "sunburst.h"
#include "actions.h"
#include "ui.h"
#include "sunburst.h"
#include "dontscan.h"
#include "DarkMode.h"
#include "res.h"
#include "version.h"
#include <windowsx.h>
#include <iosfwd>
extern const WCHAR c_fontface[];
void inset_rect_for_stroke(D2D1_RECT_F& rect, FLOAT stroke)
{
rect.left += 0.5f;
rect.top += 0.5f;
rect.right -= 0.5f;
rect.bottom -= 0.5f;
if (stroke > 1.0f)
{
stroke -= 1.0f;
rect.left += stroke / 2;
rect.top += stroke / 2;
rect.right -= stroke / 2;
rect.bottom -= stroke / 2;
}
}
FLOAT MakeUpIcon(DirectHwndRenderTarget& target, RECT rc, const DpiScaler& dpi, ID2D1Geometry** out)
{
SPI<ID2D1PathGeometry> spGeometry;
if (target.Factory() && SUCCEEDED(target.Factory()->CreatePathGeometry(&spGeometry)))
{
const LONG dim = std::min<LONG>(rc.right - rc.left, rc.bottom - rc.top) * 3 / 4;
const LONG cx = dim * 3 / 5;
const LONG cy = dim * 4 / 5;
const FLOAT thickness = FLOAT(std::max<LONG>(3, dim / 8));
const FLOAT left = FLOAT(rc.left + ((rc.right - rc.left) - cx) / 2);
const FLOAT top = FLOAT(rc.top + ((rc.bottom - rc.top) - cy) / 2);
D2D1_RECT_F rect = D2D1::RectF(left, top, left + cx, top + cy);
SPI<ID2D1GeometrySink> spSink;
if (SUCCEEDED(spGeometry->Open(&spSink)))
{
spSink->SetFillMode(D2D1_FILL_MODE_WINDING);
spSink->BeginFigure(D2D1::Point2F(rect.right, rect.bottom), D2D1_FIGURE_BEGIN_FILLED);
D2D1_POINT_2F points[] =
{
D2D1::Point2F(rect.left + thickness, rect.bottom),
D2D1::Point2F(rect.left + thickness, rect.top + thickness*2),
D2D1::Point2F(rect.left, rect.top + thickness*2),
D2D1::Point2F(rect.left + thickness*3/2, rect.top + (LONG(thickness) & 1)),
D2D1::Point2F(rect.left + thickness*3, rect.top + thickness*2),
D2D1::Point2F(rect.left + thickness*2, rect.top + thickness*2),
D2D1::Point2F(rect.left + thickness*2, rect.bottom - thickness),
D2D1::Point2F(rect.right, rect.bottom - thickness),
};
spSink->AddLines(points, _countof(points));
spSink->EndFigure(D2D1_FIGURE_END_CLOSED);
spSink->Close();
*out = spGeometry.Transfer();
}
}
return 0.0f; // FillGeometry.
}
FLOAT MakeBackIcon(DirectHwndRenderTarget& target, RECT rc, const DpiScaler& dpi, ID2D1Geometry** out)
{
SPI<ID2D1PathGeometry> spGeometry;
if (target.Factory() && SUCCEEDED(target.Factory()->CreatePathGeometry(&spGeometry)))
{
const LONG dim = std::min<LONG>(rc.right - rc.left, rc.bottom - rc.top) * 3 / 4;
const LONG cx = dim * 4 / 5;
const FLOAT thickness = FLOAT(std::max<LONG>(3, dim / 8));
const LONG cy = LONG(thickness) * 3;
const FLOAT left = FLOAT(rc.left + ((rc.right - rc.left) - cx) / 2);
const FLOAT top = FLOAT(rc.top + ((rc.bottom - rc.top) - cy) / 2);
D2D1_RECT_F rect = D2D1::RectF(left, top, left + cx, top + cy);
SPI<ID2D1GeometrySink> spSink;
if (SUCCEEDED(spGeometry->Open(&spSink)))
{
spSink->SetFillMode(D2D1_FILL_MODE_WINDING);
spSink->BeginFigure(D2D1::Point2F(rect.right, rect.bottom - thickness), D2D1_FIGURE_BEGIN_FILLED);
D2D1_POINT_2F points[] =
{
D2D1::Point2F(rect.left + thickness*2, rect.bottom - thickness),
D2D1::Point2F(rect.left + thickness*2, rect.bottom),
D2D1::Point2F(rect.left + (LONG(thickness) & 1), rect.top + thickness*3/2),
D2D1::Point2F(rect.left + thickness*2, rect.top),
D2D1::Point2F(rect.left + thickness*2, rect.top + thickness),
D2D1::Point2F(rect.right, rect.top + thickness),
};
spSink->AddLines(points, _countof(points));
spSink->EndFigure(D2D1_FIGURE_END_CLOSED);
spSink->Close();
*out = spGeometry.Transfer();
}
}
return 0.0f; // FillGeometry.
}
FLOAT MakeRefreshIcon(DirectHwndRenderTarget& target, RECT rc, const DpiScaler& dpi, ID2D1Geometry** out)
{
SPI<ID2D1PathGeometry> spGeometry;
if (target.Factory() && SUCCEEDED(target.Factory()->CreatePathGeometry(&spGeometry)))
{
const LONG dim = std::min<LONG>(rc.right - rc.left, rc.bottom - rc.top) * 3 / 4;
const LONG cx = dim & ~1;
const LONG cy = dim & ~1;
const FLOAT thickness = FLOAT(std::max<LONG>(3, dim / 8));
const FLOAT left = FLOAT(rc.left + ((rc.right - rc.left) - cx) / 2);
const FLOAT top = FLOAT(rc.top + ((rc.bottom - rc.top) - cy) / 2);
D2D1_RECT_F rect = D2D1::RectF(left, top, left + cx, top + cy);
SPI<ID2D1GeometrySink> spSink;
if (SUCCEEDED(spGeometry->Open(&spSink)))
{
const D2D1_POINT_2F center = D2D1::Point2F((rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2);
spSink->SetFillMode(D2D1_FILL_MODE_WINDING);
spSink->BeginFigure(D2D1::Point2F(center.x, rect.top + thickness), D2D1_FIGURE_BEGIN_FILLED);
const FLOAT outer_radius = (rect.right - rect.left) / 2 - thickness;
D2D1_ARC_SEGMENT outer;
outer.size = D2D1::SizeF(outer_radius, outer_radius);
outer.sweepDirection = D2D1_SWEEP_DIRECTION_CLOCKWISE;
outer.point = D2D1::Point2F(rect.left + thickness, center.y);
outer.rotationAngle = -90.0f;
outer.arcSize = D2D1_ARC_SIZE_LARGE;
spSink->AddArc(outer);
D2D1_POINT_2F points[] =
{
D2D1::Point2F(rect.left, center.y),
D2D1::Point2F(rect.left + thickness*3/2, center.y - thickness*2 + (LONG(thickness) & 1)),
D2D1::Point2F(rect.left + thickness*3, center.y),
D2D1::Point2F(rect.left + thickness*2, center.y),
};
spSink->AddLines(points, _countof(points));
D2D1_ARC_SEGMENT inner;
inner.size = D2D1::SizeF(outer_radius - thickness, outer_radius - thickness);
inner.sweepDirection = D2D1_SWEEP_DIRECTION_COUNTER_CLOCKWISE;
inner.point = D2D1::Point2F(center.x, rect.top + thickness*2);
inner.rotationAngle = 270.0f;
inner.arcSize = D2D1_ARC_SIZE_LARGE;
spSink->AddArc(inner);
spSink->EndFigure(D2D1_FIGURE_END_CLOSED);
spSink->Close();
*out = spGeometry.Transfer();
}
}
return 0.0f; // FillGeometry.
}
FLOAT MakeAppsIcon(DirectHwndRenderTarget& target, RECT rc, const DpiScaler& dpi, ID2D1Geometry** out)
{
FLOAT stroke = 0.0f;
SPI<ID2D1PathGeometry> spGeometry;
if (target.Factory() && SUCCEEDED(target.Factory()->CreatePathGeometry(&spGeometry)))
{
const LONG dim = std::min<LONG>(rc.right - rc.left, rc.bottom - rc.top) * 7 / 10;
const LONG margin = std::max<LONG>(2, dim / 10);
const LONG extent = dim - margin*2;
stroke = FLOAT(std::max<LONG>(2, extent / 10));
const FLOAT halfstroke = stroke/2;
const FLOAT leg = FLOAT(extent/2 - std::max<LONG>(1, LONG(halfstroke)));
const FLOAT effectiveleg = leg - halfstroke;
const FLOAT left = FLOAT(rc.left + ((rc.right - rc.left) - extent) / 2);
const FLOAT top = FLOAT(rc.top + ((rc.bottom - rc.top) - extent) / 2);
D2D1_RECT_F rect = D2D1::RectF(left, top, left + extent, top + extent);
SPI<ID2D1GeometrySink> spSink;
if (SUCCEEDED(spGeometry->Open(&spSink)))
{
spSink->SetFillMode(D2D1_FILL_MODE_WINDING);
{
spSink->BeginFigure(D2D1::Point2F(rect.right - halfstroke, rect.bottom - halfstroke), D2D1_FIGURE_BEGIN_FILLED);
D2D1_POINT_2F points[] =
{
D2D1::Point2F(rect.right - effectiveleg, rect.bottom - halfstroke),
D2D1::Point2F(rect.right - effectiveleg, rect.bottom - effectiveleg),
D2D1::Point2F(rect.right - halfstroke, rect.bottom - effectiveleg),
};
spSink->AddLines(points, _countof(points));
spSink->EndFigure(D2D1_FIGURE_END_CLOSED);
}
{
spSink->BeginFigure(D2D1::Point2F(rect.left + halfstroke, rect.bottom - halfstroke), D2D1_FIGURE_BEGIN_FILLED);
D2D1_POINT_2F points[] =
{
D2D1::Point2F(rect.left + effectiveleg, rect.bottom - halfstroke),
D2D1::Point2F(rect.left + effectiveleg, rect.bottom - effectiveleg),
D2D1::Point2F(rect.left + halfstroke, rect.bottom - effectiveleg),
};
spSink->AddLines(points, _countof(points));
spSink->EndFigure(D2D1_FIGURE_END_CLOSED);
}
{
spSink->BeginFigure(D2D1::Point2F(rect.left + halfstroke, rect.top + halfstroke), D2D1_FIGURE_BEGIN_FILLED);
D2D1_POINT_2F points[] =
{
D2D1::Point2F(rect.left + effectiveleg, rect.top + halfstroke),
D2D1::Point2F(rect.left + effectiveleg, rect.top + effectiveleg),
D2D1::Point2F(rect.left + halfstroke, rect.top + effectiveleg),
};
spSink->AddLines(points, _countof(points));
spSink->EndFigure(D2D1_FIGURE_END_CLOSED);
}
{
const FLOAT tiltedoffset = sqrtf((leg-stroke)*(leg-stroke)/2);
spSink->BeginFigure(D2D1::Point2F(rect.right - effectiveleg, rect.top + effectiveleg - tiltedoffset), D2D1_FIGURE_BEGIN_FILLED);
D2D1_POINT_2F points[] =
{
D2D1::Point2F(rect.right - effectiveleg + tiltedoffset, rect.top + effectiveleg - tiltedoffset*2),
D2D1::Point2F(rect.right - effectiveleg + tiltedoffset*2, rect.top + effectiveleg - tiltedoffset),
D2D1::Point2F(rect.right - effectiveleg + tiltedoffset, rect.top + effectiveleg),
};
spSink->AddLines(points, _countof(points));
spSink->EndFigure(D2D1_FIGURE_END_CLOSED);
}
spSink->Close();
*out = spGeometry.Transfer();
}
}
return stroke; // DrawGeometry when > 0.0f.
}
FLOAT MakeFolderIcon(DirectHwndRenderTarget& target, RECT rc, const DpiScaler& dpi, ID2D1Geometry** out)
{
FLOAT stroke = 0.0f;
SPI<ID2D1PathGeometry> spGeometry;
if (target.Factory() && SUCCEEDED(target.Factory()->CreatePathGeometry(&spGeometry)))
{
const LONG dim = std::min<LONG>(rc.right - rc.left, rc.bottom - rc.top) * 2 / 3;
const LONG cx = (dim & ~1) | ((rc.right - rc.left) & 1);
const LONG cy = ((dim*4/5) & ~1) | ((rc.bottom - rc.top) & 1);
stroke = FLOAT(std::max<LONG>(2, dim / 10));
const FLOAT halfstroke = stroke/2;
const FLOAT tab = FLOAT(dpi.Scale(3));
const FLOAT left = FLOAT(rc.left + ((rc.right - rc.left) - cx) / 2);
const FLOAT top = FLOAT(rc.top + ((rc.bottom - rc.top) - cy) / 2);
D2D1_RECT_F rect = D2D1::RectF(left, top, left + cx, top + cy);
SPI<ID2D1GeometrySink> spSink;
if (SUCCEEDED(spGeometry->Open(&spSink)))
{
spSink->SetFillMode(D2D1_FILL_MODE_WINDING);
spSink->BeginFigure(D2D1::Point2F(rect.right - halfstroke, rect.bottom - halfstroke), D2D1_FIGURE_BEGIN_FILLED);
D2D1_POINT_2F points[] =
{
D2D1::Point2F(rect.left + halfstroke, rect.bottom - halfstroke),
D2D1::Point2F(rect.left + halfstroke, rect.top + halfstroke),
D2D1::Point2F(rect.left + halfstroke + cx/3, rect.top + halfstroke),
D2D1::Point2F(rect.left + halfstroke + cx/3 + tab, rect.top + halfstroke + tab),
D2D1::Point2F(rect.right - halfstroke, rect.top + halfstroke + tab),
};
spSink->AddLines(points, _countof(points));
spSink->EndFigure(D2D1_FIGURE_END_CLOSED);
spSink->Close();
*out = spGeometry.Transfer();
}
}
return stroke; // DrawGeometry when > 0.0f.
}
//----------------------------------------------------------------------------
// Hourglass.
class Hourglass
{
public:
Hourglass(bool now=true) { if (now) Begin(); }
~Hourglass() { if (m_hourglass) End(); }
void Begin();
void End();
private:
bool m_hourglass = false;
static int s_count;
static HCURSOR s_old;
};
int Hourglass::s_count = 0;
HCURSOR Hourglass::s_old = 0;
void Hourglass::Begin()
{
assert(!m_hourglass);
if (s_count++ == 0)
s_old = SetCursor(LoadCursor(0, IDC_WAIT));
ShowCursor(true);
m_hourglass = true;
}
void Hourglass::End()
{
assert(m_hourglass);
assert(s_count);
if (--s_count == 0)
SetCursor(s_old);
ShowCursor(false);
m_hourglass = false;
}
//----------------------------------------------------------------------------
// ScannerThread.
class ScannerThread
{
public:
ScannerThread(std::recursive_mutex& ui_mutex);
~ScannerThread() { Stop(); }
std::vector<std::shared_ptr<DirNode>> Start(int argc, const WCHAR** argv);
void Start(const std::shared_ptr<DirNode>& dir);
void Stop();
bool IsComplete();
void GetScanningPath(std::wstring& out);
protected:
void StartInternal(const std::vector<std::shared_ptr<DirNode>>& roots, bool fullscan);
static void ThreadProc(ScannerThread* pThis);
private:
std::mutex m_mutex;
HANDLE m_hWake;
HANDLE m_hStop;
volatile LONG m_generation = 0;
size_t m_cursor = 0;
std::vector<std::shared_ptr<DirNode>> m_roots;
bool m_fullscan = false;
bool m_new_roots = false;
std::unique_ptr<std::thread> m_thread;
std::recursive_mutex& m_ui_mutex;
std::shared_ptr<Node> m_current;
};
ScannerThread::ScannerThread(std::recursive_mutex& ui_mutex)
: m_ui_mutex(ui_mutex)
{
m_hWake = CreateEvent(nullptr, false, false, nullptr);
m_hStop = CreateEvent(nullptr, true, false, nullptr);
}
std::vector<std::shared_ptr<DirNode>> ScannerThread::Start(int argc, const WCHAR** argv)
{
std::vector<std::shared_ptr<DirNode>> roots;
if (argc)
{
for (int ii = 0; ii < argc; ++ii)
{
const auto root = MakeRoot(argv[ii]);
if (root)
roots.emplace_back(root);
}
}
if (roots.empty())
roots.emplace_back(MakeRoot(nullptr));
m_fullscan = true;
StartInternal(roots, true);
return roots;
}
void ScannerThread::Start(const std::shared_ptr<DirNode>& dir)
{
std::vector<std::shared_ptr<DirNode>> dirs;
dirs.emplace_back(dir);
StartInternal(dirs, false);
}
void ScannerThread::StartInternal(const std::vector<std::shared_ptr<DirNode>>& roots, bool fullscan)
{
assert(fullscan || !m_fullscan); // Can't Rescan while a Scan is in progress.
if (!m_thread)
m_thread = std::make_unique<std::thread>(ThreadProc, this);
{
std::lock_guard<std::mutex> lock1(m_mutex);
std::lock_guard<std::recursive_mutex> lock2(m_ui_mutex);
if (fullscan)
{
m_current.reset();
m_roots = roots;
m_cursor = 0;
}
else
{
m_roots.insert(m_roots.begin() + m_cursor, roots.begin(), roots.end());
}
m_new_roots = true;
InterlockedIncrement(&m_generation);
}
SetEvent(m_hWake);
}
void ScannerThread::Stop()
{
if (m_thread)
{
SetEvent(m_hStop);
InterlockedIncrement(&m_generation);
m_thread->join();
std::lock_guard<std::mutex> lock1(m_mutex);
std::lock_guard<std::recursive_mutex> lock2(m_ui_mutex);
m_current.reset();
m_roots.clear();
m_cursor = 0;
m_fullscan = false;
m_new_roots = false;
ResetEvent(m_hStop);
}
}
bool ScannerThread::IsComplete()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_roots.empty();
}
void ScannerThread::GetScanningPath(std::wstring& out)
{
std::lock_guard<std::recursive_mutex> lock(m_ui_mutex);
if (m_current)
m_current->GetFullPath(out);
else
out.clear();
}
void ScannerThread::ThreadProc(ScannerThread* pThis)
{
while (true)
{
DWORD dw;
const HANDLE handles[2] = { pThis->m_hWake, pThis->m_hStop };
dw = WaitForMultipleObjects(_countof(handles), handles, false, INFINITE);
if (dw != WAIT_OBJECT_0)
break;
const LONG generation = pThis->m_generation;
ScanContext context = { pThis->m_ui_mutex, pThis->m_current, g_use_compressed_size };
if (!g_show_dontscan_anyway)
ReadRegStrings(TEXT("DontScanDirectories"), context.dontscan);
for (auto& ignore : context.dontscan)
ensure_separator(ignore);
while (generation == pThis->m_generation)
{
std::shared_ptr<DirNode> root;
{
std::lock_guard<std::mutex> lock(pThis->m_mutex);
if (pThis->m_cursor >= pThis->m_roots.size())
{
// This is important for the Rescan case.
for (const auto& top : pThis->m_roots)
{
std::shared_ptr<DirNode> parent = top;
while (parent)
{
std::shared_ptr<DirNode> up = parent->GetParent();
if (!up)
parent->Finish();
parent = up;
}
}
std::lock_guard<std::recursive_mutex> lock2(pThis->m_ui_mutex);
pThis->m_current.reset();
pThis->m_roots.clear();
pThis->m_cursor = 0;
pThis->m_fullscan = false;
break;
}
if (pThis->m_new_roots)
{
pThis->m_new_roots = false;
for (const auto& root : pThis->m_roots)
{
#ifdef DEBUG
if (root->IsFake())
continue;
#endif
DriveNode* drive = root->AsDrive();
if (drive)
{
std::lock_guard<std::recursive_mutex> lock2(pThis->m_ui_mutex);
// Create a temporary fake node purely for
// progress purposes, since the drive might not
// actually get a FreeSpaceNode if getting its
// free space fails.
pThis->m_current = std::make_shared<FreeSpaceNode>(drive->GetName(), 0, 0, nullptr);
drive->AddFreeSpace();
}
else
{
// For each drive, update its free space. This is
// intended for the Rescan case.
std::shared_ptr<DirNode> parent = root;
while (parent)
{
std::shared_ptr<DirNode> up = parent->GetParent();
if (!up)
break;
if (up->IsDrive())
{
up->AsDrive()->AddFreeSpace();
break;
}
parent = up;
}
}
}
}
root = pThis->m_roots[pThis->m_cursor++];
}
Scan(root, generation, &pThis->m_generation, context);
}
}
}
//----------------------------------------------------------------------------
// SizeTracker.
class SizeTracker
{
public:
SizeTracker(LONG default_cx, LONG default_cy);
void OnCreate(HWND hwnd);
void OnSize();
void OnDestroy();
protected:
void ReadPosition();
void WritePosition();
private:
HWND m_hwnd = 0;
bool m_resized = false;
bool m_maximized = false;
RECT m_rcRestore = {};
DpiScaler m_dpi;
const SIZE m_default_size;
};
SizeTracker::SizeTracker(LONG default_cx, LONG default_cy)
: m_default_size({ default_cx, default_cy })
{
}
void SizeTracker::OnCreate(HWND hwnd)
{
m_hwnd = hwnd;
m_dpi = __GetDpiForWindow(hwnd);
ReadPosition();
}
void SizeTracker::OnSize()
{
if (!m_hwnd || IsIconic(m_hwnd))
return;
bool const maximized = !!IsMaximized(m_hwnd);
DpiScaler dpi(__GetDpiForWindow(m_hwnd));
RECT rc;
GetWindowRect(m_hwnd, &rc);
if (!maximized &&
(memcmp(&m_rcRestore, &rc, sizeof(m_rcRestore)) || !dpi.IsDpiEqual(m_dpi)))
{
m_resized = true;
m_rcRestore = rc;
m_dpi = dpi;
}
if (maximized != m_maximized)
{
m_resized = true;
m_maximized = maximized;
}
}
void SizeTracker::OnDestroy()
{
if (m_resized)
{
WritePosition();
m_resized = false;
}
m_hwnd = 0;
}
void SizeTracker::ReadPosition()
{
assert(m_hwnd);
LONG cx = ReadRegLong(TEXT("WindowWidth"), 0);
LONG cy = ReadRegLong(TEXT("WindowHeight"), 0);
const bool maximized = !!ReadRegLong(TEXT("Maximized"), false);
MONITORINFO info = { sizeof(info) };
HMONITOR hmon = MonitorFromWindow(m_hwnd, MONITOR_DEFAULTTOPRIMARY);
GetMonitorInfo(hmon, &info);
cx = m_dpi.Scale(cx ? cx : m_default_size.cx);
cy = m_dpi.Scale(cy ? cy : m_default_size.cy);
cx = std::min<LONG>(cx, info.rcWork.right - info.rcWork.left);
cy = std::min<LONG>(cy, info.rcWork.bottom - info.rcWork.top);
cx = std::max<LONG>(cx, m_dpi.Scale(480));
cy = std::max<LONG>(cy, m_dpi.Scale(360));
const LONG xx = info.rcWork.left + ((info.rcWork.right - info.rcWork.left) - cx) / 2;
const LONG yy = info.rcWork.top + ((info.rcWork.bottom - info.rcWork.top) - cy) / 2;
SetWindowPos(m_hwnd, 0, xx, yy, cx, cy, SWP_NOZORDER);
GetWindowRect(m_hwnd, &m_rcRestore);
ShowWindow(m_hwnd, maximized ? SW_MAXIMIZE : SW_NORMAL);
m_resized = false;
}
void SizeTracker::WritePosition()
{
assert(m_hwnd);
const LONG cx = m_dpi.ScaleTo(m_rcRestore.right - m_rcRestore.left, 96);
const LONG cy = m_dpi.ScaleTo(m_rcRestore.bottom - m_rcRestore.top, 96);
WriteRegLong(TEXT("WindowWidth"), cx);
WriteRegLong(TEXT("WindowHeight"), cy);
WriteRegLong(TEXT("Maximized"), m_maximized);
m_resized = false;
}
//----------------------------------------------------------------------------
// Buttons.
class Buttons
{
typedef FLOAT (MakeButtonIconFn)(DirectHwndRenderTarget& target, RECT rc, const DpiScaler& dpi, ID2D1Geometry** out);
struct Button
{
UINT m_id = 0;
RECT m_rect = {};
std::wstring m_caption;
std::wstring m_desc;
SPI<ID2D1Geometry> m_spGeometry;
FLOAT m_stroke = 0.0f;
MakeButtonIconFn* m_make_icon = nullptr;
bool m_hidden = false;
};
public:
void Attach(HWND hwnd);
void OnDpiChanged(const DpiScaler& dpi);
void AddButton(UINT id, const RECT& rect, const WCHAR* caption=nullptr, const WCHAR* desc=nullptr, MakeButtonIconFn* make_icon=nullptr);
void ShowButton(UINT id, bool show);
void RenderButtons(DirectHwndRenderTarget& target);
const WCHAR* GetHoverDescription();
void OnMouseMessage(UINT msg, const POINT* pt);
void OnCancelMode();
void SuppressDescriptionTemporarily(UINT id);
void UseDarkMode(bool dark) { m_dark_mode = dark; }
#ifdef DEBUG
bool GetButtonRect(UINT id, RECT& rect);
#endif
protected:
int HitTest(const POINT* pt) const;
void InvalidateButton(int index) const;
void SetHover(int hover, int pressed);
private:
HWND m_hwnd = 0;
std::vector<Button> m_buttons;
int m_hover = -1;
int m_pressed = -1;
int m_suppress_desc = -1;
DpiScaler m_dpi;
DpiScaler m_dpiWithTextScaling;
bool m_dark_mode = false;
};
void Buttons::Attach(HWND hwnd)
{
OnCancelMode();
m_buttons.clear();
m_hwnd = hwnd;
}
void Buttons::OnDpiChanged(const DpiScaler& dpi)
{
m_dpi.OnDpiChanged(dpi);
m_dpiWithTextScaling.OnDpiChanged(dpi, true);
}
void Buttons::AddButton(const UINT id, const RECT& rect, const WCHAR* caption, const WCHAR* desc, MakeButtonIconFn* make_icon)
{
Button button;
button.m_id = id;
button.m_rect = rect;
if (caption)
button.m_caption = caption;
if (desc)
{
button.m_desc = TEXT("\u2192 ");
button.m_desc.append(desc);
}
button.m_make_icon = make_icon;
m_buttons.emplace_back(std::move(button));
}
void Buttons::ShowButton(const UINT id, const bool show)
{
bool changed = false;
for (auto& button : m_buttons)
{
if (id == button.m_id)
{
changed = (changed || button.m_hidden == show);
button.m_hidden = !show;
}
}
if (changed)
InvalidateRect(m_hwnd, nullptr, false);
}
void Buttons::RenderButtons(DirectHwndRenderTarget& target)
{
auto oldColor = target.TextBrush()->GetColor();
target.TextBrush()->SetColor(D2D1::ColorF(GetForeColor(m_dark_mode)));
for (size_t ii = 0; ii < m_buttons.size(); ++ii)
{
auto& button = m_buttons[ii];
if (button.m_hidden)
continue;
const RECT& rect = button.m_rect;
D2D1_RECT_F rectF;
rectF.left = FLOAT(rect.left);
rectF.top = FLOAT(rect.top);
rectF.right = FLOAT(rect.right);
rectF.bottom = FLOAT(rect.bottom);
const bool pressed = (m_hover == ii && m_pressed == ii);
target.FillBrush()->SetColor(D2D1::ColorF(pressed ? (m_dark_mode ? 0x505050 : D2D1::ColorF::LightSteelBlue) : GetBackColor(m_dark_mode)));
target.Target()->FillRectangle(rectF, target.FillBrush());
const FLOAT stroke = std::max<FLOAT>(FLOAT(m_dpi.Scale(1)), FLOAT(1));
inset_rect_for_stroke(rectF, stroke);
target.FillBrush()->SetColor(D2D1::ColorF((pressed || m_hover == ii) ? GetForeColor(m_dark_mode) : (m_dark_mode ? 0x303030 : 0xD0D0D0)));
target.Target()->DrawRectangle(rectF, target.FillBrush(), stroke);
if (!button.m_spGeometry && button.m_make_icon)
button.m_stroke = (*button.m_make_icon)(target, button.m_rect, m_dpi, button.m_spGeometry.UnsafeAddress());
if (button.m_spGeometry)
{
if (button.m_stroke > 0.0f)
target.Target()->DrawGeometry(button.m_spGeometry, target.TextBrush(), button.m_stroke, target.RoundedStrokeStyle());
else
target.Target()->FillGeometry(button.m_spGeometry, target.TextBrush());
}
if (!button.m_caption.empty())
target.WriteText(target.TextFormat(), 0.0f, 0.0f, rectF, button.m_caption, WTO_HCENTER|WTO_VCENTER);
}
target.TextBrush()->SetColor(oldColor);
}
const WCHAR* Buttons::GetHoverDescription()
{
return (m_suppress_desc < 0 && m_hover >= 0 && m_hover < m_buttons.size()) ? m_buttons[m_hover].m_desc.c_str() : nullptr;
}
void Buttons::OnMouseMessage(UINT msg, const POINT* pt)
{
const int hover = HitTest(pt);
switch (msg)
{
case WM_MOUSEMOVE:
SetHover(hover, m_pressed);
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
SetHover(hover, hover);
break;
case WM_LBUTTONUP:
if (m_hover >= 0 && m_hover < m_buttons.size() && m_hover == m_pressed)
SendMessage(m_hwnd, WM_COMMAND, GET_WM_COMMAND_MPS(m_buttons[m_hover].m_id, m_hwnd, 0));
InvalidateButton(m_pressed);
m_pressed = -1;
break;
}
}
void Buttons::OnCancelMode()
{
m_hover = -1;
m_suppress_desc = -1;
if (m_pressed >= 0)
{
InvalidateButton(m_pressed);
m_pressed = -1;
}
}
#ifdef DEBUG
bool Buttons::GetButtonRect(UINT id, RECT& rect)
{
for (const auto& btn : m_buttons)
{
if (btn.m_id == id)
{
rect = btn.m_rect;
return true;
}
}
return false;
}
#endif
int Buttons::HitTest(const POINT* pt) const
{
if (pt)
{
for (size_t ii = 0; ii < m_buttons.size(); ++ii)
{
if (m_buttons[ii].m_hidden)
continue;
if (PtInRect(&m_buttons[ii].m_rect, *pt))
return int(ii);
}
}
return -1;
}
void Buttons::InvalidateButton(int index) const
{
assert(m_hwnd);
if (index >= 0 && index < m_buttons.size())
{
// Hybrid D2D+GDI painting doesn't mix well with partial invalidation.
//InvalidateRect(m_hwnd, &m_buttons[index].m_rect, false);
InvalidateRect(m_hwnd, nullptr, false);
}
}
void Buttons::SetHover(int hover, int pressed)
{
if (hover != m_suppress_desc)
{
// Stop suppressing once the mouse moves away from the button.
m_suppress_desc = -1;
}
if (m_hover != hover || m_pressed != pressed)
{
InvalidateButton(m_hover);
m_hover = hover;
m_pressed = pressed;
InvalidateButton(m_hover);
}
if (m_hover >= 0)
{
TRACKMOUSEEVENT track = { sizeof(track) };
track.dwFlags = TME_LEAVE;
track.hwndTrack = m_hwnd;
track.dwHoverTime = HOVER_DEFAULT;
_TrackMouseEvent(&track);
}
}
void Buttons::SuppressDescriptionTemporarily(const UINT id)
{
for (size_t index = m_buttons.size(); index--;)
{
if (id == m_buttons[index].m_id)
{
if (int(index) == m_hover)
m_suppress_desc = int(index);
break;
}
}
}
//----------------------------------------------------------------------------
// MainWindow.
class MainWindow
{
enum
{
TIMER_PROGRESS = 1,
INTERVAL_PROGRESS = 100,
};
public:
MainWindow(HINSTANCE hinst);
HWND Create();
void Scan(int argc, const WCHAR** argv, bool rescan);
protected:
~MainWindow() {}
LRESULT WndProc(UINT msg, WPARAM wParam, LPARAM lParam);
void OnCommand(WORD id, HWND hwndCtrl, WORD code);
void ContextMenu(const POINT& ptScreen, const std::shared_ptr<Node>& node=nullptr);
void OnDpiChanged(const DpiScaler& dpi);
void OnLayout(RECT* prc);
LRESULT OnDestroy();
LRESULT OnNcDestroy();
void DrawNodeInfo(DirectHwndRenderTarget& target, D2D1_RECT_F rect, const std::shared_ptr<Node>& node, bool free_space);
void DrawAppInfo(DirectHwndRenderTarget& target, D2D1_RECT_F rect);
void Expand(const std::shared_ptr<Node>& node);
void SetRoot(const std::shared_ptr<DirNode>& root);
void SetRoots(const std::vector<std::shared_ptr<DirNode>>& roots);
void Up();
void Back();
void Forward();