1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using Xamarin.PropertyEditing.ViewModels;
namespace Xamarin.PropertyEditing.Windows
{
[TemplatePart (Name = "search", Type = typeof(TextBox))]
[TemplatePart (Name = "propertyItems", Type = typeof(ItemsControl))]
[TemplatePart (Name = "paneSelector", Type = typeof(ChoiceControl))]
public class PropertyEditorPanel
: Control, IPropertiesHost
{
public PropertyEditorPanel ()
{
DefaultStyleKey = typeof(PropertyEditorPanel);
Resources.MergedDictionaries.Add (new ResourceDictionary { Source = new Uri ("pack://application:,,,/Xamarin.PropertyEditing.Windows;component/Themes/Resources.xaml") });
var selectedItems = new ObservableCollectionEx<object> ();
selectedItems.CollectionChanged += OnSelectedItemsChanged;
SelectedItems = selectedItems;
}
public static readonly DependencyProperty ResourceProviderProperty = DependencyProperty.Register (
nameof(ResourceProvider), typeof(IResourceProvider), typeof(PropertyEditorPanel), new PropertyMetadata (default(IResourceProvider), (o, args) => ((PropertyEditorPanel)o).OnTargetPlatformChanged()));
public IResourceProvider ResourceProvider
{
get { return (IResourceProvider) GetValue (ResourceProviderProperty); }
set { SetValue (ResourceProviderProperty, value); }
}
public static readonly DependencyProperty TargetPlatformProperty = DependencyProperty.Register (
"TargetPlatform", typeof(TargetPlatform), typeof(PropertyEditorPanel), new PropertyMetadata (null, (o,e) => ((PropertyEditorPanel)o).OnTargetPlatformChanged ()));
public TargetPlatform TargetPlatform
{
get { return (TargetPlatform) GetValue (TargetPlatformProperty); }
set { SetValue (TargetPlatformProperty, value); }
}
private static readonly DependencyPropertyKey SelectedItemsPropertyKey = DependencyProperty.RegisterReadOnly (
nameof(SelectedItems), typeof(IList), typeof(PropertyEditorPanel), new PropertyMetadata (default(IList)));
public static readonly DependencyProperty SelectedItemsProperty = SelectedItemsPropertyKey.DependencyProperty;
public IList SelectedItems
{
get { return (IList) GetValue (SelectedItemsProperty); }
private set { SetValue (SelectedItemsPropertyKey, value); }
}
public static readonly DependencyProperty IsArrangeEnabledProperty = DependencyProperty.Register (
nameof(IsArrangeEnabled), typeof(bool), typeof(PropertyEditorPanel), new PropertyMetadata (true));
public bool IsArrangeEnabled
{
get { return (bool) GetValue (IsArrangeEnabledProperty); }
set { SetValue (IsArrangeEnabledProperty, value); }
}
public static readonly DependencyProperty ArrangeModeProperty = DependencyProperty.Register (
nameof(ArrangeMode), typeof(PropertyArrangeMode), typeof(PropertyEditorPanel), new PropertyMetadata (PropertyArrangeMode.Name, (o, args) => ((PropertyEditorPanel)o).OnArrangeModeChanged ((PropertyArrangeMode)args.NewValue)));
public PropertyArrangeMode ArrangeMode
{
get { return (PropertyArrangeMode) GetValue (ArrangeModeProperty); }
set { SetValue (ArrangeModeProperty, value); }
}
public static PropertyEditing.Themes.WinThemeManager ThemeManager = new PropertyEditing.Themes.WinThemeManager();
public void Select (IEnumerable<object> selectedItems)
{
if (selectedItems == null)
throw new ArgumentNullException (nameof(selectedItems));
((ObservableCollectionEx<object>)SelectedItems).Reset (selectedItems);
}
public override void OnApplyTemplate ()
{
base.OnApplyTemplate ();
this.root = (FrameworkElement) GetTemplateChild ("root");
this.typeIcon = (Image) GetTemplateChild ("typeIcon");
this.items = (ItemsControl) GetTemplateChild ("propertyItems");
this.propertiesPane = (FrameworkElement) GetTemplateChild ("propertiesPane");
this.eventsPane = (FrameworkElement) GetTemplateChild ("eventsPane");
this.paneSelector = (ChoiceControl) GetTemplateChild ("paneSelector");
this.paneSelector.SelectedValue = EditingPane.Properties;
this.paneSelector.SelectedItemChanged += OnPaneChanged;
OnTargetPlatformChanged();
if (this.vm.SelectedObjects.Count > 0)
OnArrangeModeChanged (ArrangeMode);
}
private FrameworkElement root;
private PanelViewModel vm;
private ItemsControl items;
private ChoiceControl paneSelector;
private FrameworkElement propertiesPane, eventsPane;
private Image typeIcon;
private void OnPaneChanged (object sender, EventArgs e)
{
object selected = this.paneSelector.SelectedValue;
EditingPane pane = EditingPane.Properties;
if (selected != null)
pane = (EditingPane)selected;
if (pane == EditingPane.Properties) {
this.eventsPane.Visibility = Visibility.Collapsed;
this.propertiesPane.Visibility = Visibility.Visible;
} else if (pane == EditingPane.Events) {
this.propertiesPane.Visibility = Visibility.Collapsed;
this.eventsPane.Visibility = Visibility.Visible;
}
}
private void OnSelectedItemsChanged (object sender, NotifyCollectionChangedEventArgs e)
{
if (this.vm == null)
return;
switch (e.Action) {
case NotifyCollectionChangedAction.Add:
for (int i = 0; i < e.NewItems.Count; i++)
this.vm.SelectedObjects.Add (e.NewItems[i]);
break;
case NotifyCollectionChangedAction.Remove:
for (int i = 0; i < e.OldItems.Count; i++)
this.vm.SelectedObjects.Remove (e.OldItems[i]);
break;
case NotifyCollectionChangedAction.Replace: // TODO properly
case NotifyCollectionChangedAction.Move:
case NotifyCollectionChangedAction.Reset:
this.vm.SelectedObjects.Clear();
this.vm.SelectedObjects.AddItems (SelectedItems);
break;
}
if (ArrangeMode == PropertyArrangeMode.Name)
OnArrangeModeChanged (ArrangeMode);
UpdateIcon ();
}
private async void UpdateIcon ()
{
if (this.typeIcon == null)
return;
Stream icon = await this.vm.GetIconAsync ();
if (icon == null) {
this.typeIcon.Source = null;
this.typeIcon.Visibility = Visibility.Collapsed;
} else {
var source = new BitmapImage();
source.BeginInit();
source.StreamSource = icon;
source.EndInit();
this.typeIcon.Source = source;
this.typeIcon.Visibility = Visibility.Visible;
}
}
private void OnTargetPlatformChanged ()
{
if (this.root == null)
return;
if (this.vm != null)
this.vm.PropertyChanged -= OnVmPropertyChanged;
PanelViewModel newVm = null;
if (TargetPlatform != null)
newVm = new PanelViewModel (TargetPlatform);
this.root.DataContext = this.vm = newVm;
if (this.vm != null)
this.vm.PropertyChanged += OnVmPropertyChanged;
}
private void OnVmPropertyChanged (object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(PanelViewModel.ArrangeMode))
SetCurrentValue (ArrangeModeProperty, this.vm.ArrangeMode);
}
private void OnArrangeModeChanged (PropertyArrangeMode newMode)
{
if (this.items == null)
return;
Binding itemsSource;
if (newMode == PropertyArrangeMode.Name)
itemsSource = new Binding ("ArrangedEditors[0].Editors");
else
itemsSource = new Binding ("ArrangedEditors");
this.items.SetBinding (ItemsControl.ItemsSourceProperty, itemsSource);
}
}
internal enum EditingPane
{
Properties,
Events
}
}
|