-
Notifications
You must be signed in to change notification settings - Fork 6
/
VirtualDesktopManager.pas
1752 lines (1573 loc) · 56.6 KB
/
VirtualDesktopManager.pas
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
// Created by Alexey Andriukhin (dr. F.I.N.) http://www.delphisources.ru/forum/member.php?u=9721
// Tested on Win10 x32 (build 14393), Delphi7
{
CHANGELOG:
2022-05-26:
- Updated IVirtualDesktop to include hstring functions
TODO : handle those hstring
}
unit VirtualDesktopManager;
interface
uses
{ CodeGear }
Windows, // used for many functions
Classes, // used for: TList
SysUtils, // used for: FreeAndNil()
ActiveX, // used for: CoInitialize(), CoUninitialize, Succeeded(), Failed(), IsEqualGUID()
ComObj, // used for: GUIDToString()
{ VirtualDesktopAPI }
VirtualDesktopAPI,
Winapi.Winrt {HSTRING};
type
TNonRefInterfacedObject = class(TObject, IInterface)
private
fRefCount: Integer;
fDestroyed: Boolean;
protected
{ IInterface }
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
{ Self }
procedure DoFreeInstance; virtual;
public
procedure FreeInstance; override;
end;
TVirtualDesktopManager = class;
TVirtualDesktopManagerW11 = class;
TVirtualDesktop = class(TNonRefInterfacedObject, IVirtualDesktop)
private
fManager: TVirtualDesktopManager;
fIDesktop: IVirtualDesktop;
function _GetId: TGUID;
function _GetIdAsString: AnsiString;
function _GetIndex: Integer;
function _GetIsCurrent: Boolean;
protected
{ IVirtualDesktop }
function IsViewVisible(View: IApplicationView; pfVisible: PBOOL): HRESULT; stdcall;
function GetId(Id: PGUID): HRESULT; stdcall;
function GetName(Hs: HSTRING): HRESULT; stdcall;
function GetWallpaperPath(Hs: HSTRING): HRESULT; stdcall;
{ IInterface }
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
{ Self }
constructor Create(Manager: TVirtualDesktopManager; iDesktop: IVirtualDesktop);
procedure DoFreeInstance; override;
public
{ Methods }
// Switch
function SwitchHere: Boolean;
// Remove
function Remove: Boolean; overload;
function Remove(FallbackDesktop: TVirtualDesktop): Boolean; overload;
function Remove(FallbackDesktopIndex: Integer): Boolean; overload;
function Remove(FallbackDesktopId: TGUID): Boolean; overload;
// Additional
function IsWindowVisible(Wnd: HWND): Boolean;
{ Properties }
property Id: TGUID read _GetId;
property IdAsString: AnsiString read _GetIdAsString;
property Index: Integer read _GetIndex;
property IsCurrent: Boolean read _GetIsCurrent;
end;
TVirtualDesktopW11 = class(TNonRefInterfacedObject, IVirtualDesktopW11)
private
fManager: TVirtualDesktopManagerW11;
fIDesktop: IVirtualDesktopW11;
function _GetId: TGUID;
function _GetIdAsString: AnsiString;
function _GetIndex: Integer;
function _GetIsCurrent: Boolean;
protected
{ IVirtualDesktop }
function IsViewVisible(View: IApplicationViewW11; pfVisible: PBOOL): HRESULT; stdcall;
function GetId(Id: PGUID): HRESULT; stdcall;
function Proc5(PProc5: PUINT): HRESULT; stdcall;
function GetName(Hs: HSTRING): HRESULT; stdcall;
function GetWallpaperPath(Hs: HSTRING): HRESULT; stdcall;
{ IInterface }
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
{ Self }
constructor Create(Manager: TVirtualDesktopManagerW11; iDesktop: IVirtualDesktopW11);
procedure DoFreeInstance; override;
public
{ Methods }
// Switch
function SwitchHere: Boolean;
// Remove
function Remove: Boolean; overload;
function Remove(FallbackDesktop: TVirtualDesktopW11): Boolean; overload;
function Remove(FallbackDesktopIndex: Integer): Boolean; overload;
function Remove(FallbackDesktopId: TGUID): Boolean; overload;
// Additional
function IsWindowVisible(Wnd: HWND): Boolean;
{ Properties }
property Id: TGUID read _GetId;
property IdAsString: AnsiString read _GetIdAsString;
property Index: Integer read _GetIndex;
property IsCurrent: Boolean read _GetIsCurrent;
end;
TErrorNotify = procedure(Sender: TObject; ErroCode: HRESULT) of object;
TVirtualDesktopCreatedNotify = procedure(Sender: TObject; Desktop: TVirtualDesktop) of object;
TVirtualDesktopDestroyNotify = procedure(Sender: TObject; Desktop, DesktopFallback: TVirtualDesktop) of object;
TCurrentVirtualDesktopChangedNotify = procedure(Sender: TObject; OldDesktop, NewDesktop: TVirtualDesktop) of object;
TVirtualDesktopCreatedNotifyW11 = procedure(Sender: TObject; Desktop: TVirtualDesktopW11) of object;
TVirtualDesktopDestroyNotifyW11 = procedure(Sender: TObject; Desktop, DesktopFallback: TVirtualDesktopW11) of object;
TCurrentVirtualDesktopChangedNotifyW11 = procedure(Sender: TObject; OldDesktop, NewDesktop: TVirtualDesktopW11) of object;
TVirtualDesktopManager = class(TNonRefInterfacedObject, IVirtualDesktopNotification)
private
fISP: IServiceProvider;
fIVDM: IVirtualDesktopManager;
fIVDMI: IVirtualDesktopManagerInternal;
fIVDMI11: IVirtualDesktopManagerInternalW11;
fIVNS: IVirtualNotificationService;
fIAVC: IApplicationViewCollection;
fIVDPA: IVirtualDesktopPinnedApps;
fCookie: DWORD;
protected
{ IVirtualDesktopNotification }
function VirtualDesktopCreated(Desktop: IVirtualDesktop): HRESULT; stdcall;
function VirtualDesktopDestroyBegin(Desktop: IVirtualDesktop; DesktopFallback: IVirtualDesktop): HRESULT; stdcall;
function VirtualDesktopDestroyFailed(Desktop: IVirtualDesktop; DesktopFallback: IVirtualDesktop): HRESULT; stdcall;
function VirtualDesktopDestroyed(Desktop: IVirtualDesktop; DesktopFallback: IVirtualDesktop): HRESULT; stdcall;
function ViewVirtualDesktopChanged(View: IApplicationView): HRESULT; stdcall;
function CurrentVirtualDesktopChanged(DesktopOld: IVirtualDesktop; DesktopNew: IVirtualDesktop): HRESULT; stdcall;
{ Self }
procedure DoFreeInstance; override;
private
fEnabled: Boolean;
fLastError: HRESULT;
fDesktops: TList;
fOnCurrentChanged: TCurrentVirtualDesktopChangedNotify;
fOnCreated: TVirtualDesktopCreatedNotify;
fOnDestroyBegin: TVirtualDesktopDestroyNotify;
fOnDestroyFailed: TVirtualDesktopDestroyNotify;
fOnDestroyed: TVirtualDesktopDestroyNotify;
fOnError: TErrorNotify;
procedure FreeDesktops;
function CheckError(res: HRESULT): Boolean;
procedure Initialize;
procedure Finalize;
function _GetCount: Integer;
function _GetDesktop(Index: Integer): TVirtualDesktop;
function _GetCurrentDesktop: TVirtualDesktop;
function _GetCurrentDesktopIndex: Integer;
procedure _SetCurrentDesktopIndex(const Value: Integer);
function _GetCurrentDesktopId: TGUID;
protected
procedure SetLastError(error: HRESULT);
public
procedure AfterConstruction; override;
{ Methods }
// DESKTOPS
// Additional
procedure Refresh;
function GetDesktopIndexById(Id: TGUID): Integer;
// Create
function CreateDesktop: Boolean;
function CreateDesktopAndSwitch: Boolean;
// Switch
function SwitchToDesktop(Desktop: TVirtualDesktop): Boolean; overload;
function SwitchToDesktop(DesktopId: TGUID): Boolean; overload;
function SwitchToDesktop(DesktopIndex: Integer): Boolean; overload;
// Remove
function RemoveDesktop(Desktop: TVirtualDesktop): Boolean; overload;
function RemoveDesktop(Desktop, DesktopFallback: TVirtualDesktop): Boolean; overload;
function RemoveDesktop(DesktopId: TGUID): Boolean; overload;
function RemoveDesktop(DesktopId, DesktopFallbackId: TGUID): Boolean; overload;
function RemoveDesktop(DesktopIndex: Integer): Boolean; overload;
function RemoveDesktop(DesktopIndex, DesktopFallbackIndex: Integer): Boolean; overload;
// WINDOWS AND APPS
function IsWindowOnCurrentDesktop(Wnd: HWND): Boolean;
function GetWindowDesktop(Wnd: HWND): TVirtualDesktop;
// Visibility
function IsWindowVisibleAtDesktop(Wnd: HWND; Desktop: TVirtualDesktop): Boolean; overload;
function IsWindowVisibleAtDesktop(Wnd: HWND; DesktopId: TGUID): Boolean; overload;
function IsWindowVisibleAtDesktop(Wnd: HWND; DesktopIndex: Integer): Boolean; overload;
// Move
function MoveWindowToDesktop(Wnd: HWND; Desktop: TVirtualDesktop): Boolean; overload;
function MoveWindowToDesktop(Wnd: HWND; DesktopId: TGUID): Boolean; overload;
function MoveWindowToDesktop(Wnd: HWND; DesktopIndex: Integer): Boolean; overload;
// Additional
function IsWindowHaveView(Wnd: HWND): Boolean;
function FlashWindow(Wnd: HWND): Boolean;
function SwithToWindow(Wnd: HWND): Boolean;
// Pin
function IsPinnedWindow(Wnd: HWND): Boolean;
function IsPinnedApplication(Wnd: HWND): Boolean;
function PinWindow(Wnd: HWND): Boolean;
function PinApplication(Wnd: HWND): Boolean;
function UnpinWindow(Wnd: HWND): Boolean;
function UnpinApplication(Wnd: HWND): Boolean;
{ Properties }
property Enabled: Boolean read fEnabled;
property LastError: HRESULT read fLastError;
property Count: Integer read _GetCount;
property Desktops[Index: Integer]: TVirtualDesktop read _GetDesktop;
property CurrentDesktop: TVirtualDesktop read _GetCurrentDesktop;
property CurrentDesktopId: TGUID read _GetCurrentDesktopId;
property CurrentDesktopIndex: Integer read _GetCurrentDesktopIndex write _SetCurrentDesktopIndex;
{ Events }
property OnDesktopCreated: TVirtualDesktopCreatedNotify read fOnCreated write fOnCreated;
property OnDesktopDestroyBegin: TVirtualDesktopDestroyNotify read fOnDestroyBegin write fOnDestroyBegin;
property OnDesktopDestroyFailed: TVirtualDesktopDestroyNotify read fOnDestroyFailed write fOnDestroyFailed;
property OnDesktopDestroyed: TVirtualDesktopDestroyNotify read fOnDestroyed write fOnDestroyed;
property OnCurrentDesktopChanged: TCurrentVirtualDesktopChangedNotify read fOnCurrentChanged write fOnCurrentChanged;
property OnError: TErrorNotify read fOnError write fOnError;
end;
TVirtualDesktopManagerW11 = class(TNonRefInterfacedObject, IVirtualDesktopNotification21313)
private
fISP: IServiceProvider;
fIVDM: IVirtualDesktopManager;
fIVDMI11: IVirtualDesktopManagerInternalW11;
fIVNS: IVirtualNotificationServiceW11;
fIAVC: IApplicationViewCollectionW11;
fIVDPA: IVirtualDesktopPinnedAppsW11;
fCookie: DWORD;
protected
{ IVirtualDesktopNotification }
function VirtualDesktopCreated(p0: IObjectArray; Desktop: IVirtualDesktopW11): HRESULT; stdcall; // ok
function VirtualDesktopDestroyBegin(p0: IObjectArray; Desktop: IVirtualDesktopW11; DesktopFallback: IVirtualDesktopW11): HRESULT; stdcall; // ok
function VirtualDesktopDestroyFailed(p0: IObjectArray; Desktop: IVirtualDesktopW11; DesktopFallback: IVirtualDesktopW11): HRESULT; stdcall; // ok
function VirtualDesktopDestroyed(p0: IObjectArray; Desktop: IVirtualDesktopW11; DesktopFallback: IVirtualDesktopW11): HRESULT; stdcall; // ok
function Unknown1(Number: Integer): HRESULT; stdcall;
function VirtualDesktopMoved(p0: IObjectArray; Desktop: IVirtualDesktopW11; nFromIndex: Integer; nToIndex: Integer): HRESULT; stdcall;
function VirtualDesktopRenamed(Desktop: IVirtualDesktopW11; chName: HSTRING): HRESULT; stdcall;
function ViewVirtualDesktopChanged(View: IApplicationViewW11): HRESULT; stdcall; // ok
function CurrentVirtualDesktopChanged(p0: IObjectArray; DesktopOld: IVirtualDesktopW11; DesktopNew: IVirtualDesktopW11): HRESULT; stdcall; // ok
function VirtualDesktopWallpaperChanged(Desktop: IVirtualDesktopW11; chPath: HSTRING): HRESULT; stdcall;
{ Self }
procedure DoFreeInstance; override;
private
fEnabled: Boolean;
fLastError: HRESULT;
fDesktops: TList;
fOnCurrentChanged: TCurrentVirtualDesktopChangedNotifyW11;
fOnCreated: TVirtualDesktopCreatedNotifyW11;
fOnDestroyBegin: TVirtualDesktopDestroyNotifyW11;
fOnDestroyFailed: TVirtualDesktopDestroyNotifyW11;
fOnDestroyed: TVirtualDesktopDestroyNotifyW11;
fOnError: TErrorNotify;
procedure FreeDesktops;
function CheckError(res: HRESULT): Boolean;
procedure Initialize;
procedure Finalize;
function _GetCount: Integer;
function _GetDesktop(Index: Integer): TVirtualDesktopW11;
function _GetCurrentDesktop: TVirtualDesktopW11;
function _GetCurrentDesktopIndex: Integer;
procedure _SetCurrentDesktopIndex(const Value: Integer);
function _GetCurrentDesktopId: TGUID;
protected
procedure SetLastError(error: HRESULT);
public
procedure AfterConstruction; override;
{ Methods }
// DESKTOPS
// Additional
procedure Refresh;
function GetDesktopIndexById(Id: TGUID): Integer;
// Create
function CreateDesktop: Boolean;
function CreateDesktopAndSwitch: Boolean;
// Switch
function SwitchToDesktop(Desktop: TVirtualDesktopW11): Boolean; overload;
function SwitchToDesktop(DesktopId: TGUID): Boolean; overload;
function SwitchToDesktop(DesktopIndex: Integer): Boolean; overload;
// Remove
function RemoveDesktop(Desktop: TVirtualDesktopW11): Boolean; overload;
function RemoveDesktop(Desktop, DesktopFallback: TVirtualDesktopW11): Boolean; overload;
function RemoveDesktop(DesktopId: TGUID): Boolean; overload;
function RemoveDesktop(DesktopId, DesktopFallbackId: TGUID): Boolean; overload;
function RemoveDesktop(DesktopIndex: Integer): Boolean; overload;
function RemoveDesktop(DesktopIndex, DesktopFallbackIndex: Integer): Boolean; overload;
// WINDOWS AND APPS
function IsWindowOnCurrentDesktop(Wnd: HWND): Boolean;
function GetWindowDesktop(Wnd: HWND): TVirtualDesktopW11;
// Visibility
function IsWindowVisibleAtDesktop(Wnd: HWND; Desktop: TVirtualDesktopW11): Boolean; overload;
function IsWindowVisibleAtDesktop(Wnd: HWND; DesktopId: TGUID): Boolean; overload;
function IsWindowVisibleAtDesktop(Wnd: HWND; DesktopIndex: Integer): Boolean; overload;
// Move
function MoveWindowToDesktop(Wnd: HWND; Desktop: TVirtualDesktopW11): Boolean; overload;
function MoveWindowToDesktop(Wnd: HWND; DesktopId: TGUID): Boolean; overload;
function MoveWindowToDesktop(Wnd: HWND; DesktopIndex: Integer): Boolean; overload;
// Additional
function IsWindowHaveView(Wnd: HWND): Boolean;
function FlashWindow(Wnd: HWND): Boolean;
function SwithToWindow(Wnd: HWND): Boolean;
// Pin
function IsPinnedWindow(Wnd: HWND): Boolean;
function IsPinnedApplication(Wnd: HWND): Boolean;
function PinWindow(Wnd: HWND): Boolean;
function PinApplication(Wnd: HWND): Boolean;
function UnpinWindow(Wnd: HWND): Boolean;
function UnpinApplication(Wnd: HWND): Boolean;
{ Properties }
property Enabled: Boolean read fEnabled;
property LastError: HRESULT read fLastError;
property Count: Integer read _GetCount;
property Desktops[Index: Integer]: TVirtualDesktopW11 read _GetDesktop;
property CurrentDesktop: TVirtualDesktopW11 read _GetCurrentDesktop;
property CurrentDesktopId: TGUID read _GetCurrentDesktopId;
property CurrentDesktopIndex: Integer read _GetCurrentDesktopIndex write _SetCurrentDesktopIndex;
{ Events }
property OnDesktopCreated: TVirtualDesktopCreatedNotifyW11 read fOnCreated write fOnCreated;
property OnDesktopDestroyBegin: TVirtualDesktopDestroyNotifyW11 read fOnDestroyBegin write fOnDestroyBegin;
property OnDesktopDestroyFailed: TVirtualDesktopDestroyNotifyW11 read fOnDestroyFailed write fOnDestroyFailed;
property OnDesktopDestroyed: TVirtualDesktopDestroyNotifyW11 read fOnDestroyed write fOnDestroyed;
property OnCurrentDesktopChanged: TCurrentVirtualDesktopChangedNotifyW11 read fOnCurrentChanged write fOnCurrentChanged;
property OnError: TErrorNotify read fOnError write fOnError;
end;
var
DesktopManager: TVirtualDesktopManager;
DesktopManagerW11: TVirtualDesktopManagerW11;
implementation
function IsW11:Boolean;
begin
Result := TOSVersion.Build >= 22000;
end;
{ TNonRefInterfacedObject }
function TNonRefInterfacedObject._AddRef: Integer;
begin
Result := InterlockedIncrement(fRefCount);
end;
function TNonRefInterfacedObject._Release: Integer;
begin
Assert(fRefCount > 0, 'Reference count must be greater than zero.');
Result := InterlockedDecrement(fRefCount);
if (fRefCount = 0) and fDestroyed then
FreeInstance;
end;
procedure TNonRefInterfacedObject.FreeInstance;
begin
fDestroyed := True;
if fRefCount = 0 then
begin
DoFreeInstance;
inherited FreeInstance;
end;
end;
function TNonRefInterfacedObject.QueryInterface(const IID: TGUID; out Obj): HRESULT;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
procedure TNonRefInterfacedObject.DoFreeInstance;
begin
end;
{ TVirtualDesktop }
constructor TVirtualDesktop.Create(Manager: TVirtualDesktopManager; iDesktop: IVirtualDesktop);
begin
Assert((Manager <> nil), 'Manager must be assigned!');
Assert((iDesktop <> nil), 'Desktop interface must be assigned!');
fManager := Manager;
fIDesktop := iDesktop;
fIDesktop._AddRef;
end;
procedure TVirtualDesktop.DoFreeInstance;
begin
fIDesktop._Release;
end;
function TVirtualDesktop.GetId(Id: PGUID): HRESULT;
begin
Result := fIDesktop.GetId(Id);
end;
function TVirtualDesktop.GetName(Hs: HSTRING): HRESULT;
begin
Result := S_OK;
end;
function TVirtualDesktop.GetWallpaperPath(Hs: HSTRING): HRESULT;
begin
end;
function TVirtualDesktop.IsViewVisible(View: IApplicationView; pfVisible: PBOOL): HRESULT;
begin
Result := fIDesktop.IsViewVisible(View, pfVisible);
end;
function TVirtualDesktop.IsWindowVisible(Wnd: HWND): Boolean;
begin
Result := fManager.IsWindowVisibleAtDesktop(Wnd, Self);
end;
function TVirtualDesktop.QueryInterface(const IID: TGUID; out Obj): HRESULT;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := fIDesktop.QueryInterface(IID, Obj);
end;
function TVirtualDesktop.Remove: Boolean;
begin
Result := fManager.RemoveDesktop(Self);
end;
function TVirtualDesktop.Remove(FallbackDesktop: TVirtualDesktop): Boolean;
begin
Result := fManager.RemoveDesktop(Self, FallbackDesktop);
end;
function TVirtualDesktop.Remove(FallbackDesktopId: TGUID): Boolean;
begin
Result := fManager.RemoveDesktop(_GetId, FallbackDesktopId);
end;
function TVirtualDesktop.Remove(FallbackDesktopIndex: Integer): Boolean;
begin
Result := fManager.RemoveDesktop(_GetIndex, FallbackDesktopIndex);
end;
function TVirtualDesktop.SwitchHere: Boolean;
begin
Result := fManager.SwitchToDesktop(Self);
end;
function TVirtualDesktop._GetId: TGUID;
var
error_code: HRESULT;
begin
error_code := fIDesktop.GetId(@Result);
if Failed(error_code) then
Result := EMPTY_GUID;
fManager.SetLastError(error_code);
end;
function TVirtualDesktop._GetIdAsString: AnsiString;
begin
Result := GUIDToString(_GetId);
end;
function TVirtualDesktop._GetIndex: Integer;
begin
Result := fManager.GetDesktopIndexById(_GetId);
end;
function TVirtualDesktop._GetIsCurrent: Boolean;
begin
Result := IsEqualGUID(fManager.CurrentDesktop.Id, _GetId);
end;
{ TVirtualDesktopManager }
procedure ReleaseAndNil(var Intf);
var
Temp: IUnknown;
begin
Temp := IUnknown(Intf);
if Temp = nil then
Exit;
Temp._Release;
Pointer(Intf) := nil;
end;
procedure TVirtualDesktopManager.AfterConstruction;
begin
inherited AfterConstruction;
Initialize;
if fEnabled then
Refresh;
end;
function TVirtualDesktopManager.CheckError(res: HRESULT): Boolean;
begin
fLastError := Res;
Result := Succeeded(Res);
if (not Result) and Assigned(fOnError) then
fOnError(Self, res);
end;
function TVirtualDesktopManager.CreateDesktop: Boolean;
var
intf_desktop: IVirtualDesktop;
begin
Result := CheckError(fIVDMI.CreateDesktopW(@intf_desktop));
end;
function TVirtualDesktopManager.CreateDesktopAndSwitch: Boolean;
var
intf_desktop: IVirtualDesktop;
begin
Result := CheckError(fIVDMI.CreateDesktopW(@intf_desktop));
if Result then
Result := CheckError(fIVDMI.SwitchDesktop(intf_desktop));
end;
function TVirtualDesktopManager.CurrentVirtualDesktopChanged(DesktopOld, DesktopNew: IVirtualDesktop): HRESULT;
var
old_desktop_id, new_desktop_id: TGUID;
begin
if Assigned(fOnCurrentChanged) then
begin
CheckError(DesktopOld.GetId(@old_desktop_id));
CheckError(DesktopNew.GetId(@new_desktop_id));
fOnCurrentChanged(Self, _GetDesktop(GetDesktopIndexById(old_desktop_id)), _GetDesktop(GetDesktopIndexById(new_desktop_id)));
end;
Result := S_OK;
end;
procedure TVirtualDesktopManager.DoFreeInstance;
begin
if fEnabled then
Finalize;
FreeDesktops;
end;
procedure TVirtualDesktopManager.Finalize;
begin
if fIVNS <> nil then
fIVNS.Unregister(fCookie);
ReleaseAndNil(fIVDM);
ReleaseAndNil(fIVDMI);
ReleaseAndNil(fIVNS);
ReleaseAndNil(fIAVC);
ReleaseAndNil(fIVDPA);
ReleaseAndNil(fISP);
end;
procedure TVirtualDesktopManager.FreeDesktops;
var
desktop_index: Integer;
begin
if fDesktops <> nil then
begin
for desktop_index := fDesktops.Count - 1 downto 0 do
TVirtualDesktop(fDesktops[desktop_index]).Free;
FreeAndNil(fDesktops);
end;
end;
function TVirtualDesktopManager.GetDesktopIndexById(Id: TGUID): Integer;
begin
Result := 0;
while (Result < Count) and (not IsEqualGUID(TVirtualDesktop(fDesktops[Result]).Id, Id)) do
Inc(Result);
if Result = Count then
Result := -1;
end;
function TVirtualDesktopManager.GetWindowDesktop(Wnd: HWND): TVirtualDesktop;
var
desktop_id: TGUID;
begin
Result := nil;
if fEnabled then
if CheckError(fIVDM.GetWindowDesktopId(Wnd, @desktop_id)) then
Result := _GetDesktop(GetDesktopIndexById(desktop_id));
end;
function TVirtualDesktopManager.FlashWindow(Wnd: HWND): Boolean;
var
intf_view: IApplicationView;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
Result := CheckError(intf_view.Flash);
end;
end;
procedure TVirtualDesktopManager.Initialize;
begin
fISP := nil;
fIVDM := nil;
fIVDMI := nil;
fIVDMI11 := nil;
fIVNS := nil;
fIAVC := nil;
fIVDPA := nil;
if IsW11 then
begin
fEnabled := CheckError(CoCreateInstance(CLSID_VirtualDesktopManager, nil, CLSCTX_INPROC_SERVER, IID_VirtualDesktopManager, fIVDMI11)) and //
CheckError(CoCreateInstance(CLSID_ImmersiveShell, nil, CLSCTX_LOCAL_SERVER, IID_ServiceProvider, fISP));
if fEnabled then
begin
//22000 and later using IVirtualDesktopManagerInternalW11 instead
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopManagerInternal, IID_VirtualDesktopManagerInternal_22000, fIVDMI11));
if not fEnabled then
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopManagerInternal, IID_VirtualDesktopManagerInternal_22621, fIVDMI11));
if not fEnabled then
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopManagerInternal, IID_VirtualDesktopManagerInternal_22631, fIVDMI11));
fEnabled := fEnabled and //
CheckError(fISP.QueryService(CLSID_VirtualNotificationService, IID_VirtualNotificationService, fIVNS)) and //
CheckError(fISP.QueryService(CLSID_ApplicationViewCollection, IID_ApplicationViewCollection, fIAVC)) and //
CheckError(fISP.QueryService(CLSID_VirtualDesktopPinnedApps, IID_VirtualDesktopPinnedApps, fIVDPA));
if fEnabled then
begin
// fEnabled := CheckError(fIVNS.Register(Self, @fCookie));
OleCheck(fIVNS.Register(Self, @fCookie));
end;
end;
end
else
begin
fEnabled := CheckError(CoCreateInstance(CLSID_VirtualDesktopManager, nil, CLSCTX_INPROC_SERVER, IID_VirtualDesktopManager, fIVDM)) and //
CheckError(CoCreateInstance(CLSID_ImmersiveShell, nil, CLSCTX_LOCAL_SERVER, IID_ServiceProvider, fISP));
if fEnabled then
begin
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopManagerInternal, IID_VirtualDesktopManagerInternal_14393, fIVDMI));
if not fEnabled then
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopManagerInternal, IID_VirtualDesktopManagerInternal_10240, fIVDMI));
if not fEnabled then
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopManagerInternal, IID_VirtualDesktopManagerInternal_10130, fIVDMI));
if not fEnabled then
begin
//22000 and later using IVirtualDesktopManagerInternalW11 instead
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopManagerInternal, IID_VirtualDesktopManagerInternal_22000, fIVDMI11));
end;
fEnabled := fEnabled and //
CheckError(fISP.QueryService(CLSID_VirtualNotificationService, IID_VirtualNotificationService, fIVNS)) and //
CheckError(fISP.QueryService(CLSID_ApplicationViewCollection, IID_ApplicationViewCollection, fIAVC)) and //
CheckError(fISP.QueryService(CLSID_VirtualDesktopPinnedApps, IID_VirtualDesktopPinnedApps, fIVDPA));
if fEnabled then
begin
fEnabled := CheckError(fIVNS.Register(Self, @fCookie));
// OleCheck(fIVNS.Register(Self, @fCookie));
end;
end;
end;
if not fEnabled then
Finalize;
end;
function TVirtualDesktopManager.IsWindowOnCurrentDesktop(Wnd: HWND): Boolean;
var
bool_result: BOOL;
begin
Result := False;
if fEnabled then
if CheckError(fIVDM.IsWindowOnCurrentVirtualDesktop(Wnd, @bool_result)) then
Result := bool_result;
end;
function TVirtualDesktopManager.MoveWindowToDesktop(Wnd: HWND; Desktop: TVirtualDesktop): Boolean;
var
intf_view: IApplicationView;
begin
Result := False;
if fEnabled then
if GetWindowThreadProcessId(Wnd) = GetCurrentProcessId then
Result := CheckError(fIVDM.MoveWindowToDesktop(Wnd, Desktop.Id))
else
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
Result := CheckError(fIVDMI.MoveViewToDesktop(intf_view, (Desktop as IVirtualDesktop)));
end;
end;
function TVirtualDesktopManager.MoveWindowToDesktop(Wnd: HWND; DesktopId: TGUID): Boolean;
begin
Result := MoveWindowToDesktop(Wnd, _GetDesktop(GetDesktopIndexById(DesktopId)));
end;
function TVirtualDesktopManager.MoveWindowToDesktop(Wnd: HWND; DesktopIndex: Integer): Boolean;
begin
Result := MoveWindowToDesktop(Wnd, _GetDesktop(DesktopIndex));
end;
function TVirtualDesktopManager.IsWindowHaveView(Wnd: HWND): Boolean;
var
intf_view: IApplicationView;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
Result := Succeeded(fIAVC.GetViewForHwnd(Wnd, @intf_view));
end;
end;
function TVirtualDesktopManager.IsPinnedApplication(Wnd: HWND): Boolean;
var
intf_view: IApplicationView;
app_id: PLPWSTR;
bool_result: BOOL;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
begin
GetMem(app_id, 1024);
if CheckError(intf_view.GetAppUserModelId(app_id)) then
if CheckError(fIVDPA.IsAppIdPinned(app_id^, @bool_result)) then
Result := bool_result;
FreeMem(app_id);
end;
end;
end;
function TVirtualDesktopManager.IsPinnedWindow(Wnd: HWND): Boolean;
var
intf_view: IApplicationView;
bool_result: BOOL;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
if CheckError(fIVDPA.IsViewPinned(intf_view, @bool_result)) then
Result := bool_result;
end;
end;
function TVirtualDesktopManager.IsWindowVisibleAtDesktop(Wnd: HWND; Desktop: TVirtualDesktop): Boolean;
var
intf_view: IApplicationView;
bool_result: Boolean;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
if CheckError((Desktop as IVirtualDesktop).IsViewVisible(intf_view, @bool_result)) then
Result := bool_result;
end;
end;
function TVirtualDesktopManager.IsWindowVisibleAtDesktop(Wnd: HWND; DesktopId: TGUID): Boolean;
begin
Result := IsWindowVisibleAtDesktop(Wnd, _GetDesktop(GetDesktopIndexById(DesktopId)));
end;
function TVirtualDesktopManager.IsWindowVisibleAtDesktop(Wnd: HWND; DesktopIndex: Integer): Boolean;
begin
Result := IsWindowVisibleAtDesktop(Wnd, _GetDesktop(DesktopIndex));
end;
procedure TVirtualDesktopManager.Refresh;
var
desktop_count: UINT;
desktop_index: Integer;
desktop_new: TVirtualDesktop;
intf_desktop: IVirtualDesktop;
intf_desktops: IObjectArray;
begin
FreeDesktops;
if fEnabled then
if CheckError(fIVDMI.GetDesktops(@intf_desktops)) then
if CheckError(intf_desktops.GetCount(@desktop_count)) and (desktop_count > 0) then
begin
fDesktops := TList.Create;
for desktop_index := 0 to desktop_count - 1 do
if CheckError(intf_desktops.GetAt(desktop_index, @IID_VirtualDesktop, @intf_desktop)) then
begin
desktop_new := TVirtualDesktop.Create(Self, intf_desktop);
fDesktops.Add(desktop_new);
end;
end;
end;
function TVirtualDesktopManager.RemoveDesktop(Desktop: TVirtualDesktop): Boolean;
var
intf_desktop: IVirtualDesktop;
candidate: Integer;
begin
Result := False;
if fEnabled then
if Count = 1 then
begin
Result := CheckError(fIVDMI.CreateDesktopW(@intf_desktop));
if Result then
Result := CheckError(fIVDMI.RemoveDesktop((Desktop as IVirtualDesktop), intf_desktop));
end
else if Desktop.IsCurrent then
begin
candidate := 0;
while IsEqualGUID(_GetDesktop(candidate).Id, Desktop.Id) do
Inc(candidate);
Result := CheckError(fIVDMI.RemoveDesktop((Desktop as IVirtualDesktop), (_GetDesktop(candidate) as IVirtualDesktop)));
end
else
Result := CheckError(fIVDMI.RemoveDesktop((Desktop as IVirtualDesktop), (_GetCurrentDesktop as IVirtualDesktop)));
end;
function TVirtualDesktopManager.RemoveDesktop(DesktopId: TGUID): Boolean;
begin
Result := RemoveDesktop(_GetDesktop(GetDesktopIndexById(DesktopId)));
end;
function TVirtualDesktopManager.RemoveDesktop(DesktopIndex: Integer): Boolean;
begin
Result := RemoveDesktop(_GetDesktop(DesktopIndex));
end;
function TVirtualDesktopManager.RemoveDesktop(Desktop, DesktopFallback: TVirtualDesktop): Boolean;
begin
Result := CheckError(fIVDMI.RemoveDesktop((Desktop as IVirtualDesktop), (DesktopFallback as IVirtualDesktop)));
end;
function TVirtualDesktopManager.RemoveDesktop(DesktopId, DesktopFallbackId: TGUID): Boolean;
begin
Result := RemoveDesktop(_GetDesktop(GetDesktopIndexById(DesktopId)), _GetDesktop(GetDesktopIndexById(DesktopFallbackId)));
end;
function TVirtualDesktopManager.RemoveDesktop(DesktopIndex, DesktopFallbackIndex: Integer): Boolean;
begin
Result := RemoveDesktop(_GetDesktop(DesktopIndex), _GetDesktop(DesktopFallbackIndex));
end;
function TVirtualDesktopManager.PinApplication(Wnd: HWND): Boolean;
var
intf_view: IApplicationView;
app_id: PLPWSTR;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
begin
GetMem(app_id, 1024);
if CheckError(intf_view.GetAppUserModelId(app_id)) then
Result := CheckError(fIVDPA.PinAppID(app_id^));
FreeMem(app_id);
end;
end;
end;
function TVirtualDesktopManager.PinWindow(Wnd: HWND): Boolean;
var
intf_view: IApplicationView;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
Result := CheckError(fIVDPA.PinView(intf_view));
end;
end;
procedure TVirtualDesktopManager.SetLastError(error: HRESULT);
begin
fLastError := error;
end;
function TVirtualDesktopManager.SwitchToDesktop(Desktop: TVirtualDesktop): Boolean;
begin
if fEnabled then
Result := CheckError(fIVDMI.SwitchDesktop((Desktop as IVirtualDesktop)))
else
Result := False;
end;
function TVirtualDesktopManager.SwitchToDesktop(DesktopId: TGUID): Boolean;
begin
Result := SwitchToDesktop(_GetDesktop(GetDesktopIndexById(DesktopId)));
end;
function TVirtualDesktopManager.SwitchToDesktop(DesktopIndex: Integer): Boolean;
begin
Result := SwitchToDesktop(_GetDesktop(DesktopIndex));
end;
function TVirtualDesktopManager.SwithToWindow(Wnd: HWND): Boolean;
var
intf_view: IApplicationView;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
Result := CheckError(intf_view.SwitchTo);
end;
end;
function TVirtualDesktopManager.UnpinApplication(Wnd: HWND): Boolean;
var
intf_view: IApplicationView;
app_id: PLPWSTR;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
begin
GetMem(app_id, 1024);
if CheckError(intf_view.GetAppUserModelId(app_id)) then
Result := CheckError(fIVDPA.UnpinAppID(app_id^));
FreeMem(app_id);
end;
end;
end;
function TVirtualDesktopManager.UnpinWindow(Wnd: HWND): Boolean;
var
intf_view: IApplicationView;
begin
Result := False;
if fEnabled then
begin
CheckError(fIAVC.RefreshCollection);
if CheckError(fIAVC.GetViewForHwnd(Wnd, @intf_view)) then
Result := CheckError(fIVDPA.UnpinView(intf_view));
end;
end;
function TVirtualDesktopManager.ViewVirtualDesktopChanged(View: IApplicationView): HRESULT;
begin
// to do
Result := S_OK;
end;
function TVirtualDesktopManager.VirtualDesktopCreated(Desktop: IVirtualDesktop): HRESULT;
var
new_desktop: TVirtualDesktop;
begin
new_desktop := TVirtualDesktop.Create(Self, Desktop);
fDesktops.Add(new_desktop);
if Assigned(fOnCreated) then
fOnCreated(Self, new_desktop);
Result := S_OK;
end;
function TVirtualDesktopManager.VirtualDesktopDestroyBegin(Desktop, DesktopFallback: IVirtualDesktop): HRESULT;
var
desktop_id, desktop_fallback_id: TGUID;
begin
if Assigned(fOnDestroyBegin) then
begin
CheckError(Desktop.GetId(@desktop_id));
CheckError(DesktopFallback.GetId(@desktop_fallback_id));
fOnDestroyBegin(Self, _GetDesktop(GetDesktopIndexById(desktop_id)), _GetDesktop(GetDesktopIndexById(desktop_fallback_id)));
end;
Result := S_OK;
end;
function TVirtualDesktopManager.VirtualDesktopDestroyed(Desktop, DesktopFallback: IVirtualDesktop): HRESULT;
var
desktop_id, desktop_fallback_id: TGUID;
destroyed_desktop: TVirtualDesktop;
begin
CheckError(Desktop.GetId(@desktop_id));
destroyed_desktop := TVirtualDesktop(fDesktops.Extract(_GetDesktop(GetDesktopIndexById(desktop_id))));
if Assigned(fOnDestroyed) then
begin
CheckError(DesktopFallback.GetId(@desktop_fallback_id));
fOnDestroyed(Self, destroyed_desktop, _GetDesktop(GetDesktopIndexById(desktop_fallback_id)));
end;
destroyed_desktop.Free;
Result := S_OK;
end;
function TVirtualDesktopManager.VirtualDesktopDestroyFailed(Desktop, DesktopFallback: IVirtualDesktop): HRESULT;