Skip to content
This repository was archived by the owner on Sep 25, 2024. It is now read-only.

Commit 28eb6e1

Browse files
author
Eric Maupin
committed
[Core/Win] Add ITypeInfo property editor
1 parent f332e8a commit 28eb6e1

File tree

11 files changed

+162
-2
lines changed

11 files changed

+162
-2
lines changed

Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public MockSampleControl ()
3737
AddProperty<CommonThickness> ("Thickness", ReadWrite);
3838
AddProperty<object> ("Object", ReadWrite);
3939
AddProperty<IList> ("Collection", ReadWrite);
40+
AddProperty<ITypeInfo> ("Type", ReadWrite, realType: typeof(Type).ToTypeInfo());
4041

4142
AddReadOnlyProperty<bool> ("ReadOnlyBoolean", ReadOnly);
4243
AddReadOnlyProperty<int> ("ReadOnlyInteger", ReadOnly);

Xamarin.PropertyEditing.Windows/EditorPropertySelector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private bool TryGetTemplate (Type type, out DataTemplate template)
108108
{ typeof(BrushPropertyViewModel), typeof(BrushEditorControl) },
109109
{ typeof(PropertyGroupViewModel), typeof(GroupEditorControl) },
110110
{ typeof(ObjectPropertyViewModel), typeof(ObjectEditorControl) },
111+
{ typeof(TypePropertyViewModel), typeof(TypeEditorControl) },
111112
{ typeof(CollectionPropertyViewModel), typeof(CollectionEditor) },
112113
{ typeof(RatioViewModel), typeof(RatioEditorControl) },
113114
};

Xamarin.PropertyEditing.Windows/Themes/Resources.xaml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,25 @@
417417
</Grid.ColumnDefinitions>
418418

419419
<TextBlock Text="{Binding ValueType.Name,StringFormat=({0})}" Grid.Column="0" VerticalAlignment="Center" />
420-
<Button AutomationProperties.Name="{Binding Property.Name,Mode=OneTime,StringFormat={x:Static prop:Resources.NewInstanceForProperty}}" MinHeight="20" MinWidth="40" Grid.Column="1" HorizontalAlignment="Right" Content="{x:Static prop:Resources.New}" Command="{Binding CreateInstanceCommand}" />
420+
<Button AutomationProperties.Name="{Binding Property.Name,Mode=OneTime,StringFormat={x:Static prop:Resources.NewInstanceForProperty}}" MinHeight="20" MinWidth="40" Grid.Column="1" HorizontalAlignment="Right" Content="{x:Static prop:Resources.New}" Command="{Binding CreateInstanceCommand,Mode=OneTime}" />
421+
</Grid>
422+
</ControlTemplate>
423+
</Setter.Value>
424+
</Setter>
425+
</Style>
426+
427+
<Style TargetType="local:TypeEditorControl">
428+
<Setter Property="Template">
429+
<Setter.Value>
430+
<ControlTemplate TargetType="local:TypeEditorControl">
431+
<Grid>
432+
<Grid.ColumnDefinitions>
433+
<ColumnDefinition Width="*" />
434+
<ColumnDefinition Width="Auto" />
435+
</Grid.ColumnDefinitions>
436+
437+
<TextBlock Text="{Binding Value.Name,StringFormat=({0})}" Grid.Column="0" VerticalAlignment="Center" />
438+
<Button AutomationProperties.Name="{Binding Property.Name,Mode=OneTime,StringFormat={x:Static prop:Resources.SelectTypeForProperty}}" MinHeight="20" MinWidth="40" Grid.Column="1" HorizontalAlignment="Right" Content="{x:Static prop:Resources.Select}" Command="{Binding SelectTypeCommand,Mode=OneTime}" />
421439
</Grid>
422440
</ControlTemplate>
423441
</Setter.Value>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Threading.Tasks;
2+
using System.Windows;
3+
using Xamarin.PropertyEditing.ViewModels;
4+
5+
namespace Xamarin.PropertyEditing.Windows
6+
{
7+
internal class TypeEditorControl
8+
: PropertyEditorControl
9+
{
10+
static TypeEditorControl ()
11+
{
12+
DefaultStyleKeyProperty.OverrideMetadata (typeof (TypeEditorControl), new FrameworkPropertyMetadata (typeof (TypeEditorControl)));
13+
}
14+
15+
public TypeEditorControl ()
16+
{
17+
DataContextChanged += OnDataContextChanged;
18+
}
19+
20+
private TypePropertyViewModel vm;
21+
22+
private void OnDataContextChanged (object sender, DependencyPropertyChangedEventArgs e)
23+
{
24+
if (this.vm != null)
25+
this.vm.TypeRequested -= OnTypeRequested;
26+
27+
this.vm = e.NewValue as TypePropertyViewModel;
28+
if (this.vm != null)
29+
this.vm.TypeRequested += OnTypeRequested;
30+
}
31+
32+
private void OnTypeRequested (object sender, TypeRequestedEventArgs e)
33+
{
34+
var vsender = (TypePropertyViewModel)sender;
35+
36+
var panel = this.FindPropertiesHost ();
37+
38+
ITypeInfo type = TypeSelectorWindow.RequestType (panel, vsender.AssignableTypes);
39+
e.SelectedType = Task.FromResult (type);
40+
}
41+
}
42+
}

Xamarin.PropertyEditing.Windows/Xamarin.PropertyEditing.Windows.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
<Compile Include="ThicknessEditorControl.cs" />
143143
<Compile Include="StringEditorControl.cs" />
144144
<Compile Include="TreeViewItemEx.cs" />
145+
<Compile Include="TypeEditorControl.cs" />
145146
<Compile Include="TypeSelectorControl.xaml.cs">
146147
<DependentUpon>TypeSelectorControl.xaml</DependentUpon>
147148
</Compile>

Xamarin.PropertyEditing/Properties/Resources.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Xamarin.PropertyEditing/Properties/Resources.resx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,11 @@
570570
<value>New {0}</value>
571571
<comment>New {PropertyName}</comment>
572572
</data>
573+
<data name="Select" xml:space="preserve">
574+
<value>Select</value>
575+
</data>
576+
<data name="SelectTypeForProperty" xml:space="preserve">
577+
<value>Select {0} type</value>
578+
<comment>Select {property} type</comment>
579+
</data>
573580
</root>

Xamarin.PropertyEditing/Reflection/ReflectionObjectEditor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ internal static Task<AssignableTypesResult> GetAssignableTypes (ITypeInfo type,
159159
}
160160
}
161161

162-
types = types.Where (t => realType.IsAssignableFrom (t));
162+
if (realType != typeof(Type))
163+
types = types.Where (t => realType.IsAssignableFrom (t));
163164

164165
return new AssignableTypesResult (types.Select (t => {
165166
string asmName = t.Assembly.GetName ().Name;

Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ protected internal static AsyncWorkQueue AsyncWork
614614
{ typeof(BindingSource), (tp,p,e,v) => new PropertyViewModel<BindingSource> (tp, p, e, v) },
615615
{ typeof(Resource), (tp,p,e,v) => new PropertyViewModel<Resource> (tp, p, e, v) },
616616
{ typeof(object), (tp,p,e,v) => new ObjectPropertyViewModel (tp, p, e, v) },
617+
{ typeof(ITypeInfo), (tp,p,e,v) => new TypePropertyViewModel (tp, p, e, v) },
617618
{ typeof(CommonRatio), (tp, p, e, v) => new RatioViewModel (tp, p, e, v) },
618619
};
619620
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows.Input;
7+
8+
namespace Xamarin.PropertyEditing.ViewModels
9+
{
10+
internal class TypePropertyViewModel
11+
: PropertyViewModel<ITypeInfo>
12+
{
13+
public TypePropertyViewModel (TargetPlatform platform, IPropertyInfo propertyInfo, IEnumerable<IObjectEditor> editors, PropertyVariation variation = null)
14+
: base (platform, propertyInfo, editors, variation)
15+
{
16+
SelectTypeCommand = new RelayCommand (SetType, () => Property.CanWrite);
17+
}
18+
19+
public event EventHandler<TypeRequestedEventArgs> TypeRequested;
20+
21+
public ICommand SelectTypeCommand
22+
{
23+
get;
24+
}
25+
26+
public AsyncValue<IReadOnlyDictionary<IAssemblyInfo, ILookup<string, ITypeInfo>>> AssignableTypes
27+
{
28+
get
29+
{
30+
if (this.assignableTypes == null)
31+
this.assignableTypes = new AsyncValue<IReadOnlyDictionary<IAssemblyInfo, ILookup<string, ITypeInfo>>> (GetAssignableTypesAsync ());
32+
33+
return this.assignableTypes;
34+
}
35+
}
36+
37+
private AsyncValue<IReadOnlyDictionary<IAssemblyInfo, ILookup<string, ITypeInfo>>> assignableTypes;
38+
39+
private async Task<IReadOnlyDictionary<IAssemblyInfo, ILookup<string, ITypeInfo>>> GetAssignableTypesAsync ()
40+
{
41+
AssignableTypesResult result = await Editors.GetCommonAssignableTypes (Property, childTypes: false).ConfigureAwait (false);
42+
return result.GetTypeTree ();
43+
}
44+
45+
private async void SetType ()
46+
{
47+
using (await AsyncWork.RequestAsyncWork (this)) {
48+
ITypeInfo selectedType = null;
49+
var args = new TypeRequestedEventArgs ();
50+
TypeRequested?.Invoke (this, args);
51+
if (args.SelectedType == null)
52+
return;
53+
54+
try {
55+
selectedType = await args.SelectedType;
56+
if (selectedType == null)
57+
return;
58+
} catch (OperationCanceledException) {
59+
return;
60+
}
61+
62+
await SetValueAsync (new ValueInfo<ITypeInfo> {
63+
Value = selectedType,
64+
Source = ValueSource.Local
65+
});
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)