1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
using System;
using System.Collections;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using AppKit;
using CoreGraphics;
using Foundation;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;
namespace Xamarin.PropertyEditing.Mac
{
internal class ColorPopUpButton : FocusablePopUpButton
{
public NSPopover Popover { get; set; }
public override void MouseDown (NSEvent theEvent)
{
if (Popover == null)
return;
if (!Popover.Shown) {
Popover.Show (new CGRect (26, this.Frame.Height / 2 - 2, 2, 2), this, NSRectEdge.MinYEdge);
Window.MakeFirstResponder (this);
Window.MakeFirstResponder (Popover);
}
}
public override void KeyDown (NSEvent theEvent)
{
if (theEvent.KeyCode == (ushort)NSKey.Space) {
MouseDown (theEvent);
} else {
base.KeyDown (theEvent);
}
}
}
internal class ColorPopOverDelegate : NSPopoverDelegate
{
private BrushTabViewController brushTabViewController;
private CommonBrushType selectedBrushType = CommonBrushType.None;
internal ColorPopOverDelegate (BrushTabViewController brushTabViewController)
{
if (brushTabViewController == null)
throw new ArgumentNullException (nameof (brushTabViewController));
this.brushTabViewController = brushTabViewController;
}
public override void WillShow (NSNotification notification)
{
if (this.brushTabViewController.ViewModel != null) {
var ct = new CancellationTokenSource ();
Task.Factory.StartNew (()=> {
if (this.selectedBrushType == CommonBrushType.None)
this.selectedBrushType = this.brushTabViewController.ViewModel.SelectedBrushType;
else
this.brushTabViewController.ViewModel.SelectedBrushType = this.selectedBrushType;
}, ct.Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext ());
}
}
}
internal class BrushEditorControl : PropertyEditorControl<BrushPropertyViewModel>
{
public BrushEditorControl (IHostResourceProvider hostResources)
: base (hostResources)
{
TranslatesAutoresizingMaskIntoConstraints = false;
this.previewLayer = new CommonBrushLayer (hostResources) {
Frame = new CGRect (0, 0, 30, 10)
};
this.popover = new AutoClosePopOver (hostResources, hostResources.GetVibrantAppearance (EffectiveAppearance)) {
ContentViewController = this.brushTabViewController = new BrushTabViewController (hostResources) {
PreferredContentSize = new CGSize (550, 363)
}
};
this.popover.Delegate = new ColorPopOverDelegate (this.brushTabViewController);
this.popUpButton = new ColorPopUpButton {
ControlSize = NSControlSize.Small,
Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)),
TranslatesAutoresizingMaskIntoConstraints = false,
};
this.popupButtonList = new NSMenu ();
this.popUpButton.Menu = this.popupButtonList;
AddSubview (this.popUpButton);
this.AddConstraints (new[] {
NSLayoutConstraint.Create (this.popUpButton, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, 0),
NSLayoutConstraint.Create (this.popUpButton, NSLayoutAttribute.Height, NSLayoutRelation.Equal, 1f, 16),
NSLayoutConstraint.Create (this.popUpButton, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0),
});
AppearanceChanged ();
}
readonly ColorPopUpButton popUpButton;
readonly NSPopover popover;
readonly BrushTabViewController brushTabViewController;
readonly NSMenu popupButtonList;
readonly CommonBrushLayer previewLayer;
public override NSView FirstKeyView => this.popUpButton;
public override NSView LastKeyView => this.popUpButton;
protected override void SetEnabled ()
{
this.popUpButton.Enabled = this.ViewModel?.Property.CanWrite ?? false;
}
string GetTitle ()
{
var title = Properties.Resources.CommonBrushTitleUnknown;
switch (ViewModel.Value) {
case CommonSolidBrush solid:
title = solid.Color.ToString ();
break;
case CommonGradientBrush gradient:
title = Properties.Resources.CommonBrushTitleGradient;
break;
default:
if (ViewModel.Value == null)
title = Properties.Resources.NoBrush;
break;
}
return title;
}
protected override void UpdateValue ()
{
this.brushTabViewController.ViewModel = ViewModel;
this.popUpButton.Popover = (ViewModel?.Property.CanWrite ?? false) ? this.popover : null;
if (ViewModel.Solid != null) {
var title = GetTitle ();
if (this.popupButtonList.Count == 0)
this.popupButtonList.AddItem (new NSMenuItem ());
this.previewLayer.Brush = ViewModel.Value;
var item = this.popupButtonList.ItemAt (0);
if (item.Title != title) {
item.Title = title;
item.Image = this.previewLayer.RenderPreview ();
}
}
}
protected override void AppearanceChanged ()
{
base.AppearanceChanged ();
this.popover.SetAppearance (HostResources.GetVibrantAppearance (EffectiveAppearance));
this.popover.ContentViewController.View.Appearance = EffectiveAppearance;
}
protected override void UpdateAccessibilityValues ()
{
this.popUpButton.AccessibilityEnabled = this.popUpButton.Enabled;
this.popUpButton.AccessibilityTitle = string.Format (Properties.Resources.AccessibilityPopUp, ViewModel.Property.Name);
}
}
}
|