-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMainForm.cs
569 lines (495 loc) · 17.7 KB
/
MainForm.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
using Finsemble.Core.Clients.FDC3.Interfaces;
using Finsemble.Core.Clients.FDC3.Types;
using Finsemble.Core.Clients.Router;
using Finsemble.Winform.Core;
using Finsemble.Winform.Core.Clients.DragAndDrop;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinformExampleCore
{
public partial class MainForm : Form
{
private string[] _startupArgs;
private FinsembleWinform _bridge;
private IListener _contextListenter;
const int LinkerPillWidth = 15;
const int LinkerPillHeight = 24;
private SortedDictionary<string, Button> LinkerGroups = new SortedDictionary<string, Button>();
private System.Drawing.Text.PrivateFontCollection finfont = new System.Drawing.Text.PrivateFontCollection();
private JsonWebKey JWK = new JsonWebKey()
{
D = "S7msrBKYM_VhXmAWTLhoRobLTevToYbX3xkbkN-EiaZ6Hg-xfozn5uAQGnBnoP1ldKOgoj5Z3dx6kTgR-3xfonEfdkk6wn0OVNbuFYyGkeeV4ts5JmyVpihFqE3RbkWuQ5D5xpIhXWl1fOWEuFfGCYIib2pmBUyc4Lz4OYmMOIGEC9nJg6ZuoKOh0nDZBjjO6vbYbXCEi0ys-FD7NAWsM8jTNDxLyXmpCNSVJOnGTX9CcxnFGdLVO8fqbooaydSHtFJE9YVqUKWp54hOBFMHdsTY5iT88urrvdBLxtGf6NGUVetpw-nFiOihDRPb9wMuLY9CT4DDzLecxadrLKh0PQ",
DP = "dw8tnJQEpXazaYFcOhCvlU0Y4kGiul1W-_MTCXml8njCEx0Gp4s8jWf_QK7PcdzZRl-t_NTu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Y9lVmqs-eGEYjfuuPVYIruN4OsskMY1nz-h_14clyUmUwfCQJDV4qjcAzf80IMu53jYEW1BydRf90snRjk1dYgSq_qtTQ",
};
public MainForm(string[] args)
{
InitializeComponent();
this.Visible = false;
_startupArgs = args;
}
private void MainForm_Load(object sender, EventArgs e)
{
this.Visible = false;
MessagesRichBox.SendToBack();
DataToSendInput.EnterKeyPressed += DataToSendInput_EnterKeyPressed; ;
ConnectToFinsemble();
}
#region Finsemble
private async void ConnectToFinsemble()
{
_bridge = new FinsembleWinform(this, this.Handle.ToString("X"), _startupArgs);
_bridge.Connected += Finsemble_Connected;
await _bridge.Connect("WinformExampleCore", JWK);
}
private async void Finsemble_Connected(object sender, EventArgs e)
{
Trace.TraceInformation("FSBL Ready.");
_bridge.Clients.Logger.OnLog += Logger_OnLog;
_bridge.Clients.Logger.Log(new JToken[] { "Winform Example Core connected to Finsemble." });
// Update UI buttons
_bridge.Clients.WindowClient.WindowStateChanged += WindowClient_WindowStateChanged;
WindowClient_WindowStateChanged(null, _bridge.Clients.WindowClient.CurrentWindowState);
#region FDC3
if (_bridge.Clients.Fdc3Client != null)
{
// Listen to Fdc3Client state change to render connected channels
_bridge.Clients.Fdc3Client.StateChanged += Fdc3Client_StateChanged;
// Show joined channels
Fdc3Client_StateChanged(null, _bridge.Clients.Fdc3Client.LastStateChangedArgs);
// Example for Fdc3Client subscribe to specific context. The "*" for subscription to all contexts.
_contextListenter = await _bridge.Clients.Fdc3Client.DesktopAgentClient.AddContextListener("fdc3.instrument", HandleContext);
}
#endregion
#region Linker Client
if (_bridge.Clients.LinkerClient != null)
{
// Listen to Linker state change to render connected channels
_bridge.Clients.LinkerClient.OnStateChange(Fdc3Client_StateChanged);
// Example for LinkerClient subscribe
_bridge.Clients.LinkerClient.Subscribe("symbol", HandleLinkerData);
}
#endregion
//Example for Handling PubSub data
_bridge.Clients.RouterClient.Subscribe("Finsemble.TestWPFPubSubSymbol", HandlePubSub);
#region Example for Drag and Drop
_bridge.Clients.DragAndDropClient.SetScrim(ScrimLabel);
_bridge.Clients.DragAndDropClient.AddReceivers(new List<KeyValuePair<string, EventHandler<FinsembleEventArgs>>>()
{
new KeyValuePair<string, EventHandler<FinsembleEventArgs>>("symbol", HandleDragAndDropReceive)
});
// Emitters for data that can be dragged using the drag icon.
_bridge.Clients.DragAndDropClient.SetEmitters(new List<KeyValuePair<string, DragAndDropClient.emitter>>()
{
new KeyValuePair<string, DragAndDropClient.emitter>("symbol", HandleDragAndDropEmit)
});
#endregion
// Example for getting Spawnable component list
HandleComponentsList();
LoadAndSetButtonsFont();
this.Visible = true;
}
private void WindowClient_WindowStateChanged(object sender, JObject state)
{
var isGrouped = state?["isGrouped"]?.ToObject<bool>() ?? false;
var groupable = state?["groupable"]?.ToObject<bool>() ?? false;
var showAlwaysOnTopButton = state?["showAlwaysOnTopButton"]?.Value<bool>() ?? true;
var isAlwaysOnTop = state?["alwaysOnTop"]?.Value<bool>() ?? false;
this.Invoke(new Action(() =>
{
// update grouping button
if (groupable)
{
DockingButton.Visible = true;
DockingButton.Text = isGrouped ? "@" : ">";
DockingButton.ButtonColor = isGrouped ? Color.FromArgb(3, 155, 255) : Color.FromArgb(34, 38, 47);
DockingButton.OnHoverButtonColor = isGrouped ? Color.FromArgb(3, 155, 255) : Color.FromArgb(40, 45, 56);
}
else
{
DockingButton.Visible = false;
}
// update alwaysOnTopButton
if (showAlwaysOnTopButton)
{
AlwaysOnTopButton.Visible = true;
AlwaysOnTopButton.ButtonColor = isAlwaysOnTop ? Color.FromArgb(3, 155, 255) : Color.FromArgb(34, 38, 47);
AlwaysOnTopButton.OnHoverButtonColor = isAlwaysOnTop ? Color.FromArgb(3, 155, 255) : Color.FromArgb(40, 45, 56);
}
else
{
AlwaysOnTopButton.Visible = false;
}
}));
}
private async void HandleComponentsList()
{
try
{
var response = await _bridge.Clients.ConfigClient.Get(new [] { "finsemble", "components" });
if (response.error != null)
{
_bridge.Clients.Logger.Error(new JToken[] { "Error when receiving spawnable component list: ", response.error.ToString() });
}
else if (response.response != null)
{
var components = (JObject)response.response;
foreach (var property in components?.Properties())
{
object value = components?[property.Name]?["foreign"]?["components"]?["App Launcher"]?["launchableByUser"];
if ((value != null) && bool.Parse(value.ToString()))
{
this.Invoke(new Action(() =>
{
//elimination of duplicate names of components after Finsemble restart
if (!ComponentDropDown.Items.Contains(property.Name))
{
ComponentDropDown.Items.Add(property.Name);
ComponentDropDown.SelectedIndex = 0;
}
}));
}
}
}
}
catch (Exception ex)
{
_bridge.Clients.Logger.Error(new JToken[] { "Error when receiving spawnable component list: ", ex.ToString() });
}
}
private void HandleLinkerData(object sender, FinsembleEventArgs response)
{
this.Invoke(new Action(() =>
{
if (response.error != null)
{
_bridge.Clients.Logger.Error(new JToken[] { "Error when receiving linker data: ", response.error.ToString() });
}
else if (response.response != null)
{
string value = response.response?["data"]?.ToString();
DataLabel.Text = value;
SourceLabel.Text = "via Linker";
DataToSendInput.Text = value;
}
}));
}
private void HandleContext(Context context, IContextMetadata metadata)
{
_bridge.Clients.Logger.Debug($"Context received: {context.Value}");
if (context == null)
{
_bridge.Clients.Logger.Error(new JToken[] { "Error when receiving context. Context is null " });
}
else
{
this.Invoke(new Action(() =>
{
string value = context.Id?["ticker"]?.ToString();
SourceLabel.Text = "via FDC3";
DataLabel.Text = value;
DataToSendInput.Text = value;
}));
}
}
private JObject HandleDragAndDropEmit()
{
return new JObject
{
["symbol"] = DataToSendInput.Text,
["description"] = "Symbol " + DataToSendInput.Text
};
}
private void HandleDragAndDropReceive(object sender, FinsembleEventArgs args)
{
this.Invoke(new Action(() =>
{
if (args.error != null)
{
_bridge.Clients.Logger.Error(new JToken[] { "Error when receiving drag and drop data: ", args.error.ToString() });
}
else if (args.response != null)
{
var data = args.response["data"]?["symbol"];
if (data != null)
{
//Check if we received an object (so data.symbol.symbol) or a string (data.symbol) to support variations in the format
if (data.HasValues)
{
data = data?["symbol"];
DataLabel.Text = data.ToString();
DataToSendInput.Text = data.ToString();
SourceLabel.Text = "via Drag and Drop";
}
}
}
}));
}
private void Fdc3Client_StateChanged(object sender, FinsembleEventArgs response)
{
if (response == null) return;
this.Invoke(new Action(async () =>
{
if (response.error != null)
{
_bridge.Clients.Logger.Error(new JToken[] { "Error when receiving linker state change data: ", response.error.ToString() });
}
else if (response.response != null)
{
var visiblePillsBeforeChange = LinkerGroups.Count(x => x.Value.Visible);
var channels = response.response["channels"] as JArray;
var allChannels = response.response["allChannels"] as JArray;
if (LinkerGroups.Count == 0)
{
// setup linker channels only once
CreateLinkerPills(allChannels);
}
foreach (JObject obj in allChannels)
{
var key = obj["name"]?.ToString();
if (key != null && LinkerGroups.ContainsKey(key))
{
LinkerGroups[key].Visible = false;
LinkerGroups[key].BackColor = ColorTranslator.FromHtml(obj["color"].ToString());
}
}
foreach (JValue channel in channels)
{
var key = channel.Value?.ToString();
if (key != null && LinkerGroups.ContainsKey(key))
{
LinkerGroups[key].Visible = true;
}
}
var visiblePillsAfterChange = LinkerGroups.Count(x => x.Value.Visible);
AlignAllLinkerPills();
DragNDropEmittingButton.Location = new Point(DragNDropEmittingButton.Location.X + CalculateDragNDropLeftMarginShift(visiblePillsBeforeChange, visiblePillsAfterChange), DragNDropEmittingButton.Location.Y);
}
}));
}
private void CreateLinkerPills(JArray allChannels)
{
this.Invoke(new Action(() =>
{
int positionX = 0;
const int positionY = 19;
var colorConverter = new ColorConverter();
foreach (var channel in allChannels)
{
if (!LinkerGroups.ContainsKey(channel["name"].ToString()))
{
var label = channel["label"]?.ToString() ?? channel["name"].ToString();
var button = new Controls.RoundedButton()
{
CornerRadius = 15,
ImageMargins = new System.Drawing.Printing.Margins(0, 0, 0, 0),
Margin = new Padding(2),
Name = $"GroupButton{label.Replace(" ", "_")}",
Visible = false,
UseEllipse = true,
UseVisualStyleBackColor = true,
TextColor = Color.White,
Size = new Size(LinkerPillWidth, LinkerPillHeight),
Text = label,
ButtonColor = (Color)colorConverter.ConvertFromString(channel["color"].ToString()),
OnHoverTextColor = Color.Black,
OnHoverButtonColor = Color.DarkOliveGreen,
Location = new Point(positionX, positionY)
};
this.Controls.Add(button);
positionX += LinkerPillWidth;
LinkerGroups.Add(channel["name"].ToString(), button);
}
}
}));
}
private void AlignAllLinkerPills()
{
var initialX = LinkerButton.Location.X + LinkerButton.Width + 2;
var visiblePills = LinkerGroups.Where(x => x.Value.Visible).ToArray();
for (int i = 0; i < visiblePills.Count(); i++)
{
var button = visiblePills[i].Value;
button.Location = new Point(initialX + (i * LinkerPillWidth), button.Location.Y);
}
}
private int CalculateDragNDropLeftMarginShift(int visiblePillsBeforeShift, int visiblePillsAfterShift)
{
return (visiblePillsAfterShift - visiblePillsBeforeShift) * (LinkerPillWidth + 2);
}
private void HandlePubSub(object sender, FinsembleEventArgs args)
{
this.Invoke(new Action(() =>
{
try
{
if (args.error != null)
{
_bridge.Clients.Logger.Error(new JToken[] { "Error when retrieving spawn data: ", args.error.ToString() });
}
else if (args.response != null)
{
var pubSubData = (JObject)args.response;
var theData = ((JValue)pubSubData?["data"]?["symbol"])?.ToString();
if (theData != null)
{
DataLabel.Text = theData;
DataToSendInput.Text = theData;
SourceLabel.Text = "via PubSub";
}
}
}
catch (Exception ex)
{
_bridge.Clients.Logger.Error(new JToken[] { "Error when retrieving linker channels: ", ex.Message });
}
}));
}
private void Logger_OnLog(object sender, JObject e)
{
MessagesRichBox.Invoke(new Action(() =>
{
MessagesRichBox.Text += e + "\n\n";
}));
}
#endregion
private void LoadAndSetButtonsFont()
{
finfont.AddFontFile(@"Resources\finfont.ttf");
finfont.AddFontFile(@"Resources\font-finance.ttf");
var finFont = new System.Drawing.Font(finfont.Families[0], 100);
var fontFinance = new System.Drawing.Font(finfont.Families[1], 7);
Invoke(new Action(() =>
{
ScrimLabel.Font = finFont;
LinkerButton.Font = fontFinance;
DragNDropEmittingButton.Font = fontFinance;
AlwaysOnTopButton.Font = fontFinance;
DockingButton.Font = fontFinance;
DockingButton.UseEllipse = true;
AlwaysOnTopButton.UseEllipse = true;
}));
}
#region Buttons click & DND
private void LinkerButton_Click(object sender, EventArgs e)
{
if (_bridge.Clients.LinkerClient != null)
{
_bridge.Clients.LinkerClient.OpenLinkerWindow();
}
else
{
_bridge.Clients.Util.OpenLinkerWindow();
}
}
private void SendButton_Click(object sender, EventArgs e)
{
if (_bridge.Clients.LinkerClient != null)
{
_bridge.Clients.LinkerClient.Publish(new JObject
{
["dataType"] = "symbol",
["data"] = DataToSendInput.Text
});
}
else
{
var param = new JObject
{
["type"] = "fdc3.instrument",
["name"] = DataToSendInput.Text,
["id"] = new JObject
{
["ticker"] = DataToSendInput.Text
}
};
_bridge.Clients.Fdc3Client.DesktopAgentClient.Broadcast(new Context(param));
}
this.Invoke(new Action(() =>
{
SourceLabel.Text = "via Text entry";
DataLabel.Text = DataToSendInput.Text;
}));
}
private async void AlwaysOnTopButton_Click(object sender, EventArgs e)
{
var newAlwaysOnTop = !await _bridge.Clients.WindowClient.IsAlwaysOnTop();
await _bridge.Clients.WindowClient.SetAlwaysOnTop(newAlwaysOnTop);
}
private void DockingButton_Click(object sender, EventArgs args)
{
var currentWindowName = _bridge.Clients.WindowClient.GetWindowIdentifier()["windowName"].ToString();
if (DockingButton.Text == ">")
{
_bridge.Clients.RouterClient.Transmit("DockingService.formGroup", new JObject { ["windowName"] = currentWindowName });
}
else
{
_bridge.Clients.RouterClient.Query("DockingService.leaveGroup", new JObject { ["name"] = currentWindowName }, delegate (object s, FinsembleEventArgs res) { });
}
}
private async void LaunchButton_Click(object sender, EventArgs e)
{
object selected = ComponentDropDown.Text;
if (selected == null) return;
var componentName = selected.ToString();
var context = new Context(new JObject
{
["type"] = "fdc3.instrument",
["name"] = DataToSendInput.Text,
["id"] = new JObject
{
["ticker"] = DataToSendInput.Text
}
});
// Check if the component able to receive the context
var contextToSend = await ShouldSendContextToComponent(context, componentName) ? context : null;
try
{
var appId = await _bridge.Clients.Fdc3Client.DesktopAgentClient.Open(componentName, contextToSend);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private async Task<bool> ShouldSendContextToComponent(Context context, string componentName)
{
var componentConfig = (await _bridge.Clients.ConfigClient.Get(new[] { "finsemble", "components", componentName }))?.response;
var intents = componentConfig?["appConfig"]?["interop"]?["intents"]?["listensFor"]?.Children();
foreach (var intent in intents)
{
if (intent?.First?["contexts"]?.ToString().Contains(context.Type) == true)
{
return true;
}
}
return false;
}
private void DragNDropEmittingButton_MouseDown(object sender, MouseEventArgs e)
{
ScrimLabel.BringToFront();
_bridge.Clients.DragAndDropClient.DragStartWithData(sender);
ScrimLabel.SendToBack();
}
private void DataToSendInput_EnterKeyPressed(object sender, EventArgs e)
{
DataLabel.Text = DataToSendInput.Text;
SourceLabel.Text = "via Text input";
}
#endregion
}
}