aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.Procedures/DialogController.cs
blob: 551b0d94273c097ad9fe2686e5e89d1be9247760 (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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Tango.Core.DI;
using Tango.FSE.Common.Notifications;
using Tango.FSE.Common.Threading;

namespace Tango.FSE.Procedures
{
    public class DialogController : IDialogController
    {
        private String _xaml;
        private FrameworkElement _rootElement;
        private DialogControllerVM _dialogVM;

        [TangoInject]
        private INotificationProvider NotificationProvider { get; set; }

        [TangoInject]
        private IDispatcherProvider DispatcherProvider { get; set; }

        internal DialogController(String xaml)
        {
            _xaml = xaml;
            _dialogVM = new DialogControllerVM();
            TangoIOC.Default.Inject(this);
        }

        internal void Init()
        {
            TaskCompletionSource<object> completion = new TaskCompletionSource<object>();

            DispatcherProvider.Invoke(() =>
            {
                FrameworkElement rootElement;
                var stream = new MemoryStream();
                var writer = new StreamWriter(stream);
                writer.Write(_xaml);
                writer.Flush();
                stream.Position = 0;
                rootElement = (FrameworkElement)XamlReader.Load(stream);
                _rootElement = rootElement;
                stream.Dispose();

                completion.SetResult(true);
            });

            completion.Task.GetAwaiter().GetResult();
        }

        public void Show()
        {
            TaskCompletionSource<object> completion = new TaskCompletionSource<object>();

            DispatcherProvider.Invoke(async () =>
            {
                await NotificationProvider.ShowDialog(_dialogVM, _rootElement);
                completion.SetResult(true);
            });

            completion.Task.GetAwaiter().GetResult();
        }

        public void Close()
        {
            _dialogVM?.Close(true);
        }

        public T FindControl<T>(String name) where T : DependencyObject
        {
            if (Thread.CurrentThread == Application.Current.Dispatcher.Thread)
            {
                return _rootElement.FindChild<T>(name) as T;
            }
            else
            {
                bool completed = false;
                T child = null;

                DispatcherProvider.Invoke(() =>
                {
                    child = _rootElement.FindChild<T>(name) as T;
                    completed = true;
                });

                while (!completed)
                {
                    Thread.Sleep(10);
                }

                return child;
            }
        }
    }
}