-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhrases.cs
More file actions
282 lines (246 loc) · 9.36 KB
/
Copy pathPhrases.cs
File metadata and controls
282 lines (246 loc) · 9.36 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// The phrase panel: a few short hidden strings kept around for repeated typing
// through a workday - rotated passwords being the motivating case. Each row is
// a label the user can word however they like, a masked single-line field, and
// a "Type after delay" button that runs the same countdown-and-send path as
// the main box.
//
// Phrase contents are held in memory only and are NEVER written to disk. The
// labels and the row count are settings; the secrets are not. The app lives in
// the tray, so "fetch once in the morning, type all day" works without ever
// storing anything - a reboot costs one re-paste.
//
// Unlike the main box, these fields mask natively: UseSystemPasswordChar works
// on a single-line TextBox, so hand-editing a masked phrase is safe here and
// needs none of the shadow-copy machinery the multiline box required.
//
// C# 5 only (in-box csc).
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace RSPaster
{
public class PhraseTypeEventArgs : EventArgs
{
public string Label;
public string Text;
}
// A single-line box that pastes only the first line of the clipboard.
// Password managers append a trailing newline; without this guard it would
// survive into the phrase and be typed as a stray Enter mid-password. The
// dedicated Paste button that used to carry this guard is gone - one
// button per row, and it is the one that types - so the box itself now
// holds the rule for plain Ctrl+V.
public class PhraseBox : TextBox
{
const int WM_PASTE = 0x0302;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_PASTE && !ReadOnly)
{
string t = null;
try
{
if (Clipboard.ContainsText()) t = Clipboard.GetText();
}
catch (System.Runtime.InteropServices.ExternalException) { } // clipboard locked
if (t != null)
{
int cut = t.IndexOfAny(new char[] { '\r', '\n' });
if (cut >= 0) t = t.Substring(0, cut);
SelectedText = t;
}
return;
}
base.WndProc(ref m);
}
}
public class PhraseRow : Panel
{
public TextBox LabelBox;
public PhraseBox SecretBox;
InputHost _labelHost;
InputHost _secretHost;
ThemedButton _type;
public event EventHandler<PhraseTypeEventArgs> TypeRequested;
public PhraseRow(string label)
{
Height = Dpi.S(34);
LabelBox = new TextBox();
LabelBox.BorderStyle = BorderStyle.None;
LabelBox.Text = label;
_labelHost = new InputHost(LabelBox, Dpi.S(6), Dpi.S(4));
SecretBox = new PhraseBox();
SecretBox.BorderStyle = BorderStyle.None;
SecretBox.UseSystemPasswordChar = true;
_secretHost = new InputHost(SecretBox, Dpi.S(6), Dpi.S(4));
// Worded identically to the main form's action button, because two
// buttons labeled Paste and Type left users unsure which one
// actually sends keystrokes.
_type = new ThemedButton();
_type.Text = "Type after delay";
_type.TakesFocus = false; // see ThemedButton.TakesFocus
_type.Click += delegate(object s, EventArgs e) { RaiseType(); };
Controls.Add(_labelHost);
Controls.Add(_secretHost);
Controls.Add(_type);
}
public void RaiseType()
{
if (TypeRequested == null) return;
PhraseTypeEventArgs e = new PhraseTypeEventArgs();
e.Label = LabelBox.Text;
e.Text = SecretBox.Text;
TypeRequested(this, e);
}
public void SetLocked(bool locked)
{
LabelBox.ReadOnly = locked;
SecretBox.ReadOnly = locked;
_type.Enabled = !locked;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
// Setting Height in the constructor raises this before the child
// controls exist.
if (_labelHost == null) return;
int h = Dpi.S(26);
int y = (Height - h) / 2;
int gap = Dpi.S(6);
int btnW = Dpi.S(112);
int labelW = Dpi.S(120);
_labelHost.SetBounds(0, y, labelW, h);
_type.SetBounds(Width - btnW, y, btnW, h);
_secretHost.SetBounds(labelW + gap, y,
Math.Max(Dpi.S(40), Width - labelW - btnW - gap * 2), h);
}
}
public class PhrasePanel : Panel
{
// Six rows is where the panel starts crowding the main text box out of
// a default-height window; past that this is a password manager, which
// is a different tool.
public const int MAX_ROWS = 6;
public const int MIN_ROWS = 1;
readonly List<PhraseRow> _rows = new List<PhraseRow>();
ThemedButton _add;
ThemedButton _remove;
Label _note;
public event EventHandler<PhraseTypeEventArgs> TypeRequested;
public PhrasePanel()
{
Dock = DockStyle.Top;
_add = new ThemedButton();
_add.Text = "Add";
_add.Click += delegate(object s, EventArgs e)
{
if (_rows.Count < MAX_ROWS) { AddRow("Phrase " + (_rows.Count + 1)); Relayout(); }
};
_remove = new ThemedButton();
_remove.Text = "Remove";
_remove.Click += delegate(object s, EventArgs e)
{
if (_rows.Count > MIN_ROWS) { RemoveLastRow(); Relayout(); }
};
_note = new Label();
_note.AutoSize = false;
_note.TextAlign = ContentAlignment.MiddleRight;
_note.Text = "Phrases are kept in memory only - never saved to disk.";
Controls.Add(_add);
Controls.Add(_remove);
Controls.Add(_note);
Th.Changed += OnThemeChanged;
OnThemeChanged(null, EventArgs.Empty);
}
void OnThemeChanged(object sender, EventArgs e)
{
BackColor = Th.T.Panel2;
_note.BackColor = Th.T.Panel2;
_note.ForeColor = Th.T.TxtDim;
Invalidate();
}
protected override void Dispose(bool disposing)
{
if (disposing) Th.Changed -= OnThemeChanged;
base.Dispose(disposing);
}
public int RowCount { get { return _rows.Count; } }
public List<string> Labels
{
get
{
List<string> l = new List<string>();
foreach (PhraseRow r in _rows) l.Add(r.LabelBox.Text);
return l;
}
}
public PhraseRow Row(int i) { return _rows[i]; }
public void Build(List<string> labels)
{
while (_rows.Count > 0) RemoveLastRow();
foreach (string l in labels)
{
if (_rows.Count >= MAX_ROWS) break;
AddRow(l);
}
if (_rows.Count == 0) AddRow("Phrase 1");
Relayout();
}
void AddRow(string label)
{
PhraseRow row = new PhraseRow(label);
row.TypeRequested += delegate(object s, PhraseTypeEventArgs e)
{
if (TypeRequested != null) TypeRequested(s, e);
};
_rows.Add(row);
Controls.Add(row);
}
void RemoveLastRow()
{
PhraseRow row = _rows[_rows.Count - 1];
_rows.RemoveAt(_rows.Count - 1);
Controls.Remove(row);
row.Dispose(); // its InputHosts unhook their theme handlers here
}
public void SetLocked(bool locked)
{
foreach (PhraseRow r in _rows) r.SetLocked(locked);
_add.Enabled = !locked && _rows.Count < MAX_ROWS;
_remove.Enabled = !locked && _rows.Count > MIN_ROWS;
}
// Manual stacking rather than per-row Dock=Top, whose top-to-bottom
// order depends on z-order and reads exactly backwards in code.
void Relayout()
{
int pad = Dpi.S(8);
int rowH = Dpi.S(34);
int y = pad;
foreach (PhraseRow r in _rows)
{
r.SetBounds(pad, y, Math.Max(Dpi.S(100), Width - pad * 2), rowH);
y += rowH;
}
int btnH = Dpi.S(26);
_add.SetBounds(pad, y + Dpi.S(4), Dpi.S(60), btnH);
_remove.SetBounds(pad + Dpi.S(66), y + Dpi.S(4), Dpi.S(76), btnH);
_note.SetBounds(pad + Dpi.S(150), y + Dpi.S(4),
Math.Max(Dpi.S(80), Width - pad * 2 - Dpi.S(150)), btnH);
Height = y + btnH + Dpi.S(12);
_add.Enabled = _rows.Count < MAX_ROWS;
_remove.Enabled = _rows.Count > MIN_ROWS;
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Relayout();
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.Clear(Th.T.Panel2);
using (Pen p = new Pen(Th.T.Border))
e.Graphics.DrawLine(p, 0, Height - 1, Width, Height - 1);
}
}
}