-
Notifications
You must be signed in to change notification settings - Fork 13
/
MainWindow.xaml.cs
127 lines (107 loc) · 3.99 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
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ReceiptPrinterEmulator.Emulator;
using ReceiptPrinterEmulator.Logging;
using ReceiptPrinterEmulator.Utils;
using Image = System.Windows.Controls.Image;
namespace ReceiptPrinterEmulator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
App.Printer!.OnActivityEvent += (o, args) =>
{
RefreshUI();
WindowsUtils.FlashWindow(this);
WindowsUtils.ExclaimSoft();
};
RefreshUI();
}
private void ResetButton_OnClick(object sender, RoutedEventArgs e)
{
Logger.Info("Resetting");
App.Printer!.ReceiptStack.Clear();
App.Printer.Initialize();
App.Printer.StartNewReceipt();
var toRemove = new List<Image>();
foreach (var childControl in ReceiptImageRoot.Children)
if (childControl is Image imgControl)
toRemove.Add(imgControl);
toRemove.ForEach(img => ReceiptImageRoot.Children.Remove(img));
RefreshUI();
}
private void TestButton_OnClick(object sender, RoutedEventArgs e)
{
if (!File.Exists("test_receipt.txt"))
return;
App.Printer?.FeedEscPos(File.ReadAllText("test_receipt.txt", Encoding.ASCII));
}
private void RefreshUI()
{
// Status label
Address.Text = $"{App.Server!.EndPoint}";
Address.Foreground = new SolidColorBrush(App.Server!.IsRunning ? Colors.SpringGreen : Colors.Crimson);
// Receipt images
foreach (var receipt in App.Printer!.ReceiptStack)
CreateOrUpdateReceiptControl(ReceiptImageRoot, receipt);
MainScrollView.ScrollToBottom();
}
private void CreateOrUpdateReceiptControl(Panel parentControl, Receipt receipt)
{
if (receipt.IsEmpty)
return;
var guidName = "R" + receipt.Guid.Replace("-", "");
Image? ourControl = null;
foreach (var childControl in parentControl.Children)
{
if (childControl is Image imgControl)
{
if (imgControl.Name == guidName)
{
ourControl = imgControl;
break;
}
}
}
if (ourControl == null)
{
ourControl = new Image();
ourControl.Name = guidName;
ourControl.Stretch = Stretch.None;
ourControl.Margin = new Thickness(0, 0, 0, 10);
parentControl.Children.Add(ourControl);
}
ourControl.Source = ConvertBitmap(receipt.Render());
}
/// <summary>
/// Takes a bitmap and converts it to an image that can be handled by WPF ImageBrush
/// </summary>
/// <param name="src">A bitmap image</param>
/// <returns>The image as a BitmapImage for WPF</returns>
public BitmapImage ConvertBitmap(Bitmap src)
{
MemoryStream ms = new MemoryStream();
((System.Drawing.Bitmap)src).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage image = new BitmapImage();
image.BeginInit();
ms.Seek(0, SeekOrigin.Begin);
image.StreamSource = ms;
image.EndInit();
return image;
}
}
}