PostableSettingsGrid.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using InABox.Core;
  2. using InABox.DynamicGrid;
  3. using InABox.Wpf.Grids;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace InABox.Wpf;
  10. public class PostableSettingsGrid : DynamicItemsListGrid<PostableSettings>
  11. {
  12. public PostableSettingsGrid()
  13. {
  14. OnCustomiseEditor += PostableSettingsGrid_OnCustomiseEditor;
  15. OnEditorValueChanged += PostableSettingsGrid_OnEditorValueChanged;
  16. }
  17. private Dictionary<string, object?> PostableSettingsGrid_OnEditorValueChanged(object sender, string name, object value)
  18. {
  19. var editorForm = (IDynamicEditorForm)sender;
  20. if (name == nameof(PostableSettings.PosterType))
  21. {
  22. var editor = (editorForm.FindEditor(name) as LookupEditorControl)!;
  23. (editor.EditorDefinition as ComboLookupEditor)!.Buttons![0].SetEnabled(!string.IsNullOrWhiteSpace(value as string));
  24. (editor.EditorDefinition as ComboLookupEditor)!.Buttons![1].SetVisible(
  25. CoreUtils.GetEntityOrNull(value as string)?.IsAssignableTo(typeof(IGlobalSettingsPoster)) == true);
  26. }
  27. return new();
  28. }
  29. private void PostableSettingsGrid_OnCustomiseEditor(IDynamicEditorForm sender, PostableSettings[]? items, DynamicGridColumn column, BaseEditor editor)
  30. {
  31. var settings = items?.FirstOrDefault();
  32. if (settings is null) return;
  33. if(column.ColumnName == nameof(PostableSettings.PosterType) && editor is ComboLookupEditor combo)
  34. {
  35. var settingsButton = new EditorButton(settings, "Settings", 60, ViewSettings, false);
  36. settingsButton.SetEnabled(!string.IsNullOrWhiteSpace(settings.PosterType));
  37. var globalSettingsButton = new EditorButton(settings, "Global Settings", 100, ViewGlobalSettings, false);
  38. globalSettingsButton.SetVisible(CoreUtils.GetEntityOrNull(settings.PosterType)?.IsAssignableTo(typeof(IGlobalSettingsPoster)) == true);
  39. combo.Buttons = [settingsButton, globalSettingsButton];
  40. }
  41. }
  42. private void ViewGlobalSettings(object editor, object? item)
  43. {
  44. if (item is not PostableSettings settings) return;
  45. var posterType = CoreUtils.GetEntityOrNull(settings.PosterType);
  46. var globalSettingsType = posterType?.GetInterfaceDefinition(typeof(IGlobalSettingsPoster<>))?.GenericTypeArguments[0];
  47. if (globalSettingsType is null) return;
  48. ConfigureGlobalPosterSettings(globalSettingsType);
  49. }
  50. private void ViewSettings(object editor, object? item)
  51. {
  52. if (item is not PostableSettings settings) return;
  53. var entityType = CoreUtils.GetEntityOrNull(settings.PostableType);
  54. if (entityType is null) return;
  55. var posterType = CoreUtils.GetEntityOrNull(settings.PosterType);
  56. var settingsType = posterType?.GetInterfaceDefinition(typeof(IPoster<,>))!.GenericTypeArguments[1];
  57. ConfigurePosterSettings(entityType, settingsType);
  58. }
  59. public static void ConfigureGlobalPosterSettings(Type globalSettingsType)
  60. {
  61. var globalPosterSettings = PosterUtils.LoadGlobalPosterSettings(globalSettingsType);
  62. var grid = DynamicGridUtils.CreateDynamicGrid(typeof(DynamicItemsListGrid<>), globalSettingsType);
  63. if(grid.EditItems(new object[] { globalPosterSettings }))
  64. {
  65. PosterUtils.SaveGlobalPosterSettings(globalSettingsType, globalPosterSettings);
  66. }
  67. }
  68. public static void ConfigurePosterSettings(Type entityType, Type settingsType)
  69. {
  70. var posterSettings = PosterUtils.LoadPosterSettings(entityType, settingsType);
  71. var grid = DynamicGridUtils.CreateDynamicGrid(typeof(PosterSettingsGrid<>), settingsType);
  72. if(grid.EditItems(new object[] { posterSettings }))
  73. {
  74. PosterUtils.SavePosterSettings(entityType, settingsType, posterSettings);
  75. }
  76. }
  77. public static void ConfigurePosterSettings<T>(Type settingsType)
  78. where T : Entity, IPostable, IRemotable, IPersistent, new() => ConfigurePosterSettings(typeof(T), settingsType);
  79. }