aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Keyboard/KeyboardView.cs
blob: 36bdaced1a59b95549fbddac8bb7441ad82f2b82 (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
using System;
using System.Collections.Generic;
using System.Linq;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tango.Touch.Keyboard
{
    /// <summary>
    /// Interaction logic for KeyboardView.xaml
    /// </summary>
    [ContentProperty(nameof(View))]
    public partial class KeyboardView : Control
    {
        private static KeyboardView _instance;

        private TouchKeyboard keyboard;

        #region Properties

        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));

        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));

        #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.PreviewMouseUp += (_, __) => OnElementFocused(element);
            element.PreviewTouchUp += (_, __) => OnElementFocused(element);
            element.GotFocus += (_, __) => OnElementFocused(element);
            element.LostFocus += (_, __) => OnElementLostFocus(element);
        }

        private async static void OnElementFocused(FrameworkElement element)
        {
            _instance.keyboard.ActionKeyMode = GetAction(element);
            _instance.keyboard.Mode = GetMode(element).Value;
            _instance.IsOpened = true;
            await Task.Delay(200);
            element.BringIntoView();
        }

        private static void OnElementLostFocus(FrameworkElement element)
        {
            _instance.IsOpened = false;
        }

        #endregion

        #region Action

        public static readonly DependencyProperty ActionProperty =
            DependencyProperty.RegisterAttached("Action",
            typeof(KeyboardActionKeyMode), typeof(KeyboardView),
            new FrameworkPropertyMetadata(KeyboardActionKeyMode.Tab, ActionChanged));

        /// <summary>
        /// Action changed.
        /// </summary>
        /// <param name="d">The d.</param>
        /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void ActionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RegisterElement(d as FrameworkElement);
        }

        /// <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

        #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;
            keyboard.ActionKeyPressed += OnActionKeyPressed;
        }

        private void OnActionKeyPressed(object sender, KeyboardActionKeyMode mode)
        {
            if (mode == KeyboardActionKeyMode.Enter || mode == KeyboardActionKeyMode.Search)
            {
                IsOpened = false;
            }
        }

        static KeyboardView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(KeyboardView), new FrameworkPropertyMetadata(typeof(KeyboardView)));
        }
    }
}