-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListItemViewModels.cs
More file actions
112 lines (98 loc) · 3.82 KB
/
ListItemViewModels.cs
File metadata and controls
112 lines (98 loc) · 3.82 KB
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
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace Image2Text
{
public class HistoryItemVM
{
public HistoryEntry Entry { get; }
public ImageSource? Thumbnail { get; }
public string Preview { get; }
public string TimestampLabel { get; }
public string ConfidenceLabel { get; }
public string SettingsLabel { get; }
public HistoryItemVM(HistoryEntry entry)
{
Entry = entry;
string imagePath = HistoryStore.GetImagePath(entry);
if (File.Exists(imagePath))
{
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(imagePath);
bmp.DecodePixelWidth = 200;
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.EndInit();
bmp.Freeze();
Thumbnail = bmp;
}
Preview = (entry.Text ?? "").Replace("\r", " ").Replace("\n", " ").Trim();
if (Preview.Length > 220) Preview = Preview.Substring(0, 220) + "…";
if (string.IsNullOrWhiteSpace(Preview)) Preview = "(no text)";
TimestampLabel = entry.Timestamp.ToString("yyyy-MM-dd HH:mm");
ConfidenceLabel = $"{entry.Confidence * 100:F0}%";
SettingsLabel = entry.AutoEnhanced ? $"{entry.PsmMode} · enhanced" : entry.PsmMode;
}
}
public enum BatchItemStatus { Pending, Processing, Done, Failed }
public class BatchItemVM : INotifyPropertyChanged
{
public string Path { get; }
public string Filename { get; }
public string Subtitle { get; set; }
public OcrResult? Result { get; set; }
private BatchItemStatus _status = BatchItemStatus.Pending;
public BatchItemStatus Status
{
get => _status;
set
{
if (_status == value) return;
_status = value;
Notify(nameof(Status));
Notify(nameof(StatusLabel));
Notify(nameof(StatusBrush));
}
}
private float _confidence;
public float Confidence
{
get => _confidence;
set
{
if (Math.Abs(_confidence - value) < 0.0001f) return;
_confidence = value;
Notify(nameof(ConfidenceLabel));
}
}
public string StatusLabel => _status switch
{
BatchItemStatus.Pending => "PENDING",
BatchItemStatus.Processing => "PROCESSING",
BatchItemStatus.Done => "DONE",
BatchItemStatus.Failed => "FAILED",
_ => "",
};
public Brush StatusBrush => _status switch
{
BatchItemStatus.Pending => new SolidColorBrush(Color.FromRgb(0x63, 0x63, 0x6e)),
BatchItemStatus.Processing => new SolidColorBrush(Color.FromRgb(0x81, 0x8c, 0xf8)),
BatchItemStatus.Done => new SolidColorBrush(Color.FromRgb(0x22, 0xc5, 0x5e)),
BatchItemStatus.Failed => new SolidColorBrush(Color.FromRgb(0xef, 0x44, 0x44)),
_ => new SolidColorBrush(Color.FromRgb(0xd4, 0xd4, 0xd8)),
};
public string ConfidenceLabel =>
_status == BatchItemStatus.Done ? $"{_confidence * 100:F0}%" : "";
public BatchItemVM(string path)
{
Path = path;
Filename = System.IO.Path.GetFileName(path);
var fi = new FileInfo(path);
Subtitle = $"{path} • {fi.Length / 1024} KB";
}
public event PropertyChangedEventHandler? PropertyChanged;
private void Notify(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}