summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla42519.cs
blob: 822943ce6efd462376ac6dcc3f5e6a37d838c595 (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
using System;
using System.Collections.Generic;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Controls
{
	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 42519, "Text Truncation in UWP")]
	public class Bugzilla42519 : TestNavigationPage
	{
		public static readonly string LongLabelSingle =
			"longleftlabelthequickbrownfoxjumpedoverthelazydogsthequickbrownfoxjumpedoverthelazydogs";

		public static readonly string LongLabelWords =
			"long left label the quick brown fox jumped over the lazy dogs the quick brown fox jumped over the lazy dogs";

		protected override void Init()
		{
			PushAsync(Menu());
		}

		static ContentPage CreateContent(Type cellType)
		{
			return new ContentPage { Content = CreateListView(new DataTemplate(cellType)) };
		}

		static ListView CreateListView(DataTemplate template)
		{
			var items = new List<_42519Item>
			{
				new _42519Item
				{
					TitleLeft = LongLabelWords,
					TitleRight = "32522665",
					SubLeft = "LeftLabel",
					SubRight = "Long Right Label"
				},
				new _42519Item
				{
					TitleLeft = LongLabelSingle,
					TitleRight = "12552665222",
					SubLeft = "LeftLabel",
					SubRight = "Long Right Label"
				},
				new _42519Item
				{
					TitleLeft = LongLabelSingle,
					TitleRight = "225565365",
					SubLeft = "LeftLabel",
					SubRight = "Long Right Label"
				},
				new _42519Item
				{
					TitleLeft = LongLabelWords,
					TitleRight = "215565365",
					SubLeft = "LeftLabel",
					SubRight = "Long Right Label"
				}
			};

			return new ListView
			{
				HasUnevenRows = true,
				ItemTemplate = template,
				ItemsSource = items
			};
		}

		static ContentPage Menu()
		{
			var page = new ContentPage();

			var heading = new Label { Text = "Select an option below to see text tail truncation in various contexts." };

			if (Device.Idiom == TargetIdiom.Phone)
			{
				heading.Text += " Rotating the phone between portrait and landscape mode should not cause the ellipsis to disappear from truncated text.";
			}

			var labelButton = new Button { Text = "Single Label" };
			var gridButton = new Button { Text = "Single Grid" };
			var listWithLabelsButton = new Button { Text = "ListView with Label ViewCell" };
			var listWithGridsButton = new Button { Text = "ListView with Grid ViewCell" };

			labelButton.Clicked += (sender, args) =>
			{
				var content = new Label { Text = LongLabelSingle, LineBreakMode = LineBreakMode.TailTruncation };
				page.Navigation.PushAsync(new ContentPage { Content = content });
			};

			gridButton.Clicked += (sender, args) =>
			{
				var content = new ContentPage
				{
					Content = _42519CustomViewCellGrid.CreateGrid(),
					BindingContext = new _42519Item
					{
						TitleLeft = LongLabelSingle,
						TitleRight = "32522665",
						SubLeft = "LeftLabel",
						SubRight = "Long Right Label"
					}
				};
				page.Navigation.PushAsync(content);
			};

			listWithLabelsButton.Clicked +=
				(sender, args) => { page.Navigation.PushAsync(CreateContent(typeof(_42519CustomViewCellLabel))); };

			listWithGridsButton.Clicked +=
				(sender, args) => { page.Navigation.PushAsync(CreateContent(typeof(_42519CustomViewCellGrid))); };

			page.Content = new StackLayout
			{
				Children =
				{
					heading,
					labelButton,
					gridButton,
					listWithLabelsButton,
					listWithGridsButton
				}
			};

			return page;
		}

		[Preserve(AllMembers = true)]
		internal class _42519Item
		{
			public string SubLeft { get; set; }

			public string SubRight { get; set; }

			public string TitleLeft { get; set; }

			public string TitleRight { get; set; }
		}

		[Preserve(AllMembers = true)]
		internal class _42519CustomViewCellGrid : ViewCell
		{
			public _42519CustomViewCellGrid()
			{
				View = CreateGrid();
			}

			public static Grid CreateGrid()
			{
				var grid = new Grid
				{
					VerticalOptions = LayoutOptions.FillAndExpand,
					RowDefinitions =
					{
						new RowDefinition { Height = new GridLength(24, GridUnitType.Absolute) },
						new RowDefinition { Height = new GridLength(24, GridUnitType.Absolute) }
					},
					ColumnDefinitions =
					{
						new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
						new ColumnDefinition { Width = GridLength.Auto }
					},
					Padding = new Thickness(16, 12, 16, 12)
				};
				var leftLabel = new Label
				{
					LineBreakMode = LineBreakMode.TailTruncation
				};
				leftLabel.SetBinding(Label.TextProperty, "TitleLeft");

				var rightLabel = new Label
				{
					FontSize = 20,
					HorizontalOptions = LayoutOptions.End
				};
				rightLabel.SetBinding(Label.TextProperty, "TitleRight");

				var subLeft = new Label
				{
					LineBreakMode = LineBreakMode.TailTruncation
				};
				subLeft.SetBinding(Label.TextProperty, "SubLeft");

				var subRight = new Label
				{
					HorizontalOptions = LayoutOptions.End
				};
				subRight.SetBinding(Label.TextProperty, "SubRight");

				grid.Children.Add(leftLabel, 0, 0);
				grid.Children.Add(rightLabel, 1, 0);
				grid.Children.Add(subLeft, 0, 1);
				grid.Children.Add(subRight, 1, 1);

				return grid;
			}
		}

		[Preserve(AllMembers = true)]
		internal class _42519CustomViewCellLabel : ViewCell
		{
			public _42519CustomViewCellLabel()
			{
				var leftLabel = new Label
				{
					LineBreakMode = LineBreakMode.TailTruncation
				};

				leftLabel.SetBinding(Label.TextProperty, "TitleLeft");

				View = leftLabel;
			}
		}
	}
}