Repository navigation
Expand file tree
/
Copy pathFolderStackTaskbarLauncher.cs
More file actions
87 lines (80 loc) · 3.8 KB
/
Copy pathFolderStackTaskbarLauncher.cs
File metadata and controls
87 lines (80 loc) · 3.8 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
using System.Diagnostics;
using System.Runtime.InteropServices;
// A native Windows shortcut can be pinned to the taskbar. Its process lives only while the popup is open.
static class FolderStackTaskbarLauncher
{
public static void RunPopup() => Application.Run(new PopupContext());
public static string CreateShortcut()
{
string exe = Environment.ProcessPath ?? throw new InvalidOperationException("winBar-Pfad nicht gefunden.");
string directory = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
Directory.CreateDirectory(directory);
string shortcutPath = Path.Combine(directory, "winBar Ordnerstapel.lnk");
ShellShortcut.Save(shortcutPath, exe, "--folders",
Localization.T("Open winBar folder shelf", "winBar-Ordnerstapel öffnen"),
Path.Combine(Environment.SystemDirectory, "shell32.dll") + ",3");
return shortcutPath;
}
public static void RevealShortcut(string path)
{
Process.Start(new ProcessStartInfo("explorer.exe") { Arguments = $"/select,\"{path}\"", UseShellExecute = true });
}
sealed class PopupContext : ApplicationContext
{
readonly Settings settings = Settings.Load();
public PopupContext()
{
Localization.Configure(settings.Language);
Application.Idle += OpenOnIdle;
}
void OpenOnIdle(object? sender, EventArgs e)
{
Application.Idle -= OpenOnIdle;
var pointer = Cursor.Position;
var bar = Taskbar.VisibleBounds().FirstOrDefault(bounds => bounds.Contains(pointer));
if (bar == Rectangle.Empty)
bar = Taskbar.VisibleBounds().FirstOrDefault(bounds => Screen.FromRectangle(bounds).Primary);
if (bar == Rectangle.Empty)
{
var screen = Screen.PrimaryScreen!.WorkingArea;
bar = new Rectangle(screen.Left + screen.Width / 2 - 24, screen.Bottom - 48, 48, 48);
}
else if (bar.Width > bar.Height * 3)
{
bar = new Rectangle(Math.Clamp(pointer.X - 24, bar.Left, bar.Right - 48), bar.Top, 48, bar.Height);
}
FolderStackPopup.ShowFolders(settings.FolderStackPaths, bar);
var popup = Application.OpenForms.OfType<FolderStackPopup>().LastOrDefault();
if (popup == null) ExitThread();
else popup.FormClosed += (_, _) => ExitThread();
}
}
}
// Writes a .lnk file through Windows Script Host.
static class ShellShortcut
{
public static void Save(string shortcutPath, string target, string arguments, string description, string iconLocation)
{
var shellType = Type.GetTypeFromProgID("WScript.Shell") ?? throw new InvalidOperationException(
Localization.T("Windows Script Host is unavailable.", "Windows Script Host ist nicht verfügbar."));
object shell = Activator.CreateInstance(shellType)!;
object? shortcut = null;
try
{
shortcut = shellType.InvokeMember("CreateShortcut", System.Reflection.BindingFlags.InvokeMethod, null, shell, [shortcutPath]);
var type = shortcut!.GetType();
void Set(string name, object value) => type.InvokeMember(name, System.Reflection.BindingFlags.SetProperty, null, shortcut, [value]);
Set("TargetPath", target);
Set("Arguments", arguments);
Set("WorkingDirectory", Path.GetDirectoryName(target)!);
Set("Description", description);
Set("IconLocation", iconLocation);
type.InvokeMember("Save", System.Reflection.BindingFlags.InvokeMethod, null, shortcut, null);
}
finally
{
if (shortcut != null) Marshal.FinalReleaseComObject(shortcut);
Marshal.FinalReleaseComObject(shell);
}
}
}