forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindows.cs
More file actions
710 lines (593 loc) · 28.3 KB
/
Windows.cs
File metadata and controls
710 lines (593 loc) · 28.3 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text;
using System.Text.Json;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Platform;
using Avalonia.Threading;
using Avalonia.VisualTree;
namespace SourceGit.Native
{
[SupportedOSPlatform("windows")]
internal class Windows : OS.IBackend
{
[StructLayout(LayoutKind.Sequential)]
internal struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
[DllImport("dwmapi.dll")]
private static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
[DllImport("shlwapi.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern bool PathFindOnPath([In, Out] StringBuilder pszFile, [In] string[] ppszOtherDirs);
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern IntPtr ILCreateFromPathW(string pszPath);
[DllImport("shell32.dll", SetLastError = false)]
private static extern void ILFree(IntPtr pidl);
[DllImport("shell32.dll", CharSet = CharSet.Unicode, SetLastError = false)]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, int cild, IntPtr apidl, int dwFlags);
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);
public void SetupApp(AppBuilder builder)
{
// Fix drop shadow issue on Windows 10
if (!OperatingSystem.IsWindowsVersionAtLeast(10, 0, 22000))
{
Window.WindowStateProperty.Changed.AddClassHandler<Window>((w, _) => FixWindowFrameOnWin10(w));
Control.LoadedEvent.AddClassHandler<Window>((w, _) => FixWindowFrameOnWin10(w));
}
}
public void SetupWindow(Window window)
{
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
window.ExtendClientAreaToDecorationsHint = true;
window.Classes.Add("fix_maximized_padding");
// Track the last hovered button for proper enter/leave handling
Control lastHoveredButton = null;
// Track the button that received the left button down event
Control pressedButton = null;
Win32Properties.CustomWndProcHookCallback hookCallback = (hWnd, msg, wParam, lParam, ref handled) =>
{
const uint WM_NCHITTEST = 0x0084;
const uint WM_NCMOUSEMOVE = 0x00A0;
const uint WM_NCLBUTTONDOWN = 0x00A1;
const uint WM_NCLBUTTONUP = 0x00A2;
const uint WM_NCMOUSELEAVE = 0x02A2;
const uint WM_MOUSEMOVE = 0x0200;
const uint WM_MOUSELEAVE = 0x02A3;
const int HTCLIENT = 1;
const int HTMINBUTTON = 8;
const int HTMAXBUTTON = 9;
const int HTCLOSE = 20;
switch (msg)
{
// Handle non-client mouse move - update button hover states
case WM_NCMOUSEMOVE:
{
var hitTest = wParam.ToInt32();
if (hitTest is HTMINBUTTON or HTMAXBUTTON or HTCLOSE)
{
var screenPoint = IntPtrToPixelPoint(lParam);
GetWindowRect(hWnd, out var rcWindow);
// Find the button control at the current position
var button = GetButtonAtPoint(window, screenPoint, rcWindow);
if (button != lastHoveredButton)
{
// Mouse left the previous button
if (lastHoveredButton != null)
{
((IPseudoClasses)lastHoveredButton.Classes).Remove(":pointerover");
}
// Mouse entered new button
if (button != null)
{
((IPseudoClasses)button.Classes).Add(":pointerover");
}
lastHoveredButton = button;
}
handled = true;
return IntPtr.Zero;
}
// Mouse moved to non-button area, clear hover state
if (lastHoveredButton != null)
{
((IPseudoClasses)lastHoveredButton.Classes).Remove(":pointerover");
lastHoveredButton = null;
}
break;
}
// Handle client area mouse move/leave - clear hover state if mouse enters client area
case WM_NCMOUSELEAVE:
case WM_MOUSEMOVE:
case WM_MOUSELEAVE:
{
if (lastHoveredButton != null)
{
((IPseudoClasses)lastHoveredButton.Classes).Remove(":pointerover");
lastHoveredButton = null;
}
break;
}
// Handle non-client left button down - trigger button press
case WM_NCLBUTTONDOWN:
{
var hitTest = wParam.ToInt32();
if (hitTest == HTMINBUTTON || hitTest == HTMAXBUTTON || hitTest == HTCLOSE)
{
var screenPoint = IntPtrToPixelPoint(lParam);
GetWindowRect(hWnd, out var rcWindow);
// Find the button control at the current position
var button = GetButtonAtPoint(window, screenPoint, rcWindow);
if (button != null)
{
((IPseudoClasses)button.Classes).Add(":pressed");
pressedButton = button;
}
handled = true;
return IntPtr.Zero;
}
break;
}
// Handle non-client left button up - trigger button click
case WM_NCLBUTTONUP:
{
var hitTest = wParam.ToInt32();
if (hitTest == HTMINBUTTON || hitTest == HTMAXBUTTON || hitTest == HTCLOSE)
{
var screenPoint = IntPtrToPixelPoint(lParam);
GetWindowRect(hWnd, out var rcWindow);
var button = GetButtonAtPoint(window, screenPoint, rcWindow);
if (pressedButton != null)
{
((IPseudoClasses)pressedButton.Classes).Remove(":pressed");
}
// If released on the same button that was pressed, trigger click
if (button != null && button == pressedButton)
{
// Use Dispatcher to invoke the click after WndProc returns
Dispatcher.UIThread.Post(() =>
{
// Simulate a button click by raising the Click event
button.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
});
}
// Reset the pressed button regardless of where the mouse was released
pressedButton = null;
handled = true;
return IntPtr.Zero;
}
break;
}
// Custom WM_NCHITTEST
case WM_NCHITTEST:
{
handled = true;
var p = IntPtrToPixelPoint(lParam);
GetWindowRect(hWnd, out var rcWindow);
// Check caption buttons first (for Snap Layouts support)
var hitTestValue = HitTestVisual(window, p, rcWindow);
// Return appropriate non-client hit test value for caption buttons
// This enables Windows 11 Snap Layouts
if (IsCaptionButton(hitTestValue))
{
var htValue = hitTestValue switch
{
Win32HitTestHelper.HitTestValue.MinButton => HTMINBUTTON,
Win32HitTestHelper.HitTestValue.MaxButton => HTMAXBUTTON,
Win32HitTestHelper.HitTestValue.Close => HTCLOSE,
_ => HTCLIENT
};
return new IntPtr(htValue);
}
if (window.WindowState is WindowState.FullScreen or WindowState.Maximized)
return new IntPtr(HTCLIENT);
var borderThickness = (int)(4 * window.RenderScaling);
var y = 1;
var x = 1;
if (p.X >= rcWindow.left && p.X < rcWindow.left + borderThickness)
x = 0;
else if (p.X < rcWindow.right && p.X >= rcWindow.right - borderThickness)
x = 2;
if (p.Y >= rcWindow.top && p.Y < rcWindow.top + borderThickness)
y = 0;
else if (p.Y < rcWindow.bottom && p.Y >= rcWindow.bottom - borderThickness)
y = 2;
var zone = y * 3 + x;
var htResult = zone switch
{
0 => 13, // HTTOPLEFT
1 => 12, // HTTOP
2 => 14, // HTTOPRIGHT
3 => 10, // HTLEFT
4 => 1, // HTCLIENT
5 => 11, // HTRIGHT
6 => 16, // HTBOTTOMLEFT
7 => 15, // HTBOTTOM
_ => 17, // HTBOTTOMRIGHT
};
return new IntPtr(htResult);
}
}
return IntPtr.Zero;
};
Win32Properties.AddWndProcHookCallback(window, hookCallback);
// Remove hook when window is closed
window.Closed += (_, _) =>
{
Win32Properties.RemoveWndProcHookCallback(window, hookCallback);
};
}
public string FindGitExecutable()
{
var reg = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine,
Microsoft.Win32.RegistryView.Registry64);
var git = reg.OpenSubKey(@"SOFTWARE\GitForWindows");
if (git?.GetValue("InstallPath") is string installPath)
return Path.Combine(installPath, "bin", "git.exe");
var builder = new StringBuilder("git.exe", 259);
if (!PathFindOnPath(builder, null))
return null;
var exePath = builder.ToString();
if (!string.IsNullOrEmpty(exePath))
return exePath;
return null;
}
public string FindTerminal(Models.ShellOrTerminal shell)
{
switch (shell.Type)
{
case "git-bash":
if (string.IsNullOrEmpty(OS.GitExecutable))
break;
var binDir = Path.GetDirectoryName(OS.GitExecutable)!;
var bash = Path.GetFullPath(Path.Combine(binDir, "..", "git-bash.exe"));
if (!File.Exists(bash))
break;
return bash;
case "pwsh":
var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine,
Microsoft.Win32.RegistryView.Registry64);
var pwsh = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\pwsh.exe");
if (pwsh != null)
{
var path = pwsh.GetValue(null) as string;
if (File.Exists(path))
return path;
}
var pwshFinder = new StringBuilder("powershell.exe", 512);
if (PathFindOnPath(pwshFinder, null))
return pwshFinder.ToString();
break;
case "cmd":
return @"C:\Windows\System32\cmd.exe";
case "wt":
var wtFinder = new StringBuilder("wt.exe", 512);
if (PathFindOnPath(wtFinder, null))
return wtFinder.ToString();
break;
}
return string.Empty;
}
public List<Models.ExternalTool> FindExternalTools()
{
var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var finder = new Models.ExternalToolsFinder();
finder.VSCode(FindVSCode);
finder.VSCodeInsiders(FindVSCodeInsiders);
finder.VSCodium(FindVSCodium);
finder.Cursor(() => Path.Combine(localAppDataDir, @"Programs\Cursor\Cursor.exe"));
finder.Fleet(() => Path.Combine(localAppDataDir, @"Programs\Fleet\Fleet.exe"));
finder.FindJetBrainsFromToolbox(() => Path.Combine(localAppDataDir, @"JetBrains\Toolbox"));
finder.SublimeText(FindSublimeText);
finder.Zed(FindZed);
FindVisualStudio(finder);
return finder.Tools;
}
public void OpenBrowser(string url)
{
var info = new ProcessStartInfo("cmd", $"""/c start "" {url.Quoted()}""");
info.CreateNoWindow = true;
Process.Start(info);
}
public void OpenTerminal(string workdir)
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var cwd = string.IsNullOrEmpty(workdir) ? home : workdir;
var terminal = OS.ShellOrTerminal;
if (!File.Exists(terminal))
{
App.RaiseException(workdir, "Terminal is not specified! Please confirm that the correct shell/terminal has been configured.");
return;
}
var startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = cwd;
startInfo.FileName = terminal;
// Directly launching `Windows Terminal` need to specify the `-d` parameter
if (terminal.EndsWith("wt.exe", StringComparison.OrdinalIgnoreCase))
startInfo.Arguments = $"-d {cwd.Quoted()}";
Process.Start(startInfo);
}
public void OpenInFileManager(string path, bool select)
{
string fullpath;
if (File.Exists(path))
{
fullpath = new FileInfo(path).FullName;
select = true;
}
else
{
fullpath = new DirectoryInfo(path!).FullName;
fullpath += Path.DirectorySeparatorChar;
}
if (select)
{
OpenFolderAndSelectFile(fullpath);
}
else
{
Process.Start(new ProcessStartInfo(fullpath)
{
UseShellExecute = true,
CreateNoWindow = true,
});
}
}
public void OpenWithDefaultEditor(string file)
{
var info = new FileInfo(file);
var start = new ProcessStartInfo("cmd", $"""/c start "" {info.FullName.Quoted()}""");
start.CreateNoWindow = true;
Process.Start(start);
}
private void FixWindowFrameOnWin10(Window w)
{
// Schedule the DWM frame extension to run in the next render frame
// to ensure proper timing with the window initialization sequence
Dispatcher.UIThread.Post(() =>
{
var platformHandle = w.TryGetPlatformHandle();
if (platformHandle == null)
return;
var margins = new MARGINS { cxLeftWidth = 1, cxRightWidth = 1, cyTopHeight = 1, cyBottomHeight = 1 };
DwmExtendFrameIntoClientArea(platformHandle.Handle, ref margins);
}, DispatcherPriority.Render);
}
private PixelPoint IntPtrToPixelPoint(IntPtr param)
{
var v = IntPtr.Size == 4 ? param.ToInt32() : (int)(param.ToInt64() & 0xFFFFFFFF);
return new PixelPoint((short)(v & 0xffff), (short)(v >> 16));
}
private static PixelPoint PointToClient(PixelPoint screenPoint, RECT windowRect)
{
return new PixelPoint(
screenPoint.X - windowRect.left,
screenPoint.Y - windowRect.top);
}
private static Control FindParentButton(Visual visual)
{
while (visual != null)
{
if (visual is Button button)
return button;
visual = visual.GetVisualParent();
}
return null;
}
private static Control GetButtonAtPoint(Window window, PixelPoint screenPoint, RECT windowRect)
{
var clientPos = PointToClient(screenPoint, windowRect);
var point = new Point(clientPos.X / window.RenderScaling, clientPos.Y / window.RenderScaling);
var visual = window.GetVisualAt(point, v =>
{
if (v is IInputElement ie && (!ie.IsHitTestVisible || !ie.IsEffectivelyVisible))
return false;
return true;
});
return FindParentButton(visual);
}
private static Win32HitTestHelper.HitTestValue HitTestVisual(Window window, PixelPoint screenPoint, RECT windowRect)
{
var clientPos = new PixelPoint(screenPoint.X - windowRect.left, screenPoint.Y - windowRect.top);
var point = new Point(clientPos.X / window.RenderScaling, clientPos.Y / window.RenderScaling);
var visual = window.GetVisualAt(point, v =>
{
if (v is IInputElement ie && (!ie.IsHitTestVisible || !ie.IsEffectivelyVisible))
return false;
return true;
});
return visual != null ? Win32HitTestHelper.GetHitTestResult(visual) : Win32HitTestHelper.HitTestValue.Client;
}
private static bool IsCaptionButton(Win32HitTestHelper.HitTestValue hitTest)
{
return hitTest is Win32HitTestHelper.HitTestValue.MinButton
or Win32HitTestHelper.HitTestValue.MaxButton
or Win32HitTestHelper.HitTestValue.Close;
}
#region EXTERNAL_EDITOR_FINDER
private string FindVSCode()
{
var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine,
Microsoft.Win32.RegistryView.Registry64);
// VSCode (system)
var systemVScode = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{EA457B21-F73E-494C-ACAB-524FDE069978}_is1");
if (systemVScode != null)
return systemVScode.GetValue("DisplayIcon") as string;
var currentUser = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.CurrentUser,
Microsoft.Win32.RegistryView.Registry64);
// VSCode (user)
var vscode = currentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{771FD6B0-FA20-440A-A002-3B3BAC16DC50}_is1");
if (vscode != null)
return vscode.GetValue("DisplayIcon") as string;
return string.Empty;
}
private string FindVSCodeInsiders()
{
var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine,
Microsoft.Win32.RegistryView.Registry64);
// VSCode - Insiders (system)
var systemVScodeInsiders = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{1287CAD5-7C8D-410D-88B9-0D1EE4A83FF2}_is1");
if (systemVScodeInsiders != null)
return systemVScodeInsiders.GetValue("DisplayIcon") as string;
var currentUser = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.CurrentUser,
Microsoft.Win32.RegistryView.Registry64);
// VSCode - Insiders (user)
var vscodeInsiders = currentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{217B4C08-948D-4276-BFBB-BEE930AE5A2C}_is1");
if (vscodeInsiders != null)
return vscodeInsiders.GetValue("DisplayIcon") as string;
return string.Empty;
}
private string FindVSCodium()
{
var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine,
Microsoft.Win32.RegistryView.Registry64);
// VSCodium (system)
var systemVSCodium = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{88DA3577-054F-4CA1-8122-7D820494CFFB}_is1");
if (systemVSCodium != null)
return systemVSCodium.GetValue("DisplayIcon") as string;
var currentUser = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.CurrentUser,
Microsoft.Win32.RegistryView.Registry64);
// VSCodium (user)
var vscodium = currentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{2E1F05D1-C245-4562-81EE-28188DB6FD17}_is1");
if (vscodium != null)
return vscodium.GetValue("DisplayIcon") as string;
return string.Empty;
}
private string FindSublimeText()
{
var localMachine = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.LocalMachine,
Microsoft.Win32.RegistryView.Registry64);
// Sublime Text 4
var sublime = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Sublime Text_is1");
if (sublime != null)
{
var icon = sublime.GetValue("DisplayIcon") as string;
return Path.Combine(Path.GetDirectoryName(icon)!, "subl.exe");
}
// Sublime Text 3
var sublime3 = localMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Sublime Text 3_is1");
if (sublime3 != null)
{
var icon = sublime3.GetValue("DisplayIcon") as string;
return Path.Combine(Path.GetDirectoryName(icon)!, "subl.exe");
}
return string.Empty;
}
private void FindVisualStudio(Models.ExternalToolsFinder finder)
{
var vswhere = Environment.ExpandEnvironmentVariables(@"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe");
if (!File.Exists(vswhere))
return;
var startInfo = new ProcessStartInfo();
startInfo.FileName = vswhere;
startInfo.Arguments = "-format json -prerelease -utf8";
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.RedirectStandardOutput = true;
startInfo.StandardOutputEncoding = Encoding.UTF8;
try
{
using var proc = Process.Start(startInfo)!;
var output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
if (proc.ExitCode == 0)
{
var instances = JsonSerializer.Deserialize(output, JsonCodeGen.Default.ListVisualStudioInstance);
foreach (var instance in instances)
{
var exec = instance.ProductPath;
var icon = instance.IsPrerelease ? "vs-preview" : "vs";
finder.TryAdd(instance.DisplayName, icon, () => exec, GenerateCommandlineArgsForVisualStudio);
}
}
}
catch
{
// Just ignore.
}
}
private string FindZed()
{
var currentUser = Microsoft.Win32.RegistryKey.OpenBaseKey(
Microsoft.Win32.RegistryHive.CurrentUser,
Microsoft.Win32.RegistryView.Registry64);
// NOTE: this is the official Zed Preview reg data.
var preview = currentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{F70E4811-D0E2-4D88-AC99-D63752799F95}_is1");
if (preview != null)
return preview.GetValue("DisplayIcon") as string;
var findInPath = new StringBuilder("zed.exe", 512);
if (PathFindOnPath(findInPath, null))
return findInPath.ToString();
return string.Empty;
}
#endregion
private void OpenFolderAndSelectFile(string folderPath)
{
var pidl = ILCreateFromPathW(folderPath);
try
{
SHOpenFolderAndSelectItems(pidl, 0, 0, 0);
}
finally
{
ILFree(pidl);
}
}
private string GenerateCommandlineArgsForVisualStudio(string path)
{
if (Directory.Exists(path))
{
var sln = FindVSSolutionFile(new DirectoryInfo(path), 4);
return string.IsNullOrEmpty(sln) ? path.Quoted() : sln.Quoted();
}
return path.Quoted();
}
private string FindVSSolutionFile(DirectoryInfo dir, int leftDepth)
{
var files = dir.GetFiles();
foreach (var f in files)
{
if (f.Name.EndsWith(".slnx", StringComparison.OrdinalIgnoreCase) ||
f.Name.EndsWith(".sln", StringComparison.OrdinalIgnoreCase))
return f.FullName;
}
if (leftDepth <= 0)
return null;
var subDirs = dir.GetDirectories();
foreach (var subDir in subDirs)
{
var first = FindVSSolutionFile(subDir, leftDepth - 1);
if (!string.IsNullOrEmpty(first))
return first;
}
return null;
}
}
}