aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Modules/Tango.PPC.Browser/Views/BrowserView.xaml.cs
blob: e7fe1ca279e9054b8f77366a1f9663d73f70bdf3 (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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using CefSharp;
using CefSharp.Wpf;
using Tango.Core.DI;
using Tango.Core.Helpers;
using Tango.Logging;
using Tango.PPC.Browser.BoundsObjects;
using Tango.PPC.Browser.ViewContracts;
using Tango.PPC.Common.Helpers;
using Tango.Touch.Keyboard;

namespace Tango.PPC.Browser.Views
{
    /// <summary>
    /// Interaction logic for MainView.xaml
    /// </summary>
    public partial class BrowserView : UserControl, IBrowserView
    {
        public event EventHandler<string> AddressChanged;

        public static BrowserView Instance { get; set; }

        public BrowserView()
        {
            try
            {
                var settings = new CefSettings();
                settings.BrowserSubprocessPath = @"x86\CefSharp.BrowserSubprocess.exe";
                settings.UserAgent = "Mozilla/5.0 (iPad; CPU OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148";

                Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
            }
            catch (Exception ex)
            {
                LogManager.Default.Log(ex, "Error loading cef.");
            }

            InitializeComponent();

            Instance = this;

            TangoIOC.Default.Register<IBrowserView>(this);

            Helpers.BoundObjectsHelper.RegisterAllBoundObjects(Browser, Dispatcher);

            KeyboardView.Default.KeyboardOpened += Default_KeyboardOpened;
            KeyboardView.Default.KeyboardClosed += Default_KeyboardClosed;

            var handler = new RequestHandlers.ChromiumRequestHandler();
            handler.AddressChanged += Handler_AddressChanged;
            Browser.RequestHandler = handler;
            Browser.LoadError += Browser_LoadError;
            Browser.LoadingStateChanged += Browser_LoadingStateChanged;
        }

        private void Browser_LoadError(object sender, LoadErrorEventArgs e)
        {
            //if (e.ErrorCode == CefErrorCode.ConnectionTimedOut || e.ErrorCode == CefErrorCode.NameNotResolved)
            //{
            InvokeUI(() =>
            {
                runError.Text = e.ErrorText;
                gridError.Visibility = Visibility.Visible;
            });
            //}
        }

        private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
        {
            if (!e.IsLoading)
            {
                InvokeUI(() =>
                {
                    KeyboardHelper.CloseKeyboard();
                });
            }
            else
            {
                InvokeUI(() =>
                {
                    gridError.Visibility = Visibility.Hidden;
                });
            }
        }

        private void Handler_AddressChanged(object sender, string address)
        {
            InvokeUI(() =>
            {
                AddressChanged?.Invoke(this, address);
            });
        }

        private void Default_KeyboardClosed(object sender, EventArgs e)
        {
            Browser.VerticalAlignment = VerticalAlignment.Stretch;
            Browser.Height = double.NaN;
        }

        private void Default_KeyboardOpened(object sender, EventArgs e)
        {
            Browser.VerticalAlignment = VerticalAlignment.Top;
            Browser.Height = 780;
        }

        public bool CanGoBack()
        {
            return Browser.CanGoBack;
        }

        public void NavigateTo(string address)
        {
            if (Browser.Address != address)
            {
                String uri;

                if (ValidHttpURL(address, out uri))
                {
                    Browser.Address = uri;
                }
                else
                {
                    Browser.Address = $"google.com/search?q={address.Replace(" ", "+")}";
                }
            }
            else
            {
                Browser.Reload();
            }
        }

        public static bool ValidHttpURL(string s, out string result)
        {
            if (Uri.IsWellFormedUriString(s, UriKind.Absolute))
            {
                result = s;
                return true;
            }
            else if (s.StartsWith("www."))
            {
                result = "http://" + s;
                return true;
            }

            result = s;
            return false;
        }

        public void GoBack()
        {
            if (Browser.CanGoBack)
            {
                Browser.Back();
            }
        }

        private async void TxtAddress_GotFocus(object sender, RoutedEventArgs e)
        {
            KeyboardHelper.OpenKeyboard(KeyboardActionKeyMode.Go);
            await Task.Delay(100);
            txtAddress.SelectAll();
        }

        private void TxtAddress_LostFocus(object sender, RoutedEventArgs e)
        {
            KeyboardHelper.CloseKeyboard();
        }

        private void TxtAddress_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                KeyboardHelper.CloseKeyboard();
                NavigateTo(txtAddress.Text);
            }
        }

        private void TxtAddress_MouseUp(object sender, MouseButtonEventArgs e)
        {
            KeyboardHelper.OpenKeyboard(KeyboardActionKeyMode.Go);
        }

        private async void TxtAddress_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            KeyboardHelper.OpenKeyboard(KeyboardActionKeyMode.Go);
            await Task.Delay(100);
            txtAddress.SelectAll();
        }

        private void InvokeUI(Action action)
        {
            Dispatcher.BeginInvoke(action);
        }
    }
}