forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssue24977.cs
More file actions
146 lines (125 loc) · 3.36 KB
/
Issue24977.cs
File metadata and controls
146 lines (125 loc) · 3.36 KB
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
namespace Maui.Controls.Sample.Issues;
[Issue(IssueTracker.Github, 24977, "Keyboard Scrolling in editors with Center or End VerticalTextAlignment is off", PlatformAffected.iOS)]
public class Issue24977 : TestNavigationPage
{
protected override void Init()
{
PushAsync(new ContentPage
{
Content = new VerticalStackLayout
{
Spacing = 12,
Padding = new Thickness(12, 24),
Children =
{
CreateButton(TextAlignment.Start),
CreateButton(TextAlignment.Center),
CreateButton(TextAlignment.End),
}
}
});
}
Button CreateButton(TextAlignment textAlignment)
{
var btn = new Button
{
Text = textAlignment.ToString(),
AutomationId = $"{textAlignment}Button"
};
btn.Clicked += (s, e) => Navigation.PushAsync(new Issue24977_1(textAlignment));
return btn;
}
}
public class Issue24977_1 : TestContentPage
{
Editor editor;
Label cursorHeightTracker;
public Issue24977_1(TextAlignment textAlignment)
{
editor.VerticalTextAlignment = textAlignment;
}
protected override void Init()
{
var rootGrid = new Grid
{
RowDefinitions =
{
new RowDefinition { Height = 50 },
new RowDefinition { Height = 50 },
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) },
new RowDefinition { Height = 50 }
},
Margin = new Thickness(30)
};
var entry = new Entry
{
Text = "Content before",
AutomationId = "EntryBefore",
ReturnType = ReturnType.Next,
BackgroundColor = Colors.Aquamarine,
};
cursorHeightTracker = new Label
{
Text = "0",
AutomationId = "CursorHeightTracker",
BackgroundColor = Colors.Aquamarine,
};
editor = new Editor
{
Text = "Hello World!",
AutomationId = "IssueEditor",
BackgroundColor = Colors.Orange,
};
editor.TextChanged += Editor_TextChanged;
var button = new Button { Text = "Erase" };
button.Clicked += (s, e) => editor.Text = string.Empty;
rootGrid.Add(entry, 0, 0);
rootGrid.Add(cursorHeightTracker, 0, 1);
rootGrid.Add(editor, 0, 2);
rootGrid.Add(button, 0, 3);
Content = rootGrid;
}
private void Editor_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender is Editor editor)
{
AddCursorHeightToLabel(editor);
}
}
void AddCursorHeightToLabel(Editor editor)
{
#if IOS
var textInput = editor.Handler.PlatformView as UIKit.UITextView;
var selectedTextRange = textInput?.SelectedTextRange;
var localCursor = selectedTextRange is not null ? textInput?.GetCaretRectForPosition(selectedTextRange.Start) : null;
if (localCursor is CoreGraphics.CGRect local && textInput is not null)
{
var container = GetContainerView(textInput);
var cursorInContainer = container.ConvertRectFromView(local, textInput);
var cursorInWindow = container.ConvertRectToView(cursorInContainer, null);
cursorHeightTracker.Text = cursorInWindow.Y.ToString();
}
UIKit.UIView GetContainerView(UIKit.UIView startingPoint)
{
var rootView = FindResponder<Microsoft.Maui.Platform.ContainerViewController>(startingPoint)?.View;
if (rootView is not null)
{
return rootView;
}
return null;
}
T FindResponder<T>(UIKit.UIView view)
where T : UIKit.UIResponder
{
var nextResponder = view as UIKit.UIResponder;
while (nextResponder is not null)
{
nextResponder = nextResponder.NextResponder;
if (nextResponder is T responder)
return responder;
}
return null;
}
#endif
}
}