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

Commit 5ed8c35

Browse files
author
Eric Maupin
committed
[mac] Fix group editor layout
1 parent f8d6e23 commit 5ed8c35

File tree

8 files changed

+144
-153
lines changed

8 files changed

+144
-153
lines changed

Xamarin.PropertyEditing.Mac/Controls/Custom/BrushTabViewController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ EditorViewModel IEditorView.ViewModel {
4444
set { ViewModel = (BrushPropertyViewModel)value; }
4545
}
4646

47-
NSView IEditorView.NativeView => View;
47+
NSView INativeContainer.NativeView => View;
4848

4949
public bool IsDynamicallySized => false;
5050
public bool NeedsPropertyButton => false;

Xamarin.PropertyEditing.Mac/Controls/Custom/CommonBrushView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public CommonBrush Brush {
1313
set => (Layer as CommonBrushLayer).Brush = value;
1414
}
1515

16-
NSView IValueView.NativeView => this;
16+
NSView INativeContainer.NativeView => this;
1717

1818
public CommonBrushView (IHostResourceProvider hostResources)
1919
{
Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,17 @@
1-
using System;
21
using AppKit;
3-
using CoreGraphics;
4-
using Foundation;
52
using Xamarin.PropertyEditing.ViewModels;
63

74
namespace Xamarin.PropertyEditing.Mac
85
{
96
internal class EditorContainer
10-
: NSView
7+
: PropertyContainer
118
{
129
public EditorContainer (IHostResourceProvider hostResources, IEditorView editorView)
10+
: base (hostResources, editorView, editorView?.NeedsPropertyButton ?? false)
1311
{
14-
if (hostResources == null)
15-
throw new ArgumentNullException (nameof (hostResources));
16-
17-
EditorView = editorView;
18-
19-
AddSubview (this.label);
20-
21-
AddConstraints (new[] {
22-
NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, Mac.Layout.GoldenRatioLeft, 0f),
23-
NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 18),
24-
});
25-
26-
if (EditorView != null) {
27-
AddSubview (EditorView.NativeView);
28-
EditorView.NativeView.TranslatesAutoresizingMaskIntoConstraints = false;
29-
30-
if (EditorView.NativeView is PropertyEditorControl pec && pec.FirstKeyView != null) {
31-
AddConstraint (NSLayoutConstraint.Create (this.label, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, pec.FirstKeyView, NSLayoutAttribute.CenterY, 1, 0));
32-
} else {
33-
AddConstraint (NSLayoutConstraint.Create (this.label, NSLayoutAttribute.Top, NSLayoutRelation.Equal, this, NSLayoutAttribute.Top, 1, 3));
34-
}
35-
36-
AddConstraints (new[] {
37-
NSLayoutConstraint.Create (EditorView.NativeView, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f),
38-
NSLayoutConstraint.Create (EditorView.NativeView, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this.label, NSLayoutAttribute.Right, 1f, LabelToControlSpacing),
39-
NSLayoutConstraint.Create (EditorView.NativeView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1f, 0)
40-
});
41-
42-
if (editorView.NeedsPropertyButton) {
43-
this.propertyButton = new PropertyButton (hostResources) {
44-
TranslatesAutoresizingMaskIntoConstraints = false
45-
};
46-
47-
AddSubview (this.propertyButton);
48-
AddConstraints (new[] {
49-
NSLayoutConstraint.Create (this.propertyButton, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this.label, NSLayoutAttribute.CenterY, 1, 0),
50-
NSLayoutConstraint.Create (EditorView.NativeView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this.propertyButton, NSLayoutAttribute.Left, 1f, -EditorToButtonSpacing),
51-
NSLayoutConstraint.Create (this.propertyButton, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, -ButtonToWallSpacing),
52-
NSLayoutConstraint.Create (this.propertyButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, 1f, PropertyButton.DefaultSize),
53-
});
54-
} else {
55-
AddConstraint (NSLayoutConstraint.Create (EditorView.NativeView, NSLayoutAttribute.Right, NSLayoutRelation.Equal, this, NSLayoutAttribute.Right, 1f, 0f));
56-
}
57-
} else {
58-
AddConstraint (NSLayoutConstraint.Create (this.label, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1, 0));
59-
}
6012
}
6113

62-
public IEditorView EditorView {
63-
get;
64-
}
14+
public IEditorView EditorView => (IEditorView)NativeContainer;
6515

6616
public EditorViewModel ViewModel
6717
{
@@ -72,15 +22,7 @@ public EditorViewModel ViewModel
7222
return;
7323

7424
EditorView.ViewModel = value;
75-
this.propertyButton.ViewModel = value as PropertyViewModel;
76-
}
77-
}
78-
79-
public string Label {
80-
get { return this.label.StringValue; }
81-
set {
82-
this.label.StringValue = value + ":";
83-
this.label.ToolTip = value;
25+
PropertyButton.ViewModel = value as PropertyViewModel;
8426
}
8527
}
8628

@@ -120,30 +62,13 @@ public override void ViewWillMoveToSuperview (NSView newSuperview)
12062
base.ViewWillMoveToSuperview (newSuperview);
12163
}
12264

123-
private UnfocusableTextField label = new UnfocusableTextField {
124-
Alignment = NSTextAlignment.Right,
125-
Cell = {
126-
LineBreakMode = NSLineBreakMode.TruncatingHead,
127-
},
128-
TranslatesAutoresizingMaskIntoConstraints = false,
129-
};
130-
13165
#if DEBUG // Currently only used to highlight which controls haven't been implemented
13266
public NSColor LabelTextColor {
133-
set { this.label.TextColor = value; }
67+
set { LabelControl.TextColor = value; }
13468
}
13569
#endif
13670

137-
138-
internal const float LabelToControlSpacing = 13f;
139-
internal static float PropertyTotalWidth => PropertyButton.DefaultSize + ButtonToWallSpacing + EditorToButtonSpacing;
140-
14171
private NSView leftEdgeView;
14272
private NSLayoutConstraint leftEdgeLeftConstraint, leftEdgeVCenterConstraint;
143-
private readonly IHostResourceProvider hostResources;
144-
private readonly PropertyButton propertyButton;
145-
146-
private const float EditorToButtonSpacing = 4f;
147-
private const float ButtonToWallSpacing = 9f;
14873
}
14974
}

Xamarin.PropertyEditing.Mac/Controls/GroupEditorControl.cs

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public GroupEditorControl (IHostResourceProvider hostResources)
2626
AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable
2727
};
2828

29-
this.table = new GroupedTableView { RowHeight = 24 };
30-
this.table.AddColumn (new NSTableColumn (PropertyIdentifier));
31-
this.table.AddColumn (new NSTableColumn (PreviewIdentifier));
29+
this.table = new GroupedTableView {
30+
RowHeight = 24,
31+
IntercellSpacing = new CGSize (0, 0)
32+
};
33+
this.table.AddColumn (new NSTableColumn ());
3234
this.container.AddView (this.table, NSStackViewGravity.Top);
3335

3436
this.host = new NSBox {
@@ -46,7 +48,7 @@ public GroupEditorControl (IHostResourceProvider hostResources)
4648
ViewDidChangeEffectiveAppearance ();
4749
}
4850

49-
NSView IEditorView.NativeView => this;
51+
NSView INativeContainer.NativeView => this;
5052

5153
EditorViewModel IEditorView.ViewModel
5254
{
@@ -101,10 +103,6 @@ internal PropertyGroupViewModel ViewModel
101103
}
102104
}
103105

104-
private const string PropertyIdentifier = "property";
105-
private const string PreviewIdentifier = "preview";
106-
private const string ButtonIdentifier = "button";
107-
108106
private readonly NSTableView table;
109107
private readonly NSStackView container;
110108
private readonly NSBox host;
@@ -165,76 +163,46 @@ public override nint GetRowCount (NSTableView tableView)
165163

166164
public override NSView GetViewForItem (NSTableView tableView, NSTableColumn tableColumn, nint row)
167165
{
168-
if (!this.resized) {
169-
var cols = tableView.TableColumns ();
170-
cols[0].Width = (tableView.Frame.Width + 45) * Mac.Layout.GoldenRatioLeft;
171-
cols[1].Width = tableView.Frame.Width - cols[0].Width;
172-
this.resized = true;
173-
}
174-
175166
PropertyViewModel pvm = ViewModel.Properties[(int)row];
176167

177-
switch (tableColumn.Identifier) {
178-
case PropertyIdentifier:
179-
return new UnfocusableTextField { StringValue = pvm.Property.Name + ":", Alignment = NSTextAlignment.Right };
180-
case PreviewIdentifier:
181-
return GetPreview (tableView, pvm);
182-
}
183-
184-
return null;
185-
}
186-
187-
public override void SelectionDidChange (NSNotification notification)
188-
{
189-
this.host.UpdateHosted ();
190-
}
191-
192-
private readonly GroupEditorControl host;
193-
private readonly PropertyInlinePreviewSelector selector = new PropertyInlinePreviewSelector ();
194-
private bool resized;
195-
196-
private NSView GetPreview (NSTableView table, PropertyViewModel pvm)
197-
{
198-
string identifier = pvm.GetType().FullName;
199-
PreviewView view = table.MakeView (identifier, table) as PreviewView;
168+
string identifier = pvm.GetType ().FullName;
169+
var view = tableView.MakeView (identifier, tableView) as PreviewView;
200170
if (view == null) {
201171
IValueView valueView = this.selector.CreateView (this.host.hostResources, pvm.Property.Type);
202172
if (valueView == null)
203173
return new NSView ();
204-
205-
view = new PreviewView (this.host.hostResources, valueView, new CGRect (0, 0, table.TableColumns ()[1].Width, table.RowHeight)) {
174+
175+
view = new PreviewView (this.host.hostResources, valueView) {
206176
Identifier = identifier
207177
};
208178
}
209179

180+
view.Label = pvm.Name;
210181
view.ViewModel = pvm;
211182
return view;
212183
}
184+
185+
public override void SelectionDidChange (NSNotification notification)
186+
{
187+
this.host.UpdateHosted ();
188+
}
189+
190+
private readonly GroupEditorControl host;
191+
private readonly PropertyInlinePreviewSelector selector = new PropertyInlinePreviewSelector ();
213192
}
214193

215194
private class PreviewView
216-
: NSView
195+
: PropertyContainer
217196
{
218-
public PreviewView (IHostResourceProvider hostResources, IValueView valueView, CGRect frame)
197+
public PreviewView (IHostResourceProvider hostResources, IValueView valueView)
198+
: base (hostResources, valueView, includePropertyButton: true, vertInset: -6f)
219199
{
220-
Frame = frame;
221-
222200
this.view = valueView;
223-
valueView.NativeView.Frame = new CGRect (0, 3, frame.Width - PropertyButton.DefaultSize, frame.Height - 6);
224-
valueView.NativeView.AutoresizingMask = NSViewResizingMask.WidthSizable | NSViewResizingMask.HeightSizable;
225-
AddSubview (valueView.NativeView);
226-
227-
this.propertyButton = new PropertyButton (hostResources) {
228-
Frame = new CGRect (valueView.NativeView.Frame.Width, 0, PropertyButton.DefaultSize, 24),
229-
AutoresizingMask = NSViewResizingMask.MinXMargin
230-
};
231-
232-
AddSubview (this.propertyButton);
233201
}
234202

235203
public PropertyViewModel ViewModel
236204
{
237-
get { return this.propertyButton.ViewModel; }
205+
get { return this.vm; }
238206
set
239207
{
240208
if (this.vm == value)
@@ -244,15 +212,17 @@ public PropertyViewModel ViewModel
244212
this.vm.PropertyChanged -= OnPropertyChanged;
245213

246214
this.vm = value;
247-
this.view.SetValue (((IPropertyValue)this.vm).Value);
248-
this.vm.PropertyChanged += OnPropertyChanged;
249-
this.propertyButton.ViewModel = value;
215+
if (this.vm != null) {
216+
this.view.SetValue (((IPropertyValue)this.vm).Value);
217+
this.vm.PropertyChanged += OnPropertyChanged;
218+
}
219+
220+
PropertyButton.ViewModel = value;
250221
}
251222
}
252223

253224
private PropertyViewModel vm;
254225
private IValueView view;
255-
private PropertyButton propertyButton;
256226

257227
private void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
258228
{

Xamarin.PropertyEditing.Mac/Controls/IEditorView.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,27 @@
66
namespace Xamarin.PropertyEditing.Mac
77
{
88
internal interface IEditorView
9+
: INativeContainer
910
{
10-
/// <summary>
11-
/// Gets the native view.
12-
/// </summary>
13-
/// <value>Generally just `this`.</value>
14-
NSView NativeView { get; }
15-
1611
EditorViewModel ViewModel { get; set; }
17-
12+
1813
bool NeedsPropertyButton { get; }
1914
bool IsDynamicallySized { get; }
2015
nint GetHeight (EditorViewModel vm);
2116
}
2217

2318
internal interface IValueView
19+
: INativeContainer
2420
{
25-
NSView NativeView { get; }
26-
2721
void SetValue (object value);
2822
}
23+
24+
internal interface INativeContainer
25+
{
26+
/// <summary>
27+
/// Gets the native view.
28+
/// </summary>
29+
/// <value>Generally just `this`.</value>
30+
NSView NativeView { get; }
31+
}
2932
}

0 commit comments

Comments
 (0)