-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rot13Encoder.cs
112 lines (95 loc) · 3.68 KB
/
Rot13Encoder.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
using Rot13.ClipboardUtils;
using System;
using System.Windows;
using System.Windows.Input;
namespace Rot13
{
/// <summary>
/// The ViewModel for rot13.exe. Exposes a Text property which may or may not be encoded and
/// Encode and PasteRot13 commands for interaction with the UI.
/// </summary>
public class Rot13Encoder : NotifiableObject, IDisposable
{
// Adapter between view and viewmodel for the Encode button
public ICommand Encode => new CommandHook(_ => DoEncode(), _ => CanEncode(), this);
// Adapter between view and viewmodel for the PasteRot13 button
public ICommand PasteRot13 => new CommandHook(_ => PasteAndEncode(), _ => CanPaste());
// Adapter between view and viewmodel for the CopyRot13 button
public ICommand CopyRot13 => new CommandHook(_ => EncodeAndCopy(), _ => CanEncode(), this);
// too much boilerplate!! The Text property, which is as close as this gets
// to the model in the MVVM pattern
private string text;
public string Text
{
get { return text; }
set
{
text = value;
OnPropertyChanged();
}
}
// Watches for clipboard changes
private IClipboardMonitor clippy;
// Constructor
public Rot13Encoder()
{
// Initialize clipboard monitoring
clippy = ClipboardMonitorFactory.Create();
clippy.ClipboardHasData += ClipboardHasData;
// start up by en/decoding the clipboard.
PasteAndEncode();
}
// *******************************************************************
// Do it - all of this code to call a single extension method. Tragic.
// *******************************************************************
private void DoEncode()
=> this.Text = this.Text.Rot13();
// Is there anything to encode?
private bool CanEncode()
=> !string.IsNullOrWhiteSpace(Text);
// When the clipboard has new data, update the command status
private void ClipboardHasData(object sender, EventArgs e)
=> (PasteRot13 as CommandHook).SuggestCanExecuteChanged();
// If there is usable data in the clipboard, encode it and show it
private void PasteAndEncode()
{
if (Clipboard.ContainsText())
{
this.Text = Clipboard.GetText().Rot13();
}
}
// Enable the paste encoded button, if there's anything to paste
private static bool CanPaste()
=> Clipboard.ContainsText();
// Encode the text right into the clipboard
private void EncodeAndCopy()
=> Clipboard.SetText(this.Text.Rot13());
#region IDisposable Support
private bool isDisposed = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (disposing)
{
clippy.Dispose();
}
isDisposed = true;
}
}
~Rot13Encoder()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Implemented to keep code analysis happy
Dispose(false);
}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
}