SettingsWindow.axaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Avalonia.Controls;
  2. using SlamCross.Models;
  3. namespace SlamCross.Views;
  4. public partial class SettingsWindow : Window
  5. {
  6. private readonly AppSettings _settings;
  7. public SettingsWindow(AppSettings settings)
  8. {
  9. _settings = settings;
  10. InitializeComponent();
  11. LoadSettings();
  12. }
  13. private void LoadSettings()
  14. {
  15. Set<CheckBox>("StartEnabled", b => b.IsChecked = _settings.StartEnabled);
  16. Set<CheckBox>("StartMinimized",b => b.IsChecked = _settings.StartMinimized);
  17. Set<CheckBox>("HoldToPlay", b => b.IsChecked = _settings.HoldToPlay);
  18. Set<CheckBox>("LogError", b => b.IsChecked = _settings.LogError);
  19. Set<CheckBox>("WriteTags", b => b.IsChecked = _settings.WriteTags);
  20. Set<CheckBox>("OverrideFolders",b => b.IsChecked = _settings.OverrideFolders);
  21. Set<TextBox>("RelayKeyBox", b => b.Text = _settings.RelayKey);
  22. Set<TextBox>("SteamAppsBox", b => b.Text = _settings.SteamAppsOverride);
  23. Set<TextBox>("UserDataBox", b => b.Text = _settings.UserDataOverride);
  24. UpdateOverrideVisibility();
  25. }
  26. private void OverrideChanged(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  27. => UpdateOverrideVisibility();
  28. private void UpdateOverrideVisibility()
  29. {
  30. var visible = this.FindControl<CheckBox>("OverrideFolders")?.IsChecked == true;
  31. if (this.FindControl<Grid>("OverridePanel") is Grid panel)
  32. panel.IsVisible = visible;
  33. }
  34. private void Save_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  35. {
  36. _settings.StartEnabled = Get<CheckBox>("StartEnabled")?.IsChecked == true;
  37. _settings.StartMinimized = Get<CheckBox>("StartMinimized")?.IsChecked == true;
  38. _settings.HoldToPlay = Get<CheckBox>("HoldToPlay")?.IsChecked == true;
  39. _settings.LogError = Get<CheckBox>("LogError")?.IsChecked == true;
  40. _settings.WriteTags = Get<CheckBox>("WriteTags")?.IsChecked == true;
  41. _settings.OverrideFolders= Get<CheckBox>("OverrideFolders")?.IsChecked == true;
  42. var relay = Get<TextBox>("RelayKeyBox")?.Text?.Trim();
  43. if (!string.IsNullOrEmpty(relay) && !relay.Equals(_settings.PlayKey, System.StringComparison.OrdinalIgnoreCase))
  44. _settings.RelayKey = relay;
  45. _settings.SteamAppsOverride = Get<TextBox>("SteamAppsBox")?.Text?.Trim() ?? "";
  46. _settings.UserDataOverride = Get<TextBox>("UserDataBox")?.Text?.Trim() ?? "";
  47. Close();
  48. }
  49. private void Close_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e) => Close();
  50. private T? Get<T>(string name) where T : Control => this.FindControl<T>(name);
  51. private void Set<T>(string name, System.Action<T> action) where T : Control
  52. {
  53. if (this.FindControl<T>(name) is T ctrl) action(ctrl);
  54. }
  55. }