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

ColorComponentEditor.cs « Custom « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aefe3a0ed4f6eca12e6d85ad1eca060b3f0e5e12 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.ComponentModel;
using System.Linq;
using AppKit;
using CoreAnimation;
using CoreGraphics;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class ColorComponentEditor : ColorEditorView
	{
		private const int DefaultPropertyButtonSize = 10;
		private const int DefaultActioButtonSize = 16;
		private const int DefaultControlHeight = 22;
		private const int DefaultGradientHeight = 4;

		private ChannelEditorType EditorType { get; }

		public bool ClickableGradients { get; set; } = true;

		public ColorComponentEditor (ChannelEditorType editorType, CGRect frame) : base (frame)
		{
			EditorType = EditorType;
			Initialize ();
		}

		public ColorComponentEditor (ChannelEditorType editorType) : base ()
		{
			EditorType = editorType;
			Initialize ();
		}

		private ChannelGroup [] Editors { get; set; }
		private UnfocusableTextField hexLabel;
		private NSTextField hexEditor;

		class ChannelGroup
		{
			public UnfocusableTextField Label { get; set; }
			public ComponentSpinEditor Editor { get; set; }
			public CAGradientLayer Gradient { get; set; }
		}

		private ChannelGroup CreateEditor (ChannelEditor editor)
		{
			var ce = new ChannelGroup {
				Label = new UnfocusableTextField {
					StringValue = $"{editor.Name}:",
					Alignment = NSTextAlignment.Right,
					BackgroundColor = NSColor.Clear
				},
				Editor = new ComponentSpinEditor (editor) {
					BackgroundColor = NSColor.Clear,
					TranslatesAutoresizingMaskIntoConstraints = true
				},
				Gradient = new UnanimatedGradientLayer {
					StartPoint = new CGPoint (0, 0),
					EndPoint = new CGPoint (1, 0),
					BorderWidth = .5f,
				}
			};

			ce.Editor.ValueChanged += UpdateComponent;
			AddSubview (ce.Label);
			AddSubview (ce.Editor);

			Layer.AddSublayer (ce.Gradient);
			return ce;
		}

		private ChannelGroup [] CreateEditors (ChannelEditorType type)
		{
			switch (type) {
				case ChannelEditorType.HSB:
					return new [] {
						CreateEditor (new HsbHueChannelEditor ()),
						CreateEditor (new HsbSaturationChannelEditor ()),
						CreateEditor (new HsbBrightnessChannelEditor ()),
						CreateEditor (new HsbAlphaChannelEditor ())
					};
				case ChannelEditorType.HLS:
					return new [] {
						CreateEditor (new HlsHueChannelEditor ()),
						CreateEditor (new HlsLightnessChannelEditor ()),
						CreateEditor (new HlsSaturationChannelEditor ()),
						CreateEditor (new HlsAlphaChannelEditor ())
					};
				case ChannelEditorType.RGB:
					return new [] {
						CreateEditor (new RedChannelEditor ()),
						CreateEditor (new GreenChannelEditor ()),
						CreateEditor (new BlueChannelEditor ()),
						CreateEditor (new AlphaChannelEditor ())
					};
				default:
				case ChannelEditorType.CMYK:
					return new [] {
						CreateEditor (new CyanChannelEditor ()),
						CreateEditor (new MagentaChannelEditor ()),
						CreateEditor (new YellowChannelEditor ()),
						CreateEditor (new BlackChannelEditor ()),
						CreateEditor (new AlphaChannelEditor ())
					};
			}
		}

		private void Initialize ()
		{
			WantsLayer = true;
			Editors = CreateEditors (EditorType);

			this.hexLabel = new UnfocusableTextField {
				StringValue = "#:",
				Alignment = NSTextAlignment.Right,
				BackgroundColor = NSColor.Clear
			};
			AddSubview (this.hexLabel);

			this.hexEditor = new NSTextField {
				Alignment = NSTextAlignment.Right,
				BackgroundColor = NSColor.Clear
			};
			AddSubview (this.hexEditor);

			this.hexEditor.EditingEnded += (o, e) => {
				if (CommonColor.TryParseArgbHex (this.hexEditor.StringValue, out CommonColor c)) {
					ViewModel.Color = c;
					this.hexEditor.StringValue = c.ToString ();
				}
			};
		}

		public override CGSize IntrinsicContentSize => new CGSize (100, 300);

		void UpdateComponent (object sender, EventArgs args)
		{
			if (ViewModel == null)
				return;

			var color = ViewModel.Color;
			var editor = sender as ComponentSpinEditor;
			ViewModel.Color = editor.ComponentEditor.UpdateColorFromValue (color, editor.Value);
			ViewModel.CommitLastColor ();
		}

		public override void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			base.OnPropertyChanged (sender, e);

			if (ViewModel == null)
				return;
			
			switch (e.PropertyName) {
				case nameof (SolidBrushViewModel.Color):
					foreach (var channelGroup in Editors) {
						var editor = channelGroup.Editor;
						editor.Value = editor.ComponentEditor.ValueFromColor (ViewModel.Color);
						editor.ComponentEditor.UpdateGradientLayer (channelGroup.Gradient, ViewModel.Color);
					}
					this.hexEditor.StringValue = ViewModel.Color.ToString ();
					break;
			}
		}

		public override void UpdateConstraints ()
		{
			base.UpdateConstraints ();
		}

		private ChannelGroup activeChannel;
		public override void MouseDown (NSEvent theEvent)
		{
			if (!ClickableGradients) {
				this.activeChannel = null;
				base.MouseDown (theEvent);
				return;
			}

			var location = ConvertPointFromView (theEvent.LocationInWindow, null);
			location = ConvertPointToLayer (location);

			foreach (var layer in Layer.Sublayers) {
				var hit = layer.PresentationLayer.HitTest (location) ?? layer.PresentationLayer.HitTest (new CGPoint (location.X, location.Y + 4));

				for (var currentLayer = hit; currentLayer != null; currentLayer = currentLayer.SuperLayer) {
					this.activeChannel = Editors.FirstOrDefault (ce => ce.Gradient == currentLayer.ModelLayer);
					if (this.activeChannel != null) {
						var channel = this.activeChannel.Editor.ComponentEditor;
						var grad = this.activeChannel.Gradient;
						ViewModel.Color = channel.UpdateColorFromLocation (
							grad,
							ViewModel.Color,
							Layer.ConvertPointToLayer (location, grad.SuperLayer));
						return;
					}
				}
			}
			base.MouseDown (theEvent);
		}

		public override void MouseDragged (NSEvent theEvent)
		{
			var location = ConvertPointFromView (theEvent.LocationInWindow, null);
			location = ConvertPointToLayer (location);

			if (this.activeChannel != null) {
				var channel = this.activeChannel.Editor.ComponentEditor;
				var grad = this.activeChannel.Gradient;
				ViewModel.Color = channel.UpdateColorFromLocation (
					grad,
					ViewModel.Color,
					Layer.ConvertPointToLayer (location, grad.SuperLayer));
				return;
			}
			base.MouseMoved (theEvent);
		}

		public override void MouseUp (NSEvent theEvent)
		{
			if (this.activeChannel != null)
				ViewModel.CommitLastColor ();

			this.activeChannel = null;
			base.MouseUp (theEvent);
		}

		public override void Layout ()
		{
			base.Layout ();

			var frame = Bounds.Inset (padding, padding);
			var labelFrame = new CGRect (frame.X, frame.Height - DefaultControlHeight, 20, DefaultControlHeight);
			var editorFrame = new CGRect (labelFrame.Right, labelFrame.Y, frame.Width - labelFrame.Right, DefaultControlHeight);
			var yOffset = DefaultControlHeight + DefaultGradientHeight + 3;

			foreach (var channelGroup in Editors) {
				channelGroup.Label.Frame = labelFrame;
				channelGroup.Editor.Frame = editorFrame;
				channelGroup.Gradient.Frame = new CGRect (
					editorFrame.X,
					editorFrame.Y - DefaultGradientHeight + 1,
					editorFrame.Width - 16, DefaultGradientHeight);

				channelGroup.Gradient.BorderColor = NSColor.DisabledControlText.CGColor;
				channelGroup.Gradient.ContentsScale = Window?.Screen?.BackingScaleFactor ?? NSScreen.MainScreen.BackingScaleFactor;
				labelFrame = labelFrame.Translate (0, -yOffset);
				editorFrame = editorFrame.Translate (0, -yOffset);
			}

			this.hexLabel.Frame = new CGRect (frame.X, padding, 20, DefaultControlHeight);
			this.hexEditor.Frame = new CGRect (
				labelFrame.Right,
				padding,
				frame.Width - labelFrame.Right - 16,
				DefaultControlHeight);
		}
	}
}