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 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Waehlhilfe.App/Views/DialWindow.axaml.cs b/src/Waehlhilfe.App/Views/DialWindow.axaml.cs
new file mode 100644
index 0000000..2eded01
--- /dev/null
+++ b/src/Waehlhilfe.App/Views/DialWindow.axaml.cs
@@ -0,0 +1,37 @@
+using Avalonia.Controls;
+using Waehlhilfe.AppLogic.ViewModels;
+
+namespace Waehlhilfe.App.Views;
+
+internal sealed partial class DialWindow : Window
+{
+ private DialViewModel? _viewModel;
+
+ public DialWindow() => InitializeComponent();
+
+ protected override void OnDataContextChanged(EventArgs e)
+ {
+ base.OnDataContextChanged(e);
+ _viewModel?.CloseRequested -= OnCloseRequested;
+ _viewModel = DataContext as DialViewModel;
+ _viewModel?.CloseRequested += OnCloseRequested;
+ }
+
+ // However the window goes — title bar, or a newer link replacing it — a dial still running
+ // behind it is cancelled, so no call is placed from a window nobody can see.
+ protected override void OnClosed(EventArgs e)
+ {
+ if (_viewModel is { } viewModel)
+ {
+ viewModel.CloseRequested -= OnCloseRequested;
+ if (viewModel.IsBusy)
+ {
+ viewModel.CancelCommand.Execute(null);
+ }
+ }
+
+ base.OnClosed(e);
+ }
+
+ private void OnCloseRequested(object? sender, EventArgs e) => Close();
+}
diff --git a/src/Waehlhilfe.App/Views/SettingsWindow.axaml b/src/Waehlhilfe.App/Views/SettingsWindow.axaml
index fc9f275..3c954ad 100644
--- a/src/Waehlhilfe.App/Views/SettingsWindow.axaml
+++ b/src/Waehlhilfe.App/Views/SettingsWindow.axaml
@@ -1,12 +1,72 @@
-
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Waehlhilfe.App/Waehlhilfe.App.csproj b/src/Waehlhilfe.App/Waehlhilfe.App.csproj
index a1be28c..f6de772 100644
--- a/src/Waehlhilfe.App/Waehlhilfe.App.csproj
+++ b/src/Waehlhilfe.App/Waehlhilfe.App.csproj
@@ -8,6 +8,8 @@
Wählhilfe
Wählhilfe
0.1.0
+
+ true
diff --git a/src/Waehlhilfe.App/WaehlhilfeApp.axaml.cs b/src/Waehlhilfe.App/WaehlhilfeApp.axaml.cs
index 847173b..22d1cb1 100644
--- a/src/Waehlhilfe.App/WaehlhilfeApp.axaml.cs
+++ b/src/Waehlhilfe.App/WaehlhilfeApp.axaml.cs
@@ -1,19 +1,68 @@
using Avalonia;
+using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
+using Avalonia.Threading;
using Waehlhilfe.App.Views;
namespace Waehlhilfe.App;
internal sealed partial class WaehlhilfeApp : Application
{
+ // How long macOS gets to deliver the link that launched the app before the settings open.
+ private static readonly TimeSpan LaunchLinkGrace = TimeSpan.FromMilliseconds(500);
+
public override void Initialize() => AvaloniaXamlLoader.Load(this);
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
- desktop.MainWindow = new SettingsWindow();
+ var root = new CompositionRoot();
+ desktop.ShutdownMode = ShutdownMode.OnLastWindowClose;
+
+ // Subscribed before anything awaits, so a link that launched the app is not missed.
+ // One dial window at a time: a second link replaces the first instead of stacking
+ // windows a stray keystroke could confirm.
+ var linkArrived = false;
+ DialWindow? dialWindow = null;
+ if (this.TryGetFeature() is { } activation)
+ {
+ activation.Activated += (_, e) =>
+ {
+ if (e is ProtocolActivatedEventArgs { Kind: ActivationKind.OpenUri } link)
+ {
+ linkArrived = true;
+ var previous = dialWindow;
+ dialWindow = new DialWindow { DataContext = root.Dial(link.Uri.OriginalString) };
+ dialWindow.Show();
+ previous?.Close();
+ }
+ };
+ }
+
+ if (LaunchArguments.TelLink(desktop.Args ?? []) is { } telLink)
+ {
+ desktop.MainWindow = dialWindow = new DialWindow { DataContext = root.Dial(telLink) };
+ }
+ else if (OperatingSystem.IsMacOS())
+ {
+ // A tel: click starts the app with no arguments and the link follows as an Apple
+ // event; only when none comes was the app opened by hand.
+ DispatcherTimer.RunOnce(
+ () =>
+ {
+ if (!linkArrived)
+ {
+ new SettingsWindow { DataContext = root.Settings() }.Show();
+ }
+ },
+ LaunchLinkGrace);
+ }
+ else
+ {
+ desktop.MainWindow = new SettingsWindow { DataContext = root.Settings() };
+ }
}
base.OnFrameworkInitializationCompleted();
diff --git a/tests/Waehlhilfe.App.Tests/DialWindowTests.cs b/tests/Waehlhilfe.App.Tests/DialWindowTests.cs
new file mode 100644
index 0000000..b7d1666
--- /dev/null
+++ b/tests/Waehlhilfe.App.Tests/DialWindowTests.cs
@@ -0,0 +1,128 @@
+using Avalonia.Automation;
+using Avalonia.Controls;
+using Avalonia.Headless.XUnit;
+using Avalonia.Input;
+using Avalonia.LogicalTree;
+using Avalonia.Threading;
+using Microsoft.Extensions.Time.Testing;
+using Waehlhilfe.App.Views;
+using Waehlhilfe.AppLogic.Tests;
+using Waehlhilfe.AppLogic.ViewModels;
+using Waehlhilfe.Domain;
+
+namespace Waehlhilfe.App.Tests;
+
+public sealed class DialWindowTests
+{
+ private readonly FakeDialer _dialer = new();
+ private readonly FakeTimeProvider _time = new();
+
+ private DialWindow Open(string link = "tel:+49 89 1234567", bool armed = true)
+ {
+ var settings = DialSettings.Default with { PortName = "COM7" };
+ var window = new DialWindow { DataContext = new DialViewModel(link, settings, _dialer, _time) };
+ window.Show();
+ Dispatcher.UIThread.RunJobs();
+ if (armed)
+ {
+ _time.Advance(DialViewModel.ArmDelay);
+ Dispatcher.UIThread.RunJobs();
+ }
+
+ return window;
+ }
+
+ [AvaloniaFact]
+ public void Shows_the_number_that_will_be_dialled()
+ {
+ var window = Open();
+
+ Assert.Equal("0891234567", window.FindControl("NumberText")?.Text);
+ Assert.Empty(_dialer.Dialled);
+ }
+
+ [AvaloniaFact]
+ public void Enter_dials()
+ {
+ var window = Open();
+
+ window.Press(PhysicalKey.Enter);
+
+ Assert.Equal([("COM7", "0891234567")], _dialer.Dialled);
+ }
+
+ // The window takes focus when a link arrives; an Enter that was meant for the program the
+ // Lagebuchführer was typing in must not place the call.
+ [AvaloniaFact]
+ public void Enter_right_after_the_window_opens_does_not_dial()
+ {
+ var window = Open(armed: false);
+
+ window.Press(PhysicalKey.Enter);
+
+ Assert.Empty(_dialer.Dialled);
+ Assert.False(window.FindControl