using System.Diagnostics; using System.Runtime.InteropServices; using System.Text.Json; using Windows.ApplicationModel; internal static class Program { [DllImport("user32.dll")] static extern short GetAsyncKeyState(int key); [DllImport("user32.dll")] static extern bool GetCursorPos(out Point point); [DllImport("user32.dll")] static extern bool SetProcessDpiAwarenessContext(IntPtr context); static readonly object OutputLock = new(); static void Write(object data) { lock (OutputLock) { Console.WriteLine(JsonSerializer.Serialize(data)); Console.Out.Flush(); } } [STAThread] static void Main(string[] args) { SetProcessDpiAwarenessContext(new IntPtr(-4)); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); using var dispatch = new Control(); dispatch.CreateControl(); var timer = new System.Windows.Forms.Timer { Interval = 35 }; bool monitor = false, wasDown = false, dragging = false; Point origin = default; string[] dragFiles = []; timer.Tick += (_, _) => { bool down = (GetAsyncKeyState(1) & 0x8000) != 0; GetCursorPos(out var point); if (monitor && down && !wasDown) { origin = point; dragFiles = Selection(); } if (monitor && down && !dragging && Math.Abs(point.X - origin.X) + Math.Abs(point.Y - origin.Y) > 24) { var currentSelection = Selection(); if (currentSelection.Length > 0) dragFiles = currentSelection; if (dragFiles.Length == 0) { wasDown = down; return; } dragging = true; Write(new { type = "drag", phase = "start", x = point.X, y = point.Y, files = dragFiles, tools = (GetAsyncKeyState(18) & 0x8000) != 0 }); } if (!down && wasDown && dragging) { Write(new { type = "drag", phase = "drop", x = point.X, y = point.Y, files = dragFiles }); dragging = false; dragFiles = []; } wasDown = down; }; timer.Start(); _ = Task.Run(() => { string? line; while ((line = Console.ReadLine()) != null) { string request = line; dispatch.BeginInvoke(new Action(async () => { int id = 0; try { using var doc = JsonDocument.Parse(request); var value = doc.RootElement; id = value.GetProperty("id").GetInt32(); string command = value.GetProperty("command").GetString() ?? ""; object result; switch (command) { case "selection": result = Selection(); break; case "monitor": monitor = value.GetProperty("enabled").GetBoolean(); result = monitor; break; case "identity": try { result = new { packaged = true, family = Package.Current.Id.FamilyName }; } catch { result = new { packaged = false, family = "" }; } break; case "startup": var task = await StartupTask.GetAsync("FileBowStartup"); if (value.TryGetProperty("enabled", out var enabled)) { if (enabled.GetBoolean()) await task.RequestEnableAsync(); else task.Disable(); } result = task.State.ToString(); break; default: throw new InvalidOperationException("Unknown command."); } Write(new { id, result }); } catch (Exception ex) { Write(new { id, error = ex.Message }); } })); } dispatch.BeginInvoke(new Action(Application.Exit)); }); Write(new { type = "ready" }); Application.Run(); } static string[] Selection() => ExplorerSelection.Read(); }