|
| 1 | +using System; |
| 2 | +using System.Windows; |
| 3 | +using NETworkManager.Utilities; |
| 4 | + |
| 5 | +namespace NETworkManager.Settings; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Shared expanded/width state of the profile panel, used by every tool that hosts a profile list. Because all |
| 9 | +/// tools observe the same <see cref="Current" /> instance, resizing or collapsing the profile panel in one tool |
| 10 | +/// is reflected in every other tool automatically through data binding - no manual synchronization needed. |
| 11 | +/// </summary> |
| 12 | +/// <remarks> |
| 13 | +/// <see cref="Current" /> reads <see cref="SettingsManager.Current" /> the first time it is accessed. This is |
| 14 | +/// only safe once <see cref="SettingsManager.Load" /> has completed (done early in App.xaml.cs, before the main |
| 15 | +/// window - and therefore any tool view - is created). Do not reference <see cref="Current" /> from eagerly |
| 16 | +/// loaded resources (e.g. App.xaml or globally merged style dictionaries). |
| 17 | +/// </remarks> |
| 18 | +public sealed class ProfileViewState : PropertyChangedBase |
| 19 | +{ |
| 20 | + public static ProfileViewState Current { get; } |
| 21 | + |
| 22 | + static ProfileViewState() |
| 23 | + { |
| 24 | + Current = new ProfileViewState(); |
| 25 | + } |
| 26 | + |
| 27 | + private readonly bool _isLoading; |
| 28 | + private bool _canProfileWidthChange = true; |
| 29 | + private double _tempProfileWidth; |
| 30 | + |
| 31 | + private ProfileViewState() |
| 32 | + { |
| 33 | + _isLoading = true; |
| 34 | + |
| 35 | + // Must be set before ExpandProfileView below, since assigning it can synchronously trigger |
| 36 | + // ResizeProfile(false), which reads _tempProfileWidth. |
| 37 | + _tempProfileWidth = SettingsManager.Current.Profile_Width; |
| 38 | + |
| 39 | + ExpandProfileView = SettingsManager.Current.Profile_ExpandView; |
| 40 | + |
| 41 | + ProfileWidth = ExpandProfileView |
| 42 | + ? new GridLength(SettingsManager.Current.Profile_Width) |
| 43 | + : new GridLength(GlobalStaticConfiguration.Profile_WidthCollapsed); |
| 44 | + |
| 45 | + _isLoading = false; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Gets or sets a value indicating whether the profile panel is expanded, shared across all tools. |
| 50 | + /// </summary> |
| 51 | + public bool ExpandProfileView |
| 52 | + { |
| 53 | + get; |
| 54 | + set |
| 55 | + { |
| 56 | + if (value == field) |
| 57 | + return; |
| 58 | + |
| 59 | + if (!_isLoading) |
| 60 | + SettingsManager.Current.Profile_ExpandView = value; |
| 61 | + |
| 62 | + field = value; |
| 63 | + |
| 64 | + if (_canProfileWidthChange) |
| 65 | + ResizeProfile(false); |
| 66 | + |
| 67 | + OnPropertyChanged(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets or sets the width of the profile panel, shared across all tools. |
| 73 | + /// </summary> |
| 74 | + public GridLength ProfileWidth |
| 75 | + { |
| 76 | + get; |
| 77 | + set |
| 78 | + { |
| 79 | + if (value == field) |
| 80 | + return; |
| 81 | + |
| 82 | + if (!_isLoading && Math.Abs(value.Value - GlobalStaticConfiguration.Profile_WidthCollapsed) > |
| 83 | + GlobalStaticConfiguration.FloatPointFix) // Do not save the size when collapsed |
| 84 | + SettingsManager.Current.Profile_Width = value.Value; |
| 85 | + |
| 86 | + field = value; |
| 87 | + |
| 88 | + if (_canProfileWidthChange) |
| 89 | + ResizeProfile(true); |
| 90 | + |
| 91 | + OnPropertyChanged(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void ResizeProfile(bool dueToChangedSize) |
| 96 | + { |
| 97 | + _canProfileWidthChange = false; |
| 98 | + |
| 99 | + if (dueToChangedSize) |
| 100 | + { |
| 101 | + ExpandProfileView = Math.Abs(ProfileWidth.Value - GlobalStaticConfiguration.Profile_WidthCollapsed) > |
| 102 | + GlobalStaticConfiguration.FloatPointFix; |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + if (ExpandProfileView) |
| 107 | + { |
| 108 | + ProfileWidth = |
| 109 | + Math.Abs(_tempProfileWidth - GlobalStaticConfiguration.Profile_WidthCollapsed) < |
| 110 | + GlobalStaticConfiguration.FloatPointFix |
| 111 | + ? new GridLength(GlobalStaticConfiguration.Profile_DefaultWidthExpanded) |
| 112 | + : new GridLength(_tempProfileWidth); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + _tempProfileWidth = ProfileWidth.Value; |
| 117 | + ProfileWidth = new GridLength(GlobalStaticConfiguration.Profile_WidthCollapsed); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + _canProfileWidthChange = true; |
| 122 | + } |
| 123 | +} |
0 commit comments