PowerLauncherPluginViewModel.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Collections.Generic; 7 using System.ComponentModel; 8 using System.IO; 9 using System.Linq; 10 using System.Runtime.CompilerServices; 11 12 using global::PowerToys.GPOWrapper; 13 using Microsoft.PowerToys.Settings.UI.Library; 14 15 namespace Microsoft.PowerToys.Settings.UI.ViewModels 16 { 17 public partial class PowerLauncherPluginViewModel : INotifyPropertyChanged 18 { 19 private readonly PowerLauncherPluginSettings settings; 20 private readonly Func<bool> isDark; 21 22 public PowerLauncherPluginViewModel(PowerLauncherPluginSettings settings, Func<bool> isDark) 23 { 24 if (settings == null) 25 { 26 throw new ArgumentNullException(nameof(settings), "PowerLauncherPluginSettings object is null"); 27 } 28 29 this.settings = settings; 30 this.isDark = isDark; 31 foreach (var item in AdditionalOptions) 32 { 33 item.PropertyChanged += (object sender, PropertyChangedEventArgs e) => 34 { 35 NotifyPropertyChanged(nameof(AdditionalOptions)); 36 }; 37 } 38 39 _enabledGpoRuleConfiguration = (GpoRuleConfigured)settings.EnabledPolicyUiState; 40 _enabledGpoRuleIsConfigured = _enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled || _enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled; 41 42 _hasValidWebsiteUri = Uri.IsWellFormedUriString(settings.Website, UriKind.Absolute); 43 _websiteUri = _hasValidWebsiteUri ? settings.Website : WebsiteFallbackUri; 44 } 45 46 public string Id { get => settings.Id; } 47 48 public string Name { get => settings.Name; } 49 50 public string Description { get => settings.Description; } 51 52 public string Version { get => settings.Version; } 53 54 public string Author { get => settings.Author; } 55 56 // Fallback value for the website in case the uri from json is not well formatted 57 private const string WebsiteFallbackUri = "https://aka.ms/PowerToys"; 58 private string _websiteUri; 59 private bool _hasValidWebsiteUri; 60 61 public string WebsiteUri => _websiteUri; 62 63 public bool HasValidWebsiteUri => _hasValidWebsiteUri; 64 65 private GpoRuleConfigured _enabledGpoRuleConfiguration; 66 private bool _enabledGpoRuleIsConfigured; 67 68 public bool Disabled 69 { 70 get 71 { 72 if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Disabled) 73 { 74 return true; 75 } 76 else if (_enabledGpoRuleConfiguration == GpoRuleConfigured.Enabled) 77 { 78 return false; 79 } 80 else 81 { 82 return settings.Disabled; 83 } 84 } 85 86 set 87 { 88 if (settings.Disabled != value) 89 { 90 settings.Disabled = value; 91 92 NotifyPropertyChanged(); 93 NotifyPropertyChanged(nameof(ShowNotAccessibleWarning)); 94 NotifyPropertyChanged(nameof(Enabled)); 95 NotifyPropertyChanged(nameof(DisabledOpacity)); 96 NotifyPropertyChanged(nameof(IsGlobalAndEnabled)); 97 NotifyPropertyChanged(nameof(ShowBadgeOnPluginSettingError)); 98 } 99 } 100 } 101 102 public bool Enabled => !Disabled; 103 104 public bool EnabledGpoRuleIsConfigured => _enabledGpoRuleIsConfigured; 105 106 public double DisabledOpacity => Disabled ? 0.5 : 1; 107 108 public bool IsGlobalAndEnabled 109 { 110 get 111 { 112 return IsGlobal && Enabled; 113 } 114 } 115 116 public bool IsGlobal 117 { 118 get 119 { 120 return settings.IsGlobal; 121 } 122 123 set 124 { 125 if (settings.IsGlobal != value) 126 { 127 settings.IsGlobal = value; 128 NotifyPropertyChanged(); 129 NotifyPropertyChanged(nameof(ShowNotAccessibleWarning)); 130 NotifyPropertyChanged(nameof(IsGlobalAndEnabled)); 131 NotifyPropertyChanged(nameof(ShowBadgeOnPluginSettingError)); 132 } 133 } 134 } 135 136 public int WeightBoost 137 { 138 get 139 { 140 return settings.WeightBoost; 141 } 142 143 set 144 { 145 if (settings.WeightBoost != value) 146 { 147 settings.WeightBoost = value; 148 NotifyPropertyChanged(); 149 } 150 } 151 } 152 153 public string ActionKeyword 154 { 155 get 156 { 157 return settings.ActionKeyword; 158 } 159 160 set 161 { 162 if (settings.ActionKeyword != value) 163 { 164 settings.ActionKeyword = value; 165 NotifyPropertyChanged(); 166 NotifyPropertyChanged(nameof(ShowNotAccessibleWarning)); 167 NotifyPropertyChanged(nameof(ShowBadgeOnPluginSettingError)); 168 } 169 } 170 } 171 172 private IEnumerable<PluginAdditionalOptionViewModel> _additionalOptions; 173 174 public IEnumerable<PluginAdditionalOptionViewModel> AdditionalOptions 175 { 176 get 177 { 178 if (_additionalOptions == null) 179 { 180 _additionalOptions = settings.AdditionalOptions.Select(x => new PluginAdditionalOptionViewModel(x)).ToList(); 181 } 182 183 return _additionalOptions; 184 } 185 } 186 187 public bool ShowAdditionalOptions 188 { 189 get => AdditionalOptions.Any(); 190 } 191 192 public override string ToString() 193 { 194 return $"{Name}. {Description}"; 195 } 196 197 #nullable enable 198 public Uri? IconPath => Uri.TryCreate(isDark() ? settings.IconPathDark : settings.IconPathLight, UriKind.Absolute, out var uri) ? uri : null; 199 #nullable restore 200 201 public event PropertyChangedEventHandler PropertyChanged; 202 203 private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 204 { 205 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 206 } 207 208 public bool ShowNotAccessibleWarning 209 { 210 get => !Disabled && !IsGlobal && string.IsNullOrWhiteSpace(ActionKeyword); 211 } 212 213 // The Badge is shown in case of ANY error event, but NEVER when the plugin is disabled. 214 // Logic = !disabled && (errorA or errorB or errorC...) 215 // Current count of possible error events: 1 (NotAccessible) 216 public bool ShowBadgeOnPluginSettingError 217 { 218 get => !Disabled && ShowNotAccessibleWarning; 219 } 220 } 221 }