-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppLauncherService.cs
More file actions
218 lines (187 loc) · 7.67 KB
/
Copy pathAppLauncherService.cs
File metadata and controls
218 lines (187 loc) · 7.67 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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MonitorLauncher
{
public class AppLauncherService
{
public async Task<LaunchResult> LaunchAsync(LaunchRequest request)
{
if (!CanLaunch(request.ExecutablePath))
{
return new LaunchResult
{
FileMissing = true,
StatusMessage = "선택한 파일이 존재하지 않습니다."
};
}
if (!TryResolveTargetScreen(request, out var targetScreen, out var usedFallbackMonitor))
{
return new LaunchResult
{
MonitorMissing = true,
StatusMessage = "저장된 모니터를 찾을 수 없습니다."
};
}
var startInfo = BuildStartInfo(request);
var existingWindows = startInfo.UseShellExecute ? null : WindowController.CaptureVisibleWindows();
Process? process;
bool launchStarted = false;
try
{
process = Process.Start(startInfo);
launchStarted = process != null || startInfo.UseShellExecute;
}
catch
{
process = null;
}
if (!launchStarted)
{
return new LaunchResult
{
StatusMessage = "프로그램 실행 실패"
};
}
await Task.Delay(100);
bool success = false;
bool skippedWindowControl = startInfo.UseShellExecute;
bool canControlWindow = process != null && !startInfo.UseShellExecute;
if (canControlWindow)
{
success = await WindowController.MoveWindowToMonitor(process, targetScreen!, request.WindowState);
}
if (success)
{
await Task.Delay(500);
if (process != null)
{
success = await WindowController.EnsureWindowOnMonitor(process, targetScreen!, request.WindowState);
}
}
else if (canControlWindow && existingWindows != null)
{
success = await WindowController.MoveNewWindowToMonitorAsync(existingWindows, targetScreen!, request.WindowState, process?.Id);
}
return new LaunchResult
{
Succeeded = launchStarted,
WindowMoved = success,
UsedMonitorFallback = usedFallbackMonitor,
StatusMessage = BuildStatusMessage(request, targetScreen!, success, usedFallbackMonitor, skippedWindowControl)
};
}
public bool TryResolveTargetScreen(LaunchRequest request, out Screen? screen, out bool usedFallback)
{
var screenMatch = FindTargetScreen(request);
screen = screenMatch?.Screen;
usedFallback = screenMatch?.UsedFallback ?? false;
return screen != null;
}
private static bool CanLaunch(string executablePath)
{
if (string.IsNullOrWhiteSpace(executablePath))
{
return false;
}
if (Uri.TryCreate(executablePath, UriKind.Absolute, out var uri) && !uri.IsFile)
{
return true;
}
return File.Exists(executablePath);
}
private static ProcessStartInfo BuildStartInfo(LaunchRequest request)
{
bool useShellExecute = ShouldUseShellExecute(request.ExecutablePath);
var startInfo = new ProcessStartInfo
{
FileName = request.ExecutablePath,
UseShellExecute = useShellExecute
};
if (!string.IsNullOrWhiteSpace(request.Arguments))
{
startInfo.Arguments = request.Arguments;
}
return startInfo;
}
private static bool ShouldUseShellExecute(string executablePath)
{
if (Uri.TryCreate(executablePath, UriKind.Absolute, out var uri) && !uri.IsFile)
{
return true;
}
string extension = Path.GetExtension(executablePath).ToLowerInvariant();
return extension != ".exe";
}
private static ScreenMatchResult? FindTargetScreen(LaunchRequest request)
{
foreach (var screen in Screen.AllScreens)
{
if (screen.DeviceName == request.MonitorDeviceName)
{
return new ScreenMatchResult(screen, false);
}
}
foreach (var screen in Screen.AllScreens)
{
if (screen.Bounds.X == request.MonitorBoundsX &&
screen.Bounds.Y == request.MonitorBoundsY &&
screen.Bounds.Width == request.MonitorBoundsWidth &&
screen.Bounds.Height == request.MonitorBoundsHeight)
{
return new ScreenMatchResult(screen, true);
}
}
foreach (var screen in Screen.AllScreens)
{
if (screen.Primary == request.MonitorWasPrimary &&
screen.Bounds.Width == request.MonitorBoundsWidth &&
screen.Bounds.Height == request.MonitorBoundsHeight)
{
return new ScreenMatchResult(screen, true);
}
}
var closestScreen = Screen.AllScreens
.OrderBy(screen => GetScreenDistance(screen, request))
.FirstOrDefault();
return closestScreen == null ? null : new ScreenMatchResult(closestScreen, true);
}
private static int GetScreenDistance(Screen screen, LaunchRequest request)
{
int positionDistance = Math.Abs(screen.Bounds.X - request.MonitorBoundsX) + Math.Abs(screen.Bounds.Y - request.MonitorBoundsY);
int sizeDistance = Math.Abs(screen.Bounds.Width - request.MonitorBoundsWidth) + Math.Abs(screen.Bounds.Height - request.MonitorBoundsHeight);
int primaryPenalty = screen.Primary == request.MonitorWasPrimary ? 0 : 1000;
return positionDistance + sizeDistance + primaryPenalty;
}
private static string BuildStatusMessage(LaunchRequest request, Screen targetScreen, bool success, bool usedFallbackMonitor, bool skippedWindowControl)
{
string prefix = usedFallbackMonitor ? "저장된 모니터 대신 가장 유사한 모니터를 사용했습니다. " : string.Empty;
if (!success)
{
if (skippedWindowControl)
{
return $"{prefix}프로그램은 실행되었지만 이 실행 방식은 창 위치 제어를 지원하지 않습니다.";
}
return $"{prefix}프로그램은 실행되었지만 창 위치 제어에 실패했습니다.";
}
if (!string.IsNullOrWhiteSpace(request.ProfileName))
{
return $"{prefix}프로필 '{request.ProfileName}' 실행 완료";
}
return $"{prefix}프로그램이 {targetScreen.DeviceName}에서 실행되었습니다.";
}
private sealed class ScreenMatchResult
{
public ScreenMatchResult(Screen screen, bool usedFallback)
{
Screen = screen;
UsedFallback = usedFallback;
}
public Screen Screen { get; }
public bool UsedFallback { get; }
}
}
}