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

BrushEditorControl.cs « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca7cadb1ab67c5c15b66adccc5b6b835467140b2 (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
using System;
using System.Collections;
using System.ComponentModel;
using AppKit;
using CoreAnimation;
using CoreGraphics;
using CoreImage;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal class ColorPopUpButton : NSPopUpButton
	{
		public NSPopover Popover { get; set; }

		public ColorPopUpButton () : base ()
		{
		}

		public ColorPopUpButton (CGRect frame) : base (frame, true)
		{
		}

		public ColorPopUpButton (IntPtr handle) : base (handle)
		{
		}

		public override void MouseDown (NSEvent theEvent)
		{
			if (Popover == null)
				return;
			Popover.SetAppearance (PropertyEditorPanel.ThemeManager.CurrentAppearance);
			Popover?.Show (new CGRect (20, this.Frame.Height / 2 - 2.5, 5, 5), this, NSRectEdge.MinYEdge);
		}
    }

	internal class BrushEditorControl : PropertyEditorControl
	{
		public BrushEditorControl ()
		{
			base.TranslatesAutoresizingMaskIntoConstraints = false;


			this.colorEditor = new BrushTabViewController ();

			this.popover = new NSPopover ();
			popover.Behavior = NSPopoverBehavior.Transient;
			popover.ContentViewController = brushTabViewController = new BrushTabViewController {
				PreferredContentSize = new CGSize (200, 200)
			};
			RowHeight = 200 + 30;

			this.popUpButton = new ColorPopUpButton {
				TranslatesAutoresizingMaskIntoConstraints = false,
				ControlSize = NSControlSize.Small,
				Font = NSFont.FromFontName (DefaultFontName, DefaultFontSize),
				Popover = this.popover
			};

			popupButtonList = new NSMenu ();
			popUpButton.Menu = popupButtonList;

			UpdateTheme ();
		}

		internal new BrushPropertyViewModel ViewModel
		{
			get  => (BrushPropertyViewModel)base.ViewModel;
			set  => base.ViewModel = value;
		}

		readonly NSPopUpButton popUpButton;
		readonly BrushTabViewController colorEditor;
		readonly NSPopover popover;
		readonly BrushTabViewController brushTabViewController;
		readonly NSMenu popupButtonList;
		readonly CommonBrushLayer previewLayer = new CommonBrushLayer {
			Frame = new CGRect (0, 0, 30, 10)
		};

		bool dataPopulated;

		public override NSView FirstKeyView => this.popUpButton;
		public override NSView LastKeyView => this.popUpButton;

		protected override void HandleErrorsChanged (object sender, DataErrorsChangedEventArgs e)
		{
		}

		protected override void SetEnabled ()
		{
		}

		protected override void UpdateAccessibilityValues ()
		{
		}

		protected override void UpdateErrorsDisplayed (IEnumerable errors)
		{
		}

		string GetTitle ()
		{
			var title = "Unknown";
			switch (ViewModel.Value) {
				case CommonSolidBrush solid:
					title = solid.Color.ToString ();
					break;
				case CommonGradientBrush gradient:
					title = "Gradient";
					break;
				default:
					if (ViewModel.Value == null)
					title = "null";
					break;
			}

			//return ViewModel.Resource == null ? title : $"{title} - (Resource: {ViewModel?.Resource?.Name})";
			return title;
		}

        protected override void UpdateValue ()
		{
			this.colorEditor.ViewModel = ViewModel;
			this.brushTabViewController.ViewModel = ViewModel;

			if (ViewModel.Solid != null) {
				var title = GetTitle ();

				if (popupButtonList.Count == 0)
					popupButtonList.AddItem (new NSMenuItem ());

				previewLayer.Brush = ViewModel.Value;
				var item = popupButtonList.ItemAt (0);
				if (item.Title != title) {
					item.Title = title;
					item.Image = previewLayer.RenderPreview ();
				}
			}
		}

		protected override void OnViewModelChanged (PropertyViewModel oldModel)
		{
			if (!dataPopulated) {
				this.DoConstraints (new[] {
						popUpButton.ConstraintTo (this, (pub, c) => pub.Width == c.Width - 34),
						popUpButton.ConstraintTo (this, (pub, c) => pub.Height == DefaultControlHeight + 1),
						popUpButton.ConstraintTo (this, (pub, c) => pub.Left == pub.Left + 4),
						popUpButton.ConstraintTo (this, (pub, c) => pub.Top == pub.Top + 0),
					});

				AddSubview (this.popUpButton);
				AddSubview (this.colorEditor.View);
			}
			UpdateValue ();
			dataPopulated = true;
		}
	}
}