-
Notifications
You must be signed in to change notification settings - Fork 1
/
ResourceView.cs
137 lines (120 loc) · 3.46 KB
/
ResourceView.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
using System.Collections;
using System.Data;
using System.Resources;
using ICSharpCode.Decompiler.Metadata;
using Terminal.Gui;
namespace Cilurbo;
class ResourceView : View {
public ResourceView ()
{
Width = Dim.Fill ();
Height = Dim.Fill ();
TextView = new TextView () {
Width = Dim.Fill (),
Height = Dim.Fill (),
ReadOnly = true,
Visible = false,
};
Add (TextView);
TableView = new ExportableTableView () {
Width = Dim.Fill (),
Height = Dim.Fill (),
Visible = false,
};
Add (TableView);
ScrollBarView sbv = new (TextView, true);
// copied from terminal.gui sample
sbv.ChangedPosition += () => {
TextView.TopRow = sbv.Position;
if (TextView.TopRow != sbv.Position) {
sbv.Position = TextView.TopRow;
}
TextView.SetNeedsDisplay ();
};
sbv.OtherScrollBarView.ChangedPosition += () => {
TextView.LeftColumn = sbv.OtherScrollBarView.Position;
if (TextView.LeftColumn != sbv.OtherScrollBarView.Position) {
sbv.OtherScrollBarView.Position = TextView.LeftColumn;
}
TextView.SetNeedsDisplay ();
};
sbv.VisibleChanged += () => {
if (sbv.Visible && TextView.RightOffset == 0) {
TextView.RightOffset = 1;
} else if (!sbv.Visible && TextView.RightOffset == 1) {
TextView.RightOffset = 0;
}
};
sbv.OtherScrollBarView.VisibleChanged += () => {
if (sbv.OtherScrollBarView.Visible && TextView.BottomOffset == 0) {
TextView.BottomOffset = 1;
} else if (!sbv.OtherScrollBarView.Visible && TextView.BottomOffset == 1) {
TextView.BottomOffset = 0;
}
};
TextView.DrawContent += (e) => {
sbv.Size = TextView.Lines;
sbv.Position = TextView.TopRow;
if (sbv.OtherScrollBarView != null) {
sbv.OtherScrollBarView.Size = TextView.Maxlength;
sbv.OtherScrollBarView.Position = TextView.LeftColumn;
}
sbv.LayoutSubviews ();
sbv.Refresh ();
};
}
public TextView TextView { get; private set; }
public ExportableTableView TableView { get; private set; }
public string? Title { get; set; }
public void Show (Resource resource)
{
var tab = (SuperView.SuperView as TabView)!.SelectedTab;
Title = resource.Name;
tab.Text = resource.Name;
switch (Path.GetExtension (resource.Name)) {
case ".resources":
TextView.Visible = false;
TableView.Table = GetDataTable (resource);
TableView.Visible = true;
break;
// TODO - images/video/sounds... open externally ?
default: // read as text
TableView.Visible = false;
try {
var stream = resource.TryOpenStream ();
if (stream is not null)
TextView.Text = new StreamReader (stream).ReadToEnd ();
} catch (Exception ex) {
TextView.Text = ex.ToString ();
}
TextView.Visible = true;
break;
}
}
static DataTable GetDataTable (Resource resource)
{
DataTable dt = new ();
dt.Columns.Add (new DataColumn ("Key", typeof (string)));
dt.Columns.Add (new DataColumn ("Value", typeof (string)));
dt.Columns.Add (new DataColumn ("Type", typeof (string)));
var stream = resource.TryOpenStream ();
if (stream is null)
return dt;
try {
ResourceReader res = new (stream);
foreach (DictionaryEntry kvp in res) {
var key = kvp.Key as string;
res.GetResourceData (key!, out var type, out var _);
var value = kvp.Value is null ? "<null>" : kvp.Value.ToString ()!;
dt.Rows.Add (new object [] {
key is null ? "<null>" : key.ToString (),
value,
type,
});
}
} catch (Exception ex) {
dt.Rows.Add (new object [] { ex.ToString (), "", "" });
}
return dt;
}
}