Merge branch 'fix/shell-notify': taskbar progress, OS notifications, shortcuts, job notices, badges, General settings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
Claude committed 2026-10-12 11:13:04 +00:00
commit 979a27e3d3
117 files changed
+1807 -190

No files matched your search

+7
View File
@@ -22,6 +22,13 @@ public sealed class Toast : ObservableObject
/// <summary>Something failed: screen readers say it at once, ahead of other notices (assertive, not polite).</summary>
public bool IsError { get; init; }
/// <summary>
/// A click on the toast's Windows notification also runs its action (it opens what the toast names). Off by
/// default: people click a notification to bring the window back, so a Cancel, Undo or Retry there would act
/// before they have read why.
/// </summary>
public bool RunsFromNotification { get; init; }
}
/// <summary>
+267
View File
@@ -0,0 +1,267 @@
using System.Runtime.InteropServices;
using System.Windows.Interop;
using Titano.Core.Diagnostics;
using Titano.Core.Localization;
namespace Titano.Ui.Interop;
/// <summary>One entry of the tray menu; an id of 0 is a separator.</summary>
public readonly record struct TrayMenuItem(int Id, string? Text);
/// <summary>
/// Notification-area icon through <c>Shell_NotifyIconW</c>: the program icon from the executable's resources, a tooltip,
/// a native context menu and balloons (Windows notifications on Windows 10/11). A hidden window receives the callbacks
/// and re-adds the icon after Explorer restarts. Used by the Uploader (tray) and the client (notifications while
/// minimised); create, use and dispose it on the UI thread.
/// </summary>
public sealed unsafe partial class TrayIcon : IDisposable
{
private const uint NimAdd = 0;
private const uint NimModify = 1;
private const uint NimDelete = 2;
private const uint NifMessage = 0x1;
private const uint NifIcon = 0x2;
private const uint NifTip = 0x4;
private const uint NifInfo = 0x10;
private const uint NiifError = 0x3;
private const uint NiifUser = 0x4;
private const int CallbackMessage = 0x8000 + 1; // WM_APP + 1
private const int WmContextMenu = 0x007B;
private const int WmLButtonUp = 0x0202;
private const int WmLButtonDblClk = 0x0203;
private const int WmRButtonUp = 0x0205;
private const int NinBalloonUserClick = 0x0405;
private const uint MfString = 0x0;
private const uint MfSeparator = 0x800;
private const uint TpmRightButton = 0x2;
private const uint TpmNoNotify = 0x80;
private const uint TpmReturnCmd = 0x100;
private const uint TpmLayoutRtl = 0x8000;
private readonly HwndSource _window;
private readonly IntPtr _icon;
private readonly bool _ownsIcon;
private readonly uint _taskbarCreated;
private readonly Func<IReadOnlyList<TrayMenuItem>> _menu;
private readonly Action<int> _command;
private readonly Action _show;
private readonly Action? _balloonClicked;
private string _toolTip;
private bool _added;
/// <param name="toolTip">Hover text (up to 127 characters).</param>
/// <param name="menu">Builds the context menu when it opens.</param>
/// <param name="command">Runs a chosen menu id.</param>
/// <param name="show">Left click, and balloon click when <paramref name="balloonClicked"/> is null.</param>
/// <param name="balloonClicked">Click on the last balloon (or its Windows notification).</param>
public TrayIcon(string toolTip, Func<IReadOnlyList<TrayMenuItem>> menu, Action<int> command, Action show, Action? balloonClicked = null)
{
_toolTip = toolTip;
_menu = menu;
_command = command;
_show = show;
_balloonClicked = balloonClicked;
// A hidden top-level window: message-only windows miss the TaskbarCreated broadcast.
_window = new HwndSource(new HwndSourceParameters("TitanoTray") { Width = 0, Height = 0, WindowStyle = 0 });
_window.AddHook(WndProc);
_taskbarCreated = RegisterWindowMessageW("TaskbarCreated");
(_icon, _ownsIcon) = LoadProgramIcon();
Add();
}
public void SetToolTip(string text)
{
_toolTip = text;
var data = NewData(NifTip);
Copy(_toolTip, data.Tip, 128);
Notify(NimModify, &data);
}
/// <summary>
/// Shows a balloon (a Windows notification on Windows 10/11) with the program icon, or the error icon when
/// <paramref name="isError"/>. <paramref name="text"/> must not be empty: an empty text removes the balloon.
/// </summary>
public void ShowBalloon(string title, string text, bool isError = false)
{
var data = NewData(NifInfo);
Copy(title, data.InfoTitle, 64);
Copy(text, data.Info, 256);
data.InfoFlags = isError ? NiifError : NiifUser;
Notify(NimModify, &data);
}
public void Dispose()
{
if (_added)
{
var data = NewData(0);
Notify(NimDelete, &data);
_added = false;
}
if (_ownsIcon) DestroyIcon(_icon);
_window.Dispose();
}
private void Add()
{
var data = NewData(NifMessage | NifIcon | NifTip);
data.CallbackMessage = CallbackMessage;
data.Icon = _icon;
Copy(_toolTip, data.Tip, 128);
_added = Notify(NimAdd, &data);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == CallbackMessage)
{
switch ((int)(lParam.ToInt64() & 0xFFFF))
{
case WmLButtonUp or WmLButtonDblClk:
_show();
break;
case NinBalloonUserClick:
(_balloonClicked ?? _show)();
break;
case WmRButtonUp or WmContextMenu:
ShowMenu();
break;
}
handled = true;
}
else if (_taskbarCreated != 0 && msg == (int)_taskbarCreated)
{
Add();
}
return IntPtr.Zero;
}
private void ShowMenu()
{
var menu = CreatePopupMenu();
if (menu == IntPtr.Zero) return;
try
{
foreach (var item in _menu())
{
if (item.Id == 0 || item.Text is null) AppendMenuW(menu, MfSeparator, 0, null);
else AppendMenuW(menu, MfString, (nuint)item.Id, item.Text);
}
GetCursorPos(out var point);
// Without foreground the menu would not close when the user clicks elsewhere.
SetForegroundWindow(_window.Handle);
// Right-to-left languages get a mirrored menu, as the window itself is (BRIEF §16).
var layout = Loc.IsRightToLeft ? TpmLayoutRtl : 0;
var chosen = TrackPopupMenuEx(menu, TpmReturnCmd | TpmRightButton | TpmNoNotify | layout, point.X, point.Y, _window.Handle, IntPtr.Zero);
PostMessageW(_window.Handle, 0, IntPtr.Zero, IntPtr.Zero);
if (chosen != 0) _command(chosen);
}
finally
{
DestroyMenu(menu);
}
}
private NotifyIconData NewData(uint flags) => new()
{
Size = (uint)sizeof(NotifyIconData),
Window = _window.Handle,
Id = 1,
Flags = flags,
};
private static bool Notify(uint message, NotifyIconData* data)
{
if (Shell_NotifyIconW(message, data)) return true;
Log.Info("tray", $"Shell_NotifyIcon({message}) failed");
return false;
}
private static void Copy(string text, char* destination, int capacity)
{
var length = Math.Min(text.Length, capacity - 1);
text.AsSpan(0, length).CopyTo(new Span<char>(destination, capacity));
destination[length] = '\0';
}
/// <summary>The program's generated icon embedded in its executable; the stock icon if it cannot be read.</summary>
private static (IntPtr Icon, bool Owned) LoadProgramIcon()
{
if (Environment.ProcessPath is { } exe)
{
IntPtr small;
if (ExtractIconExW(exe, 0, null, &small, 1) > 0 && small != IntPtr.Zero) return (small, true);
}
return (LoadIconW(IntPtr.Zero, 32512), false); // IDI_APPLICATION
}
[StructLayout(LayoutKind.Sequential)]
private struct NotifyIconData
{
public uint Size;
public IntPtr Window;
public uint Id;
public uint Flags;
public uint CallbackMessage;
public IntPtr Icon;
public fixed char Tip[128];
public uint State;
public uint StateMask;
public fixed char Info[256];
public uint TimeoutOrVersion;
public fixed char InfoTitle[64];
public uint InfoFlags;
public Guid Item;
public IntPtr BalloonIcon;
}
[StructLayout(LayoutKind.Sequential)]
private struct Point
{
public int X;
public int Y;
}
[LibraryImport("shell32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool Shell_NotifyIconW(uint message, NotifyIconData* data);
[LibraryImport("shell32.dll", StringMarshalling = StringMarshalling.Utf16)]
private static partial uint ExtractIconExW(string file, int index, IntPtr* large, IntPtr* small, uint count);
[LibraryImport("user32.dll")]
private static partial IntPtr LoadIconW(IntPtr instance, nint name);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool DestroyIcon(IntPtr icon);
[LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16)]
private static partial uint RegisterWindowMessageW(string name);
[LibraryImport("user32.dll")]
private static partial IntPtr CreatePopupMenu();
[LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16)]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool AppendMenuW(IntPtr menu, uint flags, nuint id, string? text);
[LibraryImport("user32.dll")]
private static partial int TrackPopupMenuEx(IntPtr menu, uint flags, int x, int y, IntPtr window, IntPtr parameters);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool DestroyMenu(IntPtr menu);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool GetCursorPos(out Point point);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool SetForegroundWindow(IntPtr window);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool PostMessageW(IntPtr window, int message, IntPtr wParam, IntPtr lParam);
}
+4 -3
View File
@@ -42,8 +42,9 @@ public readonly record struct MotionVector(float X, float Y, float Dx, float Dy)
/// polyline) and a crop mask (normalised <see cref="CropRect"/>; dims or hides what the output loses). Tools go
/// bottom-left (<see cref="ToolsContent"/>), the shot data pill bottom-right (<see cref="InfoText"/>). Images and
/// overlays are retained visuals under one shared transform, so panning re-renders nothing. Double-click toggles
/// Fit and 1:1; Ctrl+0 fits, Ctrl+1 shows 1:1. Screen readers see an image (named by AutomationProperties.Name)
/// whose status is the zoom and whose help text is the shot data, holding the tools.
/// Fit and 1:1; Ctrl+0 fits, Ctrl+Shift+1 shows 1:1 (Ctrl+1 to Ctrl+6 switch the client's sections). Screen readers
/// see an image (named by AutomationProperties.Name) whose status is the zoom and whose help text is the shot data,
/// holding the tools.
/// </summary>
[TemplatePart(Name = PartSurface, Type = typeof(FrameworkElement))]
[TemplatePart(Name = PartTools, Type = typeof(FrameworkElement))]
@@ -55,7 +56,7 @@ public class PreviewViewport : Control
public const string PartInfo = "PART_Info";
public static readonly RoutedCommand FitCommand = new(nameof(FitCommand), typeof(PreviewViewport), [new KeyGesture(Key.D0, ModifierKeys.Control)]);
public static readonly RoutedCommand ActualSizeCommand = new(nameof(ActualSizeCommand), typeof(PreviewViewport), [new KeyGesture(Key.D1, ModifierKeys.Control)]);
public static readonly RoutedCommand ActualSizeCommand = new(nameof(ActualSizeCommand), typeof(PreviewViewport), [new KeyGesture(Key.D1, ModifierKeys.Control | ModifierKeys.Shift)]);
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(
nameof(Source), typeof(ImageSource), typeof(PreviewViewport), new PropertyMetadata(null, OnImagesChanged));