forked from Necrobot-Private/NecroBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
347 lines (294 loc) · 14.7 KB
/
MainWindow.xaml.cs
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
#region using directives
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using Awesomium.Core;
using Awesomium.Windows.Controls;
using PoGo.NecroBot.GUI.WebUiClient;
#endregion
namespace PoGo.NecroBot.GUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
#region Fields
private readonly WebUiClientConfig _settings = new WebUiClientConfig();
#endregion
#region Overrides
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// Destroy the WebControl and its underlying view.
webControl.Dispose();
}
#endregion
#region Ctors
public MainWindow()
{
InitializeComponent();
// Always handle ShowCreatedWebView. This is fired for
// links and forms with |target="_blank"| or for JavaScript
// 'window.open' calls.
webControl.ShowCreatedWebView += webControl_ShowCreatedWebView;
// We demonstrate interaction with the page. We handle these events
// and execute the examples, only on the initial main window.
webControl.NativeViewInitialized += OnNativeViewInitialized;
webControl.ConsoleMessage += OnConsoleMessage;
// Start with the specified Home URL.
Source = WebCore.Configuration.HomeURL;
}
public MainWindow(IntPtr nativeView)
{
InitializeComponent();
// Always handle ShowCreatedWebView. This is fired for
// links and forms with |target="_blank"| or for JavaScript
// 'window.open' calls.
webControl.ShowCreatedWebView += webControl_ShowCreatedWebView;
webControl.ConsoleMessage += OnConsoleMessage;
// For popups, you usually want to handle WindowClose,
// fired when the page calls 'window.close'.
webControl.WindowClose += webControl_WindowClose;
// Tell the WebControl that is should wrap a created child view.
NativeView = nativeView;
// This window will host a WebControl that is the result of
// JavaScript 'window.open'. Hide the address and status bar.
IsRegularWindow = false;
}
public MainWindow(Uri url)
{
InitializeComponent();
// Always handle ShowCreatedWebView. This is fired for
// links and forms with |target="_blank"| or for JavaScript
// 'window.open' calls.
webControl.ShowCreatedWebView += webControl_ShowCreatedWebView;
webControl.ConsoleMessage += OnConsoleMessage;
// For popups, you usually want to handle WindowClose,
// fired when the page calls 'window.close'.
webControl.WindowClose += webControl_WindowClose;
// Tell the WebControl to load a specified target URL.
Source = url;
}
#endregion
#region Methods
#endregion
#region Properties
// This will be set to the target URL, when this window does not
// host a created child view. The WebControl, is bound to this property.
public Uri Source
{
get { return (Uri) GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
/// <summary>
/// Identifies the <see cref="Source" /> dependency property.
/// </summary>
public static readonly DependencyProperty SourceProperty =
DependencyProperty.Register("Source",
typeof(Uri), typeof(MainWindow),
new FrameworkPropertyMetadata(null));
// This will be set to the created child view that the WebControl will wrap,
// when ShowCreatedWebView is the result of 'window.open'. The WebControl,
// is bound to this property.
public IntPtr NativeView
{
get { return (IntPtr) GetValue(NativeViewProperty); }
private set { SetValue(NativeViewPropertyKey, value); }
}
private static readonly DependencyPropertyKey NativeViewPropertyKey =
DependencyProperty.RegisterReadOnly("NativeView",
typeof(IntPtr), typeof(MainWindow),
new FrameworkPropertyMetadata(IntPtr.Zero));
/// <summary>
/// Identifies the <see cref="NativeView" /> dependency property.
/// </summary>
public static readonly DependencyProperty NativeViewProperty =
NativeViewPropertyKey.DependencyProperty;
// The visibility of the address bar and status bar, depends
// on the value of this property. We set this to false when
// the window wraps a WebControl that is the result of JavaScript
// 'window.open'.
public bool IsRegularWindow
{
get { return (bool) GetValue(IsRegularWindowProperty); }
private set { SetValue(IsRegularWindowPropertyKey, value); }
}
private static readonly DependencyPropertyKey IsRegularWindowPropertyKey =
DependencyProperty.RegisterReadOnly("IsRegularWindow",
typeof(bool), typeof(MainWindow),
new FrameworkPropertyMetadata(true));
/// <summary>
/// Identifies the <see cref="IsRegularWindow" /> dependency property.
/// </summary>
public static readonly DependencyProperty IsRegularWindowProperty =
IsRegularWindowPropertyKey.DependencyProperty;
#endregion
#region Event Handlers
private void webWindow_Loaded(object sender, RoutedEventArgs e)
{
var settingsPath = Path.Combine(Directory.GetCurrentDirectory(),
"WebUi" + Path.DirectorySeparatorChar + "Config.json");
_settings.Load(settingsPath);
var webUi = _settings.WebUiClients[_settings.CurrentWebUiClient];
if (!webUi.IsInstalled())
{
var inputDialog = new WebUiClientSelector(_settings);
if (inputDialog.ShowDialog() == true)
{
if (inputDialog.DialogResult == true)
{
_settings.CurrentWebUiClient = inputDialog.CurrentWebUiClient;
webUi = _settings.WebUiClients[_settings.CurrentWebUiClient];
}
}
}
else if (_settings.AutoUpdateWebUiClient && !webUi.IsUpToDate())
{
var inputDialog = new WebUiClientSelector(_settings, true);
if (inputDialog.ShowDialog() == true)
{
if (inputDialog.DialogResult == true)
{
_settings.CurrentWebUiClient = inputDialog.CurrentWebUiClient;
webUi = _settings.WebUiClients[_settings.CurrentWebUiClient];
}
}
}
if (!webUi.IsInstalled()) return;
Title = "NecroBot-Private Team GUI / " + _settings.CurrentWebUiClient;
// Tell the WebControl to load a specified target URL.
var baseUri = new Uri(Assembly.GetEntryAssembly().Location);
Source = new Uri(baseUri, webUi.HomeUri);
}
private void BtnWebUiConfig_Click(object sender, RoutedEventArgs e)
{
var inputDialog = new WebUiClientSelector(_settings);
if (inputDialog.ShowDialog() != true)
return;
if (inputDialog.DialogResult != true)
return;
_settings.CurrentWebUiClient = inputDialog.CurrentWebUiClient;
var webUi = _settings.WebUiClients[_settings.CurrentWebUiClient];
Title = "NecroBot / " + _settings.CurrentWebUiClient;
// Tell the WebControl to load a specified target URL.
var baseUri = new Uri(Assembly.GetEntryAssembly().Location);
Source = new Uri(baseUri, webUi.HomeUri);
}
private void OnNativeViewInitialized(object sender, WebViewEventArgs e)
{
// The native view is created. You can create global JavaScript objects
// at this point. These objects persist throughout the lifetime of the view
// and are available to all pages loaded by this view.
}
// Any JavaScript errors or JavaScript console.log calls,
// will call this method.
private void OnConsoleMessage(object sender, ConsoleMessageEventArgs e)
{
Debug.Print("[Line: " + e.LineNumber + "] " + e.Message);
}
private void webControl_ShowCreatedWebView(object sender, ShowCreatedWebViewEventArgs e)
{
if (webControl == null)
return;
if (!webControl.IsLive)
return;
// An instance of our application's web window, that will
// host the new view instance, either we wrap the created child view,
// or we let the WebControl create a new underlying web-view.
MainWindow newWindow;
// Treat popups differently. If IsPopup is true, the event is always
// the result of 'window.open' (IsWindowOpen is also true, so no need to check it).
// Our application does not recognize user defined, non-standard specs.
// Therefore child views opened with non-standard specs, will not be presented as
// popups but as regular new windows (still wrapping the child view however -- see below).
if (e.IsPopup && !e.IsUserSpecsOnly)
{
// JSWindowOpenSpecs.InitialPosition indicates screen coordinates.
var screenRect = e.Specs.InitialPosition.GetInt32Rect();
// Set the created native view as the underlying view of the
// WebControl. This will maintain the relationship between
// the parent view and the child, usually required when the new view
// is the result of 'window.open' (JS can access the parent window through
// 'window.opener'; the parent window can manipulate the child through the 'window'
// object returned from the 'window.open' call).
newWindow = new MainWindow(e.NewViewInstance);
// Do not show in the taskbar.
newWindow.ShowInTaskbar = false;
newWindow.Topmost = true;
// Set a border-style to indicate a popup.
newWindow.WindowStyle = WindowStyle.ToolWindow;
// Set resizing mode depending on the indicated specs.
newWindow.ResizeMode = e.Specs.Resizable ? ResizeMode.CanResizeWithGrip : ResizeMode.NoResize;
// If the caller has not indicated a valid size for the new popup window,
// let it be opened with the default size specified at design time.
if ((screenRect.Width > 0) && (screenRect.Height > 0))
{
// The indicated size, is client size.
var horizontalBorderHeight = SystemParameters.ResizeFrameHorizontalBorderHeight;
var verticalBorderWidth = SystemParameters.ResizeFrameVerticalBorderWidth;
var captionHeight = SystemParameters.CaptionHeight;
// Set the indicated size.
newWindow.Width = screenRect.Width + verticalBorderWidth*2;
newWindow.Height = screenRect.Height + captionHeight + horizontalBorderHeight*2;
}
// Show the window.
newWindow.Show();
// If the caller has not indicated a valid position for the new popup window,
// let it be opened in the default position specified at design time.
if ((screenRect.Y >= 0) && (screenRect.X >= 0))
{
// Move it to the indicated coordinates.
newWindow.Top = screenRect.Y;
newWindow.Left = screenRect.X;
}
}
else if (e.IsWindowOpen || e.IsPost)
{
// No specs or only non-standard specs were specified, but the event is still
// the result of 'window.open' or of an HTML form with target="_blank" and method="post".
// We will open a normal window but we will still wrap the new native child view,
// maintaining its relationship with the parent window.
newWindow = new MainWindow(e.NewViewInstance);
// Show the window.
newWindow.Show();
}
else
{
// The event is not the result of 'window.open' or of an HTML form with target="_blank"
// and method="post"., therefore it's most probably the result of a link with target='_blank'.
// We will not be wrapping the created view; we let the WebControl hosted in ChildWindow
// create its own underlying view. Setting Cancel to true tells the core to destroy the
// created child view.
//
// Why don't we always wrap the native view passed to ShowCreatedWebView?
//
// - In order to maintain the relationship with their parent view,
// child views execute and render under the same process (awesomium_process)
// as their parent view. If for any reason this child process crashes,
// all views related to it will be affected. When maintaining a parent-child
// relationship is not important, we prefer taking advantage of the isolated process
// architecture of Awesomium and let each view be rendered in a separate process.
e.Cancel = true;
// Note that we only explicitly navigate to the target URL, when a new view is
// about to be created, not when we wrap the created child view. This is because
// navigation to the target URL (if any), is already queued on created child views.
// We must not interrupt this navigation as we would still be breaking the parent-child
// relationship.
newWindow = new MainWindow(e.TargetURL);
// Show the window.
newWindow.Show();
}
}
private void webControl_WindowClose(object sender, WindowCloseEventArgs e)
{
// The page called 'window.close'. If the call
// comes from a frame, ignore it.
if (!e.IsCalledFromFrame)
Close();
}
#endregion
}
}