Repository navigation
Expand file tree
/
Copy pathMainForm.cs
More file actions
231 lines (207 loc) · 7.2 KB
/
Copy pathMainForm.cs
File metadata and controls
231 lines (207 loc) · 7.2 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
namespace Sample.WinFormsClient;
sealed class MainForm : Form
{
readonly ScryQuery query;
readonly ScryPendingWorkStore work;
readonly BindingSource binding = new();
readonly Button refresh;
readonly CheckBox live;
readonly Label status;
readonly TextBox renameTo;
readonly DataGridView grid;
readonly ListBox pending;
ScrySubscription? subscription;
public MainForm(ScryQuery query, ScryPendingWorkStore work)
{
this.query = query;
this.work = work;
Text = "Scry - Windows Forms sample";
Width = 760;
Height = 560;
refresh = new()
{
Text = "Refresh",
Left = 12,
Top = 12,
Width = 100
};
refresh.Click += async (_, _) => await LoadEmployees();
live = new()
{
Text = "Live",
Left = 124,
Top = 15,
Width = 60
};
live.CheckedChanged += (_, _) => OnLiveChanged();
status = new()
{
Left = 190,
Top = 16,
Width = 540,
AutoSize = false
};
renameTo = new()
{
Left = 12,
Top = 48,
Width = 200,
Text = "Renamed"
};
Button rename = new()
{
Text = "Rename selected",
Left = 220,
Top = 46,
Width = 130
};
rename.Click += async (_, _) => await RenameSelected();
grid = new()
{
Left = 12,
Top = 80,
Width = 720,
Height = 330,
ReadOnly = true,
AllowUserToAddRows = false,
MultiSelect = false,
SelectionMode = DataGridViewSelectionMode.FullRowSelect,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right,
DataSource = binding
};
// The key travels with every row, because a command names its row by key; it just is not
// anything a person needs to read.
grid.DataBindingComplete += (_, _) =>
{
if (grid.Columns["Id"] is { } id)
{
id.Visible = false;
}
};
pending = new()
{
Left = 12,
Top = 418,
Width = 720,
Height = 90,
Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right
};
work.Changed += ShowPendingWork;
Controls.Add(refresh);
Controls.Add(live);
Controls.Add(status);
Controls.Add(renameTo);
Controls.Add(rename);
Controls.Add(grid);
Controls.Add(pending);
Shown += async (_, _) => await LoadEmployees();
}
// The shape the form wants, declared here rather than anywhere the server knows about. The
// response comes back keyed by these names.
// ReSharper disable NotAccessedPositionalProperty.Local
record EmployeeRow(int Id, string Name, Status Status, string? Manager, string Department);
// ReSharper restore NotAccessedPositionalProperty.Local
// The same LINQ the WPF and console samples write. It is captured rather than executed, then
// translated to the wire AST, validated against the allow-list on the server, and run there.
async Task LoadEmployees()
{
refresh.Enabled = false;
status.Text = "Loading...";
try
{
var rows = await query
.Employee
.Where(_ => _.Active)
.OrderBy(_ => _.Name)
.Select(_ => new EmployeeRow(_.Id, _.Name, _.Status, _.Manager!.Name, _.Department!.Name))
.ToListAsync();
binding.DataSource = rows;
status.Text = $"{rows.Count} active employees";
}
catch (HttpRequestException exception)
{
binding.DataSource = Array.Empty<EmployeeRow>();
status.Text = $"Cannot reach {Program.ServerAddress}. Start the server with " +
$"'dotnet run --project samples/Sample.WebServer'. ({exception.Message})";
}
finally
{
refresh.Enabled = true;
}
}
// The same query, kept answered. Subscribe is called on the UI thread, so that is where each
// answer is delivered: the callback touches the binding with no Invoke, while the reading and
// deserializing happen off the thread that paints.
// begin-snippet: winFormsLive
void OnLiveChanged()
{
subscription?.Dispose();
subscription = null;
refresh.Enabled = !live.Checked;
if (!live.Checked)
{
return;
}
status.Text = "Connecting...";
subscription = query
.Employee
.Where(_ => _.Active)
.OrderBy(_ => _.Name)
.Select(_ => new EmployeeRow(_.Id, _.Name, _.Status, _.Manager!.Name, _.Department!.Name))
.Live()
.Subscribe(
rows =>
{
binding.DataSource = rows;
status.Text = $"{rows.Count} active employees, live as of {DateTime.Now:T}";
},
exception => status.Text = $"The live query ended: {exception.Message}");
}
// end-snippet
// A command acts on a row by its key, which the grid carries for exactly this. The grid shows the
// new name when the live query's next answer arrives, or on the next Refresh; nothing here edits it.
// begin-snippet: winFormsCommand
async Task RenameSelected()
{
if (grid.CurrentRow?.DataBoundItem is not EmployeeRow row)
{
status.Text = "Select an employee to rename.";
return;
}
try
{
var outcome = await query.Commands.RenameEmployee(new() {Id = row.Id, Name = renameTo.Text});
status.Text = outcome.Status switch
{
ScryCommandStatus.Completed => $"Renamed {row.Name} to {renameTo.Text}.",
ScryCommandStatus.Pending => "Still renaming. It is listed under pending work.",
_ => outcome.Error
};
}
catch (Exception exception) when (exception is HttpRequestException or ScryRequestException or ScryPermissionException)
{
status.Text = exception.Message;
}
}
// The store says it changed on the context the command was sent from — the UI thread, since
// RenameSelected runs there — so the list is redrawn with no Invoke.
void ShowPendingWork() =>
pending.DataSource = work.Items.Select(Describe).ToList();
static string Describe(ScryPendingCommand item)
{
var line = $"{item.Command} {string.Join(", ", item.Keys)}: {item.Status}, {item.Elapsed.TotalSeconds:0.0}s";
if (item.Error is { } error)
{
return $"{line} — {error}";
}
return line;
}
// end-snippet
protected override void OnFormClosed(FormClosedEventArgs args)
{
work.Changed -= ShowPendingWork;
subscription?.Dispose();
base.OnFormClosed(args);
}
}