Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Maupin <ermaup@microsoft.com>2019-03-21 22:25:00 +0300
committerEric Maupin <ermaup@microsoft.com>2019-03-21 22:42:03 +0300
commit05850cfee27656d614b6b1ed82aeedef619a7fb2 (patch)
treeaefb095324f38c839003d08d2534a3fa94003d08
parent80d08acc4c46a90853dbe7e8e92b02492f62aeca (diff)
[mac] Refactor string editor into common base entry
-rw-r--r--Xamarin.PropertyEditing.Mac/Controls/EntryPropertyEditor.cs99
-rw-r--r--Xamarin.PropertyEditing.Mac/Controls/StringEditorControl.cs60
-rw-r--r--Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj1
3 files changed, 119 insertions, 41 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/EntryPropertyEditor.cs b/Xamarin.PropertyEditing.Mac/Controls/EntryPropertyEditor.cs
new file mode 100644
index 0000000..ed250c0
--- /dev/null
+++ b/Xamarin.PropertyEditing.Mac/Controls/EntryPropertyEditor.cs
@@ -0,0 +1,99 @@
+using System;
+using AppKit;
+using Foundation;
+using Xamarin.PropertyEditing.Mac.Resources;
+using Xamarin.PropertyEditing.ViewModels;
+
+namespace Xamarin.PropertyEditing.Mac
+{
+ internal abstract class EntryPropertyEditor<T>
+ : PropertyEditorControl<PropertyViewModel<T>>
+ {
+ public EntryPropertyEditor (IHostResourceProvider hostResources)
+ : base (hostResources)
+ {
+ Entry = new PropertyTextField {
+ BackgroundColor = NSColor.Clear,
+ ControlSize = NSControlSize.Small,
+ Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)),
+ TranslatesAutoresizingMaskIntoConstraints = false,
+ };
+ AddSubview (Entry);
+
+ RightEdgeConstraint = NSLayoutConstraint.Create (Entry, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, 0);
+ AddConstraints (new[] {
+ NSLayoutConstraint.Create (Entry, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0),
+ NSLayoutConstraint.Create (Entry, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, 0),
+ RightEdgeConstraint,
+ NSLayoutConstraint.Create (Entry, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1, -6),
+ });
+ }
+
+ public override NSView FirstKeyView => Entry;
+ public override NSView LastKeyView => Entry;
+
+ protected PropertyTextField Entry
+ {
+ get;
+ }
+
+ protected NSLayoutConstraint RightEdgeConstraint { get; }
+
+ protected override void OnViewModelChanged (PropertyViewModel oldModel)
+ {
+ base.OnViewModelChanged (oldModel);
+ Entry.Delegate = (ViewModel != null) ? CreateDelegate (ViewModel) : null;
+ }
+
+ protected override void UpdateValue ()
+ {
+ Entry.StringValue = GetValue (ViewModel.Value) ?? String.Empty;
+ }
+
+ protected override void UpdateAccessibilityValues ()
+ {
+ Entry.AccessibilityEnabled = Entry.Enabled;
+ }
+
+ protected virtual EntryPropertyEditorDelegate<T> CreateDelegate (PropertyViewModel<T> viewModel)
+ {
+ return new EntryPropertyEditorDelegate<T> (viewModel);
+ }
+
+ protected virtual string GetValue (T value)
+ {
+ return value?.ToString ();
+ }
+ }
+
+ internal class EntryPropertyEditorDelegate<T>
+ : NSTextFieldDelegate
+ {
+ public EntryPropertyEditorDelegate (PropertyViewModel<T> viewModel)
+ {
+ if (viewModel == null)
+ throw new ArgumentNullException (nameof (viewModel));
+
+ ViewModel = viewModel;
+ }
+
+ public override void EditingEnded (NSNotification notification)
+ {
+ var text = (NSTextField)notification.Object;
+ ViewModel.Value = GetValue (text.StringValue);
+ }
+
+ protected PropertyViewModel<T> ViewModel
+ {
+ get;
+ }
+
+ protected virtual T GetValue (string value)
+ {
+ if (String.IsNullOrEmpty (value))
+ return default (T);
+
+ return (T)Convert.ChangeType (value, typeof(T));
+ }
+ }
+}
diff --git a/Xamarin.PropertyEditing.Mac/Controls/StringEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/StringEditorControl.cs
index 0b2933a..7696f79 100644
--- a/Xamarin.PropertyEditing.Mac/Controls/StringEditorControl.cs
+++ b/Xamarin.PropertyEditing.Mac/Controls/StringEditorControl.cs
@@ -10,46 +10,24 @@ using Xamarin.PropertyEditing.ViewModels;
namespace Xamarin.PropertyEditing.Mac
{
internal class StringEditorControl
- : PropertyEditorControl<PropertyViewModel<string>>
+ : EntryPropertyEditor<string>
{
- public override NSView FirstKeyView => this.stringEditor;
- private NSView lastKeyView;
- public override NSView LastKeyView => this.lastKeyView;
-
public StringEditorControl (IHostResourceProvider hostResources)
: base (hostResources)
{
- this.stringEditor = new PropertyTextField {
- BackgroundColor = NSColor.Clear,
- ControlSize = NSControlSize.Small,
- Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)),
- StringValue = string.Empty,
- TranslatesAutoresizingMaskIntoConstraints = false,
- };
-
- // update the value on keypress
- this.stringEditor.Changed += (sender, e) => {
- ViewModel.Value = this.stringEditor.StringValue;
- };
- AddSubview (this.stringEditor);
-
- this.lastKeyView = this.stringEditor;
- this.editorRightConstraint = NSLayoutConstraint.Create (this.stringEditor, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, 0);
-
- this.AddConstraints (new[] {
- NSLayoutConstraint.Create (this.stringEditor, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0),
- NSLayoutConstraint.Create (this.stringEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, 0),
- this.editorRightConstraint,
- NSLayoutConstraint.Create (this.stringEditor, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1, -6),
- });
+ this.lastKeyView = Entry;
}
+ public override NSView LastKeyView => this.lastKeyView;
+
protected override void UpdateValue ()
{
- this.stringEditor.StringValue = ViewModel.Value ?? string.Empty;
- this.stringEditor.Enabled = CanEnable;
+ base.UpdateValue ();
+
if (this.inputModePopup != null)
this.inputModePopup.SelectItem ((ViewModel.InputMode == null) ? string.Empty : ViewModel.InputMode.Identifier);
+
+ SetEnabled ();
}
protected override void OnViewModelChanged (PropertyViewModel oldModel)
@@ -59,7 +37,7 @@ namespace Xamarin.PropertyEditing.Mac
if (ViewModel == null)
return;
- this.editorRightConstraint.Active = !ViewModel.HasInputModes;
+ RightEdgeConstraint.Active = !ViewModel.HasInputModes;
if (ViewModel.HasInputModes) {
if (this.inputModePopup == null) {
this.inputModePopup = new FocusablePopUpButton {
@@ -76,14 +54,14 @@ namespace Xamarin.PropertyEditing.Mac
AddSubview (this.inputModePopup);
- this.editorInputModeConstraint = NSLayoutConstraint.Create (this.stringEditor, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.inputModePopup, NSLayoutAttribute.Left, 1, -4);
+ this.editorInputModeConstraint = NSLayoutConstraint.Create (Entry, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.inputModePopup, NSLayoutAttribute.Left, 1, -4);
AddConstraints (new[] {
this.editorInputModeConstraint,
NSLayoutConstraint.Create (this.inputModePopup, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
NSLayoutConstraint.Create (this.inputModePopup, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, 0),
NSLayoutConstraint.Create (this.inputModePopup, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, 80f),
- NSLayoutConstraint.Create (this.inputModePopup, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this.stringEditor, NSLayoutAttribute.Height, 1, 0),
+ NSLayoutConstraint.Create (this.inputModePopup, NSLayoutAttribute.Height, NSLayoutRelation.Equal, Entry, NSLayoutAttribute.Height, 1, 0),
});
this.lastKeyView = this.inputModePopup;
@@ -107,27 +85,27 @@ namespace Xamarin.PropertyEditing.Mac
protected override void SetEnabled ()
{
- this.stringEditor.Enabled = CanEnable;
+ Entry.Enabled = ViewModel.Property.CanWrite && (((ViewModel.InputMode != null) && !ViewModel.InputMode.IsSingleValue) || (this.inputModePopup == null));
+
if (this.inputModePopup != null)
this.inputModePopup.Enabled = ViewModel.Property.CanWrite;
}
protected override void UpdateAccessibilityValues ()
{
- this.stringEditor.AccessibilityEnabled = this.stringEditor.Enabled;
- this.stringEditor.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityString, ViewModel.Property.Name);
+ base.UpdateAccessibilityValues ();
+ Entry.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityString, ViewModel.Property.Name);
+
if (this.inputModePopup != null) {
this.inputModePopup.AccessibilityEnabled = this.inputModePopup.Enabled;
this.inputModePopup.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityInpueModeEditor, ViewModel.Property.Name);
}
}
- private readonly NSTextField stringEditor;
- private NSLayoutConstraint editorRightConstraint, editorInputModeConstraint;
- internal NSPopUpButton inputModePopup;
+ private NSView lastKeyView;
+ private NSLayoutConstraint editorInputModeConstraint;
+ private NSPopUpButton inputModePopup;
private IReadOnlyList<InputMode> viewModelInputModes;
-
- private bool CanEnable => ViewModel.Property.CanWrite && (((ViewModel.InputMode != null) && !ViewModel.InputMode.IsSingleValue) || (this.inputModePopup == null));
}
}
diff --git a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj
index 65bcdba..d483fe3 100644
--- a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj
+++ b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj
@@ -164,6 +164,7 @@
<Compile Include="Controls\BasePathEditorControl.cs" />
<Compile Include="Controls\DateTimeEditorControl.cs" />
<Compile Include="Controls\DateExtensions.cs" />
+ <Compile Include="Controls\EntryPropertyEditor.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controls\" />