Skip to content

Commit 1894ab8

Browse files
committed
Add working example toggle with UAC prompt
1 parent d94b167 commit 1894ab8

6 files changed

Lines changed: 112 additions & 6 deletions

File tree

src/UniGetUI.Core.SecureSettings/SecureSettings.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ public static async Task<bool> TrySet(string setting, bool enabled)
3535
using Process p = new Process();
3636
p.StartInfo = new()
3737
{
38-
UseShellExecute = false,
38+
UseShellExecute = true,
3939
CreateNoWindow = true,
4040
FileName = CoreData.UniGetUIExecutableFile,
41+
Verb = "runas",
4142
ArgumentList =
4243
{
4344
enabled? Args.ENABLE_FOR_USER: Args.DISABLE_FOR_USER,
@@ -58,7 +59,7 @@ public static int ApplyForUser(string username, string setting, bool enable)
5859
string purifiedUser = CoreTools.MakeValidFileName(username);
5960
string purifiedSetting = CoreTools.MakeValidFileName(setting);
6061

61-
var appData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
62+
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
6263
var settingsLocation = Path.Join(appData, "UniGetUI\\SecureSettings", purifiedUser);
6364
var settingFile = Path.Join(settingsLocation, purifiedSetting);
6465

src/UniGetUI.Core.SecureSettings/UniGetUI.Core.SecureSettings.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<ItemGroup>
3-
<ProjectReference Include="..\UniGetUI.Core.Data\UniGetUI.Core.Tools.csproj" />
43
<ProjectReference Include="..\UniGetUI.Core.Tools\UniGetUI.Core.Tools.csproj" />
54
</ItemGroup>
65

src/UniGetUI/CLIHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public static int EnableSecureSettingForUser()
292292
{
293293
var args = Environment.GetCommandLineArgs().ToList();
294294

295-
var basePos = args.IndexOf(ENABLE_SECURE_SETTING);
295+
var basePos = args.IndexOf(ENABLE_SECURE_SETTING_FOR_USER);
296296
if (basePos < 0)
297297
return (int)HRESULT.STATUS_INVALID_PARAMETER; // The base paramater was not found
298298

@@ -316,7 +316,7 @@ public static int DisableSecureSettingForUser()
316316
{
317317
var args = Environment.GetCommandLineArgs().ToList();
318318

319-
var basePos = args.IndexOf(ENABLE_SECURE_SETTING);
319+
var basePos = args.IndexOf(DISABLE_SECURE_SETTING_FOR_USER);
320320
if (basePos < 0)
321321
return (int)HRESULT.STATUS_INVALID_PARAMETER; // The base paramater was not found
322322

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using CommunityToolkit.WinUI.Controls;
2+
using Microsoft.UI.Xaml;
3+
using Microsoft.UI.Xaml.Controls;
4+
using UniGetUI.Core.Logging;
5+
using UniGetUI.Core.SettingsEngine.SecureSettings;
6+
using UniGetUI.Core.Tools;
7+
8+
// To learn more about WinUI, the WinUI project structure,
9+
// and more about our project templates, see: http://aka.ms/winui-project-info.
10+
11+
namespace UniGetUI.Interface.Widgets
12+
{
13+
public partial class SecureCheckboxCard : SettingsCard
14+
{
15+
public ToggleSwitch _checkbox;
16+
public TextBlock _textblock;
17+
public ProgressRing _loading;
18+
protected bool IS_INVERTED;
19+
20+
protected string setting_name = "";
21+
public virtual string SettingName
22+
{
23+
set
24+
{
25+
setting_name = value;
26+
IS_INVERTED = value.StartsWith("Disable");
27+
_checkbox.IsOn = SecureSettings.Get(setting_name) ^ IS_INVERTED ^ ForceInversion;
28+
_textblock.Opacity = _checkbox.IsOn ? 1 : 0.7;
29+
}
30+
}
31+
32+
public bool ForceInversion { get; set; }
33+
34+
public bool Checked
35+
{
36+
get => _checkbox.IsOn;
37+
}
38+
public virtual event EventHandler<EventArgs>? StateChanged;
39+
40+
public string Text
41+
{
42+
set => _textblock.Text = CoreTools.Translate(value);
43+
}
44+
45+
public SecureCheckboxCard()
46+
{
47+
_checkbox = new ToggleSwitch()
48+
{
49+
Margin = new Thickness(0, 0, 8, 0)
50+
};
51+
52+
_loading = new ProgressRing() { IsIndeterminate = true, Visibility = Visibility.Collapsed};
53+
_textblock = new TextBlock()
54+
{
55+
VerticalAlignment = VerticalAlignment.Center,
56+
Margin = new Thickness(0, 0, 0, 0),
57+
TextWrapping = TextWrapping.Wrap
58+
};
59+
IS_INVERTED = false;
60+
Content = new StackPanel()
61+
{
62+
Spacing = 4,
63+
Orientation = Orientation.Horizontal,
64+
Children = { _loading, _checkbox },
65+
};
66+
Header = _textblock;
67+
68+
_checkbox.HorizontalAlignment = HorizontalAlignment.Stretch;
69+
_checkbox.Toggled += (s, e) => _ = _checkbox_Toggled();
70+
}
71+
protected virtual async Task _checkbox_Toggled()
72+
{
73+
try
74+
{
75+
if (_checkbox.IsEnabled is false)
76+
return;
77+
78+
_loading.Visibility = Visibility.Visible;
79+
_checkbox.IsEnabled = false;
80+
StateChanged?.Invoke(this, EventArgs.Empty);
81+
await SecureSettings.TrySet(setting_name, _checkbox.IsOn ^ IS_INVERTED ^ ForceInversion);
82+
_textblock.Opacity = _checkbox.IsOn ? 1 : 0.7;
83+
_checkbox.IsOn = SecureSettings.Get(setting_name);
84+
_loading.Visibility = Visibility.Collapsed;
85+
_checkbox.IsEnabled = true;
86+
}
87+
catch (Exception ex)
88+
{
89+
Logger.Warn(ex);
90+
_checkbox.IsOn = SecureSettings.Get(setting_name);
91+
_loading.Visibility = Visibility.Collapsed;
92+
_checkbox.IsEnabled = true;
93+
}
94+
}
95+
}
96+
}

src/UniGetUI/Pages/SettingsPages/GeneralPages/Experimental.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@
9191
CornerRadius="8"
9292
SettingName="DisableDMWThreadOptimizations"
9393
Text="Enable background CPU Usage optimizations (see Pull Request #3278)" />
94+
95+
<UserControl Height="16" />
96+
97+
<widgets:SecureCheckboxCard
98+
CornerRadius="8"
99+
SettingName="SecureSettingTest"
100+
Text="This is a test"
101+
/>
102+
94103
</StackPanel>
95104
</ScrollViewer>
96105
</Page>

src/UniGetUI/Properties/launchSettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"commandName": "MsixPackage"
55
},
66
"UniGetUI (Unpackaged)": {
7-
"commandName": "Project"
7+
"commandName": "Project",
8+
"commandLineArgs": ""
89
}
910
}
1011
}

0 commit comments

Comments
 (0)