-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathApp.cs
More file actions
288 lines (258 loc) · 8.65 KB
/
App.cs
File metadata and controls
288 lines (258 loc) · 8.65 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
283
284
285
286
287
288
using Microsoft.UI.Reactor;
using Microsoft.UI.Reactor.Core;
using static Microsoft.UI.Reactor.Factories;
using Microsoft.UI.Xaml;
using Windows.System;
ReactorApp.Run<CommandingApp>("Commanding", width: 650, height: 550
#if DEBUG
, preview: true
#endif
);
// <snippet:basic-command>
class BasicCommandExample : Component
{
public override Element Render()
{
var (text, setText) = UseState("Hello, World!");
var (saved, setSaved) = UseState(false);
var saveCmd = new Command
{
Label = "Save",
Execute = () => setSaved(true),
CanExecute = !saved,
Icon = SymbolIcon("Save"),
Accelerator = Accelerator(VirtualKey.S, VirtualKeyModifiers.Control)
};
return VStack(12,
TextBox(text, v => { setText(v); setSaved(false); })
.Width(400),
HStack(8,
Button(saveCmd),
When(saved, () => TextBlock("Saved!").Foreground(Theme.SystemSuccess))
)
).Padding(24);
}
}
// </snippet:basic-command>
// <snippet:standard-commands>
class StandardCommandsExample : Component
{
public override Element Render()
{
var (log, updateLog) = UseReducer(new List<string>());
var cut = StandardCommand.Cut(() => updateLog(l => [.. l, "Cut"]));
var copy = StandardCommand.Copy(() => updateLog(l => [.. l, "Copy"]));
var paste = StandardCommand.Paste(() => updateLog(l => [.. l, "Paste"]));
var undo = StandardCommand.Undo(
() => updateLog(l => [.. l, "Undo"]),
canExecute: log.Count > 0);
return VStack(12,
CommandBar(
primaryCommands: new[] { AppBarButton(cut), AppBarButton(copy),
AppBarButton(paste), AppBarButton(undo) }
),
TextBlock($"Actions: {string.Join(", ", log)}").Padding(12)
).Padding(24);
}
}
// </snippet:standard-commands>
// <snippet:async-command>
class AsyncCommandExample : Component
{
public override Element Render()
{
var (status, setStatus) = UseState("Ready");
var saveCmd = UseCommand(new Command
{
Label = "Save to Cloud",
ExecuteAsync = async () =>
{
setStatus("Saving...");
await Task.Delay(2000);
setStatus("Saved at " + DateTime.Now.ToString("HH:mm:ss"));
},
Icon = SymbolIcon("Save")
});
return VStack(12,
HStack(8,
Button(saveCmd),
TextBlock(status).Foreground(Theme.SecondaryText)
),
When(saveCmd.IsExecuting, () =>
ProgressRing().Width(20).Height(20))
).Padding(24);
}
}
// </snippet:async-command>
// <snippet:command-bar>
class CommandBarExample : Component
{
public override Element Render()
{
var (text, setText) = UseState("Edit me");
var save = StandardCommand.Save(() => { });
var copy = StandardCommand.Copy(() => { });
var delete = StandardCommand.Delete(
() => setText(""), canExecute: text.Length > 0);
return VStack(0,
CommandBar(
primaryCommands: new[] {
AppBarButton(save), AppBarButton(copy) },
secondaryCommands: new[] {
AppBarButton(delete) }
),
TextBox(text, setText).Margin(16)
);
}
}
// </snippet:command-bar>
// <snippet:menu-bar>
class MenuBarExample : Component
{
public override Element Render()
{
var (text, setText) = UseState("Document text");
var save = StandardCommand.Save(() => { });
var close = StandardCommand.Close(() => setText(""));
var undo = StandardCommand.Undo(() => { });
var redo = StandardCommand.Redo(() => { });
return VStack(0,
MenuBar(
Menu("File", MenuItem(save), MenuItem(close)),
Menu("Edit", MenuItem(undo), MenuItem(redo))
),
TextBlock(text).Padding(16)
);
}
}
// </snippet:menu-bar>
// <snippet:button-and-menu>
class ButtonAndMenuExample : Component
{
public override Element Render()
{
var (saves, setSaves) = UseState(0);
// One Command. Two surfaces. Identical enabled-state, label, icon, accelerator.
var save = new Command
{
Label = "Save",
Icon = SymbolIcon("Save"),
Accelerator = Accelerator(VirtualKey.S, VirtualKeyModifiers.Control),
Execute = () => setSaves(saves + 1),
CanExecute = saves < 3,
};
return VStack(12,
// Button surface.
Button(save),
// MenuFlyout surface — same Command record.
MenuFlyout(
Button("File…"),
MenuItem(save)),
TextBlock($"Saved {saves} time(s); CanExecute={save.CanExecute}")
.Foreground(Theme.SecondaryText)
).Padding(24);
}
}
// </snippet:button-and-menu>
// <snippet:parameterized-command>
record TodoItem(int Id, string Title);
class ParameterizedCommandExample : Component
{
public override Element Render()
{
var (items, setItems) = UseState<IReadOnlyList<TodoItem>>(
new[] { new TodoItem(1, "Buy milk"), new TodoItem(2, "Walk dog"), new TodoItem(3, "Ship doc") });
// One Command<TodoItem> drives every row.
var delete = new Command<TodoItem>
{
Label = "Delete",
Icon = SymbolIcon("Delete"),
Execute = item => setItems(items.Where(i => i.Id != item.Id).ToList()),
};
return VStack(8,
ForEach(items, item =>
HStack(8,
TextBlock(item.Title).Width(180),
// Inline button — Command<T> doesn't have a Button(cmd, arg) overload
// by design, so call .Execute(arg) directly from the click handler.
Button(delete.Label, () => delete.Execute?.Invoke(item))
.IsEnabled(delete.IsEnabled)))
).Padding(24);
}
}
// </snippet:parameterized-command>
// <snippet:async-with-progress>
class AsyncWithProgressExample : Component
{
public override Element Render()
{
var (progress, setProgress) = UseState(0.0);
var upload = UseCommand(new Command
{
Label = "Upload",
Icon = SymbolIcon("Upload"),
ExecuteAsync = async () =>
{
for (var i = 0; i <= 100; i += 10)
{
setProgress(i / 100.0);
await Task.Delay(120);
}
},
});
return VStack(12,
HStack(8,
Button(upload),
When(upload.IsExecuting, () =>
TextBlock($"{(int)(progress * 100)}%")
.Foreground(Theme.SecondaryText))
),
When(upload.IsExecuting, () =>
Progress(progress * 100).Width(300))
).Padding(24);
}
}
// </snippet:async-with-progress>
// <snippet:dont-create-in-render>
// Don't: re-create the Command on every render — every surface that
// holds the previous reference sees a fresh identity each frame, which
// thrashes the WinUI keyboard-accelerator wiring and re-renders every
// consumer. Lift to a memo or hoist out of Render().
class DontCreateInRender : Component
{
public override Element Render()
{
// BAD — Command identity churns every render:
// var save = new Command { Label = "Save", Execute = () => { } };
// GOOD — UseMemo pins identity until deps change:
var (count, setCount) = UseState(0);
var save = UseMemo(() => new Command
{
Label = "Save",
Execute = () => setCount(count + 1),
}, count);
return VStack(8, Button(save), TextBlock($"Saved {count}")).Padding(24);
}
}
// </snippet:dont-create-in-render>
// Main app
class CommandingApp : Component
{
public override Element Render()
{
return ScrollView(
VStack(24,
Heading("Commanding"),
Component<BasicCommandExample>(),
Component<StandardCommandsExample>(),
Component<AsyncCommandExample>(),
Component<CommandBarExample>(),
Component<MenuBarExample>(),
Component<ButtonAndMenuExample>(),
Component<ParameterizedCommandExample>(),
Component<AsyncWithProgressExample>(),
Component<DontCreateInRender>()
).Padding(24)
);
}
}