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

MaterialView.cs « Custom « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8d357546a075e01f10db370c1de2e92d0822e271 (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
using System;
using System.ComponentModel;
using System.Linq;
using System.Runtime.InteropServices;
using AppKit;
using CoreAnimation;
using CoreGraphics;
using Foundation;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class MaterialView : NotifyingView<MaterialDesignColorViewModel>
	{
		public override bool AcceptsFirstResponder ()
		{
			return false;
		}

		public override bool IsFlipped => true;

		public MaterialView ()
		{
			Initialize ();
		}

		private void Initialize ()
		{
			WantsLayer = true;
		}

		public override void OnViewModelChanged (MaterialDesignColorViewModel oldModel)
		{
			NeedsLayout = true;
		}

		public override void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			switch (e.PropertyName) {
			case nameof (ViewModel.ColorName):
			case nameof (ViewModel.AccentColor):
			case nameof (ViewModel.NormalColor):
				NeedsLayout = true;
				break;
			}
		}

		public override void Layout ()
		{
			if (Layer?.Sublayers != null) {
				foreach (var l in Layer.Sublayers) {
					l.RemoveFromSuperLayer ();
					l.Dispose ();
				}
			}

			if (ViewModel == null)
				return;

			var colors = ViewModel.Palettes.Select (p => new { p.Name, Color = p.MainColor }).ToArray ();
			int col = 0;
			nfloat x = 0;
			nfloat y = 6;
			var width = (Frame.Width - 54) / 10;
			var height = (Frame.Height - 49) / 4;

			MaterialColorLayer CreateLayer (CommonColor color)
			{
				var selectedColor = color.Lightness > 0.58 ? NSColor.Black : NSColor.White;
				return new MaterialColorLayer {
					BackgroundColor = color,
					ForegroundColor = selectedColor.CGColor,
					Text = color.Label,
					FontSize = 12,
					ContentsScale = NSScreen.MainScreen.BackingScaleFactor,
					TextAlignmentMode = CATextLayerAlignmentMode.Center,
				};
			}

			foreach (var p in colors) {
				var frame = new CGRect (x, y, width, height);
				var selectedColor = p.Color.Lightness > 0.58 ? NSColor.Black : NSColor.White;
				var l = new MaterialColorLayer {
					Frame = frame,
					ForegroundColor = selectedColor.CGColor,
					BackgroundColor = p.Color,
					CornerRadius = 3,
					BorderColor = new CGColor (.5f, .5f, .5f, .5f),
					MasksToBounds = false,
					IsSelected = ViewModel.Color == p.Color || ViewModel.ColorName == p.Name
				};

				l.BorderColor = new CGColor (.5f, .5f, .5f, .5f);
				l.Frame = new CGRect (x, y, width, height);
				Layer.AddSublayer (l);
				AddToolTip (new CGRect (x, y, width, height), new NSString(p.Name), IntPtr.Zero);
				x += width + 6;
				col++;
				if (col >= 10) {
					x = 0;
					y += height + 6;
					col = 0;
				}
			}

			Layer.AddSublayer (new CATextLayer {
				ForegroundColor = NSColor.ControlText.CGColor,
				Frame = new CGRect (x, y + 6, Frame.Width, 25),
				String = ViewModel.ColorName,
				FontSize = NSFont.SmallSystemFontSize,
				ContentsScale = Window?.Screen?.BackingScaleFactor ?? NSScreen.MainScreen.BackingScaleFactor
			});

			y += 25;
			x = 0;
			width = Frame.Width / ViewModel.NormalColorScale.Count ();
			var normal = new CALayer {
				CornerRadius = 3,
				MasksToBounds = true,
				Frame = new CGRect (x, y, Frame.Width, height),
				BorderColor = new CGColor (.5f, .5f, .5f, .5f),
				BorderWidth = 1
			};
			Layer.AddSublayer (normal);
			foreach (var color in ViewModel.NormalColorScale) {
				var l = CreateLayer (color.Value);
				l.ColorType = MaterialColorType.Normal;
				l.IsSelected = color.Value == ViewModel.NormalColor || color.Value == ViewModel.Color;
				l.Frame = new CGRect (x, 0, width, height);
				AddToolTip (new CGRect (x, y, width, height), new NSString (color.ToString ()), IntPtr.Zero);
				normal.AddSublayer (l);
				x += width;
			}

			if (ViewModel.AccentColorScale.Count () <= 0)
				return;

			y += height + 6;
			x = 0;

			var accent = new CALayer {
				CornerRadius = 3,
				MasksToBounds = true,
				Frame = new CGRect (x, y, Frame.Width, height),
				BorderColor = new CGColor (.5f, .5f, .5f, .5f),
				BorderWidth = 1
			};
			Layer.AddSublayer (accent);
			width = Frame.Width / ViewModel.AccentColorScale.Count ();
			foreach (var color in ViewModel.AccentColorScale) {
				var l = CreateLayer (color.Value);
				l.ColorType = MaterialColorType.Accent;
				l.IsSelected = color.Value == ViewModel.AccentColor || color.Value == ViewModel.Color;
				l.Frame = new CGRect (x, 0, width, height);
				AddToolTip (new CGRect (x, y, width, height), new NSString (color.ToString ()), IntPtr.Zero);
				accent.AddSublayer (l);
				x += width;
			}
		}

		public override void MouseDown (NSEvent theEvent)
		{
			UpdateFromEvent (theEvent);
		}

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

			foreach (var layer in Layer.Sublayers) {
				var hit = layer.HitTest (location);
				for (var c = hit; c != null; c = c.SuperLayer) {
					var editor = c as MaterialColorLayer;
					if (editor != null) {
						switch (editor.ColorType) {
						case MaterialColorType.Accent:
							ViewModel.AccentColor = editor.BackgroundColor;
							break;
						case MaterialColorType.Normal:
							ViewModel.NormalColor = editor.BackgroundColor;
							break;
						case MaterialColorType.Palette:
							var match = ViewModel.Palettes.First (palette => palette.MainColor == editor.BackgroundColor);
							ViewModel.ColorName = match.Name;
							break;
						}
						NeedsLayout = true;
					}
				}
			}
		}
	}
}