Update uses of Avalonia APIs & safeguard clipboard code

This commit is contained in:
chylex 2022-02-27 15:29:49 +01:00
parent 20aac4c47a
commit ebfe972a98
No known key found for this signature in database
GPG Key ID: 4DE42C8F19A80548
3 changed files with 20 additions and 10 deletions

View File

@ -4,11 +4,11 @@ using Avalonia.Data.Converters;
namespace DHT.Desktop.Common { namespace DHT.Desktop.Common {
sealed class NumberValueConverter : IValueConverter { sealed class NumberValueConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
return string.Format(Program.Culture, "{0:n0}", value); return string.Format(Program.Culture, "{0:n0}", value);
} }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
throw new NotSupportedException(); throw new NotSupportedException();
} }
} }

View File

@ -24,9 +24,7 @@ namespace DHT.Desktop.Main.Pages {
var originalText = button.Content; var originalText = button.Content;
button.MinWidth = button.Bounds.Width; button.MinWidth = button.Bounds.Width;
await model.OnClickCopyTrackingScript(); if (await model.OnClickCopyTrackingScript() && !isCopyingScript) {
if (!isCopyingScript) {
isCopyingScript = true; isCopyingScript = true;
button.Content = "Script Copied!"; button.Content = "Script Copied!";

View File

@ -67,7 +67,7 @@ namespace DHT.Desktop.Main.Pages {
if (!IsToggleAppDevToolsButtonEnabled) { if (!IsToggleAppDevToolsButtonEnabled) {
return "Unavailable"; return "Unavailable";
} }
return AreDevToolsEnabled ? "Disable Ctrl+Shift+I" : "Enable Ctrl+Shift+I"; return AreDevToolsEnabled ? "Disable Ctrl+Shift+I" : "Enable Ctrl+Shift+I";
} }
} }
@ -149,7 +149,7 @@ namespace DHT.Desktop.Main.Pages {
} }
} }
public async Task OnClickCopyTrackingScript() { public async Task<bool> OnClickCopyTrackingScript() {
string bootstrap = await Resources.ReadTextAsync("Tracker/bootstrap.js"); string bootstrap = await Resources.ReadTextAsync("Tracker/bootstrap.js");
string script = bootstrap.Replace("= 0; /*[PORT]*/", "= " + ServerPort + ";") string script = bootstrap.Replace("= 0; /*[PORT]*/", "= " + ServerPort + ";")
.Replace("/*[TOKEN]*/", HttpUtility.JavaScriptStringEncode(ServerToken)) .Replace("/*[TOKEN]*/", HttpUtility.JavaScriptStringEncode(ServerToken))
@ -157,7 +157,19 @@ namespace DHT.Desktop.Main.Pages {
.Replace("/*[CSS-CONTROLLER]*/", await Resources.ReadTextAsync("Tracker/styles/controller.css")) .Replace("/*[CSS-CONTROLLER]*/", await Resources.ReadTextAsync("Tracker/styles/controller.css"))
.Replace("/*[CSS-SETTINGS]*/", await Resources.ReadTextAsync("Tracker/styles/settings.css")); .Replace("/*[CSS-SETTINGS]*/", await Resources.ReadTextAsync("Tracker/styles/settings.css"));
await Application.Current.Clipboard.SetTextAsync(script); var clipboard = Application.Current?.Clipboard;
if (clipboard == null) {
await Dialog.ShowOk(window, "Copy Tracking Script", "Clipboard is not available on this system.");
return false;
}
try {
await clipboard.SetTextAsync(script);
return true;
} catch {
await Dialog.ShowOk(window, "Copy Tracking Script", "An error occurred while copying to clipboard.");
return false;
}
} }
public void OnClickRandomizeToken() { public void OnClickRandomizeToken() {
@ -179,7 +191,7 @@ namespace DHT.Desktop.Main.Pages {
public async void OnClickToggleAppDevTools() { public async void OnClickToggleAppDevTools() {
const string DialogTitle = "Discord App Settings File"; const string DialogTitle = "Discord App Settings File";
bool oldState = AreDevToolsEnabled; bool oldState = AreDevToolsEnabled;
bool newState = !oldState; bool newState = !oldState;
@ -188,7 +200,7 @@ namespace DHT.Desktop.Main.Pages {
AreDevToolsEnabled = newState; AreDevToolsEnabled = newState;
await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I was " + (newState ? "enabled." : "disabled.") + " Restart the Discord app for the change to take effect."); await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I was " + (newState ? "enabled." : "disabled.") + " Restart the Discord app for the change to take effect.");
break; break;
case SettingsJsonResult.AlreadySet: case SettingsJsonResult.AlreadySet:
await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I is already " + (newState ? "enabled." : "disabled.")); await Dialog.ShowOk(window, DialogTitle, "Ctrl+Shift+I is already " + (newState ? "enabled." : "disabled."));
AreDevToolsEnabled = newState; AreDevToolsEnabled = newState;