|
1 | 1 | using PropertyTools.Wpf; |
| 2 | +using System.Collections; |
2 | 3 | using System.ComponentModel.DataAnnotations; |
3 | 4 | using System.ComponentModel; |
4 | 5 | using System.Windows.Data; |
5 | 6 | using System.Globalization; |
6 | 7 | using System.Windows.Input; |
7 | 8 | using PropertyTools.DataAnnotations; |
| 9 | +using WindowTranslator.ComponentModel; |
| 10 | +using WindowTranslator.Stores; |
8 | 11 |
|
9 | 12 | namespace WindowTranslator.Modules.Settings; |
| 13 | + |
| 14 | +internal interface IEditableItemsPropertyItem |
| 15 | +{ |
| 16 | + IEnumerable? EditableCandidates { get; set; } |
| 17 | +} |
| 18 | + |
10 | 19 | internal class SettingsPropertyGridOperator : PropertyGridOperator |
11 | 20 | { |
| 21 | + public IModelHistoryStore? HistoryStore { get; set; } |
| 22 | + |
12 | 23 | public SettingsPropertyGridOperator() |
13 | 24 | { |
14 | 25 | this.ModifyCamelCaseDisplayNames = false; |
@@ -86,17 +97,35 @@ protected override void SetAttribute(Attribute attribute, PropertyItem pi, objec |
86 | 97 | { |
87 | 98 | pi.SortIndex = order; |
88 | 99 | } |
| 100 | + if (attribute is EditableItemsSourceAttribute _ && pi is IEditableItemsPropertyItem editableItem) |
| 101 | + { |
| 102 | + var key = $"{instance.GetType().Name}.{pi.Descriptor.Name}"; |
| 103 | + editableItem.EditableCandidates = this.HistoryStore?.GetHistory(key) ?? []; |
| 104 | + if (this.HistoryStore is { } store) |
| 105 | + { |
| 106 | + pi.Descriptor.AddValueChanged(instance, (s, e) => |
| 107 | + { |
| 108 | + if (pi.Descriptor.GetValue(instance) is string value && !string.IsNullOrWhiteSpace(value)) |
| 109 | + { |
| 110 | + store.AddHistory(key, value); |
| 111 | + store.Save(); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + } |
89 | 116 | base.SetAttribute(attribute, pi, instance); |
90 | 117 | } |
91 | 118 |
|
92 | 119 | protected override PropertyItem CreateCore(PropertyDescriptor pd, PropertyDescriptorCollection propertyDescriptors) |
93 | 120 | => new ParentablePropertyItem(pd, propertyDescriptors); |
94 | 121 |
|
95 | 122 | private class ParentablePropertyItem(PropertyDescriptor propertyDescriptor, PropertyDescriptorCollection propertyDescriptors) |
96 | | - : PropertyItem(propertyDescriptor, propertyDescriptors) |
| 123 | + : PropertyItem(propertyDescriptor, propertyDescriptors), IEditableItemsPropertyItem |
97 | 124 | { |
98 | 125 | private readonly Stack<string> parents = new(); |
99 | 126 |
|
| 127 | + public IEnumerable? EditableCandidates { get; set; } |
| 128 | + |
100 | 129 | public void AddParent(string parent) |
101 | 130 | => parents.Push(parent); |
102 | 131 |
|
|
0 commit comments