forked from Grabacr07/VirtualDesktop
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
165 lines (141 loc) · 5.46 KB
/
MainWindow.xaml.cs
File metadata and controls
165 lines (141 loc) · 5.46 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using WindowsDesktop;
using WindowsDesktop.Properties;
namespace VirtualDesktopShowcase
{
partial class MainWindow
{
private static readonly int _delay = 2000;
public MainWindow()
{
this.InitializeComponent();
this.InitializeComObjects();
}
private void InitializeComObjects()
{
VirtualDesktop.Configure(new VirtualDesktopConfiguration());
VirtualDesktop.CurrentChanged += (_, args) => Debug.WriteLine($"Switched: {args.OldDesktop.Name} -> {args.NewDesktop.Name}");
VirtualDesktop.Renamed += (_, args) => Debug.WriteLine($"Renamed: {args.Desktop}");
VirtualDesktop.WallpaperChanged += (_, args) => Debug.WriteLine($"Wallpaper changed: {args.Desktop}, {args.Path}");
VirtualDesktop.ApplicationViewChanged += HandleApplicationViewChanged;
this.Closed += (_, _) => VirtualDesktop.ApplicationViewChanged -= HandleApplicationViewChanged;
foreach (var desktop in VirtualDesktop.GetDesktops())
{
Debug.WriteLine($"Detected: {desktop}, {desktop.WallpaperPath}");
}
void HandleApplicationViewChanged(object? sender, ApplicationViewChangedEventArgs e)
{
this.Dispatcher.Invoke(() =>
{
var window = Application.Current.Windows
.OfType<Window>()
.FirstOrDefault(x => x.GetHandle() == e.Hwnd);
var message = window != null
? $"{window.Title}"
: $"hwnd[{e.Hwnd}]";
Debug.WriteLine("App view changed: " + message + $", {e.Desktop?.ToString() ?? "(pinned)"}");
});
}
}
private void CreateNew(object sender, RoutedEventArgs e)
=> VirtualDesktop.Create().Switch();
private async void CreateNewAndMove(object sender, RoutedEventArgs e)
{
var desktop = VirtualDesktop.Create();
if (this.ThisWindowMenu.IsChecked ?? false)
{
this.MoveToDesktop(desktop);
}
else
{
await Task.Delay(_delay);
VirtualDesktop.MoveToDesktop(GetForegroundWindow(), desktop);
}
desktop.Switch();
}
private void SwitchLeft(object sender, RoutedEventArgs e)
{
this.GetCurrentDesktop()?.GetLeft()?.Switch();
}
private async void SwitchLeftAndMove(object sender, RoutedEventArgs e)
{
var left = this.GetCurrentDesktop()?.GetLeft();
if (left == null) return;
if (this.ThisWindowMenu.IsChecked ?? false)
{
this.MoveToDesktop(left);
}
else
{
await Task.Delay(_delay);
VirtualDesktop.MoveToDesktop(GetForegroundWindow(), left);
}
left.Switch();
}
private void SwitchRight(object sender, RoutedEventArgs e)
{
this.GetCurrentDesktop()?.GetRight()?.Switch();
}
private async void SwitchRightAndMove(object sender, RoutedEventArgs e)
{
var right = this.GetCurrentDesktop()?.GetRight();
if (right == null) return;
if (this.ThisWindowMenu.IsChecked ?? false)
{
this.MoveToDesktop(right);
}
else
{
await Task.Delay(_delay);
VirtualDesktop.MoveToDesktop(GetForegroundWindow(), right);
}
right.Switch();
}
private async void Pin(object sender, RoutedEventArgs e)
{
if (this.ThisWindowMenu.IsChecked ?? false)
{
this.TogglePin();
}
else
{
await Task.Delay(_delay);
var handle = GetForegroundWindow();
(VirtualDesktop.IsPinnedWindow(handle) ? VirtualDesktop.UnpinWindow : (Action<IntPtr>)VirtualDesktop.PinWindow)(handle);
}
}
private async void PinApp(object sender, RoutedEventArgs e)
{
if (this.ThisWindowMenu.IsChecked ?? false)
{
Application.Current.TogglePin();
}
else
{
await Task.Delay(_delay);
var appId = VirtualDesktop.GetAppId(GetForegroundWindow());
if (appId != null) (VirtualDesktop.IsPinnedApplication(appId) ? VirtualDesktop.UnpinApplication : (Action<string>)VirtualDesktop.PinApplication)(appId);
}
}
private async void Remove(object sender, RoutedEventArgs e)
{
if (this.ThisWindowMenu.IsChecked ?? false)
{
this.GetCurrentDesktop()?.Remove();
}
else
{
await Task.Delay(_delay);
this.GetCurrentDesktop()?.Remove();
}
}
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
}
}