aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Keyboard/KeyboardView.cs
blob: a42b6f6666e28c1f6f3b6234e71dd7e60b45ee38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
generated by cgit v1.3.1 (git 2.54.0) at 2026-07-31 15:25:49 +0000
 


f='#n257'>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
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.Core.EventArguments;

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;
        private Grid gridContent;
        private ContentPresenter _content_presenter;
        private FrameworkElement _lastFocusedElement;
        private DateTime _lastFocusTime = DateTime.Now;


        #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, (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));

        #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 (GetContainer(_lastFocusedElement) != null)
                {
                    ThicknessAnimation ani = new ThicknessAnimation();
                    ani.To = new Thickness(0);
                    ani.Duration = TimeSpan.FromSeconds(0.2);
                    GetContainer(_lastFocusedElement).BeginAnimation(FrameworkElement.MarginProperty, ani);
                }
            }
        }

        private static async void Element_LostFocus(object sender, RoutedEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;

            IInputElement focusedControl = FocusManager.GetFocusedElement(Application.Current.MainWindow);

            DateTime _last = _instance._lastFocusTime;

            await Task.Delay(100);

            if (_last == _instance._lastFocusTime)
            {
                _instance.IsOpened = false;
            }
        }

        private async static void Element_GotFocus(object sender, RoutedEventArgs e)
        {
            var element = sender as FrameworkElement;

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

            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.Source == _content_presenter && IsOpened)
            {
                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)));
        }
    }
}