-
-
Notifications
You must be signed in to change notification settings - Fork 584
Expand file tree
/
Copy pathDialogJump.cs
More file actions
1079 lines (949 loc) · 37.9 KB
/
DialogJump.cs
File metadata and controls
1079 lines (949 loc) · 37.9 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.DialogJump.Models;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
using NHotkey;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Accessibility;
namespace Flow.Launcher.Infrastructure.DialogJump
{
public static class DialogJump
{
#region Public Properties
public static Func<nint, Task> ShowDialogJumpWindowAsync { get; set; } = null;
public static Action UpdateDialogJumpWindow { get; set; } = null;
public static Action ResetDialogJumpWindow { get; set; } = null;
public static Action HideDialogJumpWindow { get; set; } = null;
public static DialogJumpWindowPositions DialogJumpWindowPosition { get; private set; }
public static DialogJumpExplorerPair WindowsDialogJumpExplorer { get; } = new()
{
Metadata = new()
{
ID = "298b197c08a24e90ab66ac060ee2b6b8", // ID is for calculating the hash id of the Dialog Jump pairs
Disabled = false // Disabled is for enabling the Windows DialogJump explorers & dialogs
},
Plugin = new WindowsExplorer()
};
public static DialogJumpDialogPair WindowsDialogJumpDialog { get; } = new()
{
Metadata = new()
{
ID = "a4a113dc51094077ab4abb391e866c7b", // ID is for calculating the hash id of the Dialog Jump pairs
Disabled = false // Disabled is for enabling the Windows DialogJump explorers & dialogs
},
Plugin = new WindowsDialog()
};
#endregion
#region Private Fields
private static readonly string ClassName = nameof(DialogJump);
private static readonly Settings _settings = Ioc.Default.GetRequiredService<Settings>();
// We should not initialize API in static constructor because it will create another API instance
private static IPublicAPI api = null;
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
private static HWND _mainWindowHandle = HWND.Null;
private static readonly Dictionary<DialogJumpExplorerPair, IDialogJumpExplorerWindow> _dialogJumpExplorers = new();
private static DialogJumpExplorerPair _lastExplorer = null;
private static readonly object _lastExplorerLock = new();
private static readonly Dictionary<DialogJumpDialogPair, IDialogJumpDialogWindow> _dialogJumpDialogs = new();
private static IDialogJumpDialogWindow _dialogWindow = null;
private static readonly object _dialogWindowLock = new();
private static HWINEVENTHOOK _foregroundChangeHook = HWINEVENTHOOK.Null;
private static HWINEVENTHOOK _locationChangeHook = HWINEVENTHOOK.Null;
private static HWINEVENTHOOK _destroyChangeHook = HWINEVENTHOOK.Null;
private static HWINEVENTHOOK _hideChangeHook = HWINEVENTHOOK.Null;
private static HWINEVENTHOOK _dialogEndChangeHook = HWINEVENTHOOK.Null;
private static readonly WINEVENTPROC _fgProc = ForegroundChangeCallback;
private static readonly WINEVENTPROC _locProc = LocationChangeCallback;
private static readonly WINEVENTPROC _desProc = DestroyChangeCallback;
private static readonly WINEVENTPROC _hideProc = HideChangeCallback;
private static readonly WINEVENTPROC _dialogEndProc = DialogEndChangeCallback;
private static DispatcherTimer _dragMoveTimer = null;
// A list of all file dialog windows that are auto switched already
private static readonly List<HWND> _autoSwitchedDialogs = new();
private static readonly object _autoSwitchedDialogsLock = new();
private static HWINEVENTHOOK _moveSizeHook = HWINEVENTHOOK.Null;
private static readonly WINEVENTPROC _moveProc = MoveSizeCallBack;
private static readonly SemaphoreSlim _foregroundChangeLock = new(1, 1);
private static readonly SemaphoreSlim _navigationLock = new(1, 1);
private static bool _initialized = false;
private static bool _enabled = false;
#endregion
#region Initialize & Setup
public static void InitializeDialogJump(IList<DialogJumpExplorerPair> dialogJumpExplorers,
IList<DialogJumpDialogPair> dialogJumpDialogs)
{
if (_initialized) return;
// Initialize Dialog Jump explorers & dialogs
_dialogJumpExplorers.Add(WindowsDialogJumpExplorer, null);
foreach (var explorer in dialogJumpExplorers)
{
_dialogJumpExplorers.Add(explorer, null);
}
_dialogJumpDialogs.Add(WindowsDialogJumpDialog, null);
foreach (var dialog in dialogJumpDialogs)
{
_dialogJumpDialogs.Add(dialog, null);
}
// Initialize main window handle
_mainWindowHandle = Win32Helper.GetMainWindowHandle();
// Initialize timer
_dragMoveTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(10) };
_dragMoveTimer.Tick += (s, e) => InvokeUpdateDialogJumpWindow();
// Initialize Dialog Jump window position
DialogJumpWindowPosition = _settings.DialogJumpWindowPosition;
_initialized = true;
}
public static void SetupDialogJump(bool enabled)
{
if (enabled == _enabled) return;
if (enabled)
{
// Check if there are explorer windows and get the topmost one
try
{
if (RefreshLastExplorer())
{
Log.Debug(ClassName, $"Explorer window found");
}
}
catch (System.Exception)
{
// Ignored
}
// Unhook events
if (!_foregroundChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_foregroundChangeHook);
_foregroundChangeHook = HWINEVENTHOOK.Null;
}
if (!_locationChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_locationChangeHook);
_locationChangeHook = HWINEVENTHOOK.Null;
}
if (!_destroyChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_destroyChangeHook);
_destroyChangeHook = HWINEVENTHOOK.Null;
}
if (!_hideChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_hideChangeHook);
_hideChangeHook = HWINEVENTHOOK.Null;
}
if (!_dialogEndChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_dialogEndChangeHook);
_dialogEndChangeHook = HWINEVENTHOOK.Null;
}
// Hook events
_foregroundChangeHook = PInvoke.SetWinEventHook(
PInvoke.EVENT_SYSTEM_FOREGROUND,
PInvoke.EVENT_SYSTEM_FOREGROUND,
PInvoke.GetModuleHandle((PCWSTR)null),
_fgProc,
0,
0,
PInvoke.WINEVENT_OUTOFCONTEXT);
_locationChangeHook = PInvoke.SetWinEventHook(
PInvoke.EVENT_OBJECT_LOCATIONCHANGE,
PInvoke.EVENT_OBJECT_LOCATIONCHANGE,
PInvoke.GetModuleHandle((PCWSTR)null),
_locProc,
0,
0,
PInvoke.WINEVENT_OUTOFCONTEXT);
_destroyChangeHook = PInvoke.SetWinEventHook(
PInvoke.EVENT_OBJECT_DESTROY,
PInvoke.EVENT_OBJECT_DESTROY,
PInvoke.GetModuleHandle((PCWSTR)null),
_desProc,
0,
0,
PInvoke.WINEVENT_OUTOFCONTEXT);
_hideChangeHook = PInvoke.SetWinEventHook(
PInvoke.EVENT_OBJECT_HIDE,
PInvoke.EVENT_OBJECT_HIDE,
PInvoke.GetModuleHandle((PCWSTR)null),
_hideProc,
0,
0,
PInvoke.WINEVENT_OUTOFCONTEXT);
_dialogEndChangeHook = PInvoke.SetWinEventHook(
PInvoke.EVENT_SYSTEM_DIALOGEND,
PInvoke.EVENT_SYSTEM_DIALOGEND,
PInvoke.GetModuleHandle((PCWSTR)null),
_dialogEndProc,
0,
0,
PInvoke.WINEVENT_OUTOFCONTEXT);
if (_foregroundChangeHook.IsNull ||
_locationChangeHook.IsNull ||
_destroyChangeHook.IsNull ||
_hideChangeHook.IsNull ||
_dialogEndChangeHook.IsNull)
{
Log.Error(ClassName, "Failed to enable DialogJump");
return;
}
}
else
{
// Remove explorer windows
foreach (var explorer in _dialogJumpExplorers.Keys)
{
_dialogJumpExplorers[explorer] = null;
}
// Remove dialog windows
foreach (var dialog in _dialogJumpDialogs.Keys)
{
_dialogJumpDialogs[dialog] = null;
}
// Remove dialog window handle
var dialogWindowExists = false;
lock (_dialogWindowLock)
{
if (_dialogWindow != null)
{
_dialogWindow = null;
dialogWindowExists = true;
}
}
// Remove auto switched dialogs
lock (_autoSwitchedDialogsLock)
{
_autoSwitchedDialogs.Clear();
}
// Unhook events
if (!_foregroundChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_foregroundChangeHook);
_foregroundChangeHook = HWINEVENTHOOK.Null;
}
if (!_locationChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_locationChangeHook);
_locationChangeHook = HWINEVENTHOOK.Null;
}
if (!_destroyChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_destroyChangeHook);
_destroyChangeHook = HWINEVENTHOOK.Null;
}
if (!_hideChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_hideChangeHook);
_hideChangeHook = HWINEVENTHOOK.Null;
}
if (!_dialogEndChangeHook.IsNull)
{
PInvoke.UnhookWinEvent(_dialogEndChangeHook);
_dialogEndChangeHook = HWINEVENTHOOK.Null;
}
// Stop drag move timer
_dragMoveTimer?.Stop();
// Reset Dialog Jump window
if (dialogWindowExists)
{
InvokeResetDialogJumpWindow();
}
}
_enabled = enabled;
}
private static bool RefreshLastExplorer()
{
var found = false;
lock (_lastExplorerLock)
{
// Enum windows from the top to the bottom
PInvoke.EnumWindows((hWnd, _) =>
{
foreach (var explorer in _dialogJumpExplorers.Keys)
{
if (API.PluginModified(explorer.Metadata.ID) || // Plugin is modified
explorer.Metadata.Disabled) continue; // Plugin is disabled
var explorerWindow = explorer.Plugin.CheckExplorerWindow(hWnd);
if (explorerWindow != null)
{
_dialogJumpExplorers[explorer] = explorerWindow;
_lastExplorer = explorer;
found = true;
return false;
}
}
// If we reach here, it means that the window is not a file explorer
return true;
}, IntPtr.Zero);
}
return found;
}
#endregion
#region Active Explorer
public static string GetActiveExplorerPath()
{
return RefreshLastExplorer() ? _dialogJumpExplorers[_lastExplorer].GetExplorerPath() : string.Empty;
}
#endregion
#region Events
#region Invoke Property Events
private static async Task InvokeShowDialogJumpWindowAsync(bool dialogWindowChanged)
{
// Show Dialog Jump window
if (_settings.ShowDialogJumpWindow)
{
// Save Dialog Jump window position for one file dialog
if (dialogWindowChanged)
{
DialogJumpWindowPosition = _settings.DialogJumpWindowPosition;
}
// Call show Dialog Jump window
IDialogJumpDialogWindow dialogWindow;
lock (_dialogWindowLock)
{
dialogWindow = _dialogWindow;
}
if (dialogWindow != null && ShowDialogJumpWindowAsync != null)
{
await ShowDialogJumpWindowAsync.Invoke(dialogWindow.Handle);
}
// Hook move size event if Dialog Jump window is under dialog & dialog window changed
if (DialogJumpWindowPosition == DialogJumpWindowPositions.UnderDialog)
{
if (dialogWindowChanged)
{
HWND dialogWindowHandle = HWND.Null;
lock (_dialogWindowLock)
{
if (_dialogWindow != null)
{
dialogWindowHandle = new(_dialogWindow.Handle);
}
}
if (dialogWindowHandle == HWND.Null) return;
if (!_moveSizeHook.IsNull)
{
PInvoke.UnhookWinEvent(_moveSizeHook);
_moveSizeHook = HWINEVENTHOOK.Null;
}
// Call _moveProc when the window is moved or resized
SetMoveProc(dialogWindowHandle);
}
}
}
static unsafe void SetMoveProc(HWND handle)
{
uint processId;
var threadId = PInvoke.GetWindowThreadProcessId(handle, &processId);
_moveSizeHook = PInvoke.SetWinEventHook(
PInvoke.EVENT_SYSTEM_MOVESIZESTART,
PInvoke.EVENT_SYSTEM_MOVESIZEEND,
PInvoke.GetModuleHandle((PCWSTR)null),
_moveProc,
processId,
threadId,
PInvoke.WINEVENT_OUTOFCONTEXT);
}
}
private static void InvokeUpdateDialogJumpWindow()
{
UpdateDialogJumpWindow?.Invoke();
}
private static void InvokeResetDialogJumpWindow()
{
lock (_dialogWindowLock)
{
_dialogWindow = null;
}
// Reset Dialog Jump window
ResetDialogJumpWindow?.Invoke();
// Stop drag move timer
_dragMoveTimer?.Stop();
// Unhook move size event
if (!_moveSizeHook.IsNull)
{
PInvoke.UnhookWinEvent(_moveSizeHook);
_moveSizeHook = HWINEVENTHOOK.Null;
}
}
private static void InvokeHideDialogJumpWindow()
{
// Hide Dialog Jump window
HideDialogJumpWindow?.Invoke();
// Stop drag move timer
_dragMoveTimer?.Stop();
}
#endregion
#region Hotkey
public static void ToggleHotkey()
{
_ = Task.Run(async () =>
{
try
{
await NavigateDialogPathAsync(PInvoke.GetForegroundWindow());
}
catch (System.Exception ex)
{
Log.Exception(ClassName, "Failed to navigate dialog path", ex);
}
});
}
#endregion
#region Windows Events
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<Pending>")]
private static async void ForegroundChangeCallback(
HWINEVENTHOOK hWinEventHook,
uint eventType,
HWND hwnd,
int idObject,
int idChild,
uint dwEventThread,
uint dwmsEventTime
)
{
await _foregroundChangeLock.WaitAsync();
try
{
// Check if it is a file dialog window
var isDialogWindow = false;
var dialogWindowChanged = false;
foreach (var dialog in _dialogJumpDialogs.Keys)
{
if (API.PluginModified(dialog.Metadata.ID) || // Plugin is modified
dialog.Metadata.Disabled) continue; // Plugin is disabled
IDialogJumpDialogWindow dialogWindow;
var existingDialogWindow = _dialogJumpDialogs[dialog];
if (existingDialogWindow != null && existingDialogWindow.Handle == hwnd)
{
// If the dialog window is already in the list, no need to check again
dialogWindow = existingDialogWindow;
}
else
{
dialogWindow = dialog.Plugin.CheckDialogWindow(hwnd);
}
// If the dialog window is found, set it
if (dialogWindow != null)
{
lock (_dialogWindowLock)
{
dialogWindowChanged = _dialogWindow == null || _dialogWindow.Handle != hwnd;
_dialogWindow = dialogWindow;
}
isDialogWindow = true;
break;
}
}
// Handle window based on its type
if (isDialogWindow)
{
Log.Debug(ClassName, $"Dialog Window: {hwnd}");
// Navigate to path
if (_settings.AutoDialogJump)
{
// Check if we have already switched for this dialog
bool alreadySwitched;
lock (_autoSwitchedDialogsLock)
{
alreadySwitched = _autoSwitchedDialogs.Contains(hwnd);
}
// Just show Dialog Jump window
if (alreadySwitched)
{
await InvokeShowDialogJumpWindowAsync(dialogWindowChanged);
}
// Show Dialog Jump window after navigating the path
else
{
if (!await Task.Run(async () =>
{
try
{
return await NavigateDialogPathAsync(hwnd, true);
}
catch (System.Exception ex)
{
Log.Exception(ClassName, "Failed to navigate dialog path", ex);
return false;
}
}))
{
await InvokeShowDialogJumpWindowAsync(dialogWindowChanged);
}
}
}
else
{
await InvokeShowDialogJumpWindowAsync(dialogWindowChanged);
}
}
// Dialog jump window
else if (hwnd == _mainWindowHandle)
{
Log.Debug(ClassName, $"Main Window: {hwnd}");
}
// Other window
else
{
Log.Debug(ClassName, $"Other Window: {hwnd}");
var dialogWindowExist = false;
lock (_dialogWindowLock)
{
if (_dialogWindow != null)
{
dialogWindowExist = true;
}
}
if (dialogWindowExist) // Neither Dialog Jump window nor file dialog window is foreground
{
// Hide Dialog Jump window until the file dialog window is brought to the foreground
InvokeHideDialogJumpWindow();
}
// Check if there are foreground explorer windows
try
{
lock (_lastExplorerLock)
{
foreach (var explorer in _dialogJumpExplorers.Keys)
{
if (API.PluginModified(explorer.Metadata.ID) || // Plugin is modified
explorer.Metadata.Disabled) continue; // Plugin is disabled
var explorerWindow = explorer.Plugin.CheckExplorerWindow(hwnd);
if (explorerWindow != null)
{
Log.Debug(ClassName, $"Explorer window: {hwnd}");
_dialogJumpExplorers[explorer] = explorerWindow;
_lastExplorer = explorer;
break;
}
}
}
}
catch (System.Exception ex)
{
Log.Exception(ClassName, "An error occurred while checking foreground explorer windows", ex);
}
}
}
catch (System.Exception ex)
{
Log.Exception(ClassName, "Failed to invoke ForegroundChangeCallback", ex);
}
finally
{
_foregroundChangeLock.Release();
}
}
private static void LocationChangeCallback(
HWINEVENTHOOK hWinEventHook,
uint eventType,
HWND hwnd,
int idObject,
int idChild,
uint dwEventThread,
uint dwmsEventTime
)
{
// If the dialog window is moved, update the Dialog Jump window position
var dialogWindowExist = false;
lock (_dialogWindowLock)
{
if (_dialogWindow != null && _dialogWindow.Handle == hwnd)
{
dialogWindowExist = true;
}
}
if (dialogWindowExist)
{
InvokeUpdateDialogJumpWindow();
}
}
private static void MoveSizeCallBack(
HWINEVENTHOOK hWinEventHook,
uint eventType,
HWND hwnd,
int idObject,
int idChild,
uint dwEventThread,
uint dwmsEventTime
)
{
// If the dialog window is moved or resized, update the Dialog Jump window position
if (_dragMoveTimer != null)
{
switch (eventType)
{
case PInvoke.EVENT_SYSTEM_MOVESIZESTART:
_dragMoveTimer.Start(); // Start dragging position
break;
case PInvoke.EVENT_SYSTEM_MOVESIZEEND:
_dragMoveTimer.Stop(); // Stop dragging
break;
}
}
}
private static void DestroyChangeCallback(
HWINEVENTHOOK hWinEventHook,
uint eventType,
HWND hwnd,
int idObject,
int idChild,
uint dwEventThread,
uint dwmsEventTime
)
{
// If the dialog window is destroyed, set _dialogWindowHandle to null
var dialogWindowExist = false;
lock (_dialogWindowLock)
{
if (_dialogWindow != null && _dialogWindow.Handle == hwnd)
{
Log.Debug(ClassName, $"Destory dialog: {hwnd}");
_dialogWindow = null;
dialogWindowExist = true;
}
}
if (dialogWindowExist)
{
lock (_autoSwitchedDialogsLock)
{
_autoSwitchedDialogs.Remove(hwnd);
}
InvokeResetDialogJumpWindow();
}
}
private static void HideChangeCallback(
HWINEVENTHOOK hWinEventHook,
uint eventType,
HWND hwnd,
int idObject,
int idChild,
uint dwEventThread,
uint dwmsEventTime
)
{
// If the dialog window is hidden, set _dialogWindowHandle to null
var dialogWindowExist = false;
lock (_dialogWindowLock)
{
if (_dialogWindow != null && _dialogWindow.Handle == hwnd)
{
Log.Debug(ClassName, $"Hide dialog: {hwnd}");
_dialogWindow = null;
dialogWindowExist = true;
}
}
if (dialogWindowExist)
{
lock (_autoSwitchedDialogsLock)
{
_autoSwitchedDialogs.Remove(hwnd);
}
InvokeResetDialogJumpWindow();
}
}
private static void DialogEndChangeCallback(
HWINEVENTHOOK hWinEventHook,
uint eventType,
HWND hwnd,
int idObject,
int idChild,
uint dwEventThread,
uint dwmsEventTime
)
{
// If the dialog window is ended, set _dialogWindowHandle to null
var dialogWindowExist = false;
lock (_dialogWindowLock)
{
if (_dialogWindow != null && _dialogWindow.Handle == hwnd)
{
Log.Debug(ClassName, $"End dialog: {hwnd}");
_dialogWindow = null;
dialogWindowExist = true;
}
}
if (dialogWindowExist)
{
lock (_autoSwitchedDialogsLock)
{
_autoSwitchedDialogs.Remove(hwnd);
}
InvokeResetDialogJumpWindow();
}
}
#endregion
#endregion
#region Path Navigation
// Edited from: https://github.com/idkidknow/Flow.Launcher.Plugin.DirQuickJump
public static async Task<bool> JumpToPathAsync(nint hwnd, string path)
{
// Check handle
if (hwnd == nint.Zero) return false;
// Check path null or empty
if (string.IsNullOrEmpty(path)) return false;
// Check path
if (!CheckPath(path, out var isFile)) return false;
// Get dialog tab
var dialogWindowTab = GetDialogWindowTab(new(hwnd));
if (dialogWindowTab == null) return false;
return await JumpToPathAsync(dialogWindowTab, path, isFile, false);
}
private static async Task<bool> NavigateDialogPathAsync(HWND hwnd, bool auto = false)
{
// Check handle
if (hwnd == HWND.Null) return false;
// Get explorer path
string path;
lock (_lastExplorerLock)
{
path = _dialogJumpExplorers[_lastExplorer]?.GetExplorerPath();
}
// Check path null or empty
if (string.IsNullOrEmpty(path)) return false;
// Check path
if (!CheckPath(path, out var isFile)) return false;
// Get dialog tab
var dialogWindowTab = GetDialogWindowTab(hwnd);
if (dialogWindowTab == null) return false;
// Jump to path
return await JumpToPathAsync(dialogWindowTab, path, isFile, auto);
}
private static bool CheckPath(string path, out bool file)
{
file = false;
try
{
// shell: and shell::: paths
if (path.StartsWith("shell:", StringComparison.OrdinalIgnoreCase))
{
return true;
}
// file: URI paths
var localPath = path.StartsWith("file:", StringComparison.OrdinalIgnoreCase)
? new Uri(path).LocalPath
: path;
// Is folder?
var isFolder = Directory.Exists(localPath);
// Is file?
var isFile = File.Exists(localPath);
file = isFile;
return isFolder || isFile;
}
catch (System.Exception e)
{
Log.Exception(ClassName, "Failed to check path", e);
return false;
}
}
private static IDialogJumpDialogWindowTab GetDialogWindowTab(HWND hwnd)
{
var dialogWindow = GetDialogWindow(hwnd);
if (dialogWindow == null) return null;
var dialogWindowTab = dialogWindow.GetCurrentTab();
return dialogWindowTab;
}
private static IDialogJumpDialogWindow GetDialogWindow(HWND hwnd)
{
// First check dialog window
lock (_dialogWindowLock)
{
if (_dialogWindow != null && _dialogWindow.Handle == hwnd)
{
return _dialogWindow;
}
}
// Then check all dialog windows
foreach (var dialog in _dialogJumpDialogs.Keys)
{
if (API.PluginModified(dialog.Metadata.ID) || // Plugin is modified
dialog.Metadata.Disabled) continue; // Plugin is disabled
var dialogWindow = _dialogJumpDialogs[dialog];
if (dialogWindow != null && dialogWindow.Handle == hwnd)
{
return dialogWindow;
}
}
// Finally search for the dialog window again
foreach (var dialog in _dialogJumpDialogs.Keys)
{
if (API.PluginModified(dialog.Metadata.ID) || // Plugin is modified
dialog.Metadata.Disabled) continue; // Plugin is disabled
IDialogJumpDialogWindow dialogWindow;
var existingDialogWindow = _dialogJumpDialogs[dialog];
if (existingDialogWindow != null && existingDialogWindow.Handle == hwnd)
{
// If the dialog window is already in the list, no need to check again
dialogWindow = existingDialogWindow;
}
else
{
dialogWindow = dialog.Plugin.CheckDialogWindow(hwnd);
}
// Update dialog window if found
if (dialogWindow != null)
{
_dialogJumpDialogs[dialog] = dialogWindow;
return dialogWindow;
}
}
return null;
}
private static async Task<bool> JumpToPathAsync(IDialogJumpDialogWindowTab dialog, string path, bool isFile, bool auto = false)
{
// Jump after flow launcher window vanished (after JumpAction returned true)
// and the dialog had been in the foreground.
var dialogHandle = dialog.Handle;
var timeOut = !SpinWait.SpinUntil(() => Win32Helper.IsForegroundWindow(dialogHandle), 1000);
if (timeOut) return false;
// Assume that the dialog is in the foreground now
await _navigationLock.WaitAsync();
try
{
bool result;
if (isFile)
{
switch (_settings.DialogJumpFileResultBehaviour)
{
case DialogJumpFileResultBehaviours.FullPath:
Log.Debug(ClassName, $"File Jump FullPath: {path}");
result = FileJump(path, dialog);
break;
case DialogJumpFileResultBehaviours.FullPathOpen:
Log.Debug(ClassName, $"File Jump FullPathOpen: {path}");
result = FileJump(path, dialog, openFile: true);
break;
case DialogJumpFileResultBehaviours.Directory:
Log.Debug(ClassName, $"File Jump Directory (Auto: {auto}): {path}");
result = DirJump(Path.GetDirectoryName(path), dialog, auto);
break;
default:
return false;
}
}
else
{
Log.Debug(ClassName, $"Dir Jump: {path}");
result = DirJump(path, dialog, auto);
}
if (result)
{
lock (_autoSwitchedDialogsLock)
{
_autoSwitchedDialogs.Add(new(dialogHandle));
}
}
return result;
}
catch (System.Exception e)
{
Log.Exception(ClassName, "Failed to jump to path", e);
return false;
}
finally
{
_navigationLock.Release();
}
}
private static bool FileJump(string filePath, IDialogJumpDialogWindowTab dialog, bool openFile = false)
{
if (!dialog.JumpFile(filePath))
{
Log.Error(ClassName, "Failed to jump file");
return false;
}
if (openFile && !dialog.Open())
{
Log.Error(ClassName, "Failed to open file");
return false;
}
return true;
}
private static bool DirJump(string dirPath, IDialogJumpDialogWindowTab dialog, bool auto = false)
{
if (!dialog.JumpFolder(dirPath, auto))
{
Log.Error(ClassName, "Failed to jump folder");
return false;
}
return true;
}