Repository navigation
Expand file tree
/
Copy pathApp.xaml.cs
More file actions
420 lines (371 loc) · 17.8 KB
/
Copy pathApp.xaml.cs
File metadata and controls
420 lines (371 loc) · 17.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
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
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Win32;
using TaskbarTYOL.Models;
using TaskbarTYOL.Native;
using TaskbarTYOL.Services;
using TaskbarTYOL.Views;
namespace TaskbarTYOL;
public partial class App : Application
{
public static new App Current => (App)Application.Current;
public Settings Settings { get; private set; } = null!;
internal WindowTracker Tracker { get; private set; } = null!;
internal TrayService? Tray { get; private set; }
/// <summary>All taskbars, primary monitor first.</summary>
public List<TaskbarWindow> Taskbars { get; } = new();
public TaskbarWindow? Taskbar => Taskbars.FirstOrDefault();
public StartMenuWindow? StartMenu { get; private set; }
public SearchWindow? Search { get; private set; }
public TrayFlyoutWindow? TrayFlyout { get; private set; }
private SettingsWindow? _settingsWin;
/// <summary>True once shutdown began — lets popup windows that normally refuse to close actually close.</summary>
public bool IsShuttingDown { get; private set; }
private const string ExitEventName = "TaskbarTYOL_ExitRequest";
private Mutex? _mutex;
private EventWaitHandle? _exitEvent;
private WinKeyHook? _hook;
private DesktopRightClickHook? _desktopHook;
private ExplorerInjector? _injector;
private DispatcherTimer? _enforceTimer;
private DispatcherTimer? _displayDebounce;
private DateTime _lastErrorDialog = DateTime.MinValue;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// "TaskbarTYOL.exe --exit" asks a running instance to quit gracefully (restoring the Windows taskbar).
if (e.Args.Any(a => a.Equals("--exit", StringComparison.OrdinalIgnoreCase)))
{
try { using var evt = EventWaitHandle.OpenExisting(ExitEventName); evt.Set(); } catch { /* not running */ }
Shutdown();
return;
}
_mutex = new Mutex(true, "TaskbarTYOL_SingleInstance", out bool createdNew);
if (!createdNew)
{
// Started twice (e.g. autostart + manual launch) → just go away quietly, like Explorer would.
_mutex = null;
Shutdown();
return;
}
_exitEvent = new EventWaitHandle(false, EventResetMode.AutoReset, ExitEventName);
var exitWaiter = new Thread(() => { _exitEvent.WaitOne(); Dispatcher.BeginInvoke(ExitApp); }) { IsBackground = true, Name = "exit-listener" };
exitWaiter.Start();
// Never leave the user without a taskbar if we crash.
AppDomain.CurrentDomain.UnhandledException += (_, args) => { EmergencyRestore(); if (args.ExceptionObject is Exception ex) LogError(ex); };
DispatcherUnhandledException += (_, args) =>
{
args.Handled = true;
LogError(args.Exception);
// A fault in a 400ms/1s timer would otherwise pop a new modal dialog every tick, stacking dozens of
// blocking boxes and freezing the app. Always log; surface a dialog at most once per 30s.
if ((DateTime.UtcNow - _lastErrorDialog).TotalSeconds >= 30)
{
_lastErrorDialog = DateTime.UtcNow;
MessageBox.Show(args.Exception.Message + "\n\nDetails were written to " + ErrorLogPath,
"TaskbarTYOL error", MessageBoxButton.OK, MessageBoxImage.Error);
}
};
SessionEnding += (_, _) => { IsShuttingDown = true; EmergencyRestore(); };
Settings = Settings.Load();
ImportExplorerPinsOnce();
Task.Run(ExplorerIntegration.CleanupDeadRegistration); // scrub dead SSO entries left by earlier builds
// Keep the Windows right-click menu in agreement with our settings (registry I/O off the startup path).
Task.Run(() =>
{
try { ContextMenuManager.SetClassicMenu(Settings.ClassicContextMenu); ContextMenuManager.Sync(Settings); }
catch (Exception ex) { LogError(ex); }
});
Tracker = new WindowTracker(Settings);
ThemeManager.Apply(Settings.Theme);
// Hide Explorer's taskbar FIRST so its strip is no longer reserved when our AppBars claim the edge.
if (Settings.HideNativeTaskbar) NativeTaskbar.Hide(Settings);
CreateTaskbars();
Tracker.Start();
ApplyBehavior();
foreach (var bar in Taskbars) bar.ApplyFromSettings(); // bars must learn that the tray host / hook now exist
// Startup diagnostics (only written when %AppData%\TaskbarTYOL\debug.log exists).
if (Taskbar != null)
Taskbar.ContentRendered += (_, _) =>
Log($"bar rendered {(DateTime.Now - System.Diagnostics.Process.GetCurrentProcess().StartTime).TotalMilliseconds:F0} ms after process start");
// Warm the start-menu index a few seconds after we are up, so the first Win-key press is instant.
var prewarm = new DispatcherTimer { Interval = TimeSpan.FromSeconds(4) };
prewarm.Tick += (_, _) => { prewarm.Stop(); AppIndexer.PrewarmAsync(); };
prewarm.Start();
// Inject the supervisor into explorer.exe so the bar lives with the shell.
TryInject();
_enforceTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
_enforceTimer.Tick += (_, _) => Enforce();
_enforceTimer.Start();
// Monitors plugged / unplugged / resolution changed → rebuild bars (debounced, the event fires in bursts).
_displayDebounce = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(800) };
_displayDebounce.Tick += (_, _) =>
{
_displayDebounce.Stop();
CreateTaskbars();
// New bar objects must learn the current settings and re-establish the tray-rect provider on the new
// primary bar (otherwise it still points at the old, now-closed bar).
foreach (var bar in Taskbars) bar.ApplyFromSettings();
};
SystemEvents.DisplaySettingsChanged += (_, _) => Dispatcher.BeginInvoke(() => { _displayDebounce.Stop(); _displayDebounce.Start(); });
}
private void Enforce()
{
if (!Settings.HideNativeTaskbar) return;
bool explorerCameBack = NativeTaskbar.Enforce(Settings);
if (explorerCameBack)
{
// Explorer restarted: re-claim the screen edge and pull the tray icons back from its brand-new tray window.
foreach (var bar in Taskbars) bar.Reposition();
if (Tray is { IsRunning: true }) { Tray.EnsureOnTop(); Tray.BroadcastTaskbarCreated(); }
// The old hook died with the old explorer.exe (its handle is now stale). Drop it; the retry below hooks
// the new shell over the next ticks (GetShellWindow may not point at the new explorer immediately).
_injector?.Uninstall();
}
else if (Tray is { IsRunning: true } && Tray.EnsureOnTop())
{
Tray.BroadcastTaskbarCreated();
}
// Keep the hook attached to the current shell (retries harmlessly until it sticks; no-op once installed).
if (Settings.InjectIntoExplorer && _injector is { IsInstalled: false }) TryInject();
}
/// <summary>Human-readable state of the explorer.exe injection, for the settings window.</summary>
public string ExplorerHookStatus
{
get
{
if (!Settings.InjectIntoExplorer) return "Off. The bar runs as its own process (still started with the shell by the logon task).";
if (_injector is not { DllPresent: true }) return "The TaskbarTYOLHook.dll module is not installed next to the app. Run install.ps1.";
return _injector.IsInstalled
? "Active. TaskbarTYOL's code is running inside explorer.exe, keeping the Windows taskbar suppressed from the shell thread. It is removed cleanly when the app exits and nothing persists across a reboot."
: "Enabled, but not injected yet — it attaches once the shell is available.";
}
}
/// <summary>Install (or re-install) the explorer.exe supervisor hook, if enabled and the DLL shipped.</summary>
private void TryInject()
{
if (!Settings.InjectIntoExplorer) return;
_injector ??= new ExplorerInjector();
if (!_injector.DllPresent || _injector.IsInstalled) return;
try
{
if (_injector.Install()) Log("explorer hook installed");
else Log("explorer hook install failed (err " + Marshal.GetLastWin32Error() + ")");
}
catch (Exception ex) { LogError(ex); }
}
private void CreateTaskbars()
{
foreach (var bar in Taskbars) bar.Close();
Taskbars.Clear();
var monitors = Monitors.All();
if (monitors.Count == 0) return;
foreach (var mon in monitors)
{
if (!mon.IsPrimary && !Settings.ShowOnAllMonitors) continue;
var bar = new TaskbarWindow(mon);
bar.Show();
Taskbars.Add(bar);
}
}
private void ApplyBehavior()
{
if (Settings.HideNativeTaskbar) NativeTaskbar.Hide(Settings); else NativeTaskbar.Show(Settings);
// Tray hijack only makes sense while Explorer's taskbar is out of the way.
bool wantTray = Settings.HideNativeTaskbar && Settings.ShowTrayIcons;
if (wantTray && Tray is not { IsRunning: true })
{
try { Tray = new TrayService(); Tray.Start(); }
catch (Exception ex) { LogError(ex); Tray = null; }
}
else if (!wantTray && Tray != null)
{
Tray.Dispose();
Tray = null;
}
if (Settings.ReplaceWinKey)
{
if (_hook == null)
{
_hook = new WinKeyHook();
_hook.WinKeyPressed += () => Dispatcher.BeginInvoke(() => ToggleStartMenu());
_hook.SearchRequested += () => Dispatcher.BeginInvoke(() => ToggleSearch());
_hook.Install();
}
_hook.Enabled = true;
}
else if (_hook != null) _hook.Enabled = false;
// Themed desktop right-click menu: install the mouse hook on demand.
if (Settings.ThemedDesktopMenu)
{
if (_desktopHook == null)
{
_desktopHook = new DesktopRightClickHook();
_desktopHook.Triggered += () => Dispatcher.BeginInvoke(() => DesktopMenu.ShowAtCursor(Settings));
_desktopHook.Install();
}
_desktopHook.Enabled = true;
}
else if (_desktopHook != null) _desktopHook.Enabled = false;
// Autostart via a logon scheduled task (starts with the shell, restarts itself on failure). Registration
// runs schtasks → keep it off the startup path.
bool wantAutostart = Settings.StartWithWindows;
Task.Run(() =>
{
try
{
bool enabled = StartupManager.IsEnabled();
if (enabled != wantAutostart || (wantAutostart && !StartupManager.IsCurrent()))
StartupManager.SetEnabled(wantAutostart);
}
catch (Exception ex) { LogError(ex); }
});
// Apply the explorer-injection toggle live.
if (Settings.InjectIntoExplorer) TryInject();
else if (_injector is { IsInstalled: true }) { _injector.Uninstall(); Log("explorer hook removed by settings"); }
}
/// <summary>Persist settings and push every change live into the running UI.</summary>
public void ApplySettings()
{
Settings.Save();
if (!string.Equals(ThemeManager.CurrentName, Settings.Theme, StringComparison.OrdinalIgnoreCase))
ThemeManager.Apply(Settings.Theme);
ApplyBehavior(); // native taskbar hide/show first, so the bars below re-claim the correct edge
Tray?.ApplyPromotion();
bool wantSecondary = Settings.ShowOnAllMonitors && Monitors.All().Count > 1;
bool haveSecondary = Taskbars.Count > 1;
if (wantSecondary != haveSecondary) CreateTaskbars();
else foreach (var bar in Taskbars) bar.ApplyFromSettings();
Tracker.Refresh();
}
/// <summary>Opens/closes the start menu on the given bar (or the bar under the mouse / primary).</summary>
public void ToggleStartMenu(TaskbarWindow? bar = null)
{
bar ??= BarUnderMouse() ?? Taskbar;
if (bar == null) return;
StartMenu ??= new StartMenuWindow();
if (StartMenu.IsVisible || StartMenu.JustDeactivated) StartMenu.Hide();
else { Search?.Hide(); StartMenu.ShowAt(bar); }
}
/// <summary>Opens/closes the Everything-powered search on the given bar.</summary>
public void ToggleSearch(TaskbarWindow? bar = null)
{
bar ??= BarUnderMouse() ?? Taskbar;
if (bar == null) return;
Search ??= new SearchWindow();
if (Search.IsVisible || Search.JustDeactivated) Search.Hide();
else { StartMenu?.Hide(); Search.ShowAt(bar); }
}
/// <summary>Opens/closes the "^" overflow flyout for hidden notification icons.</summary>
public void ToggleTrayFlyout(TaskbarWindow bar)
{
TrayFlyout ??= new TrayFlyoutWindow();
if (TrayFlyout.IsVisible || TrayFlyout.JustDeactivated) TrayFlyout.Hide();
else { StartMenu?.Hide(); Search?.Hide(); TrayFlyout.ShowAt(bar, bar.GetChevronRectDip()); }
}
/// <summary>Is this notification icon shown on the bar (true) or parked in the overflow flyout (false)?</summary>
public bool IsTrayPromoted(TrayIcon icon) =>
Settings.TrayShowAll || Settings.TrayPromoted.Contains(icon.PersistKey, StringComparer.OrdinalIgnoreCase);
/// <summary>Move an icon between the bar and the overflow flyout, persistently.</summary>
public void PromoteTrayIcon(string persistKey, bool promote)
{
Settings.TrayPromoted.RemoveAll(k => string.Equals(k, persistKey, StringComparison.OrdinalIgnoreCase));
if (promote) Settings.TrayPromoted.Add(persistKey);
Settings.Save();
Tray?.ApplyPromotion();
foreach (var bar in Taskbars) bar.ApplyFromSettings();
if (TrayFlyout is { IsVisible: true } && Tray != null && !Tray.Icons.Any(i => !i.IsPromoted && !i.IsHidden)) TrayFlyout.Hide();
}
private TaskbarWindow? BarUnderMouse()
{
if (Taskbars.Count <= 1) return Taskbar;
Win32.GetCursorPos(out var p);
return Taskbars.FirstOrDefault(b => p.X >= b.Monitor.Bounds.Left && p.X < b.Monitor.Bounds.Right
&& p.Y >= b.Monitor.Bounds.Top && p.Y < b.Monitor.Bounds.Bottom);
}
public void OpenSettings()
{
if (_settingsWin == null || !_settingsWin.IsLoaded)
{
_settingsWin = new SettingsWindow();
_settingsWin.Closed += (_, _) => _settingsWin = null;
}
_settingsWin.Show();
_settingsWin.Activate();
}
public void ExitApp()
{
// Clean exit → exit code 0 → the logon task's restart-on-failure does NOT fire, and the hook (owned by this
// process) is unmapped from explorer.exe automatically. The bar stays gone until next sign-in.
IsShuttingDown = true;
Shutdown();
}
/// <summary>First run: adopt the shortcuts the user had pinned to the Windows taskbar.</summary>
private void ImportExplorerPinsOnce()
{
if (Settings.ImportedExplorerPins) return;
try
{
foreach (var lnk in ExplorerIntegration.ExplorerPinnedShortcuts())
{
var target = ShortcutResolver.GetTarget(lnk) ?? lnk;
bool already = Settings.Pinned.Any(p =>
string.Equals(p, lnk, StringComparison.OrdinalIgnoreCase) ||
string.Equals(ShortcutResolver.GetTarget(p) ?? p, target, StringComparison.OrdinalIgnoreCase));
if (!already) Settings.Pinned.Add(lnk);
}
}
catch (Exception ex) { LogError(ex); }
Settings.ImportedExplorerPins = true;
Settings.Save();
}
/// <summary>Best-effort restoration used from crash / logoff paths.</summary>
private void EmergencyRestore()
{
try { Tray?.Dispose(); } catch { /* ignore */ }
try { NativeTaskbar.Show(Settings); } catch { /* ignore */ }
}
public static string ErrorLogPath => System.IO.Path.Combine(Settings.Folder, "error.log");
public static string DebugLogPath => System.IO.Path.Combine(Settings.Folder, "debug.log");
/// <summary>Lightweight diagnostics (drag/drop, tray protocol…). Only written when debug.log already exists.</summary>
public static void Log(string message)
{
try
{
if (!System.IO.File.Exists(DebugLogPath)) return;
System.IO.File.AppendAllText(DebugLogPath, $"[{DateTime.Now:HH:mm:ss.fff}] {message}\n");
}
catch { /* ignore */ }
}
public static void LogError(Exception ex)
{
try
{
System.IO.Directory.CreateDirectory(Settings.Folder);
System.IO.File.AppendAllText(ErrorLogPath, $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {ex}\n\n");
}
catch { /* nothing more we can do */ }
}
protected override void OnExit(ExitEventArgs e)
{
IsShuttingDown = true;
try
{
_enforceTimer?.Stop();
Tracker?.Stop();
_hook?.Dispose();
_desktopHook?.Dispose();
_injector?.Dispose(); // remove our DLL from explorer.exe (clean unmap, no thread left)
foreach (var bar in Taskbars) bar.Close(); // ABM_REMOVE → shell recomputes the work area
Tray?.Dispose(); // hands the notification icons back to Explorer
NativeTaskbar.Show(Settings);
Settings?.Save();
}
finally
{
_mutex?.ReleaseMutex();
base.OnExit(e);
}
}
}