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