-
Notifications
You must be signed in to change notification settings - Fork 0
/
PasteBox.cs
229 lines (200 loc) · 6.53 KB
/
PasteBox.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Eto.Forms;
namespace ThothGui
{
class URL
{
public string Text { get; }
public string Href { get; }
public URL(string text, string href)
{
Text = text;
Href = href;
}
public URL(NSoup.Nodes.Element tag)
{
Text = tag.OwnText();
Href = tag.Attr("abs:href");
}
}
class InputDialog : Dialog<string>
{
private TextArea _text;
private Button _acceptButton;
public InputDialog()
{
_text = new TextArea();
_acceptButton = new Button { Text = "Ok" };
_acceptButton.Click += _acceptButton_Click;
//Size = new Eto.Drawing.Size(300, 100);
Width = 400;
Result = "";
Content = new TableLayout
{
Rows =
{
new Label { Text = "Manually Input URLS" },
_text,
_acceptButton
}
};
}
private void _acceptButton_Click(object sender, EventArgs e)
{
this.Close(_text.Text);
}
public void Reset()
{
Result = "";
_text.Text = "";
}
}
class PasteBox : TableLayout
{
private List<URL> _urls;
private ListBox _box;
private Button _upButton;
private Button _downButton;
private Button _deleteButton;
private Button _manualButton;
private Button _clearButton;
private Button _reverseButton;
private TableLayout _buttonLayout;
private InputDialog _manualInputDialog;
public PasteBox()
{
_box = new ListBox();
_upButton = new Button { Text = "Move Up" };
_downButton = new Button { Text = "Move Down" };
_deleteButton = new Button { Text = "Delete" };
_manualButton = new Button { Text = "Manually Input URLs" };
_clearButton = new Button { Text = "Clear" };
_reverseButton = new Button { Text = "Reverse Links" };
_buttonLayout = new TableLayout { Rows = { _upButton, _downButton, _deleteButton, _clearButton, _reverseButton, _manualButton, null} };
_manualInputDialog = new InputDialog();
_box.BindDataContext((c) => c.DataStore, (List<URL> m) => m);
_urls = new List<URL>();
_box.DataStore = new List<URL> { new URL("Paste URLs here", "") };
Rows.Add(new TableRow(
new TableCell(_box, true),
new TableCell(_buttonLayout, false)));
Size = new Eto.Drawing.Size(300, 300);
BindHandlers();
}
public IEnumerable<URL> GetUrls()
{
return _urls;
}
private void BindHandlers()
{
_box.KeyDown += DeleteKeyHandler;
_upButton.Click += (object o, EventArgs e) =>
{
var selected = _box.SelectedValue;
if (selected != null && selected is URL)
{
var selectedURL = (URL)selected;
MoveURLUp(selectedURL);
}
};
_downButton.Click += (object o, EventArgs e) =>
{
var selected = _box.SelectedValue;
if (selected != null && selected is URL)
{
var selectedURL = (URL)selected;
MoveURLDown(selectedURL);
}
};
_deleteButton.Click += (object o, EventArgs e) =>
{
var selected = _box.SelectedValue;
if (selected != null && selected is URL)
{
var selectedURL = (URL)selected;
DeleteURL(selectedURL);
}
};
_clearButton.Click += (object o, EventArgs e) =>
{
_urls.Clear();
updateDisplay();
};
_reverseButton.Click += (object o, EventArgs e) =>
{
_urls.Reverse();
updateDisplay();
};
_manualButton.Click += (object o, EventArgs e) =>
{
_manualInputDialog.Reset();
_manualInputDialog.ShowModal();
};
_manualInputDialog.Closed += _manualInputDialog_Closed;
}
private void _manualInputDialog_Closed(object sender, EventArgs e)
{
var text = _manualInputDialog.Result;
if (text != "")
{
var lines = text.Split(new string[] { "\n" }, StringSplitOptions.None);
var urls = lines.Select(x => new URL(x, x));
_urls.AddRange(urls);
updateDisplay();
}
}
private void DeleteKeyHandler(object sender, KeyEventArgs e)
{
if(e.Key == Keys.Delete)
{
var selected = _box.SelectedValue;
if(selected != null && selected is URL)
{
var selectedURL = (URL)selected;
DeleteURL(selectedURL);
}
}
}
private void DeleteURL(URL url)
{
_urls.Remove(url);
updateDisplay();
}
private void MoveURLUp(URL url)
{
var index = _urls.FindIndex((x => x == url));
_urls.Remove(url);
var newIndex = (index - 1 >= 0) ? index - 1 : 0;
_urls.Insert(newIndex, url);
updateDisplay();
_box.SelectedIndex = newIndex;
}
private void MoveURLDown(URL url)
{
var index = _urls.FindIndex((x => x == url));
_urls.Remove(url);
var newIndex = (index + 1 <= _urls.Count) ? index + 1 : _urls.Count;
_urls.Insert(newIndex, url);
updateDisplay();
_box.SelectedIndex = newIndex;
}
private void updateDisplay()
{
_box.DataStore = _urls;
}
public void ExtractPasteData(string data)
{
var soup = NSoup.NSoupClient.Parse(data);
var links = soup.Body.Select("a");
foreach(var link in links)
{
_urls.Add(new URL(link));
updateDisplay();
}
}
}
}