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

SolidColorBrushEditor.cs « Custom « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 80ee61a8f1fa34449a174e2543ba05f1cb856567 (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
using System;
using System.ComponentModel;
using System.Linq;

using AppKit;
using CoreAnimation;
using CoreGraphics;
using ObjCRuntime;

using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class EditorInteraction
	{
		public EditorInteraction (SolidBrushViewModel viewModel, ColorEditorLayer layer)
		{
			ViewModel = viewModel;
			Layer = layer;
		}

		public ColorEditorLayer Layer { get; set; }
		public SolidBrushViewModel ViewModel { get; set; }

		public CommonColor LastColor => ViewModel.LastColor;
		public CommonColor InitialColor => ViewModel.InitialColor;

		public CommonColor Color {
			get => ViewModel.Color;
			set => ViewModel.Color = value;
		}

		public void Commit ()
		{
			Layer.Commit (this);
		}
	}

	internal class SolidColorBrushEditor
		: ColorEditorView
	{

		public SolidColorBrushEditor (IHostResourceProvider hostResources, CGRect frame)
			: base (frame)
		{
			Initialize (hostResources);
		}

		public SolidColorBrushEditor (IHostResourceProvider hostResources)
		{
			Initialize (hostResources);
		}

		private void Initialize (IHostResourceProvider hostResources)
		{
			this.historyLayer = new HistoryLayer (hostResources);

			this.componentTabs = new ColorComponentTabViewController (hostResources) {
				EditorType = ChannelEditorType.RGB
			};

			Layer = new CALayer ();
			Layer.AddSublayer (this.background);
			Layer.AddSublayer (this.shadeLayer);
			Layer.AddSublayer (this.hueLayer);
			Layer.AddSublayer (this.historyLayer);
			Layer.AddSublayer (this.componentBackground);
			WantsLayer = true;
			AddSubview (this.componentTabs.View);
		}

		public override bool AcceptsFirstResponder () => true;

		private readonly ShadeLayer shadeLayer = new ShadeLayer ();
		private readonly HueLayer hueLayer = new HueLayer ();
		private HistoryLayer historyLayer;
		private ColorComponentTabViewController componentTabs;

		private readonly CALayer background = new CALayer {
			CornerRadius = 3,
			BorderWidth = 1,
			BorderColor = new CGColor (.5f, .5f, .5f, .5f),
		};

		private readonly CALayer componentBackground = new CALayer {
			CornerRadius = 3,
			BorderWidth = 1,
			BorderColor = new CGColor (.5f, .5f, .5f, .5f)
		};

		private EditorInteraction interaction;
		public override void UpdateFromEvent (NSEvent theEvent)
		{
		}

		public override void MouseDown (NSEvent theEvent)
		{
			var location = ConvertPointFromView (theEvent.LocationInWindow, null);
			location = ConvertPointToLayer (location);
			interaction = null;
			foreach (var layer in Layer.Sublayers) {
				var hit = layer.PresentationLayer.HitTest (location);

				for (var c = hit?.ModelLayer; c != null; c = c.SuperLayer) {
					var active = c as ColorEditorLayer;
					if (active != null) {
						this.interaction = new EditorInteraction (ViewModel, active);
						active.UpdateFromLocation (
							this.interaction,
							Layer.ConvertPointToLayer (location, active));
						OnPropertyChanged (ViewModel, new PropertyChangedEventArgs (nameof (SolidBrushViewModel.Color)));
						return;
					}
				}
			}
		}

		private CGPoint last;
		public override void MouseDragged (NSEvent theEvent)
		{
			var location = ConvertPointFromView (theEvent.LocationInWindow, null);
			var diff = new CGPoint (last.X - location.X, last.Y - location.Y);

			if (diff.X * diff.X < .5 && diff.Y * diff.Y < .5)
				return;

			this.interaction?.Layer?.UpdateFromLocation (
						this.interaction,
						Layer.ConvertPointToLayer (location, this.interaction.Layer));

			OnPropertyChanged (ViewModel, new PropertyChangedEventArgs (nameof (SolidBrushViewModel.Color)));
		}

		public override void MouseUp (NSEvent theEvent)
		{
			base.MouseUp (theEvent);
			this.interaction?.Commit ();
			this.interaction = null;
		}

		private bool modelChanged = true;

		public override void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			var inter = this.interaction ?? new EditorInteraction (ViewModel, null);

			switch (e.PropertyName) {
				case nameof (SolidBrushViewModel.Color):
				case nameof (SolidBrushViewModel.LastColor):
					this.modelChanged = NeedsLayout = true;
					break;
			}
		}

		public override void OnViewModelChanged (SolidBrushViewModel oldModel)
		{
			base.OnViewModelChanged (oldModel);
			var inter = this.interaction ?? new EditorInteraction (ViewModel, null);

			this.componentTabs.ViewModel = ViewModel;
			foreach (var editor in Layer.Sublayers.OfType<ColorEditorLayer> ()) {
				editor.UpdateFromModel (inter);
			}
		}

		public override void Layout ()
		{
			if (this.modelChanged) {
				var interx = this.interaction ?? new EditorInteraction (ViewModel, null);
				foreach (var editor in Layer.Sublayers.OfType<ColorEditorLayer> ()) {
					editor.UpdateFromModel (interx);
				}
				this.modelChanged = false;
			}

			base.Layout ();

			if (Frame.IsEmpty || Frame.IsInfinite () || double.IsNaN (Frame.X) || double.IsInfinity (Frame.X))
				return;

			this.background.BackgroundColor = NSColor.ControlBackground.CGColor;
			this.componentBackground.BackgroundColor = NSColor.ControlBackground.CGColor;
			this.hueLayer.GripColor = NSColor.Text.CGColor;

			const float spacing = 8, hueWidth = 20, historyHeight = 20;
			const float leftMinWidth = hueWidth + (Padding * 2) + 50;
			const float rightWidth = 170;

			nfloat leftWidth = leftMinWidth;

			nfloat spaceLeft = Frame.Width - spacing - leftWidth - rightWidth;
			if (spaceLeft > 0) {
				leftWidth += spaceLeft;
			}

			nfloat vspace = Frame.Height - (Padding * 2);

			this.background.Frame = new CGRect (0, 0, leftWidth, Frame.Height);

			var shadeFrame = new CGRect (Padding, Padding + historyHeight + Padding, leftWidth - (Padding * 3) - hueWidth, vspace - historyHeight - Padding);
			this.shadeLayer.Frame = shadeFrame;
			this.historyLayer.Frame = new CGRect (Padding, Padding, shadeFrame.Width, historyHeight);
			this.hueLayer.Frame = new CGRect (this.shadeLayer.Frame.Right + Padding, this.historyLayer.Frame.Bottom + Padding, hueWidth, shadeFrame.Height);

			const float componentPadding = 9;
			var backgroundFrame = new CGRect (this.hueLayer.Frame.Right + spacing, 0, rightWidth, Frame.Height);
			this.componentBackground.Frame = backgroundFrame;
			var inset = backgroundFrame.Inset (componentPadding, componentPadding);
			this.componentTabs.View.Frame = inset;

			var inter = this.interaction ?? new EditorInteraction (ViewModel, null);
			foreach (var editor in Layer.Sublayers.OfType<ColorEditorLayer> ()) {
				editor.UpdateFromModel (inter);
			}
		}
	}
}