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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.Core.EventArguments;
using Tango.Touch.Controls;
namespace Tango.Touch.Keyboard
{
/// <summary>
/// Interaction logic for KeyboardView.xaml
/// </summary>
[ContentProperty(nameof(View))]
public partial class KeyboardView : Control
{
public event EventHandler KeyboardOpened;
public event EventHandler KeyboardClosed;
private static KeyboardView _instance;
private List<Object> _has_focus_objects = new List<object>();
private TouchKeyboard keyboard;
private Grid gridContent;
private ContentPresenter _content_presenter;
private FrameworkElement _lastFocusedElement;
private DateTime _lastFocusTime = DateTime.Now;
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(int hwnd);
[DllImport("user32.dll")]
private static extern int GetDesktopWindow();
#region Properties
public static KeyboardView Default
{
get { return _instance; }
}
public static TouchKeyboard Keyboard
{
get
{
return Default.keyboard;
}
}
public bool IsOpened
{
get { return (bool)GetValue(IsOpenedProperty); }
set { SetValue(IsOpenedProperty, value); }
}
public static readonly DependencyProperty IsOpenedProperty =
DependencyProperty.Register("IsOpened", typeof(bool), typeof(KeyboardView), new PropertyMetadata(false, (d, e) => (d as KeyboardView).IsOpenedChanged()));
public FrameworkElement View
{
get { return (FrameworkElement)GetValue(ViewProperty); }
set { SetValue(ViewProperty, value); }
}
public static readonly DependencyProperty ViewProperty =
DependencyProperty.Register("View", typeof(FrameworkElement), typeof(KeyboardView), new PropertyMetadata(null));
public KeyboardOutputMode OutputMode
{
get { return (KeyboardOutputMode)GetValue(OutputModeProperty); }
set { SetValue(OutputModeProperty, value); }
}
public static readonly DependencyProperty OutputModeProperty =
DependencyProperty.Register("OutputMode", typeof(KeyboardOutputMode), typeof(KeyboardView), new PropertyMetadata(KeyboardOutputMode.Wpf));
#endregion
#region Attached Properties
#region Mode
public static readonly DependencyProperty ModeProperty =
DependencyProperty.RegisterAttached("Mode",
typeof(TouchKeyboardMode?), typeof(KeyboardView),
new FrameworkPropertyMetadata(null, ModeChanged));
/// <summary>
/// Mode changed.
/// </summary>
/// <param name="d">The d.</param>
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void ModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
RegisterElement(d as FrameworkElement);
}
/// <summary>
/// Sets the Mode attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">if set to <c>true</c> [value].</param>
public static void SetMode(FrameworkElement element, TouchKeyboardMode? value)
{
element.SetValue(ModeProperty, value);
}
/// <summary>
/// Gets the Mode attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
public static TouchKeyboardMode? GetMode(FrameworkElement element)
{
return (TouchKeyboardMode?)element.GetValue(ModeProperty);
}
private static void RegisterElement(FrameworkElement element)
{
element.GotFocus -= Element_GotFocus;
element.LostFocus -= Element_LostFocus;
element.GotFocus += Element_GotFocus;
element.LostFocus += Element_LostFocus;
}
private void IsOpenedChanged()
{
if (!IsOpened)
{
if (OutputMode == KeyboardOutputMode.Wpf)
{
if (GetContainer(_lastFocusedElement) != null)
{
var scrollViewers = this.FindVisualChildren<LightTouchScrollViewer>().ToList();
scrollViewers.ForEach(x => x.DisableScrolling = true);
ThicknessAnimation ani = new ThicknessAnimation();
ani.Completed += (_, __) =>
{
scrollViewers.ForEach(x => x.DisableScrolling = false);
};
ani.To = new Thickness(0);
ani.Duration = TimeSpan.FromSeconds(0.2);
GetContainer(_lastFocusedElement).BeginAnimation(FrameworkElement.MarginProperty, ani);
}
}
KeyboardClosed?.Invoke(this, new EventArgs());
}
else
{
KeyboardOpened?.Invoke(this, new EventArgs());
}
}
private static async void Element_LostFocus(object sender, RoutedEventArgs e)
{
if (_instance == null) return;
FrameworkElement element = sender as FrameworkElement;
DateTime _last = _instance._lastFocusTime;
await Task.Delay(300);
if (_last == _instance._lastFocusTime)
{
_instance.IsOpened = false;
}
}
private async static void Element_GotFocus(object sender, RoutedEventArgs e)
{
if (_instance == null) return;
var element = sender as FrameworkElement;
IInputElement focusedControl = FocusManager.GetFocusedElement(Application.Current.MainWindow);
_instance._lastFocusTime = DateTime.Now;
_instance._lastFocusedElement = element;
_instance.keyboard.ActionKeyMode = GetAction(element);
_instance.keyboard.Mode = GetMode(element).Value;
_instance.keyboard.IsSpecialCharactersOn = false;
if (!_instance.IsOpened)
{
_instance.IsOpened = true;
await Task.Delay(200);
}
if (!_instance._has_focus_objects.Contains(element))
{
SetForegroundWindow(GetDesktopWindow());
Application.Current.MainWindow.Activate();
_instance._has_focus_objects.Add(element);
}
FrameworkElement container = GetContainer(element);
if (container != null)
{
double viewPort = _instance.ActualHeight - _instance.keyboard.ActualHeight;
double centerViewPort = viewPort / 2;
Point relativeLocation = element.TranslatePoint(new Point(0, 0), _instance.gridContent);
double y = relativeLocation.Y + -container.Margin.Top;
if (relativeLocation.Y + element.ActualHeight + 10 > viewPort || relativeLocation.Y < 0)
{
var offset = GetContainerOffset(container);
double requiredMargin = ((y + (element.ActualHeight / 2)) - centerViewPort) + -offset;
ThicknessAnimation ani = new ThicknessAnimation();
ani.To = new Thickness(0, -requiredMargin, 0, 0);
ani.Duration = TimeSpan.FromSeconds(0.2);
container.BeginAnimation(FrameworkElement.MarginProperty, ani);
}
}
}
#endregion
#region Container
public static readonly DependencyProperty ContainerProperty =
DependencyProperty.RegisterAttached("Container",
typeof(FrameworkElement), typeof(KeyboardView),
new FrameworkPropertyMetadata(null));
/// <summary>
/// Sets the Container attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">if set to <c>true</c> [value].</param>
public static void SetContainer(FrameworkElement element, FrameworkElement value)
{
element.SetValue(ContainerProperty, value);
}
/// <summary>
/// Gets the Container attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
public static FrameworkElement GetContainer(FrameworkElement element)
{
return (FrameworkElement)element.GetValue(ContainerProperty);
}
#endregion
#region Action
public static readonly DependencyProperty ActionProperty =
DependencyProperty.RegisterAttached("Action",
typeof(KeyboardActionKeyMode), typeof(KeyboardView),
new FrameworkPropertyMetadata(KeyboardActionKeyMode.Next));
/// <summary>
/// Sets the Action attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">if set to <c>true</c> [value].</param>
public static void SetAction(FrameworkElement element, KeyboardActionKeyMode value)
{
element.SetValue(ActionProperty, value);
}
/// <summary>
/// Gets the Action attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
public static KeyboardActionKeyMode GetAction(FrameworkElement element)
{
return (KeyboardActionKeyMode)element.GetValue(ActionProperty);
}
#endregion
#region Container Offset
public static readonly DependencyProperty ContainerOffsetProperty =
DependencyProperty.RegisterAttached("ContainerOffset",
typeof(double), typeof(KeyboardView),
new FrameworkPropertyMetadata(0.0));
/// <summary>
/// Sets the ContainerOffset attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">if set to <c>true</c> [value].</param>
public static void SetContainerOffset(FrameworkElement element, double value)
{
element.SetValue(ContainerOffsetProperty, value);
}
/// <summary>
/// Gets the ContainerOffset attached property.
/// </summary>
/// <param name="element">The element.</param>
/// <returns></returns>
public static double GetContainerOffset(FrameworkElement element)
{
return (double)element.GetValue(ContainerOffsetProperty);
}
#endregion
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="KeyboardView"/> class.
/// </summary>
public KeyboardView()
{
_instance = this;
}
#endregion
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
keyboard = GetTemplateChild("PART_Keyboard") as TouchKeyboard;
gridContent = GetTemplateChild("PART_GridContent") as Grid;
_content_presenter = GetTemplateChild("PART_ContentPresenter") as ContentPresenter;
keyboard.ActionKeyPressed += OnActionKeyPressed;
gridContent.RegisterForMouseOrTouchDown(OnMouseDown);
}
private void OnMouseDown(object sender, MouseOrTouchEventArgs e)
{
if (e.OriginalSource.GetType().Name != "TextBoxView" && e.Source == _content_presenter && IsOpened && OutputMode == KeyboardOutputMode.Wpf)
{
IsOpened = false;
}
}
private void OnActionKeyPressed(object sender, KeyboardActionKeyMode mode)
{
if (mode == KeyboardActionKeyMode.Go || mode == KeyboardActionKeyMode.Search)
{
IsOpened = false;
}
}
static KeyboardView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(KeyboardView), new FrameworkPropertyMetadata(typeof(KeyboardView)));
}
}
}
|