11
11
using Avalonia . Controls ;
12
12
using Avalonia . Controls . Primitives ;
13
13
using Avalonia . Interactivity ;
14
+ using Avalonia . Media . Imaging ;
14
15
using Avalonia . Platform . Storage ;
15
16
using Avalonia . Threading ;
16
17
using AvaloniaEdit ;
@@ -38,13 +39,18 @@ public sealed class BufferInspectorView : TemplatedControl
38
39
39
40
public static readonly StyledProperty < bool > ShowRawProperty = AvaloniaProperty . Register < BufferInspectorView , bool > ( nameof ( ShowRaw ) ) ;
40
41
42
+ public static readonly StyledProperty < bool > ShowTextProperty = AvaloniaProperty . Register < BufferInspectorView , bool > ( nameof ( ShowText ) ) ;
43
+
44
+ public static readonly StyledProperty < bool > ShowPictureProperty = AvaloniaProperty . Register < BufferInspectorView , bool > ( nameof ( ShowPicture ) ) ;
45
+
46
+ public static readonly StyledProperty < bool > UseBase64PreDecodingProperty = AvaloniaProperty . Register < BufferInspectorView , bool > ( nameof ( UseBase64PreDecoding ) ) ;
47
+
41
48
public static readonly StyledProperty < string ? > SelectedFormatNameProperty = AvaloniaProperty . Register < BufferInspectorView , string ? > ( nameof ( SelectedFormatName ) , "UTF-8" ) ;
42
49
43
50
readonly RegistryOptions _textEditorRegistryOptions = new ( ThemeName . Dark ) ;
44
-
45
- string _content = string . Empty ;
46
51
Button ? _copyToClipboardButton ;
47
52
HexBox ? _hexBox ;
53
+ Viewbox ? _pictureBox ;
48
54
Button ? _saveToFileButton ;
49
55
TextEditor ? _textEditor ;
50
56
TextMate . Installation ? _textMateInstallation ;
@@ -103,7 +109,7 @@ public string? SelectedFormatName
103
109
return JsonSerializerService . Instance ? . Format ( json ) ?? string . Empty ;
104
110
} ) ,
105
111
106
-
112
+ new BufferConverter ( "Picture" , null , _ => "PICTURE" ) , // Special case!
107
113
new BufferConverter ( "RAW" , null , _ => "RAW" ) , // Special case!
108
114
new BufferConverter ( "Unicode" , null , b => Encoding . Unicode . GetString ( b ) ) ,
109
115
new BufferConverter ( "UTF-8" , null , b => Encoding . UTF8 . GetString ( b ) ) ,
@@ -117,42 +123,78 @@ public string? SelectedFormatName
117
123
} )
118
124
] ;
119
125
126
+ public bool ShowPicture
127
+ {
128
+ get => GetValue ( ShowPictureProperty ) ;
129
+ set => SetValue ( ShowPictureProperty , value ) ;
130
+ }
131
+
120
132
public bool ShowRaw
121
133
{
122
134
get => GetValue ( ShowRawProperty ) ;
123
135
set => SetValue ( ShowRawProperty , value ) ;
124
136
}
125
137
138
+ public bool ShowText
139
+ {
140
+ get => GetValue ( ShowTextProperty ) ;
141
+ set => SetValue ( ShowTextProperty , value ) ;
142
+ }
143
+
144
+ public bool UseBase64PreDecoding
145
+ {
146
+ get => GetValue ( UseBase64PreDecodingProperty ) ;
147
+ set => SetValue ( UseBase64PreDecodingProperty , value ) ;
148
+ }
149
+
126
150
protected override void OnApplyTemplate ( TemplateAppliedEventArgs e )
127
151
{
128
152
base . OnApplyTemplate ( e ) ;
129
153
130
154
_hexBox = ( HexBox ) this . GetTemplateChild ( "HexBox" ) ;
155
+ _pictureBox = ( Viewbox ) this . GetTemplateChild ( "PictureBox" ) ;
131
156
132
157
_textEditor = ( TextEditor ) this . GetTemplateChild ( "TextEditor" ) ;
133
158
_textMateInstallation = _textEditor . InstallTextMate ( _textEditorRegistryOptions ) ;
134
- SyncTextEditor ( ) ;
135
159
136
160
_copyToClipboardButton = ( Button ) this . GetTemplateChild ( "CopyToClipboardButton" ) ;
137
161
_copyToClipboardButton . Click += OnCopyToClipboard ;
138
162
139
163
_saveToFileButton = ( Button ) this . GetTemplateChild ( "SaveToFileButton" ) ;
140
164
_saveToFileButton . Click += OnSaveToFile ;
141
- ReadBuffer ( ) ;
165
+
166
+ Sync ( ) ;
142
167
}
143
168
144
169
protected override void OnPropertyChanged ( AvaloniaPropertyChangedEventArgs change )
145
170
{
146
171
base . OnPropertyChanged ( change ) ;
147
172
148
- if ( change . Property == BufferProperty || change . Property == SelectedFormatProperty )
173
+ if ( change . Property == SelectedFormatProperty )
149
174
{
150
- ReadBuffer ( ) ;
175
+ if ( string . Equals ( SelectedFormat ? . Name , "RAW" ) )
176
+ {
177
+ ShowRaw = true ;
178
+ ShowText = false ;
179
+ ShowPicture = false ;
180
+ }
181
+ else if ( string . Equals ( SelectedFormat ? . Name , "Picture" ) )
182
+ {
183
+ ShowPicture = true ;
184
+ ShowText = false ;
185
+ ShowRaw = false ;
186
+ }
187
+ else
188
+ {
189
+ ShowText = true ;
190
+ ShowPicture = false ;
191
+ ShowRaw = false ;
192
+ }
151
193
}
152
194
153
- if ( change . Property == SelectedFormatProperty )
195
+ if ( change . Property == UseBase64PreDecodingProperty || change . Property == SelectedFormatProperty || change . Property == BufferProperty )
154
196
{
155
- ShowRaw = ReferenceEquals ( SelectedFormat ? . Name , "RAW" ) ;
197
+ Sync ( ) ;
156
198
}
157
199
158
200
if ( change . Property == SelectedFormatNameProperty )
@@ -163,10 +205,10 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
163
205
164
206
void OnCopyToClipboard ( object ? sender , RoutedEventArgs e )
165
207
{
166
- if ( ! string . IsNullOrEmpty ( _content ) )
208
+ if ( ! string . IsNullOrEmpty ( _textEditor ! . Text ) )
167
209
{
168
210
var clipboard = TopLevel . GetTopLevel ( this ) ? . Clipboard ;
169
- _ = clipboard ? . SetTextAsync ( _content ) ;
211
+ _ = clipboard ? . SetTextAsync ( _textEditor . Text ) ;
170
212
}
171
213
}
172
214
@@ -210,33 +252,6 @@ void OnSaveToFile(object? sender, RoutedEventArgs e)
210
252
} ) ;
211
253
}
212
254
213
- void ReadBuffer ( )
214
- {
215
- var format = SelectedFormat ;
216
- if ( format == null )
217
- {
218
- throw new InvalidOperationException ( ) ;
219
- }
220
-
221
- if ( ( Buffer ? . Length ?? 0 ) == 0 )
222
- {
223
- _content = string . Empty ;
224
- }
225
- else
226
- {
227
- try
228
- {
229
- _content = format . Convert ( Buffer ! ) ;
230
- }
231
- catch ( Exception exception )
232
- {
233
- _content = $ "<{ exception . Message } >";
234
- }
235
- }
236
-
237
- SyncTextEditor ( ) ;
238
- }
239
-
240
255
void SelectFormat ( )
241
256
{
242
257
if ( string . IsNullOrEmpty ( SelectedFormatName ) )
@@ -254,9 +269,9 @@ void SelectFormat()
254
269
}
255
270
}
256
271
257
- void SyncTextEditor ( )
272
+ void Sync ( )
258
273
{
259
- if ( _textEditor == null || _hexBox == null )
274
+ if ( _textEditor == null || _hexBox == null || _pictureBox == null )
260
275
{
261
276
return ;
262
277
}
@@ -266,16 +281,61 @@ void SyncTextEditor()
266
281
return ;
267
282
}
268
283
269
- _textMateInstallation ? . SetGrammar ( SelectedFormat . Grammar ) ;
270
-
271
- // It is important to set the content after the grammar so that
272
- // the highlighting gets applied properly!
273
- _textEditor . Text = _content ;
284
+ var buffer = Buffer ?? Array . Empty < byte > ( ) ;
285
+ if ( UseBase64PreDecoding )
286
+ {
287
+ try
288
+ {
289
+ buffer = Convert . FromBase64String ( Encoding . ASCII . GetString ( buffer ) ) ;
290
+ }
291
+ catch
292
+ {
293
+ // Go ahead if decoding did not work. It is probably not required.
294
+ }
295
+ }
274
296
275
297
if ( SelectedFormat . Name == "RAW" )
276
298
{
277
299
// Only fill the data of the hex box when it is actually used!
278
- _hexBox . Value = Buffer ;
300
+ _hexBox . Value = buffer ;
301
+ }
302
+ else if ( SelectedFormat . Name == "Picture" )
303
+ {
304
+ try
305
+ {
306
+ if ( _pictureBox . Child is Image existingImage )
307
+ {
308
+ ( ( Bitmap ) existingImage . Source ! ) . Dispose ( ) ;
309
+ }
310
+
311
+ _pictureBox . Child = new Image
312
+ {
313
+ Source = new Bitmap ( new MemoryStream ( buffer ) )
314
+ } ;
315
+ }
316
+ catch
317
+ {
318
+ // Ignore. We may can set a picture here indicating that the format is not supported.
319
+ _pictureBox . Child = null ;
320
+ }
321
+ }
322
+ else
323
+ {
324
+ string text ;
325
+ try
326
+ {
327
+ text = SelectedFormat . Convert ( buffer ) ;
328
+ }
329
+ catch ( Exception exception )
330
+ {
331
+ text = exception . ToString ( ) ;
332
+ }
333
+
334
+ _textMateInstallation ? . SetGrammar ( SelectedFormat . Grammar ) ;
335
+
336
+ // It is important to set the content after the grammar so that
337
+ // the highlighting gets applied properly!
338
+ _textEditor . Text = text ;
279
339
}
280
340
}
281
341
}
0 commit comments