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

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

namespace Xamarin.PropertyEditing.Mac
{
	internal class RequestResourcePanel : NSView
	{
		internal const string ResourceImageColId = "ResourceImage";
		internal const string ResourceTypeColId = "ResourceType";
		internal const string ResourceNameColId = "ResourceName";
		internal const string ResourceValueColId = "ResourceValue";

		private NSTableView resourceTable;
		private ResourceTableDataSource dataSource;

		private readonly ResourceSelectorViewModel viewModel;

		public ResourceSelectorViewModel ViewModel => this.viewModel;
		private SimpleCollectionView collectionView => this.viewModel.Resources as SimpleCollectionView;
		public Resource SelectedResource {
			get {
				return (this.resourceTable.SelectedRow != -1) ? this.collectionView [((int)this.resourceTable.SelectedRow)] as Resource : null;
			}
		}

		NSProgressIndicator progressIndicator;

		NSScrollView tableContainer;

		RequestResourcePreviewPanel previewPanel;

		public event EventHandler ResourceSelected;
		public event EventHandler DoubleClicked;

		private object selectedValue;

		public RequestResourcePanel (IHostResourceProvider hostResources, ResourceSelectorViewModel viewModel, object value)
		{
			if (hostResources == null)
				throw new ArgumentNullException (nameof (hostResources));
				
			this.viewModel = viewModel;
			this.viewModel.PropertyChanged += OnPropertyChanged;
			Initialize (hostResources, value);
		}

		private void OnPropertyChanged (object sender, PropertyChangedEventArgs e)
		{
			if (e.PropertyName == nameof (this.viewModel.IsLoading)) {
				this.progressIndicator.Hidden = !this.viewModel.IsLoading;
				this.tableContainer.Hidden = !this.progressIndicator.Hidden;
				if (this.viewModel.IsLoading) {
					this.progressIndicator.StartAnimation (null);
				} else {
					this.progressIndicator.StopAnimation (null);
				}
			}
		}

		private void Initialize (IHostResourceProvider hostResources, object selectedValue)
		{
			this.selectedValue = selectedValue;
			Frame = new CGRect (10, 35, 630, 305);

			var FrameHeightHalf = (Frame.Height - 32) / 2;
			var FrameWidthHalf = (Frame.Width - 32) / 2;
			var FrameWidthThird = (Frame.Width - 32) / 3;

			this.progressIndicator = new NSProgressIndicator (new CGRect (FrameWidthThird, FrameHeightHalf, 32, 32)) {
				Hidden = true,
				Style = NSProgressIndicatorStyle.Spinning,
				TranslatesAutoresizingMaskIntoConstraints = false,
			};
			AddSubview (this.progressIndicator);

			this.resourceTable = new FirstResponderTableView {
				AutoresizingMask = NSViewResizingMask.WidthSizable,
				HeaderView = null,
			};

			this.dataSource = new ResourceTableDataSource (viewModel);
			var resourceTableDelegate = new ResourceTableDelegate (hostResources, dataSource);
			resourceTableDelegate.ResourceSelected += (sender, e) => {
				this.previewPanel.SelectedResource = SelectedResource;

				this.selectedValue = BrushPropertyViewModel.GetCommonBrushForResource (SelectedResource);

				ResourceSelected?.Invoke (this, EventArgs.Empty);
			};
			this.resourceTable.Delegate = resourceTableDelegate;
			this.resourceTable.DataSource = dataSource;

			NSTableColumn resourceImages = new NSTableColumn (ResourceImageColId) { Title = LocalizationResources.ResourceTableImageColumnTitle, Width = 32 };
			this.resourceTable.AddColumn (resourceImages);

			NSTableColumn resourceTypes = new NSTableColumn (ResourceTypeColId) { Title = LocalizationResources.ResourceTableTypeColumnTitle, Width = 150 };
			this.resourceTable.AddColumn (resourceTypes);

			NSTableColumn resourceName = new NSTableColumn (ResourceNameColId) { Title = LocalizationResources.ResourceTableNameColumnTitle, Width = 150 };
			resourceTable.AddColumn (resourceName);

			NSTableColumn resourceValue = new NSTableColumn (ResourceValueColId) { Title = LocalizationResources.ResourceTableValueColumnTitle, Width = 45 };
			this.resourceTable.AddColumn (resourceValue);

			this.resourceTable.DoubleClick += (object sender, EventArgs e) => {
				DoubleClicked?.Invoke (this, EventArgs.Empty);
			};

			// create a table view and a scroll view
			this.tableContainer = new NSScrollView (new CGRect (0, 0, resourceTable.TableColumns ()[0].Width + resourceTable.TableColumns ()[1].Width + resourceTable.TableColumns ()[2].Width + 10, Frame.Height)) {
				TranslatesAutoresizingMaskIntoConstraints = false,
			};

			this.tableContainer.DocumentView = resourceTable;
			AddSubview (this.tableContainer);

			this.previewPanel = new RequestResourcePreviewPanel (hostResources, new CGRect (Frame.Width - FrameWidthThird, 0, FrameWidthThird, Frame.Height));
			AddSubview (this.previewPanel);

			this.AddConstraints (new[] {
				NSLayoutConstraint.Create (this.tableContainer, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this,  NSLayoutAttribute.Width, 1f, -190f),
				NSLayoutConstraint.Create (this.tableContainer, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this,  NSLayoutAttribute.Height, 1f, 0f),
			});

			ReloadData ();
		}

		internal void ReloadData ()
		{
			this.resourceTable.ReloadData ();

			if (collectionView.Count > 0 && this.selectedValue != null) {
				for (int i = 0; i < collectionView.Count; i++) {
					var element = collectionView[i] as Resource;
					var eType = element.GetType ();
					var valuePropertyInfo = eType.GetProperty ("Value");
					var elementValue = valuePropertyInfo.GetValue (element);
					if (elementValue == this.selectedValue) {
						this.resourceTable.SelectRow (i, false);
						break;
					}
				}
			}
		}
	}
}