-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppSelectionControl.cs
148 lines (122 loc) · 5.16 KB
/
AppSelectionControl.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
using System.Linq;
using System.Windows.Controls;
using System.Windows;
using Connectifi.DesktopAgent.Fdc3;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Windows.Media.Imaging;
using System.Diagnostics;
using Svg.Skia;
using System.Net.Http;
using System.IO;
using SkiaSharp;
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Equity_Order_Book
{
public partial class AppSelectionControl : UserControl
{
private readonly HandleIntentResolution handleIntentResolution;
private readonly Stack<string> filesToDelete = new Stack<string>();
public ConnectifiApp? SelectedApp { get; private set; }
public ObservableCollection<ConnectifiApp> MyApps { get; set; }
public AppSelectionControl(HandleIntentResolution handleIntentResolution, string currentTicker, string currentIntent)
{
this.handleIntentResolution = handleIntentResolution;
this.Unloaded += AppSelectionControl_Unloaded;
InitializeComponent();
this.DataContext = this;
MyApps = new ObservableCollection<ConnectifiApp>(handleIntentResolution.Message.Data.SelectMany(x => x.Apps));
var cvs = new CollectionViewSource();
cvs.Source = MyApps;
// Create and add the group description
var pgd = new PropertyGroupDescription(".");
pgd.Converter = new GroupingTitleInstanceConverter();
cvs.GroupDescriptions.Add(pgd);
appListBox.ItemsSource = cvs.View;
appListBox.SelectedIndex = -1;
appListBox.SelectionChanged += appSelected;
IntentResolverTextBox.Text = currentIntent + " for " + currentTicker;
}
private void appSelected(object sender, RoutedEventArgs e)
{
SelectedApp = (ConnectifiApp)appListBox.SelectedItem;
if (SelectedApp != null)
{
var selectedAppIntent = handleIntentResolution.Message.Data.First(x => x.Apps.Contains(SelectedApp));
handleIntentResolution.Callback(SelectedApp, selectedAppIntent.Intent.Name);
Window.GetWindow(this).Close(); // Close the dialog
}
}
private async void OnImageLoaded(object sender, RoutedEventArgs e)
{
var img = sender as System.Windows.Controls.Image;
if (img != null && img.DataContext is ConnectifiApp dataContext && !string.IsNullOrEmpty(dataContext.Browser))
{
string url = $"{AppConfig.connectifiHost}/{dataContext.Browser.ToLower()}.svg";
// Temporary path to save the converted PNG
string tempPngPath = $"{Path.GetTempPath()}{Path.GetRandomFileName()}.png";
// Convert SVG to PNG
await ConvertSvgUrlToPngAsync(url, tempPngPath);
// Load the PNG into the image control
await LoadImageAsync(img, tempPngPath);
filesToDelete.Push(tempPngPath);
}
}
private void AppSelectionControl_Unloaded(object sender, RoutedEventArgs e)
{
while (filesToDelete.Count > 0)
{
var file = filesToDelete.Pop();
File.Delete(file);
}
}
private async Task LoadImageAsync(System.Windows.Controls.Image img, string url)
{
try
{
await img.Dispatcher.InvokeAsync(() =>
{
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(url, UriKind.Absolute);
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bi.EndInit();
img.Source = bi;
});
}
catch (Exception ex)
{
// Handle the exception or log it
Debug.WriteLine(ex.Message);
}
}
public async Task ConvertSvgUrlToPngAsync(string svgUrl, string outputPath)
{
// Fetch SVG content from URL
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; local-dotnet)");
var svgContent = await httpClient.GetStringAsync(svgUrl);
// Parse SVG content
var svg = new SKSvg();
svg.Load(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(svgContent)));
if (svg.Picture != null)
{
var svgWidth = (int)svg.Picture.CullRect.Width;
var svgHeight = (int)svg.Picture.CullRect.Height;
// Convert to PNG
var bitmap = new SKBitmap(svgWidth, svgHeight);
using (var canvas = new SKCanvas(bitmap))
{
canvas.DrawPicture(svg.Picture);
}
using (var stream = File.OpenWrite(outputPath))
{
bitmap.Encode(stream, SKEncodedImageFormat.Png, 100);
}
}
}
}
}