Skip to content

Commit c8368d1

Browse files
authored
Avalonia: Implement missing features (#4662)
1 parent f229fdc commit c8368d1

47 files changed

Lines changed: 2008 additions & 197 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/UniGetUI.Avalonia/App.axaml.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,15 @@ public override void OnFrameworkInitializationCompleted()
5252

5353
if (CoreData.WasDaemon)
5454
{
55-
// Start silently: hide the window as soon as Avalonia opens it.
56-
mainWindow.Opened += (_, _) => mainWindow.Hide();
55+
// Start silently: hide the window on first open only.
56+
// Opened fires on every Show() in Avalonia, so we must unsubscribe
57+
// immediately or every ShowFromTray() call would hide the window again.
58+
void HideOnce(object? s, EventArgs e)
59+
{
60+
mainWindow.Opened -= HideOnce;
61+
mainWindow.Hide();
62+
}
63+
mainWindow.Opened += HideOnce;
5764
}
5865

5966
_ = StartupAsync(mainWindow);
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
using UniGetUI.Core.Data;
2+
using UniGetUI.Core.Logging;
3+
using UniGetUI.Core.SettingsEngine;
4+
using UniGetUI.Core.SettingsEngine.SecureSettings;
5+
using UniGetUI.Core.Tools;
6+
7+
namespace UniGetUI.Avalonia;
8+
9+
/// <summary>
10+
/// Pre-UI CLI argument handler. Mirrors WinUI's CLIHandler.
11+
/// Methods that return a non-null exit code should cause the process to exit
12+
/// without launching the Avalonia app.
13+
/// </summary>
14+
internal static class AvaloniaCliHandler
15+
{
16+
public const string HELP = "--help";
17+
public const string DAEMON = "--daemon";
18+
public const string NO_CORRUPT_DIALOG = "--no-corrupt-dialog";
19+
20+
public const string IMPORT_SETTINGS = "--import-settings";
21+
public const string EXPORT_SETTINGS = "--export-settings";
22+
23+
public const string ENABLE_SETTING = "--enable-setting";
24+
public const string DISABLE_SETTING = "--disable-setting";
25+
public const string SET_SETTING_VAL = "--set-setting-value";
26+
27+
public const string ENABLE_SECURE_SETTING = "--enable-secure-setting";
28+
public const string DISABLE_SECURE_SETTING = "--disable-secure-setting";
29+
public const string ENABLE_SECURE_SETTING_FOR_USER = SecureSettings.Args.ENABLE_FOR_USER;
30+
public const string DISABLE_SECURE_SETTING_FOR_USER = SecureSettings.Args.DISABLE_FOR_USER;
31+
32+
private enum ExitCode
33+
{
34+
Success = 0,
35+
Failed = -1,
36+
InvalidParameter = -1073741811,
37+
NoSuchFile = -1073741809,
38+
UnknownSettingsKey = -2,
39+
}
40+
41+
/// <summary>
42+
/// Inspect <paramref name="args"/> and, for recognised pre-UI arguments,
43+
/// execute the requested action and return the desired process exit code.
44+
/// Returns null when no pre-UI argument was found and the app should start normally.
45+
/// </summary>
46+
public static int? HandlePreUiArgs(string[] args)
47+
{
48+
if (args.Contains(HELP))
49+
{
50+
CoreTools.Launch("https://github.com/Devolutions/UniGetUI/blob/main/cli-arguments.md#unigetui-command-line-parameters");
51+
return (int)ExitCode.Success;
52+
}
53+
54+
if (args.Contains(IMPORT_SETTINGS))
55+
return ImportSettings(args);
56+
57+
if (args.Contains(EXPORT_SETTINGS))
58+
return ExportSettings(args);
59+
60+
if (args.Contains(ENABLE_SETTING))
61+
return EnableSetting(args);
62+
63+
if (args.Contains(DISABLE_SETTING))
64+
return DisableSetting(args);
65+
66+
if (args.Contains(SET_SETTING_VAL))
67+
return SetSettingsValue(args);
68+
69+
if (args.Contains(ENABLE_SECURE_SETTING))
70+
return EnableSecureSetting(args);
71+
72+
if (args.Contains(DISABLE_SECURE_SETTING))
73+
return DisableSecureSetting(args);
74+
75+
if (args.Contains(ENABLE_SECURE_SETTING_FOR_USER))
76+
return EnableSecureSettingForUser(args);
77+
78+
if (args.Contains(DISABLE_SECURE_SETTING_FOR_USER))
79+
return DisableSecureSettingForUser(args);
80+
81+
return null;
82+
}
83+
84+
private static int ImportSettings(string[] args)
85+
{
86+
int idx = Array.IndexOf(args, IMPORT_SETTINGS);
87+
if (idx < 0 || idx + 1 >= args.Length)
88+
return (int)ExitCode.InvalidParameter;
89+
90+
var file = args[idx + 1].Trim('"').Trim('\'');
91+
if (!File.Exists(file))
92+
return (int)ExitCode.NoSuchFile;
93+
94+
try
95+
{
96+
Settings.ImportFromFile_JSON(file);
97+
return (int)ExitCode.Success;
98+
}
99+
catch (Exception ex)
100+
{
101+
Logger.Error(ex);
102+
return ex.HResult;
103+
}
104+
}
105+
106+
private static int ExportSettings(string[] args)
107+
{
108+
int idx = Array.IndexOf(args, EXPORT_SETTINGS);
109+
if (idx < 0 || idx + 1 >= args.Length)
110+
return (int)ExitCode.InvalidParameter;
111+
112+
var file = args[idx + 1].Trim('"').Trim('\'');
113+
try
114+
{
115+
Settings.ExportToFile_JSON(file);
116+
return (int)ExitCode.Success;
117+
}
118+
catch (Exception ex)
119+
{
120+
Logger.Error(ex);
121+
return ex.HResult;
122+
}
123+
}
124+
125+
private static int EnableSetting(string[] args)
126+
{
127+
int idx = Array.IndexOf(args, ENABLE_SETTING);
128+
if (idx < 0 || idx + 1 >= args.Length)
129+
return (int)ExitCode.InvalidParameter;
130+
131+
if (!Enum.TryParse(args[idx + 1].Trim('"').Trim('\''), out Settings.K key))
132+
return (int)ExitCode.UnknownSettingsKey;
133+
134+
try { Settings.Set(key, true); return (int)ExitCode.Success; }
135+
catch (Exception ex) { return ex.HResult; }
136+
}
137+
138+
private static int DisableSetting(string[] args)
139+
{
140+
int idx = Array.IndexOf(args, DISABLE_SETTING);
141+
if (idx < 0 || idx + 1 >= args.Length)
142+
return (int)ExitCode.InvalidParameter;
143+
144+
if (!Enum.TryParse(args[idx + 1].Trim('"').Trim('\''), out Settings.K key))
145+
return (int)ExitCode.UnknownSettingsKey;
146+
147+
try { Settings.Set(key, false); return (int)ExitCode.Success; }
148+
catch (Exception ex) { return ex.HResult; }
149+
}
150+
151+
private static int SetSettingsValue(string[] args)
152+
{
153+
int idx = Array.IndexOf(args, SET_SETTING_VAL);
154+
if (idx < 0 || idx + 2 >= args.Length)
155+
return (int)ExitCode.InvalidParameter;
156+
157+
if (!Enum.TryParse(args[idx + 1].Trim('"').Trim('\''), out Settings.K key))
158+
return (int)ExitCode.UnknownSettingsKey;
159+
160+
try { Settings.SetValue(key, args[idx + 2]); return (int)ExitCode.Success; }
161+
catch (Exception ex) { return ex.HResult; }
162+
}
163+
164+
private static int EnableSecureSetting(string[] args)
165+
{
166+
int idx = Array.IndexOf(args, ENABLE_SECURE_SETTING);
167+
if (idx < 0 || idx + 1 >= args.Length)
168+
return (int)ExitCode.InvalidParameter;
169+
170+
if (!Enum.TryParse(args[idx + 1].Trim('"').Trim('\''), out SecureSettings.K key))
171+
return (int)ExitCode.UnknownSettingsKey;
172+
173+
try
174+
{
175+
bool ok = SecureSettings.TrySet(key, true).GetAwaiter().GetResult();
176+
return ok ? (int)ExitCode.Success : (int)ExitCode.Failed;
177+
}
178+
catch (Exception ex) { return ex.HResult; }
179+
}
180+
181+
private static int DisableSecureSetting(string[] args)
182+
{
183+
int idx = Array.IndexOf(args, DISABLE_SECURE_SETTING);
184+
if (idx < 0 || idx + 1 >= args.Length)
185+
return (int)ExitCode.InvalidParameter;
186+
187+
if (!Enum.TryParse(args[idx + 1].Trim('"').Trim('\''), out SecureSettings.K key))
188+
return (int)ExitCode.UnknownSettingsKey;
189+
190+
try
191+
{
192+
bool ok = SecureSettings.TrySet(key, false).GetAwaiter().GetResult();
193+
return ok ? (int)ExitCode.Success : (int)ExitCode.Failed;
194+
}
195+
catch (Exception ex) { return ex.HResult; }
196+
}
197+
198+
private static int EnableSecureSettingForUser(string[] args)
199+
{
200+
int idx = Array.IndexOf(args, ENABLE_SECURE_SETTING_FOR_USER);
201+
if (idx < 0 || idx + 2 >= args.Length)
202+
return (int)ExitCode.InvalidParameter;
203+
204+
var user = args[idx + 1].Trim('"').Trim('\'');
205+
var setting = args[idx + 2].Trim('"').Trim('\'');
206+
try { return SecureSettings.ApplyForUser(user, setting, true); }
207+
catch (Exception ex) { return ex.HResult; }
208+
}
209+
210+
private static int DisableSecureSettingForUser(string[] args)
211+
{
212+
int idx = Array.IndexOf(args, DISABLE_SECURE_SETTING_FOR_USER);
213+
if (idx < 0 || idx + 2 >= args.Length)
214+
return (int)ExitCode.InvalidParameter;
215+
216+
var user = args[idx + 1].Trim('"').Trim('\'');
217+
var setting = args[idx + 2].Trim('"').Trim('\'');
218+
try { return SecureSettings.ApplyForUser(user, setting, false); }
219+
catch (Exception ex) { return ex.HResult; }
220+
}
221+
}

0 commit comments

Comments
 (0)