summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.ControlGallery.MacOS/AppDelegate.cs
blob: ac4940dfadd9eb5e8c8e5b0d0129c111d1b5176e (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
´╗┐using System;
using System.Globalization;
using AppKit;
using CoreGraphics;
using Foundation;
using Xamarin.Forms.Controls;
using Xamarin.Forms.Controls.Issues;
using Xamarin.Forms.Platform.MacOS;

namespace Xamarin.Forms.ControlGallery.MacOS
{
	[Register("AppDelegate")]
	public class AppDelegate : FormsApplicationDelegate
	{

		NSWindow _window;
		public AppDelegate()
		{
			ObjCRuntime.Runtime.MarshalManagedException += (sender, args) =>
			{
				Console.WriteLine(args.Exception.ToString());
			};

			var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

			var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
			//var rect = NSWindow.FrameRectFor(NSScreen.MainScreen.Frame, style);
			_window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
			_window.Title = "Twitter XF Mac";
			_window.TitleVisibility = NSWindowTitleVisibility.Hidden;
		}

		public override NSWindow MainWindow
		{
			get { return _window; }
		}

		public override void DidFinishLaunching(NSNotification notification)
		{
			Forms.Init();
			FormsMaps.Init();

			var app = new App();
			// When the native control gallery loads up, it'll let us know so we can add the nested native controls
			MessagingCenter.Subscribe<NestedNativeControlGalleryPage>(this, NestedNativeControlGalleryPage.ReadyForNativeControlsMessage, AddNativeControls);
			MessagingCenter.Subscribe<Bugzilla40911>(this, Bugzilla40911.ReadyToSetUp40911Test, SetUp40911Test);

			// When the native binding gallery loads up, it'll let us know so we can set up the native bindings
			MessagingCenter.Subscribe<NativeBindingGalleryPage>(this, NativeBindingGalleryPage.ReadyForNativeBindingsMessage, AddNativeBindings);

			LoadApplication(app);
			base.DidFinishLaunching(notification);
		}

		void AddNativeControls(NestedNativeControlGalleryPage page)
		{
			if (page.NativeControlsAdded)
			{
				return;
			}

			StackLayout sl = page.Layout;

			// Create and add a native UILabel
			var originalText = "I am a native UILabel";
			var longerText =
				"I am a native UILabel with considerably more text. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

			var uilabel = new NSTextField
			{
				StringValue = originalText,
				MaximumNumberOfLines = 0,
				LineBreakMode = NSLineBreakMode.ByWordWrapping,
				Font = NSFont.FromFontName("Helvetica", 24f)
			};

			sl?.Children.Add(uilabel);

			// Create and add a native Button 
			var uibutton = NSButtonExtensions.CreateButton("Toggle Text Amount", () =>
			{
				uilabel.StringValue = uilabel.StringValue == originalText ? longerText : originalText;
				uilabel.SizeToFit();
			});
			uibutton.Font = NSFont.FromFontName("Helvetica", 14f);


			sl?.Children.Add(uibutton.ToView());

			// Create some control which we know don't behave correctly with regard to measurement
			var difficultControl0 = new BrokenNativeControl
			{
				Font = NSFont.FromFontName("Helvetica", 14f),
				MaximumNumberOfLines = 0,
				LineBreakMode = NSLineBreakMode.ByWordWrapping,
				StringValue = "Doesn't play nice with sizing. That's why there's a big gap around it."
			};

			var difficultControl1 = new BrokenNativeControl
			{
				Font = NSFont.FromFontName("Helvetica", 14f),
				MaximumNumberOfLines = 0,
				LineBreakMode = NSLineBreakMode.ByWordWrapping,
				StringValue = "Custom size fix specified. No gaps."
			};

			var explanation0 = new NSTextField
			{
				StringValue = "The next control is a customized label with a bad SizeThatFits implementation.",
				MaximumNumberOfLines = 0,
				LineBreakMode = NSLineBreakMode.ByWordWrapping,
				Font = NSFont.FromFontName("Helvetica", 14f),
			};

			var explanation1 = new NSTextField
			{
				StringValue = "The next control is the same broken class as above, but we pass in an override to the GetDesiredSize method.",
				MaximumNumberOfLines = 0,
				LineBreakMode = NSLineBreakMode.ByWordWrapping,
				Font = NSFont.FromFontName("Helvetica", 14f),
			};

			// Add a misbehaving control
			sl?.Children.Add(explanation0);
			sl?.Children.Add(difficultControl0);

			// Add the misbehaving control with a custom delegate for FixSize
			sl?.Children.Add(explanation1);
			sl?.Children.Add(difficultControl1, FixSize);

			page.NativeControlsAdded = true;
		}

		SizeRequest? FixSize(NativeViewWrapperRenderer renderer, double width, double height)
		{
			var uiView = renderer.Control;
			var view = renderer.Element;

			if (uiView == null || view == null)
			{
				return null;
			}

			var constraint = new CGSize(width, height);

			// Let the BrokenNativeControl determine its size (which we know will be wrong)
			var badRect = uiView.FittingSize;

			// And we'll use the width (which is fine) and substitute our own height
			return new SizeRequest(new Size(badRect.Width, 20));
		}

		void AddNativeBindings(NativeBindingGalleryPage page)
		{
			if (page.NativeControlsAdded)
				return;

			StackLayout sl = page.Layout;

			int width = 200;
			int heightCustomLabelView = 100;

			var uilabel = new NSTextField(new CGRect(0, 0, width, heightCustomLabelView))
			{
				BackgroundColor = NSColor.Clear,
				Editable = false,
				Bezeled = false,
				DrawsBackground = false,
				MaximumNumberOfLines = 0,
				LineBreakMode = NSLineBreakMode.ByWordWrapping,
				Font = NSFont.FromFontName("Helvetica", 24f),
				StringValue = "DefaultText"
			};

			var uibuttonColor = NSButtonExtensions.CreateButton("Toggle Text Color Binding", () => uilabel.TextColor = NSColor.Blue);
			uibuttonColor.Font = NSFont.FromFontName("Helvetica", 14f);

			uilabel.SetBinding("StringValue", new Binding("NativeLabel"));
			uilabel.SetBinding(nameof(uilabel.TextColor), new Binding("NativeLabelColor", converter: new ColorConverter()));

			sl?.Children.Add(uilabel);
			sl?.Children.Add(uibuttonColor.ToView());
			//var colorPicker = new NSColorWell();
			//colorPicker.SetBinding("SelectedColor", new Binding("NativeLabelColor", BindingMode.TwoWay, new ColorConverter()), "ColorPicked");
			//sl?.Children.Add(colorPicker);
			page.NativeControlsAdded = true;
		}

		#region Stuff for repro of Bugzilla case 40911

		void SetUp40911Test(Bugzilla40911 page)
		{
			var button = new Button { Text = "Start" };

			button.Clicked += (s, e) =>
			{
				StartPressed40911();
			};

			page.Layout.Children.Add(button);
		}

		public void StartPressed40911()
		{
			var loginViewController = new NSViewController { View = { } };
			var button = NSButtonExtensions.CreateButton("Login", () =>
			{
				Xamarin.Forms.Application.Current.MainPage = new ContentPage { Content = new Label { Text = "40911 Success" } };
				//loginViewController.DismissViewController()true, null);

			});

			button.Frame = new CGRect(20, 100, 200, 44);
			loginViewController.View.AddSubview(button);

			var window = NSApplication.SharedApplication.KeyWindow;
			var vc = window.ContentViewController;
			while (vc.PresentedViewControllers.Length > 0)
			{
				vc = vc.PresentedViewControllers[0];
			}

			//vc.PresentViewController(loginViewController, new NSViewControllerPresentationAnimator();
		}

		#endregion

		public class ColorConverter : IValueConverter
		{
			public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
			{
				if (value is Color)
					return ((Color)value).ToNSColor();
				return value;
			}

			public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
			{
				if (value is NSColor)
					return ((NSColor)value).ToColor();
				return value;
			}
		}
	}
}