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

Commit 102c9c0

Browse files
committed
[Mac] Initial Origin Control implementation
1 parent d54d7ab commit 102c9c0

File tree

9 files changed

+745
-44
lines changed

9 files changed

+745
-44
lines changed

Xamarin.PropertyEditing.Mac/Controls/BaseRectangleEditorControl.cs

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Drawing;
55
using AppKit;
66
using CoreGraphics;
7+
using Xamarin.PropertyEditing.Drawing;
78
using Xamarin.PropertyEditing.ViewModels;
89

910
namespace Xamarin.PropertyEditing.Mac
@@ -19,9 +20,15 @@ internal abstract class BaseRectangleEditorControl<T> : PropertyEditorControl<Pr
1920
protected UnfocusableTextField HeightLabel { get; set; }
2021
protected NumericSpinEditor<T> HeightEditor { get; set; }
2122

22-
public override NSView FirstKeyView => XEditor;
23+
public override NSView FirstKeyView => this.firstKeyView;
2324
public override NSView LastKeyView => HeightEditor.DecrementButton;
2425

26+
public NSLayoutConstraint LeftXEditorEdgeConstraint { get; }
27+
private OriginControl originView;
28+
private NSLayoutConstraint originViewConstraint;
29+
private CommonOrigin lastOrigin = CommonOrigin.TopLeft;
30+
private NSView firstKeyView;
31+
2532
protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
2633
: base (hostResources)
2734
{
@@ -35,7 +42,7 @@ protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
3542
};
3643
XEditor.ValueChanged += OnInputUpdated;
3744

38-
YLabel = new UnfocusableTextField {
45+
YLabel = new UnfocusableTextField {
3946
Font = NSFont.FromFontName (DefaultFontName, DefaultDescriptionLabelFontSize),
4047
TranslatesAutoresizingMaskIntoConstraints = false,
4148
};
@@ -55,7 +62,7 @@ protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
5562
};
5663
WidthEditor.ValueChanged += OnInputUpdated;
5764

58-
HeightLabel = new UnfocusableTextField {
65+
HeightLabel = new UnfocusableTextField {
5966
Font = NSFont.FromFontName (DefaultFontName, DefaultDescriptionLabelFontSize),
6067
TranslatesAutoresizingMaskIntoConstraints = false,
6168
};
@@ -74,9 +81,11 @@ protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
7481
AddSubview (HeightLabel);
7582
AddSubview (HeightEditor);
7683

77-
this.AddConstraints (new[] {
84+
LeftXEditorEdgeConstraint = NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0);
85+
86+
AddConstraints (new[] {
7887
NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 3f),
79-
NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0f),
88+
LeftXEditorEdgeConstraint,
8089
NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Right, NSLayoutRelation.Equal, YEditor, NSLayoutAttribute.Left, 1f, -10f),
8190
NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 18),
8291

@@ -92,7 +101,7 @@ protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
92101
NSLayoutConstraint.Create (YLabel, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 18),
93102

94103
NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 33f),
95-
NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0f),
104+
NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, XEditor, NSLayoutAttribute.Left, 1f, 0f),
96105
NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Right, NSLayoutRelation.Equal, HeightEditor, NSLayoutAttribute.Left, 1f, -10f),
97106
NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 18),
98107

@@ -114,12 +123,14 @@ protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
114123
NSLayoutConstraint.Create (HeightLabel, NSLayoutAttribute.CenterX, NSLayoutRelation.Equal, HeightEditor.Subviews[0], NSLayoutAttribute.CenterX, 1f, 0),
115124
});
116125

126+
this.firstKeyView = XEditor;
127+
117128
AppearanceChanged ();
118129
}
119130

120131
protected virtual void OnInputUpdated (object sender, EventArgs e)
121132
{
122-
ViewModel.Value = (T)Activator.CreateInstance (typeof(T), XEditor.Value, YEditor.Value, WidthEditor.Value, HeightEditor.Value);
133+
ViewModel.Value = (T)Activator.CreateInstance (typeof (T), XEditor.Value, YEditor.Value, WidthEditor.Value, HeightEditor.Value);
123134
}
124135

125136
protected override void SetEnabled ()
@@ -128,6 +139,10 @@ protected override void SetEnabled ()
128139
YEditor.Enabled = ViewModel.Property.CanWrite;
129140
WidthEditor.Enabled = ViewModel.Property.CanWrite;
130141
HeightEditor.Enabled = ViewModel.Property.CanWrite;
142+
143+
if (this.originView != null) {
144+
this.originView.Enabled = ViewModel.Property.CanWrite;
145+
}
131146
}
132147

133148
protected override void UpdateAccessibilityValues ()
@@ -154,5 +169,54 @@ protected override void AppearanceChanged ()
154169
WidthLabel.TextColor = HostResources.GetNamedColor (NamedResources.DescriptionLabelColor);
155170
HeightLabel.TextColor = HostResources.GetNamedColor (NamedResources.DescriptionLabelColor);
156171
}
172+
173+
protected override void OnViewModelChanged (PropertyViewModel oldModel)
174+
{
175+
base.OnViewModelChanged (oldModel);
176+
177+
if (ViewModel == null)
178+
return;
179+
180+
LeftXEditorEdgeConstraint.Active = true;
181+
182+
if (ViewModel is RectanglePropertyViewModel rpvm && rpvm.HasOrigin) {
183+
LeftXEditorEdgeConstraint.Active = false;
184+
if (this.originView == null) {
185+
this.originView = new OriginControl (HostResources) {
186+
AccessibilityEnabled = ViewModel.Property.CanWrite,
187+
AccessibilityTitle = string.Format (Properties.Resources.AccessibilityOriginEditor, ViewModel.Property.Name),
188+
Enabled = rpvm.Property.CanWrite,
189+
//TODO Origin = rpvm.Origin,
190+
TranslatesAutoresizingMaskIntoConstraints = false,
191+
};
192+
193+
// TODO this.lastOrigin = rpvm.Origin;
194+
195+
this.originView.OriginChanged += (s, e) => {
196+
this.lastOrigin = this.originView.Origin;
197+
/* TODO MetricsPropertyEntryHelper.HandleOriginChanged (this, this.originView.Origin, v => {
198+
if (XEditor.Value != v)
199+
XEditor.Value = v;
200+
}, v => {
201+
if (YEditor.Value != v)
202+
YEditor.Value = v;
203+
});*/
204+
};
205+
206+
AddSubview (this.originView);
207+
208+
this.originViewConstraint = NSLayoutConstraint.Create (this.originView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, XEditor, NSLayoutAttribute.Left, 1, -4);
209+
210+
AddConstraints (new[] {
211+
NSLayoutConstraint.Create (this.originView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, XEditor, NSLayoutAttribute.Top, 1f, 0),
212+
NSLayoutConstraint.Create (this.originView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0),
213+
this.originViewConstraint,
214+
NSLayoutConstraint.Create (this.originView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1, -2f),
215+
});
216+
217+
this.firstKeyView = this.originView;
218+
}
219+
}
220+
}
157221
}
158222
}

0 commit comments

Comments
 (0)