-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathApp.cs
More file actions
324 lines (306 loc) · 10.3 KB
/
App.cs
File metadata and controls
324 lines (306 loc) · 10.3 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using Microsoft.UI.Reactor;
using Microsoft.UI.Reactor.Core;
using static Microsoft.UI.Reactor.Factories;
using Microsoft.UI.Xaml;
ReactorApp.Run<LayoutApp>("Layout Demo", width: 700, height: 650
#if DEBUG
, preview: true
#endif
);
// <snippet:vstack-hstack>
class StackDemo : Component
{
public override Element Render()
{
return VStack(16,
SubHeading("VStack and HStack"),
VStack(4,
TextBlock("VStack: items top to bottom"),
TextBlock("Item A"), TextBlock("Item B"), TextBlock("Item C")
),
HStack(8,
TextBlock("HStack:"),
Button("One"), Button("Two"), Button("Three")
)
);
}
}
// </snippet:vstack-hstack>
// <snippet:grid-layout>
class GridDemo : Component
{
public override Element Render()
{
return VStack(8,
SubHeading("Grid"),
Grid(
columns: [GridSize.Px(120), GridSize.Star(), GridSize.Auto],
rows: [GridSize.Auto, GridSize.Auto],
TextBlock("Label").Bold().Grid(row: 0, column: 0),
TextBox("", _ => { }, placeholder: "Input...")
.Grid(row: 0, column: 1),
Button("Go").Grid(row: 0, column: 2),
TextBlock("Status").Grid(row: 1, column: 0),
TextBlock("Ready").Foreground("#0078D4")
.Grid(row: 1, column: 1, columnSpan: 2)
).Height(80)
);
}
}
// </snippet:grid-layout>
// <snippet:card>
class CardDemo : Component
{
public override Element Render()
{
return VStack(12,
SubHeading("Card"),
Card(
VStack(8,
TextBlock("Recent activity").SemiBold(),
TextBlock("3 new messages, 2 mentions")
.Foreground(Theme.SecondaryText)
)
).Width(240)
);
}
}
// </snippet:card>
// <snippet:type-ramp>
class TypeRampDemo : Component
{
public override Element Render()
{
return VStack(8,
Title("Quarterly results"),
Subtitle("Q3 2026 highlights"),
BodyLarge("Revenue grew 18% year over year."),
BodyStrong("Net income reached an all-time high."),
Body("Full breakdown on the following pages.")
).Padding(24);
}
}
// </snippet:type-ramp>
// <snippet:scrollview-border>
class ScrollBorderDemo : Component
{
public override Element Render()
{
return VStack(8,
SubHeading("ScrollView and Border"),
Border(
ScrollView(
VStack(4,
ForEach(
Enumerable.Range(1, 20),
i => TextBlock($"Scrollable item {i}"))
).Padding(8)
).Height(120)
).CornerRadius(4).Background("#F5F5F5")
);
}
}
// </snippet:scrollview-border>
// <snippet:expander-canvas>
class ExpanderCanvasDemo : Component
{
public override Element Render()
{
return VStack(12,
SubHeading("Expander"),
Expander("Click to expand", VStack(8,
TextBlock("Hidden content revealed!"),
TextBlock("Expanders are great for optional details.")
)),
SubHeading("Canvas"),
Border(
Canvas(
TextBlock("Top-left").Set(c => {
Microsoft.UI.Xaml.Controls.Canvas.SetLeft((UIElement)c, 10);
Microsoft.UI.Xaml.Controls.Canvas.SetTop((UIElement)c, 10);
}),
TextBlock("Center").Set(c => {
Microsoft.UI.Xaml.Controls.Canvas.SetLeft((UIElement)c, 120);
Microsoft.UI.Xaml.Controls.Canvas.SetTop((UIElement)c, 40);
})
).Height(90).Width(300)
).Background("#F5F5F5").CornerRadius(4)
);
}
}
// </snippet:expander-canvas>
// <snippet:responsive>
class ResponsiveDemo : Component
{
public override Element Render()
{
var (wide, setWide) = UseState(true);
var content = new Element[]
{
Border(TextBlock("Panel A").Padding(16))
.Background("#E3F2FD").CornerRadius(4),
Border(TextBlock("Panel B").Padding(16))
.Background("#FFF3E0").CornerRadius(4),
Border(TextBlock("Panel C").Padding(16))
.Background("#E8F5E9").CornerRadius(4),
};
return VStack(8,
SubHeading("Responsive Layout"),
HStack(8,
TextBlock("Simulate wide screen:"),
ToggleSwitch(wide, setWide)
),
If(wide,
() => HStack(12, content),
() => VStack(8, content))
);
}
}
// </snippet:responsive>
// <snippet:app-shell>
// App shell scaffold: title bar + sidebar + content using a single Grid
// declaration. The two-column / three-row layout is the canonical
// shape for an admin-style app and replaces hand-rolled nested
// HStacks/VStacks.
class AppShellExample : Component
{
public override Element Render()
{
return Grid(
columns: [GridSize.Px(220), GridSize.Star()],
rows: [GridSize.Px(44), GridSize.Star(), GridSize.Px(28)],
// Top bar — spans both columns.
Border(TextBlock("My App").Bold().Padding(12))
.Background(Theme.LayerFill)
.Grid(row: 0, column: 0, columnSpan: 2),
// Sidebar.
Border(VStack(4,
TextBlock("Dashboard"),
TextBlock("Reports"),
TextBlock("Settings"))
.Padding(12))
.Background(Theme.CardBackground)
.Grid(row: 1, column: 0),
// Main content.
ScrollView(VStack(8,
Title("Welcome"),
Body("Stack -> Grid for the shell, VStack inside the panes.")))
.Padding(16)
.Grid(row: 1, column: 1),
// Status bar.
TextBlock("Ready").Padding(6)
.Grid(row: 2, column: 0, columnSpan: 2)
).Width(560).Height(360);
}
}
// </snippet:app-shell>
// <snippet:auto-grid>
// Auto-grid: WrapGrid wraps a sequence of items into columns once a
// row fills, no manual row/column placement. Picks "max 4 per row".
class AutoGridExample : Component
{
public override Element Render()
{
return WrapGrid(maxRowsOrColumns: 4,
children: Enumerable.Range(1, 11)
.Select(i =>
Border(TextBlock($"Tile {i}").Padding(12))
.Background(Theme.CardBackground)
.CornerRadius(6)
.Width(110).Height(60))
.Cast<Element?>()
.ToArray()
).Padding(24);
}
}
// </snippet:auto-grid>
// <snippet:responsive-switcher>
// Responsive switcher: the same content renders as HStack at wide
// widths, VStack when the window narrows past 480px. UseBreakpoint is
// the canonical hook for this pattern.
class ResponsiveSwitcherExample : Component
{
public override Element Render()
{
var (simulated, setSimulated) = UseState(true); // toy demo without a window handle
var panels = new Element[]
{
Border(TextBlock("Panel A").Padding(16))
.Background(Theme.CardBackground).CornerRadius(4),
Border(TextBlock("Panel B").Padding(16))
.Background(Theme.CardBackground).CornerRadius(4),
Border(TextBlock("Panel C").Padding(16))
.Background(Theme.CardBackground).CornerRadius(4),
};
return VStack(8,
HStack(8,
TextBlock("Simulate wide window:"),
ToggleSwitch(simulated, setSimulated)),
If(simulated,
() => HStack(12, panels),
() => VStack(8, panels))
).Padding(24);
}
}
// </snippet:responsive-switcher>
// <snippet:dont-deep-stack>
// BAD — deeply nested VStack/HStack hierarchies obscure intent and
// produce a layout cost that Grid would absorb in one measure pass.
class DontDeepStack : Component
{
public override Element Render()
{
return HStack(8,
VStack(4,
TextBlock("Label").Bold(),
TextBlock("Sublabel").Foreground(Theme.SecondaryText)),
VStack(0,
HStack(4, TextBlock("First:"), TextBlock("Value 1")),
HStack(4, TextBlock("Second:"), TextBlock("Value 2")),
HStack(4, TextBlock("Third:"), TextBlock("Value 3")))
).Padding(24);
}
}
// </snippet:dont-deep-stack>
// <snippet:do-grid-for-forms>
// GOOD — same shape as a 2-column Grid. Labels in column 0 are auto-
// sized to their content; column 1 stretches.
class DoGridForForms : Component
{
public override Element Render()
{
return Grid(
columns: [GridSize.Auto, GridSize.Star()],
rows: [GridSize.Auto, GridSize.Auto, GridSize.Auto],
TextBlock("First:").Margin(4).Grid(row: 0, column: 0),
TextBlock("Value 1").Margin(4).Grid(row: 0, column: 1),
TextBlock("Second:").Margin(4).Grid(row: 1, column: 0),
TextBlock("Value 2").Margin(4).Grid(row: 1, column: 1),
TextBlock("Third:").Margin(4).Grid(row: 2, column: 0),
TextBlock("Value 3").Margin(4).Grid(row: 2, column: 1)
).Padding(24).Width(300);
}
}
// </snippet:do-grid-for-forms>
class LayoutApp : Component
{
public override Element Render()
{
return ScrollView(
VStack(24,
Heading("Layout Primitives"),
Component<StackDemo>(),
Component<GridDemo>(),
Component<CardDemo>(),
Component<TypeRampDemo>(),
Component<ScrollBorderDemo>(),
Component<ExpanderCanvasDemo>(),
Component<ResponsiveDemo>(),
Component<AppShellExample>(),
Component<AutoGridExample>(),
Component<ResponsiveSwitcherExample>(),
Component<DoGridForForms>()
).Padding(24)
);
}
}