From 94d7bc5af74c8a57d432849b2fdae4e5136cbb24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= <1005065+DeepDiver1975@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:55:51 +0100 Subject: [PATCH 1/3] feat(app): dial window, settings window and the OS tel: handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A tel: link opens a small topmost window with the number: Enter dials, Escape cancels, it closes itself after a successful dial and shows why otherwise. Started on its own, Wählhilfe opens the settings window: Bluetooth port, Ländervorwahl, Amtsholung, Verbindung testen, Testanruf, and whether it opens tel: links. The link arrives as the first argument on Windows and Linux, and as an Apple event (IActivatableLifetime) on macOS, where the settings open only when no link follows the launch. Windows opens the Default apps page for the registered capability; macOS sets the handler through Launch Services; Linux has no registration. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Thomas Müller <1005065+DeepDiver1975@users.noreply.github.com> --- src/Waehlhilfe.App/CompositionRoot.cs | 34 +++++++ src/Waehlhilfe.App/LaunchArguments.cs | 12 +++ .../Platform/MacTelHandlerRegistration.cs | 94 +++++++++++++++++++ .../UnsupportedTelHandlerRegistration.cs | 12 +++ .../Platform/WindowsTelHandlerRegistration.cs | 38 ++++++++ src/Waehlhilfe.App/Views/DialWindow.axaml | 32 +++++++ src/Waehlhilfe.App/Views/DialWindow.axaml.cs | 21 +++++ src/Waehlhilfe.App/Views/SettingsWindow.axaml | 66 ++++++++++++- src/Waehlhilfe.App/Waehlhilfe.App.csproj | 2 + src/Waehlhilfe.App/WaehlhilfeApp.axaml.cs | 45 ++++++++- tests/Waehlhilfe.App.Tests/DialWindowTests.cs | 94 +++++++++++++++++++ tests/Waehlhilfe.App.Tests/KeyboardInput.cs | 37 ++++++++ .../LaunchArgumentsTests.cs | 25 +++++ .../SettingsWindowTests.cs | 81 +++++++++++++++- tests/Waehlhilfe.App.Tests/TestAppBuilder.cs | 5 +- .../Waehlhilfe.App.Tests.csproj | 6 ++ tests/Waehlhilfe.App.Tests/packages.lock.json | 6 ++ 17 files changed, 601 insertions(+), 9 deletions(-) create mode 100644 src/Waehlhilfe.App/CompositionRoot.cs create mode 100644 src/Waehlhilfe.App/LaunchArguments.cs create mode 100644 src/Waehlhilfe.App/Platform/MacTelHandlerRegistration.cs create mode 100644 src/Waehlhilfe.App/Platform/UnsupportedTelHandlerRegistration.cs create mode 100644 src/Waehlhilfe.App/Platform/WindowsTelHandlerRegistration.cs create mode 100644 src/Waehlhilfe.App/Views/DialWindow.axaml create mode 100644 src/Waehlhilfe.App/Views/DialWindow.axaml.cs create mode 100644 tests/Waehlhilfe.App.Tests/DialWindowTests.cs create mode 100644 tests/Waehlhilfe.App.Tests/KeyboardInput.cs create mode 100644 tests/Waehlhilfe.App.Tests/LaunchArgumentsTests.cs diff --git a/src/Waehlhilfe.App/CompositionRoot.cs b/src/Waehlhilfe.App/CompositionRoot.cs new file mode 100644 index 0000000..437039e --- /dev/null +++ b/src/Waehlhilfe.App/CompositionRoot.cs @@ -0,0 +1,34 @@ +using Waehlhilfe.App.Platform; +using Waehlhilfe.AppLogic; +using Waehlhilfe.AppLogic.ViewModels; +using Waehlhilfe.Modem; + +namespace Waehlhilfe.App; + +/// Wires Wählhilfe by hand: no container. +internal sealed class CompositionRoot +{ + private readonly JsonSettingsStore _store = new JsonSettingsStore(JsonSettingsStore.DefaultPath()); + private readonly AtDialer _dialer = new AtDialer(new SerialPorts()); + private readonly ITelHandlerRegistration _registration = CreateRegistration(); + + public SettingsViewModel Settings() => new(_store, _dialer, SerialPorts.List, _registration); + + // Settings are read for every dial, so a change in the settings window applies to the next click. + public DialViewModel Dial(string link) => new(link, _store.Load(), _dialer); + + private static ITelHandlerRegistration CreateRegistration() + { + if (OperatingSystem.IsWindows()) + { + return new WindowsTelHandlerRegistration(); + } + + if (OperatingSystem.IsMacOS()) + { + return new MacTelHandlerRegistration(); + } + + return new UnsupportedTelHandlerRegistration(); + } +} diff --git a/src/Waehlhilfe.App/LaunchArguments.cs b/src/Waehlhilfe.App/LaunchArguments.cs new file mode 100644 index 0000000..8746874 --- /dev/null +++ b/src/Waehlhilfe.App/LaunchArguments.cs @@ -0,0 +1,12 @@ +namespace Waehlhilfe.App; + +internal static class LaunchArguments +{ + /// + /// The tel: link Windows and Linux pass on the command line when a link is clicked, + /// or null when Wählhilfe was started on its own. macOS delivers links as an Apple + /// event instead, which arrives through IActivatableLifetime. + /// + public static string? TelLink(IEnumerable args) => + args.FirstOrDefault(a => a.StartsWith("tel:", StringComparison.OrdinalIgnoreCase)); +} diff --git a/src/Waehlhilfe.App/Platform/MacTelHandlerRegistration.cs b/src/Waehlhilfe.App/Platform/MacTelHandlerRegistration.cs new file mode 100644 index 0000000..aee22cd --- /dev/null +++ b/src/Waehlhilfe.App/Platform/MacTelHandlerRegistration.cs @@ -0,0 +1,94 @@ +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Text; +using Waehlhilfe.AppLogic; + +namespace Waehlhilfe.App.Platform; + +/// The bundle declares tel in CFBundleURLTypes; Launch Services picks the default. +[SupportedOSPlatform("macos")] +internal sealed partial class MacTelHandlerRegistration : ITelHandlerRegistration +{ + /// Must match CFBundleIdentifier in packaging/macos/build-dmg.sh. + internal const string BundleId = "io.github.deepdiver1975.waehlhilfe"; + + private const string CoreFoundation = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"; + private const string CoreServices = "/System/Library/Frameworks/CoreServices.framework/CoreServices"; + private const uint Utf8 = 0x08000100; + + public bool IsDefaultHandler() + { + var scheme = CFStringCreateWithCString(IntPtr.Zero, "tel", Utf8); + try + { + var handler = LSCopyDefaultHandlerForURLScheme(scheme); + try + { + return string.Equals(ToManaged(handler), BundleId, StringComparison.OrdinalIgnoreCase); + } + finally + { + Release(handler); + } + } + finally + { + Release(scheme); + } + } + + public Task MakeDefaultAsync(CancellationToken cancellationToken = default) + { + var scheme = CFStringCreateWithCString(IntPtr.Zero, "tel", Utf8); + var bundle = CFStringCreateWithCString(IntPtr.Zero, BundleId, Utf8); + try + { + var status = LSSetDefaultHandlerForURLScheme(scheme, bundle); + return status == 0 + ? Task.CompletedTask + : throw new InvalidOperationException($"LSSetDefaultHandlerForURLScheme returned {status}."); + } + finally + { + Release(bundle); + Release(scheme); + } + } + + private static string? ToManaged(IntPtr text) + { + if (text == IntPtr.Zero) + { + return null; + } + + var buffer = new byte[256]; + return CFStringGetCString(text, buffer, buffer.Length, Utf8) + ? Encoding.UTF8.GetString(buffer, 0, Array.IndexOf(buffer, (byte)0) is var end and >= 0 ? end : buffer.Length) + : null; + } + + private static void Release(IntPtr reference) + { + if (reference != IntPtr.Zero) + { + CFRelease(reference); + } + } + + [LibraryImport(CoreFoundation, StringMarshalling = StringMarshalling.Utf8)] + private static partial IntPtr CFStringCreateWithCString(IntPtr allocator, string text, uint encoding); + + [LibraryImport(CoreFoundation)] + [return: MarshalAs(UnmanagedType.U1)] + private static partial bool CFStringGetCString(IntPtr text, [Out] byte[] buffer, nint bufferSize, uint encoding); + + [LibraryImport(CoreFoundation)] + private static partial void CFRelease(IntPtr reference); + + [LibraryImport(CoreServices)] + private static partial IntPtr LSCopyDefaultHandlerForURLScheme(IntPtr scheme); + + [LibraryImport(CoreServices)] + private static partial int LSSetDefaultHandlerForURLScheme(IntPtr scheme, IntPtr bundleId); +} diff --git a/src/Waehlhilfe.App/Platform/UnsupportedTelHandlerRegistration.cs b/src/Waehlhilfe.App/Platform/UnsupportedTelHandlerRegistration.cs new file mode 100644 index 0000000..6e08ddd --- /dev/null +++ b/src/Waehlhilfe.App/Platform/UnsupportedTelHandlerRegistration.cs @@ -0,0 +1,12 @@ +using Waehlhilfe.AppLogic; + +namespace Waehlhilfe.App.Platform; + +/// Linux: Wählhilfe dials there (it is where the DL500A is tested), but ships no handler registration. +internal sealed class UnsupportedTelHandlerRegistration : ITelHandlerRegistration +{ + public bool IsDefaultHandler() => false; + + public Task MakeDefaultAsync(CancellationToken cancellationToken = default) => + throw new PlatformNotSupportedException("Wählhilfe registers as the tel: handler on Windows and macOS only."); +} diff --git a/src/Waehlhilfe.App/Platform/WindowsTelHandlerRegistration.cs b/src/Waehlhilfe.App/Platform/WindowsTelHandlerRegistration.cs new file mode 100644 index 0000000..d1e837a --- /dev/null +++ b/src/Waehlhilfe.App/Platform/WindowsTelHandlerRegistration.cs @@ -0,0 +1,38 @@ +using System.Diagnostics; +using System.Runtime.Versioning; +using Microsoft.Win32; +using Waehlhilfe.AppLogic; + +namespace Waehlhilfe.App.Platform; + +/// +/// The MSI registers Wählhilfe as *capable* of handling tel: (ProgId, Capabilities, +/// RegisteredApplications). Windows lets no program make itself the default, so this opens the +/// Settings page where the user picks it once. +/// +[SupportedOSPlatform("windows")] +internal sealed class WindowsTelHandlerRegistration : ITelHandlerRegistration +{ + /// Must match the ProgId and the RegisteredApplications name in packaging/windows/Waehlhilfe.wxs. + internal const string ProgId = "Waehlhilfe.tel"; + + internal const string RegisteredApplicationName = "Waehlhilfe"; + + private const string UserChoiceKey = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\tel\UserChoice"; + + public bool IsDefaultHandler() + { + using var key = Registry.CurrentUser.OpenSubKey(UserChoiceKey); + return string.Equals(key?.GetValue("ProgId") as string, ProgId, StringComparison.Ordinal); + } + + public Task MakeDefaultAsync(CancellationToken cancellationToken = default) + { + // A fixed URI, nothing from input: the Settings page that lists Wählhilfe's link types. + using var _ = Process.Start(new ProcessStartInfo($"ms-settings:defaultapps?registeredAppMachine={RegisteredApplicationName}") + { + UseShellExecute = true, + }); + return Task.CompletedTask; + } +} diff --git a/src/Waehlhilfe.App/Views/DialWindow.axaml b/src/Waehlhilfe.App/Views/DialWindow.axaml new file mode 100644 index 0000000..60f4847 --- /dev/null +++ b/src/Waehlhilfe.App/Views/DialWindow.axaml @@ -0,0 +1,32 @@ + + + + + + + + + + + +