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

BasePathEditorControl.cs « Controls « Xamarin.PropertyEditing.Mac - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 923281ec1a655446e2dcd4e5bc7b096f391fad2a (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
using System;
using System.Collections;
using AppKit;
using Foundation;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Mac
{
	internal abstract class BasePathEditorControl<T> : PropertyEditorControl<PropertyViewModel<T>>
	{
		private const string PathRevealName = "pe-path-reveal";
		private const string PathBrowseName = "pe-path-browse";

		protected readonly TextFieldSmallButtonContainer currentTextField;

		protected readonly NSOpenPanel panel;
		protected readonly SmallButton browsePathButton;
		protected readonly SmallButton revealPathButton;

		public override NSView FirstKeyView => this.currentTextField;
		public override NSView LastKeyView => this.revealPathButton.Enabled ? this.revealPathButton : this.browsePathButton;

		private readonly NSObject[] objects;
		public override NSObject[] AccessibilityChildren { get => this.objects; }

		protected BasePathEditorControl (IHostResourceProvider hostResource)
			: base (hostResource)
		{
			this.currentTextField = new TextFieldSmallButtonContainer ();
			this.currentTextField.Changed += CurrentTextField_Changed;
			AddSubview (this.currentTextField);

			#region Reveal handler

			this.revealPathButton = new SmallButton {
				Font = NSFont.FromFontName (DefaultFontName, DefaultFontSize),
				Image = HostResources.GetNamedImage (PathRevealName),
				StringValue = string.Empty
			};

			this.currentTextField.AddButton (this.revealPathButton);

			this.panel = NSOpenPanel.OpenPanel;

			// update the value on keypress
			this.revealPathButton.Activated += OnRevealPathButtonActivated;

			#endregion

			#region Browse Path

			this.browsePathButton = new SmallButton {
				Font = NSFont.FromFontName (DefaultFontName, DefaultFontSize),
				Image = HostResources.GetNamedImage (PathBrowseName),
				StringValue = string.Empty
			};
			this.browsePathButton.Activated += BrowsePathButton_Activated;
			this.currentTextField.AddButton (this.browsePathButton);

			this.objects = new NSObject[3];
			this.objects[0] = this.currentTextField;
			this.objects[1] = this.browsePathButton;
			this.objects[2] = this.revealPathButton;
			AccessibilityElement = true;
			AccessibilityRole = NSAccessibilityRoles.GroupRole;

			this.currentTextField.AccessibilityRole = NSAccessibilityRoles.TextFieldRole;
			this.revealPathButton.AccessibilityRole = NSAccessibilityRoles.ButtonRole;
			this.browsePathButton.AccessibilityRole = NSAccessibilityRoles.ButtonRole;

			#endregion

			AddConstraints (new[] {
				NSLayoutConstraint.Create (this.currentTextField, NSLayoutAttribute.Left, NSLayoutRelation.Equal, this, NSLayoutAttribute.Left, 1, 0f),
				NSLayoutConstraint.Create (this.currentTextField, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1, 0f),
				NSLayoutConstraint.Create (this.currentTextField, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1, 0f),
				NSLayoutConstraint.Create (this.currentTextField, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1, -6f),
			});
		}

		private void CurrentTextField_Changed (object sender, EventArgs e)
		{
			StoreCurrentValue ();
		}

		private void BrowsePathButton_Activated (object sender, EventArgs e)
		{
			Window.MakeFirstResponder (this.currentTextField);

			this.panel.AllowsMultipleSelection = false;
			this.panel.CanCreateDirectories = true;
			this.panel.ShowsHiddenFiles = false;
			this.panel.ShowsResizeIndicator = true;
			this.panel.TreatsFilePackagesAsDirectories = true;
			this.panel.BeginSheet (Window, HandleAction);
		}

		protected abstract void OnRevealPathButtonActivated (object sender, EventArgs e);
		protected abstract void StoreCurrentValue ();

		private void HandleAction (nint result)
		{
			if (result == 1) {
				NSUrl url = this.panel.Url;
				if (url.IsFileUrl) {
					this.currentTextField.StringValue = url.Path;
					StoreCurrentValue ();
				}
			}
		}

		protected override void SetEnabled ()
		{
			this.currentTextField.Enabled =
			this.browsePathButton.Enabled =
			this.revealPathButton.Enabled = ViewModel.Property.CanWrite;
		}

		protected override void Dispose (bool disposing)
		{
			this.currentTextField.Changed -= CurrentTextField_Changed;
			this.browsePathButton.Activated -= BrowsePathButton_Activated;
			this.revealPathButton.Activated -= OnRevealPathButtonActivated;
			base.Dispose (disposing);
		}

		protected override void AppearanceChanged ()
		{
			base.AppearanceChanged ();

			this.revealPathButton.Image = HostResources.GetNamedImage (PathRevealName);
			this.browsePathButton.Image = HostResources.GetNamedImage (PathBrowseName);
		}
	}
}