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

PopoverBackend.cs « Xwt.Mac « Xwt.XamMac - github.com/mono/xwt.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c0c1c7ef4c8c94df31a67e0746399fac9b605cde (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// PopoverBackend.cs
//
// Author:
//       Jérémie Laval <jeremie.laval@xamarin.com>
//       Vsevolod Kukol <sevoku@microsoft.com>
//
// Copyright (c) 2012 Xamarin, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using AppKit;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
using Xwt.Backends;
using Xwt.Drawing;


namespace Xwt.Mac
{
	public class PopoverBackend : IPopoverBackend
	{
		public Popover Frontend { get; private set; }
		public ApplicationContext ApplicationContext { get; set; }
		public IPopoverEventSink EventSink { get; set; }
		internal bool EnableCloseEvent { get; private set; }
		NSPopover popover;
		FactoryViewController controller;

		class FactoryViewController : NSViewController, INSPopoverDelegate
		{
			// Always retain the object in a cache until it closes. This
			// guarantees that neither the NSPopover nor the native FactoryViewController
			// can be GC'ed until after it has closed.
			static readonly HashSet<FactoryViewController> Cache = new HashSet<FactoryViewController> ();

			public Widget Child { get; private set; }
			public PopoverBackend Backend { get; private set; }
			public CGColor BackgroundColor { get; set; }
			public ViewBackend ChildBackend { get; private set; }
			public NSView NativeChild { get { return ChildBackend?.Widget; } }
			bool shown;
			NSPopover parent;

			public FactoryViewController (PopoverBackend backend, Widget child, NSPopover parentPopover) : base (null, null)
			{
				Child = child;
				ChildBackend = Toolkit.GetBackend (Child) as ViewBackend;
				Backend = backend;
				parent = parentPopover;
				Cache.Add (this);
			}

			public string EffectiveAppearanceName { get; set; }

			public override void LoadView ()
			{
				View = new ContainerView (this);

				string appearance = EffectiveAppearanceName;

				NativeChild.RemoveFromSuperview ();
				View.AddSubview (NativeChild);

				if (!string.IsNullOrEmpty(appearance) && appearance.IndexOf ("Dark", StringComparison.Ordinal) >= 0)
					View.Appearance = NSAppearance.GetAppearance (MacSystemInformation.OsVersion < MacSystemInformation.Mojave ? NSAppearance.NameVibrantDark : new NSString("NSAppearanceNameDarkAqua"));
				else
					View.Appearance = NSAppearance.GetAppearance (NSAppearance.NameAqua);

				WidgetSpacing padding = 0;
				if (Backend != null)
					padding = Backend.Frontend.Padding;
				View.AddConstraints (new NSLayoutConstraint [] {
					NSLayoutConstraint.Create (NativeChild, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1, (nfloat)padding.Left),
					NSLayoutConstraint.Create (NativeChild, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, -(nfloat)padding.Right),
					NSLayoutConstraint.Create (NativeChild, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1, (nfloat)padding.Top),
					NSLayoutConstraint.Create (NativeChild, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1, -(nfloat)padding.Bottom),
				});
			}

			class ContainerView : NSView
			{
				FactoryViewController controller;
				public ContainerView (FactoryViewController controller)
				{
					this.controller = controller;
				}

				public override bool AllowsVibrancy {
					get {
						// disable vibrancy for custom background
						return controller.BackgroundColor == null ? base.AllowsVibrancy : false;
					}
				}
			}

			[Export ("popoverWillShow:")]
			public void WillShow (NSNotification notification)
			{
				if (parent != notification.Object)
					return;
				ChildBackend.SetAutosizeMode (true);
				Child.Surface.Reallocate ();
				if (BackgroundColor != null) {
					if (View.Window.ContentView.Superview.Layer == null)
						View.Window.ContentView.Superview.WantsLayer = true;
					View.Window.ContentView.Superview.Layer.BackgroundColor = BackgroundColor;
				}
				WidgetSpacing padding = 0;
				if (Backend != null)
					padding = Backend.Frontend.Padding;
				NativeChild.SetFrameOrigin (new CGPoint ((nfloat)padding.Left, (nfloat)padding.Top));
				shown = true;
			}

			[Export ("popoverDidClose:")]
			public virtual void DidClose (NSNotification notification)
			{
				// verify that this is called for the parent popover
				// without being disposed
				if (parent != notification.Object)
					return;
				Dispose ();
			}

			protected override void Dispose (bool disposing)
			{
				if (disposing)
					RemoveChild ();
				if (parent != null) {
					Cache.Remove (this);
					parent = null;
					if (Backend?.EnableCloseEvent == true && shown)
						Backend.ApplicationContext.InvokeUserCode (Backend.EventSink.OnClosed);
				}
				shown = false;
				base.Dispose (disposing);
			}

			void RemoveChild ()
			{
				if (Child != null) {
					NativeChild.RemoveFromSuperview ();
					NativeChild.SetFrameOrigin (new CGPoint (0, 0));
					ChildBackend.SetAutosizeMode (false);
					Child = null;
					ChildBackend = null;
				}
			}
		}

		CGColor backgroundColor;

		public Color BackgroundColor {
			get {
				return (backgroundColor ?? NSColor.WindowBackground.CGColor).ToXwtColor ();
			}
			set {
				backgroundColor = value.ToCGColor ();
			}
		}

		public void Initialize (IPopoverEventSink sink)
		{
			EventSink = sink;
		}

		public void InitializeBackend (object frontend, ApplicationContext context)
		{
			ApplicationContext = context;
			Frontend = frontend as Popover;
		}

		public void EnableEvent (object eventId)
		{
			if (eventId is PopoverEvent) {
				switch ((PopoverEvent)eventId) {
				case PopoverEvent.Closed:
					EnableCloseEvent = true;
					break;
				}
			}
		}

		public void DisableEvent (object eventId)
		{
			if (eventId is PopoverEvent) {
				switch ((PopoverEvent)eventId) {
				case PopoverEvent.Closed:
					EnableCloseEvent = false;
					break;
				}
			}
		}

		public void Show (Popover.Position orientation, Widget referenceWidget, Rectangle positionRect, Widget child)
		{
			var refBackend = Toolkit.GetBackend (referenceWidget) as IWidgetBackend;

			NSView refView = (refBackend as EmbedNativeWidgetBackend)?.EmbeddedView;

			if (refView == null)
				refView = (refBackend as ViewBackend)?.Widget;

			if (refView == null) {
				if (referenceWidget.Surface.ToolkitEngine.Type == ToolkitType.Gtk) {
					try {
						refView = GtkQuartz.GetView (refBackend.NativeWidget);
						var rLocation = refView.ConvertRectToView (refView.Frame, null).Location.ToXwtPoint ();
						if (referenceWidget.WindowBounds.Location != rLocation) {
							positionRect.X += referenceWidget.WindowBounds.Location.X - rLocation.X;
							positionRect.Y += referenceWidget.WindowBounds.Location.Y - rLocation.Y;
						}
					} catch (Exception ex) {
						throw new ArgumentException ("Widget belongs to an unsupported Toolkit", nameof (referenceWidget), ex);
					}
				} else if (referenceWidget.Surface.ToolkitEngine != ApplicationContext.Toolkit)
					throw new ArgumentException ("Widget belongs to an unsupported Toolkit", nameof (referenceWidget));
			}

			// If the rect is empty, the coordinates of the rect will be ignored.
			// Set the width and height, for the positioning to function correctly.
			if (Math.Abs (positionRect.Width) < double.Epsilon)
				positionRect.Width = referenceWidget.Size.Width;
			if (Math.Abs (positionRect.Height) < double.Epsilon)
				positionRect.Height = referenceWidget.Size.Height;

			DestroyPopover ();

			popover = new NSAppearanceCustomizationPopover {
				Behavior = NSPopoverBehavior.Transient
			};
			controller = new FactoryViewController (this, child, popover) { BackgroundColor = backgroundColor };
			popover.ContentViewController = controller;
			popover.Delegate = controller;

			// if the reference has a custom appearance, use it for the popover
			if (refView.EffectiveAppearance.Name != NSAppearance.NameAqua) {

				controller.EffectiveAppearanceName = refView.EffectiveAppearance.Name;

				if (popover is INSAppearanceCustomization)
					((INSAppearanceCustomization)popover).Appearance = refView.EffectiveAppearance;
			}

			popover.Show (positionRect.ToCGRect (),
				      refView,
				      ToRectEdge (orientation));
		}

		public void Hide ()
		{
			DestroyPopover ();
		}

		public void Dispose ()
		{
			DestroyPopover ();
		}

		void DestroyPopover ()
		{
			if (popover != null) {
				popover.Close ();
				popover.Dispose ();
				popover = null;
			}
			if (controller != null) {
				controller.Dispose ();
				controller = null;
			}
		}

		public static NSRectEdge ToRectEdge (Popover.Position pos)
		{
			switch (pos) {
			case Popover.Position.Top:
				return NSRectEdge.MaxYEdge;
			default:
				return NSRectEdge.MinYEdge;
			}
		}

		public class NSAppearanceCustomizationPopover : NSPopover, INSAppearanceCustomization
		{
			public NSAppearanceCustomizationPopover ()
			{ }

			protected NSAppearanceCustomizationPopover (NativeHandle handle) : base (handle)
			{ }
		}
	}
}