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

Commit af9ca3b

Browse files
author
Eric Maupin
committed
[mac] TypeEditorControl
1 parent 82cc1d7 commit af9ca3b

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
using System;
2+
using System.Collections;
3+
using System.ComponentModel;
4+
using System.Threading.Tasks;
5+
using AppKit;
6+
using Foundation;
7+
using Xamarin.PropertyEditing.ViewModels;
8+
9+
namespace Xamarin.PropertyEditing.Mac
10+
{
11+
internal class TypeEditorControl
12+
: PropertyEditorControl<TypePropertyViewModel>
13+
{
14+
public TypeEditorControl (IHostResourceProvider hostResources)
15+
: base (hostResources)
16+
{
17+
this.typeLabel = new UnfocusableTextField {
18+
TranslatesAutoresizingMaskIntoConstraints = false
19+
};
20+
AddSubview (this.typeLabel);
21+
22+
this.selectType = new NSButton {
23+
Title = Properties.Resources.Select,
24+
TranslatesAutoresizingMaskIntoConstraints = false,
25+
BezelStyle = NSBezelStyle.Rounded
26+
};
27+
this.selectType.Activated += OnSelectPressed;
28+
AddSubview (this.selectType);
29+
30+
this.buttonConstraint = NSLayoutConstraint.Create (this.selectType, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this.typeLabel, NSLayoutAttribute.Trailing, 1f, 12);
31+
32+
AddConstraints (new[] {
33+
NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this, NSLayoutAttribute.Leading, 1f, 0f),
34+
NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
35+
NSLayoutConstraint.Create (this.typeLabel, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1, 0),
36+
this.buttonConstraint,
37+
NSLayoutConstraint.Create (this.selectType, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this, NSLayoutAttribute.Leading, 1, 0).WithPriority (NSLayoutPriority.DefaultLow),
38+
NSLayoutConstraint.Create (this.selectType, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
39+
NSLayoutConstraint.Create (this.selectType, NSLayoutAttribute.Width, NSLayoutRelation.GreaterThanOrEqual, 1f, 70f),
40+
});
41+
}
42+
43+
public override NSView FirstKeyView => this.selectType;
44+
45+
public override NSView LastKeyView => this.selectType;
46+
47+
protected override void UpdateValue ()
48+
{
49+
}
50+
51+
protected override void UpdateErrorsDisplayed (IEnumerable errors)
52+
{
53+
}
54+
55+
protected override void HandleErrorsChanged (object sender, DataErrorsChangedEventArgs e)
56+
{
57+
}
58+
59+
protected override void SetEnabled ()
60+
{
61+
this.selectType.Enabled = ViewModel.Property.CanWrite;
62+
}
63+
64+
protected override void UpdateAccessibilityValues ()
65+
{
66+
this.selectType.AccessibilityTitle = String.Format (Properties.Resources.SelectTypeForProperty, ViewModel.Property.Name);
67+
}
68+
69+
protected override void OnViewModelChanged (PropertyViewModel oldModel)
70+
{
71+
base.OnViewModelChanged (oldModel);
72+
73+
if (oldModel is TypePropertyViewModel tvm) {
74+
tvm.TypeRequested -= OnTypeRequested;
75+
}
76+
77+
if (ViewModel != null) {
78+
ViewModel.TypeRequested += OnTypeRequested;
79+
80+
OnPropertyChanged (ViewModel, new PropertyChangedEventArgs (null));
81+
}
82+
}
83+
84+
protected override void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
85+
{
86+
switch (e.PropertyName) {
87+
case nameof (TypePropertyViewModel.Value):
88+
UpdateTypeLabel ();
89+
break;
90+
case null:
91+
case "":
92+
UpdateTypeLabel ();
93+
UpdateCreateInstanceCommand ();
94+
break;
95+
}
96+
97+
base.OnPropertyChanged (sender, e);
98+
}
99+
100+
private readonly UnfocusableTextField typeLabel;
101+
private readonly NSButton selectType;
102+
private readonly NSLayoutConstraint buttonConstraint;
103+
104+
private void OnCreateInstanceExecutableChanged (object sender, EventArgs e)
105+
{
106+
UpdateCreateInstanceCommand ();
107+
}
108+
109+
private void OnTypeRequested (object sender, TypeRequestedEventArgs e)
110+
{
111+
e.SelectedType = e.RequestAt (HostResources, this.selectType, ViewModel.AssignableTypes);
112+
}
113+
114+
private void UpdateTypeLabel ()
115+
{
116+
if (ViewModel.Value == null) {
117+
this.typeLabel.StringValue = String.Empty;
118+
this.buttonConstraint.Active = false;
119+
} else {
120+
this.typeLabel.StringValue = $"({ViewModel.Value.Name})";
121+
this.buttonConstraint.Active = true;
122+
}
123+
}
124+
125+
private void UpdateCreateInstanceCommand ()
126+
{
127+
this.selectType.Enabled = ViewModel.SelectTypeCommand.CanExecute (null);
128+
}
129+
130+
private void OnSelectPressed (object sender, EventArgs e)
131+
{
132+
ViewModel.SelectTypeCommand.Execute (null);
133+
}
134+
135+
private class PopoverDelegate<T>
136+
: NSPopoverDelegate
137+
{
138+
public PopoverDelegate (TaskCompletionSource<T> tcs)
139+
{
140+
this.tcs = tcs;
141+
}
142+
143+
public override void WillClose (NSNotification notification)
144+
{
145+
this.tcs.TrySetCanceled ();
146+
}
147+
148+
private readonly TaskCompletionSource<T> tcs;
149+
}
150+
}
151+
}

Xamarin.PropertyEditing.Mac/PropertyEditorSelector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public virtual IEditorView GetEditor (IHostResourceProvider hostResources, Edito
5555
{typeof (ThicknessPropertyViewModel), typeof (CommonThicknessEditorControl) },
5656
{typeof (PropertyGroupViewModel), typeof (GroupEditorControl)},
5757
{typeof (ObjectPropertyViewModel), typeof (ObjectEditorControl)},
58+
{typeof (TypePropertyViewModel), typeof (TypeEditorControl)},
5859

5960
};
6061
}

Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
<Compile Include="Controls\TypeSelectorWindow.cs" />
151151
<Compile Include="Controls\Custom\PropertyTextField.cs" />
152152
<Compile Include="Controls\ControlExtensions.cs" />
153+
<Compile Include="Controls\TypeEditorControl.cs" />
153154
</ItemGroup>
154155
<ItemGroup>
155156
<Folder Include="Controls\" />

0 commit comments

Comments
 (0)