| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ToastSettingsMerger.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using MyNet.UI.Notifications.Models; |
| | | 8 | | |
| | | 9 | | namespace MyNet.UI.Toasting.Settings; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Merges global toast defaults, optional notification overrides, and notification-type rules. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class ToastSettingsMerger |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Builds effective toast settings for the given notification. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="notification">The notification being displayed as a toast.</param> |
| | | 20 | | /// <param name="options">Global toast manager defaults.</param> |
| | | 21 | | /// <returns>Resolved toast settings for the toast instance.</returns> |
| | | 22 | | public static ToastSettings Merge(INotification notification, ToastManagerOptions? options = null) |
| | | 23 | | { |
| | 33 | 24 | | options ??= new(); |
| | | 25 | | |
| | 33 | 26 | | var settings = new ToastSettings |
| | 33 | 27 | | { |
| | 33 | 28 | | ClosingStrategy = options.DefaultClosingStrategy, |
| | 33 | 29 | | FreezeOnMouseEnter = options.DefaultFreezeOnMouseEnter, |
| | 33 | 30 | | Duration = null |
| | 33 | 31 | | }; |
| | | 32 | | |
| | 33 | 33 | | if (notification is IHasToastSettings { ToastSettings: { } overrides }) |
| | 9 | 34 | | ApplyOverrides(settings, overrides); |
| | | 35 | | |
| | 33 | 36 | | ApplyNotificationTypeDefaults(settings, notification); |
| | | 37 | | |
| | 33 | 38 | | return settings; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | private static void ApplyOverrides(ToastSettings settings, ToastSettingsOverrides overrides) |
| | | 42 | | { |
| | 9 | 43 | | if (overrides.ClosingStrategy is { } closingStrategy) |
| | 3 | 44 | | settings.ClosingStrategy = closingStrategy; |
| | | 45 | | |
| | 9 | 46 | | if (overrides.FreezeOnMouseEnter is { } freezeOnMouseEnter) |
| | 3 | 47 | | settings.FreezeOnMouseEnter = freezeOnMouseEnter; |
| | | 48 | | |
| | 9 | 49 | | if (overrides.Duration is { } duration) |
| | 6 | 50 | | settings.Duration = duration; |
| | 9 | 51 | | } |
| | | 52 | | |
| | | 53 | | private static void ApplyNotificationTypeDefaults(ToastSettings settings, INotification notification) |
| | | 54 | | { |
| | 33 | 55 | | if (notification is not IClosableNotification { IsClosable: true }) |
| | 15 | 56 | | return; |
| | | 57 | | |
| | 18 | 58 | | settings.ClosingStrategy = settings.ClosingStrategy switch |
| | 18 | 59 | | { |
| | 12 | 60 | | ToastClosingStrategy.AutoClose => ToastClosingStrategy.Both, |
| | 3 | 61 | | ToastClosingStrategy.None => ToastClosingStrategy.CloseButton, |
| | 3 | 62 | | _ => settings.ClosingStrategy |
| | 18 | 63 | | }; |
| | 18 | 64 | | } |
| | | 65 | | } |
| | | 66 | | |