forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowWalkerSettings.cs
More file actions
205 lines (183 loc) · 8.98 KB
/
WindowWalkerSettings.cs
File metadata and controls
205 lines (183 loc) · 8.98 KB
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
// 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.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.Plugin.WindowWalker.Properties;
using Microsoft.PowerToys.Settings.UI.Library;
[assembly: InternalsVisibleTo("Microsoft.Plugin.WindowWalker.UnitTests")]
namespace Microsoft.Plugin.WindowWalker.Components
{
/// <summary>
/// Additional settings for the WindowWalker plugin.
/// </summary>
/// <remarks>Some code parts reused from TimeZone plugin.</remarks>
internal sealed class WindowWalkerSettings
{
/// <summary>
/// Are the class properties initialized with default values
/// </summary>
private readonly bool _initialized;
/// <summary>
/// An instance of the class <see cref="WindowWalkerSettings"></see>
/// </summary>
private static WindowWalkerSettings instance;
/// <summary>
/// Gets a value indicating whether we only search for windows on the currently visible desktop or on all desktops.
/// </summary>
internal bool ResultsFromVisibleDesktopOnly { get; private set; }
/// <summary>
/// Gets a value indicating whether the process id is shown in the subtitle.
/// </summary>
internal bool SubtitleShowPid { get; private set; }
/// <summary>
/// Gets a value indicating whether the desktop name is shown in the subtitle.
/// We don't show the desktop name if there is only one desktop.
/// </summary>
internal bool SubtitleShowDesktopName { get; private set; }
/// <summary>
/// Gets a value indicating whether we request a confirmation when the user kills a process.
/// </summary>
internal bool ConfirmKillProcess { get; private set; }
/// <summary>
/// Gets a value indicating whether to kill the entire process tree or the selected process only.
/// </summary>
internal bool KillProcessTree { get; private set; }
/// <summary>
/// Gets a value indicating whether PowerToys run should stay open after executing killing process and closing window.
/// </summary>
internal bool OpenAfterKillAndClose { get; private set; }
/// <summary>
/// Gets a value indicating whether the "kill process" command is hidden on processes that require additional permissions (UAC).
/// </summary>
internal bool HideKillProcessOnElevatedProcesses { get; private set; }
/// <summary>
/// Gets a value indicating whether we show the explorer settings info or not.
/// </summary>
internal bool HideExplorerSettingInfo { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="WindowWalkerSettings"/> class.
/// Private constructor to make sure there is never more than one instance of this class
/// </summary>
private WindowWalkerSettings()
{
// Init class properties with default values
UpdateSettings(null);
_initialized = true;
}
/// <summary>
/// Gets an instance property of this class that makes sure that the first instance gets created
/// and that all the requests end up at that one instance.
/// The benefit of this is that we don't need additional variables/parameters
/// to communicate the settings between plugin's classes/methods.
/// We can simply access this one instance, whenever we need the actual settings.
/// </summary>
internal static WindowWalkerSettings Instance
{
get
{
if (instance == null)
{
instance = new WindowWalkerSettings();
}
return instance;
}
}
/// <summary>
/// Return a list with all additional plugin options.
/// </summary>
/// <returns>A list with all additional plugin options.</returns>
internal static List<PluginAdditionalOption> GetAdditionalOptions()
{
var optionList = new List<PluginAdditionalOption>
{
new PluginAdditionalOption
{
Key = nameof(ResultsFromVisibleDesktopOnly),
DisplayLabel = Resources.wox_plugin_windowwalker_SettingResultsVisibleDesktop,
Value = false,
},
new PluginAdditionalOption
{
Key = nameof(SubtitleShowPid),
DisplayLabel = Resources.wox_plugin_windowwalker_SettingSubtitlePid,
Value = false,
},
new PluginAdditionalOption
{
Key = nameof(SubtitleShowDesktopName),
DisplayLabel = Resources.wox_plugin_windowwalker_SettingSubtitleDesktopName,
DisplayDescription = Resources.wox_plugin_windowwalker_SettingSubtitleDesktopName_Description,
Value = true,
},
new PluginAdditionalOption
{
Key = nameof(ConfirmKillProcess),
DisplayLabel = Resources.wox_plugin_windowwalker_SettingConfirmKillProcess,
Value = true,
},
new PluginAdditionalOption
{
Key = nameof(KillProcessTree),
DisplayLabel = Resources.wox_plugin_windowwalker_SettingKillProcessTree,
DisplayDescription = Resources.wox_plugin_windowwalker_SettingKillProcessTree_Description,
Value = false,
},
new PluginAdditionalOption
{
Key = nameof(OpenAfterKillAndClose),
DisplayLabel = Resources.wox_plugin_windowwalker_SettingOpenAfterKillAndClose,
DisplayDescription = Resources.wox_plugin_windowwalker_SettingOpenAfterKillAndClose_Description,
Value = false,
},
new PluginAdditionalOption
{
Key = nameof(HideKillProcessOnElevatedProcesses),
DisplayLabel = Resources.wox_plugin_windowwalker_SettingHideKillProcess,
Value = false,
},
new PluginAdditionalOption
{
Key = nameof(HideExplorerSettingInfo),
DisplayLabel = Resources.wox_plugin_windowwalker_SettingExplorerSettingInfo,
DisplayDescription = Resources.wox_plugin_windowwalker_SettingExplorerSettingInfo_Description,
Value = false,
},
};
return optionList;
}
/// <summary>
/// Update this settings.
/// </summary>
/// <param name="settings">The settings for all power launcher plugins.</param>
internal void UpdateSettings(PowerLauncherPluginSettings settings)
{
if ((settings is null || settings.AdditionalOptions is null) & _initialized)
{
return;
}
ResultsFromVisibleDesktopOnly = GetSettingOrDefault(settings, nameof(ResultsFromVisibleDesktopOnly));
SubtitleShowPid = GetSettingOrDefault(settings, nameof(SubtitleShowPid));
SubtitleShowDesktopName = GetSettingOrDefault(settings, nameof(SubtitleShowDesktopName));
ConfirmKillProcess = GetSettingOrDefault(settings, nameof(ConfirmKillProcess));
KillProcessTree = GetSettingOrDefault(settings, nameof(KillProcessTree));
OpenAfterKillAndClose = GetSettingOrDefault(settings, nameof(OpenAfterKillAndClose));
HideKillProcessOnElevatedProcesses = GetSettingOrDefault(settings, nameof(HideKillProcessOnElevatedProcesses));
HideExplorerSettingInfo = GetSettingOrDefault(settings, nameof(HideExplorerSettingInfo));
}
/// <summary>
/// Return one <see cref="bool"/> setting of the given settings list with the given name.
/// </summary>
/// <param name="settings">The object that contain all settings.</param>
/// <param name="name">The name of the setting.</param>
/// <returns>A settings value.</returns>
private static bool GetSettingOrDefault(PowerLauncherPluginSettings settings, string name)
{
var option = settings?.AdditionalOptions?.FirstOrDefault(x => x.Key == name);
// If a setting isn't available, we use the value defined in the method GetAdditionalOptions() as fallback.
// We can use First() instead of FirstOrDefault() because the values must exist. Otherwise, we made a mistake when defining the settings.
return option?.Value ?? GetAdditionalOptions().First(x => x.Key == name).Value;
}
}
}