Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ Conventions for contributors:

### Added

- **ReactorGallery catches up with recent framework features.** A new **System →
File pickers** page demonstrates `PickFileAsync`, `PickMultipleFilesAsync`
(with labelled `FileTypeChoices`), `PickFolderAsync` / `PickMultipleFoldersAsync`
and `PickSaveFileAsync`; a **Patterns → ErrorBoundary** page covers catch-and-retry,
static fallbacks, and the built-in fallback a `RenderErrorHandler` replaces; the
Grid page adds `GridSize.MinSize` / `MaxSize`; and the TitleBar page shows icon
inheritance and `.NoIcon()`. The gallery now declares its window icon once via
`ReactorApp.Run(icon:)` and its shell title bar inherits it.

- **Component inspection for devtools: `ReactorDiagnostics.DescribeComponent`,
`TrySetState`, `Rerender`, `GetAppliedProperties`, `GetReferenceEdges`.** An
inspector can go from a realized control to the component behind it: its name,
Expand Down
29 changes: 29 additions & 0 deletions samples/ReactorGallery/ControlPages/Layout/GridPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class GridPage : Component
{
public override Element Render()
{
var (gridWidth, setGridWidth) = UseState(420.0);

return ScrollView(
VStack(16,
PageHeader("Grid", "Arranges children in rows and columns with star/pixel/auto sizing."),
Expand Down Expand Up @@ -68,6 +70,33 @@ public override Element Render()
rows: [GridSize.Auto]
// 100px fixed, 1 star, 2 star proportional
)
"""),

SampleCard("Min & Max Track Sizes",
VStack(8,
Grid(
columns: [GridSize.Star().MinSize(120), GridSize.Star(), GridSize.Star().MaxSize(100)],
rows: [GridSize.Auto],
Border(TextBlock("1*, min 120").Center().Foreground("#FFFFFF").Padding(8)).Background("#C0392B").Grid(row: 0, column: 0),
Border(TextBlock("1*").Center().Foreground("#FFFFFF").Padding(8)).Background("#117864").Grid(row: 0, column: 1),
Border(TextBlock("1*, max 100").Center().Foreground("#FFFFFF").Padding(8)).Background("#2471A3").Grid(row: 0, column: 2)
).Width(gridWidth),
TextBlock($"Grid width: {gridWidth:0}px").Foreground(Theme.SecondaryText),
Slider(gridWidth, 200, 700, setGridWidth).Width(300).AutomationName("Grid width")
),
"""
var (gridWidth, setGridWidth) = UseState(420.0);

VStack(8,
Grid(
// Clamp a track's computed size with MinSize / MaxSize.
columns: [GridSize.Star().MinSize(120), GridSize.Star(), GridSize.Star().MaxSize(100)],
rows: [GridSize.Auto],
Border(TextBlock("1*, min 120")).Grid(row: 0, column: 0),
Border(TextBlock("1*")).Grid(row: 0, column: 1),
Border(TextBlock("1*, max 100")).Grid(row: 0, column: 2)
).Width(gridWidth),
Slider(gridWidth, 200, 700, setGridWidth).AutomationName("Grid width"))
""")
).Margin(36, 24, 36, 36)
);
Expand Down
22 changes: 22 additions & 0 deletions samples/ReactorGallery/ControlPages/Navigation/TitleBarPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ public override Element Render()
).Background(Theme.LayerFill).CornerRadius(4).Height(48),
@"TitleBar(""My Application"")"),

SampleCard("Icon: inherited, custom, or none",
VStack(8,
Border(
TitleBar("Inherits the window icon")
).Background(Theme.LayerFill).CornerRadius(4).Height(48),
Border(
TitleBar("Custom icon").Icon(new FontIconData("\uE790"))
).Background(Theme.LayerFill).CornerRadius(4).Height(48),
Border(
TitleBar("No icon").NoIcon()
).Background(Theme.LayerFill).CornerRadius(4).Height(48),
Caption("With no .Icon(...), a TitleBar shows the window's own icon: the one passed to ReactorApp.Run(icon: ...) or WindowSpec.Icon, else Assets\\AppIcon.ico. This gallery declares its icon once in Program.cs.")
.Foreground(Theme.SecondaryText).TextWrapping()
),
@"// Program.cs: the main window's icon drives its caption, Alt-Tab, and every TitleBar in that window.
// A secondary window inherits from its own WindowSpec.Icon instead.
ReactorApp.Run<Shell>(""My App"", icon: WindowIcon.FromPath(""Assets/AppIcon.ico""));

TitleBar(""Inherits the window icon"")
TitleBar(""Custom icon"").Icon(new FontIconData(""\uE790""))
TitleBar(""No icon"").NoIcon()"),

SampleCard("TitleBar with Subtitle",
VStack(8,
Border(
Expand Down
140 changes: 140 additions & 0 deletions samples/ReactorGallery/ControlPages/Patterns/ErrorBoundaryPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using Microsoft.UI.Reactor;
using Microsoft.UI.Reactor.Core;
using Microsoft.UI.Xaml;
using static Microsoft.UI.Reactor.Factories;
using static WinUIGalleryReactor.SamplePageHost;

namespace WinUIGalleryReactor.ControlPages.Patterns;

class ErrorBoundaryPage : Component
{
// Each demo is its own component, so clicking in one card re-renders only that card.
// An ErrorBoundary retries its child on every re-render, so a page-level click would
// otherwise recover every crashed card at once.
public override Element Render() =>
ScrollView(VStack(16,
PageHeader("ErrorBoundary",
"Catches an exception thrown while a subtree renders and shows a fallback in its place, so one broken component cannot take the page down with it."),

SampleCard("Catch, explain, and retry",
Component<RetryDemo>(),
sourceCode: @"
var (attempt, setAttempt) = UseState(0);

ErrorBoundary(
// Re-rendering the boundary retries its child. The new key also guarantees
// a fresh instance, so no state from the failed attempt survives.
Component<CrashingCounter>().WithKey($""attempt-{attempt}""),
ex => VStack(8,
TextBlock(""Something went wrong"").Bold().Foreground(Theme.SystemCritical),
TextBlock(ex.Message).Foreground(Theme.SecondaryText),
Button(""Retry"", () => setAttempt(attempt + 1))
).Padding(12).Background(Theme.SystemCriticalBackground).CornerRadius(8))

class CrashingCounter : Component
{
public override Element Render()
{
var (count, setCount) = UseState(0);
if (count >= 3)
throw new InvalidOperationException($""The counter refuses to go past {count - 1}."");
return Button($""Clicked {count} times"", () => setCount(count + 1));
}
}
"),

SampleCard("A static fallback",
Component<StaticFallbackDemo>(),
sourceCode: @"
var (attempt, setAttempt) = UseState(0);

VStack(8,
ErrorBoundary(
Component<CrashingCounter>().WithKey($""attempt-{attempt}""),
TextBlock(""This widget is unavailable right now."")),
Button(""Reset"", () => setAttempt(attempt + 1)))
"),

SampleCard("Without a boundary: RenderErrorHandler",
Component<UnguardedDemo>(),
sourceCode: @"
// Program.cs: replace the built-in fallback for every host, before ReactorApp.Run.
ReactorApp.DefaultRenderErrorHandler = error =>
{
System.Diagnostics.Debug.WriteLine(error.Exception);

// IsHostLevel: true replaces the whole window content,
// false fills only the failing component's slot.
return error.IsHostLevel
? TextBlock(""Something went wrong. Please restart the app."")
: TextBlock($""{error.ComponentName} could not be shown."");
};

// Exceptions an ErrorBoundary catches stay with that boundary and never reach the handler.
var (attempt, setAttempt) = UseState(0);

VStack(8,
// No boundary: the counter stays mounted with its crashing state, and every
// re-render retries it with that state, so a new key (a fresh instance) recovers it.
Component<CrashingCounter>().WithKey($""attempt-{attempt}""),
Button(""Reset"", () => setAttempt(attempt + 1)))
")
).Margin(36, 24, 36, 36));
}

class RetryDemo : Component
{
public override Element Render()
{
var (attempt, setAttempt) = UseState(0);
return ErrorBoundary(
Component<CrashingCounter>().WithKey($"attempt-{attempt}"),
ex => VStack(8,
TextBlock("Something went wrong").Bold().Foreground(Theme.SystemCritical),
TextBlock(ex.Message).Foreground(Theme.SecondaryText),
Button("Retry", () => setAttempt(attempt + 1))
).Padding(12).Background(Theme.SystemCriticalBackground).CornerRadius(8));
}
}

class StaticFallbackDemo : Component
{
public override Element Render()
{
var (attempt, setAttempt) = UseState(0);
return VStack(8,
ErrorBoundary(
Component<CrashingCounter>().WithKey($"attempt-{attempt}"),
TextBlock("This widget is unavailable right now.").Foreground(Theme.SecondaryText)),
Button("Reset", () => setAttempt(attempt + 1)),
Caption("Pass an element instead of a function when the fallback does not need the exception.")
.Foreground(Theme.SecondaryText).TextWrapping());
}
}

class UnguardedDemo : Component
{
public override Element Render()
{
var (attempt, setAttempt) = UseState(0);
return VStack(8,
Component<CrashingCounter>().WithKey($"attempt-{attempt}"),
Button("Reset", () => setAttempt(attempt + 1)),
Caption("With no ErrorBoundary, Reactor still contains the failure to the component's own slot, but its built-in fallback shows the full exception text. Ship a RenderErrorHandler so end users see your message instead. Set it app-wide, per window (WindowSpec.RenderErrorHandler), or per host. The gallery leaves the built-in fallback on so you can see it here.")
.Foreground(Theme.SecondaryText).TextWrapping());
}
}

/// <summary>Renders normally until its third click, then throws from <see cref="Render"/>.</summary>
class CrashingCounter : Component
{
public override Element Render()
{
var (count, setCount) = UseState(0);
if (count >= 3)
throw new InvalidOperationException($"The counter refuses to go past {count - 1}.");
return HStack(8,
Button($"Clicked {count} times", () => setCount(count + 1)),
Caption("Throws on the third click.").Foreground(Theme.SecondaryText).VAlign(VerticalAlignment.Center));
}
}
Loading
Loading