-
Notifications
You must be signed in to change notification settings - Fork 8
/
VirtualDesktopManager.pas
857 lines (767 loc) · 27.1 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
// Created by Alexey Andriukhin (dr. F.I.N.) http://www.delphisources.ru/forum/member.php?u=9721
// Tested on Win10 x32 (build 14393), Delphi7
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;
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;
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;
{ 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;
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;
TVirtualDesktopManager = class(TNonRefInterfacedObject, IVirtualDesktopNotification)
private
fISP: IServiceProvider;
fIVDM: IVirtualDesktopManager;
fIVDMI: IVirtualDesktopManagerInternal;
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;
var
DesktopManager: TVirtualDesktopManager;
implementation
{ 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.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;
fIVNS := nil;
fIAVC := nil;
fIVDPA := nil;
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_VirtualDesktopAPI_Unknown, IID_VirtualDesktopManagerInternal_14393, fIVDMI));
if not fEnabled then
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopAPI_Unknown, IID_VirtualDesktopManagerInternal_10240, fIVDMI));
if not fEnabled then
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopAPI_Unknown, IID_VirtualDesktopManagerInternal_10130, fIVDMI));
if not fEnabled then
fEnabled := CheckError(fISP.QueryService(CLSID_VirtualDesktopAPI_Unknown, IID_VirtualDesktopManagerInternal_22000, fIVDMI));
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;
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;
var
desktop_id, desktop_fallback_id: TGUID;
begin
if Assigned(fOnDestroyFailed) then
begin
CheckError(Desktop.GetId(@desktop_id));
CheckError(DesktopFallback.GetId(@desktop_fallback_id));
fOnDestroyFailed(Self, _GetDesktop(GetDesktopIndexById(desktop_id)), _GetDesktop(GetDesktopIndexById(desktop_fallback_id)));
end;
Result := S_OK;
end;
function TVirtualDesktopManager._GetCount: Integer;
begin
if fDesktops = nil then
Result := 0
else
Result := fDesktops.Count;
end;
function TVirtualDesktopManager._GetCurrentDesktop: TVirtualDesktop;
begin
Result := _GetDesktop(_GetCurrentDesktopIndex);
end;
function TVirtualDesktopManager._GetCurrentDesktopIndex: Integer;
var
intf_desktop: IVirtualDesktop;
desktop_id: TGUID;
begin
Result := -1;
if fEnabled then
if CheckError(fIVDMI.GetCurrentDesctop(@intf_desktop)) then
if CheckError(intf_desktop.GetId(@desktop_id)) then
Result := GetDesktopIndexById(desktop_id);
end;
function TVirtualDesktopManager._GetDesktop(Index: Integer): TVirtualDesktop;
begin
if (fDesktops = nil) or (Index < 0) or (Index >= fDesktops.Count) then
Result := nil
else
Result := TVirtualDesktop(fDesktops.Items[Index]);
end;
function TVirtualDesktopManager._GetCurrentDesktopId: TGUID;
begin
Result := CurrentDesktop.Id;
end;
procedure TVirtualDesktopManager._SetCurrentDesktopIndex(const Value: Integer);
begin
SwitchToDesktop(Value);
end;
initialization
CoInitialize(nil);
DesktopManager := TVirtualDesktopManager.Create;
finalization
DesktopManager.Free;
CoUninitialize;
end.