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

[Mac] Initial Origin Control implementation #628

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 92 additions & 7 deletions Xamarin.PropertyEditing.Mac/Controls/BaseRectangleEditorControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Drawing;
using AppKit;
using CoreGraphics;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;

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

public override NSView FirstKeyView => XEditor;
public override NSView FirstKeyView => this.firstKeyView;
public override NSView LastKeyView => HeightEditor.DecrementButton;

public NSLayoutConstraint LeftXEditorEdgeConstraint { get; }
private OriginControl originEditor;
private NSLayoutConstraint originViewConstraint;
private CommonOrigin? lastOrigin = CommonOrigin.TopLeft;
private NSView firstKeyView;

protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
: base (hostResources)
{
Expand All @@ -35,7 +42,7 @@ protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
};
XEditor.ValueChanged += OnInputUpdated;

YLabel = new UnfocusableTextField {
YLabel = new UnfocusableTextField {
Font = NSFont.FromFontName (DefaultFontName, DefaultDescriptionLabelFontSize),
TranslatesAutoresizingMaskIntoConstraints = false,
};
Expand All @@ -55,7 +62,7 @@ protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
};
WidthEditor.ValueChanged += OnInputUpdated;

HeightLabel = new UnfocusableTextField {
HeightLabel = new UnfocusableTextField {
Font = NSFont.FromFontName (DefaultFontName, DefaultDescriptionLabelFontSize),
TranslatesAutoresizingMaskIntoConstraints = false,
};
Expand All @@ -74,9 +81,11 @@ protected BaseRectangleEditorControl (IHostResourceProvider hostResources)
AddSubview (HeightLabel);
AddSubview (HeightEditor);

this.AddConstraints (new[] {
LeftXEditorEdgeConstraint = NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0);

AddConstraints (new[] {
NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 3f),
NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0f),
LeftXEditorEdgeConstraint,
NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Right, NSLayoutRelation.Equal, YEditor, NSLayoutAttribute.Left, 1f, -10f),
NSLayoutConstraint.Create (XEditor, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 18),

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

NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1f, 33f),
NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0f),
NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, XEditor, NSLayoutAttribute.Left, 1f, 0f),
NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Right, NSLayoutRelation.Equal, HeightEditor, NSLayoutAttribute.Left, 1f, -10f),
NSLayoutConstraint.Create (WidthEditor, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 18),

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

this.firstKeyView = XEditor;

AppearanceChanged ();
}

protected virtual void OnInputUpdated (object sender, EventArgs e)
{
ViewModel.Value = (T)Activator.CreateInstance (typeof(T), XEditor.Value, YEditor.Value, WidthEditor.Value, HeightEditor.Value);
ViewModel.Value = (T)Activator.CreateInstance (typeof (T), XEditor.Value, YEditor.Value, WidthEditor.Value, HeightEditor.Value, this.originEditor.Value);
}

protected override void SetEnabled ()
Expand All @@ -128,6 +139,10 @@ protected override void SetEnabled ()
YEditor.Enabled = ViewModel.Property.CanWrite;
WidthEditor.Enabled = ViewModel.Property.CanWrite;
HeightEditor.Enabled = ViewModel.Property.CanWrite;

if (this.originEditor != null) {
this.originEditor.Enabled = ViewModel.Property.CanWrite;
}
}

protected override void UpdateAccessibilityValues ()
Expand All @@ -154,5 +169,75 @@ protected override void AppearanceChanged ()
WidthLabel.TextColor = HostResources.GetNamedColor (NamedResources.DescriptionLabelColor);
HeightLabel.TextColor = HostResources.GetNamedColor (NamedResources.DescriptionLabelColor);
}

protected override void OnViewModelChanged (PropertyViewModel oldModel)
{
base.OnViewModelChanged (oldModel);

if (ViewModel == null)
return;

LeftXEditorEdgeConstraint.Active = true;

bool hasOrigin = false;
if (ViewModel is RectanglePropertyViewModel rpvm && rpvm.HasOrigin) {
hasOrigin = rpvm.HasOrigin;

LeftXEditorEdgeConstraint.Active = false;

if (this.originEditor == null) {
this.originEditor = new OriginControl (HostResources) {
AccessibilityEnabled = rpvm.Property.CanWrite,
AccessibilityTitle = string.Format (Properties.Resources.AccessibilityOriginEditor, ViewModel.Property.Name),
TranslatesAutoresizingMaskIntoConstraints = false,
};

this.originEditor.OriginChanged += (s, e) => {
this.lastOrigin = this.originEditor.Value;

if (this.originEditor.Value.HasValue) {
var location = this.originEditor.Bounds; // TODO Think this should be the DocumentFrame
if (this.originEditor.Value?.Horizontal != CommonOrigin.Position.Start)
location.X += this.originEditor.Bounds.Width / (this.originEditor.Value?.Horizontal == CommonOrigin.Position.Middle ? 2 : 1);
if (this.originEditor.Value?.Vertical != CommonOrigin.Position.Start)
location.Y += this.originEditor.Bounds.Height / (this.originEditor.Value?.Vertical == CommonOrigin.Position.Middle ? 2 : 1);

if ((nfloat)XEditor.Value != location.X)
XEditor.Value = location.X;
if ((nfloat)YEditor.Value != location.Y)
YEditor.Value = location.Y;

OnInputUpdated (s, e);
}
};

this.originEditor.Value = this.lastOrigin;

AddSubview (this.originEditor);

this.originViewConstraint = NSLayoutConstraint.Create (this.originEditor, NSLayoutAttribute.Right, NSLayoutRelation.Equal, XEditor, NSLayoutAttribute.Left, 1, -4);

AddConstraints (new[] {
NSLayoutConstraint.Create (this.originEditor, NSLayoutAttribute.Top, NSLayoutRelation.Equal, XEditor, NSLayoutAttribute.Top, 1f, 0),
NSLayoutConstraint.Create (this.originEditor, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1f, 0),
this.originViewConstraint,
NSLayoutConstraint.Create (this.originEditor, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, this, NSLayoutAttribute.Bottom, 1, -2f),
});

this.firstKeyView = this.originEditor;
}
}

// If we are reusing the control we'll have to hide the originEditor if we don't have Origin.
if (this.originEditor != null) {
this.originEditor.Hidden = !hasOrigin;
this.originViewConstraint.Active = hasOrigin;

if (hasOrigin)
this.originEditor.Value = this.lastOrigin;
}

SetEnabled ();
}
}
}
Loading