using Microsoft.AspNetCore.Components; namespace Bit.Balette.Demo.Client.Services; /// /// The commands the layout registers for the whole app, written with the fluent builder end to end. They are /// rebuilt from every time it changes, so a toggle's switch and a setting's value are /// always the current ones: BaletteCommands swaps the registration when it is handed a new list. /// public static class DemoCommands { public const string GitHubUrl = "https://github.com/bitfoundation/bitplatform/tree/develop/src/Utilities/Balette"; public const string NuGetUrl = "https://www.nuget.org/packages/Bit.Balette"; // Commands tagged with it are the ones the ">" mode lists: everything that does something, nothing that only // navigates. public const string CommandTag = "command"; public static IEnumerable Global(DemoState state) => [ // ---- Navigation: every page, also reachable with G-then-X sequences, the way Linear and GitHub do it ---- BaletteCmd.Navigate("Introduction", "/").Group("Navigation").Shortcut("G H").Keywords("home", "start", "landing").Priority(BaletteCommandPriority.High), // Not titled "Getting started": its two word starts would outrank Settings for "gs", the sequence's own letters. BaletteCmd.Navigate("Quick start", "/getting-started").Group("Navigation").Shortcut("G I").Keywords("install", "setup", "begin"), BaletteCmd.Navigate("Registering commands", "/commands").Group("Navigation").Shortcut("G R").Keywords("register", "builder", "declarative", "counter"), BaletteCmd.Navigate("Settings", "/commands#settings").Group("Navigation").Shortcut("G S").Keywords("preferences", "options", "config"), BaletteCmd.Navigate("Nested pages and flows", "/pages").Group("Navigation").Shortcut("G P").Keywords("prompt", "multi-select", "confirm", "toast", "actions"), BaletteCmd.Navigate("Search and ranking", "/search").Group("Navigation").Shortcut("G F").Keywords("fuzzy", "find", "score", "frecency", "operators"), BaletteCmd.Navigate("Modes and sources", "/modes").Group("Navigation").Shortcut("G M").Keywords("prefix", "async", "debounce"), BaletteCmd.Navigate("Keyboard and shortcuts", "/keyboard").Group("Navigation").Shortcut("G K").Keywords("hotkeys", "sequence", "accessibility"), BaletteCmd.Navigate("Theming and templates", "/theming").Group("Navigation").Shortcut("G T").Keywords("dark", "css", "variables", "detail"), BaletteCmd.Navigate("Localization and RTL", "/localization").Group("Navigation").Keywords("strings", "translate", "i18n", "right to left"), BaletteCmd.Navigate("Large registry", "/performance").Group("Navigation").Shortcut("G L").Keywords("performance", "benchmark", "bulk"), BaletteCmd.Navigate("Options and security", "/options").Group("Navigation").Shortcut("G O").Keywords("configuration", "parameters", "csp", "reference"), BaletteCmd.Navigate("MCP server", "/mcp-server").Group("Navigation").Shortcut("G A").Keywords("ai", "agent", "model context protocol"), BaletteCmd.New("GitHub repository").Href(GitHubUrl, newTab: true).Group("Navigation").Keywords("source", "code", "repo"), BaletteCmd.New("NuGet package").Href(NuGetUrl, newTab: true).Group("Navigation").Keywords("install", "package", "download"), // ---- Actions: one of each kind of flow ---- BaletteCmd.New("Rename project…") .Group("Actions") .Shortcut("F2") .Keywords("title", "name") .Tags(CommandTag) // A prompt: the input turns into a text box pre-filled with the current name, and Enter submits it - once // the validator accepts it. A name the "server" refuses comes back as an Error result, and the prompt stays. .Handler(_ => Task.FromResult(BaletteCommandResult.Prompt( "New project name", ctx => { var name = ctx.PromptValue!.Trim(); if (string.Equals(name, "admin", StringComparison.OrdinalIgnoreCase)) { return Task.FromResult(BaletteCommandResult.Error("“admin” is reserved. Pick another name.")); } state.Rename(name); return Task.FromResult(BaletteCommandResult.Toast($"Renamed to “{name}”")); }, validate: value => value.Trim() switch { "" => "A project needs a name.", { Length: > 40 } => "At most 40 characters.", _ => null, }, placeholder: "Project name…", initialValue: state.ProjectName))), BaletteCmd.New("Set labels…") .Group("Actions") .Keywords("tags", "categorize") .Tags(CommandTag) // A multi-select page: Enter or Space checks a row, Ctrl+Enter hands the checked ones to the handler. .MultiSelect() .ChildrenPlaceholder("Toggle labels, Ctrl+Enter to apply…") .Children( BaletteCmd.New("bug").Id("label:bug").Icon("demo-dot demo-dot-red"), BaletteCmd.New("enhancement").Id("label:enhancement").Icon("demo-dot demo-dot-blue"), BaletteCmd.New("documentation").Id("label:documentation").Icon("demo-dot demo-dot-green"), BaletteCmd.New("performance").Id("label:performance").Icon("demo-dot demo-dot-yellow"), BaletteCmd.New("accessibility").Id("label:accessibility").Icon("demo-dot demo-dot-purple")) .Handler(ctx => { state.SetLabels(ctx.SelectedCommands.Select(c => c.Title)); return Task.FromResult(BaletteCommandResult.Toast(ctx.SelectedCommands.Count == 1 ? "1 label applied" : $"{ctx.SelectedCommands.Count} labels applied")); }), BaletteCmd.New("Assign to…") .Group("Actions") .Keywords("owner", "person", "user") .Tags(CommandTag) // A lazily loaded page: the provider runs when the page opens, and the palette shows its loading state // until it returns. .Children(async (ctx, ct) => { await Task.Delay(400, ct); return ctx.GetRequiredService().People .Select(p => BaletteCmd.New(p.Name) .Subtitle(p.Role) .Icon("demo-avatar") .Data(p) .Handler(_ => { state.Record($"Assigned to {p.Name}"); return Task.FromResult(BaletteCommandResult.Toast($"Assigned to {p.Name}")); }) .Build()) .ToList(); }, placeholder: "Pick an assignee…"), BaletteCmd.New("Find a person…") .Group("Actions") .Keywords("search", "directory", "people", "user") .Tags(CommandTag) .ChildrenPlaceholder("Search the directory…") // A page that searches as you type rather than loading everything up front: the source is asked for every // query (debounced, cancelled when overtaken), as an API with thousands of people would be. .Source("Directory", async (query, ct) => { await Task.Delay(250, ct); return FakeDirectory.Shared.People .Where(p => query.Length == 0 || p.Name.Contains(query, StringComparison.OrdinalIgnoreCase) || p.Role.Contains(query, StringComparison.OrdinalIgnoreCase)) .Select(p => BaletteCmd.New(p.Name) .Id($"person:{p.Email}") .Subtitle(p.Role) .Icon("demo-avatar") .Data(p) .ExcludeFromHistory() .Handler(_ => { state.Record($"Opened {p.Name}"); return Task.FromResult(BaletteCommandResult.Toast($"Opened {p.Name}")); }) .Build()) .ToList(); }), BaletteCmd.New("Copy share link") .Group("Actions") .Shortcut("Mod+Shift+C") .Keywords("url", "clipboard") .Tags(CommandTag) .Handler(async ctx => { // The address of the page the palette is open on: what a share button copies. if (await state.CopyToClipboardAsync(ctx.GetRequiredService().Uri) is false) { // Expected - a browser may refuse the clipboard - so a message rather than an exception. return BaletteCommandResult.Error("The browser did not let the link be copied."); } state.Record("Share link copied"); return BaletteCommandResult.Toast("Link copied to clipboard"); }), BaletteCmd.New("Export…") .Group("Actions") .Keywords("download", "save as") .Tags(CommandTag) // An ad-hoc page: the handler decides what to list when it runs, instead of the command declaring it. .Handler(_ => Task.FromResult(BaletteCommandResult.Page("Export format", [ BaletteCmd.Action("CSV", () => state.Record("Exported CSV")).Subtitle("Comma separated values").Build(), BaletteCmd.Action("JSON", () => state.Record("Exported JSON")).Subtitle("Pretty printed").Build(), BaletteCmd.Action("PDF", () => state.Record("Exported PDF")).Subtitle("Print friendly").Badge("Pro").Disabled(true, "Requires the Pro plan").Build(), ]))), // ---- Preferences: toggles keep the palette open and update in place ---- BaletteCmd.New("Theme") .Group("Preferences") .Keywords("appearance", "dark", "light", "color scheme") .Tags(CommandTag) .Value(Capitalize(state.Theme)) .Children( ThemeOption(state, "auto", "Follow the operating system"), ThemeOption(state, "light", "Always light"), ThemeOption(state, "dark", "Always dark")), // Names as their speakers write them: "francais" or "turkce" finds them from the root (nested search, and // diacritics folded), and a right-to-left one turns the whole site - the palette included - right to left. BaletteCmd.New("Language") .Group("Preferences") .Keywords("locale", "translation", "rtl", "direction") .Tags(CommandTag) .Value(state.Language) .Children( LanguageOption(state, "English", rtl: false), LanguageOption(state, "Deutsch", rtl: false), LanguageOption(state, "Français", rtl: false), LanguageOption(state, "Español", rtl: false), LanguageOption(state, "Português (Brasil)", rtl: false), LanguageOption(state, "Türkçe", rtl: false), LanguageOption(state, "Polski", rtl: false), LanguageOption(state, "فارسی", rtl: true, "Persian"), LanguageOption(state, "العربية", rtl: true, "Arabic")), Toggle("Word wrap", "wordwrap", state.WordWrap, state.ToggleWordWrap, "Alt+Z", "soft wrap lines"), Toggle("Desktop notifications", "notifications", state.Notifications, state.ToggleNotifications, null, "alerts push"), Toggle("Show minimap", "minimap", state.ShowMinimap, state.ToggleMinimap, null, "outline overview"), // Changes the palette it is listed in: the detail pane appears or goes while the palette stays open. Toggle("Show detail pane", "detail", state.ShowDetail, () => state.SetShowDetail(!state.ShowDetail), null, "preview sidebar raycast"), // ---- Danger zone: a destructive command needs a second Enter ---- BaletteCmd.New("Delete project") .Group("Danger zone") .Keywords("remove", "destroy") .Tags(CommandTag) .Destructive("This permanently deletes the project") .Handler(_ => { state.Record("Project deleted (not really)"); return Task.FromResult(BaletteCommandResult.Toast("Project deleted")); }), BaletteCmd.New("Reset all settings") .Group("Danger zone") .Keywords("defaults", "restore") .Tags(CommandTag) .Destructive("Reset every preference to its default?") .Handler(async _ => { await state.ResetPreferencesAsync(); return BaletteCommandResult.Toast("Settings reset"); }), // ---- Help: what the "?" mode lists ---- BaletteCmd.New("Keyboard shortcuts") .Group("Help") .Shortcut("Mod+/") .Keywords("hotkeys", "keys", "bindings") .Href("/keyboard#shortcuts") // After Href, which makes the command a navigation, so the row keeps the help icon. .Kind(BaletteCommandKind.Help), BaletteCmd.New("What's new") .Group("Help") .Badge("New") .Kind(BaletteCommandKind.Help) .Handler(_ => Task.FromResult(BaletteCommandResult.Toast("You are on the latest version", keepOpen: true))), // Always listed, last, whatever was typed: the query it was reached with is the feedback. BaletteCmd.New("Feedback: couldn't find a command?") .Group("Help") .Kind(BaletteCommandKind.Help) .AlwaysShow() .Priority(BaletteCommandPriority.Lowest) .Handler(ctx => Task.FromResult(BaletteCommandResult.Toast(ctx.Query.Length > 0 ? $"Thanks! We logged “{ctx.Query}”." : "Thanks for the feedback!"))), ]; private static BaletteCommand LanguageOption(DemoState state, string name, bool rtl, string? english = null) { var option = BaletteCmd.New(name) .Id($"language:{english ?? name}") .Subtitle(rtl ? "Right to left" : null) .Checked(state.Language == name) .Handler(async _ => { await state.SetLanguageAsync(name, rtl); return BaletteCommandResult.Back; }); // The English name as a keyword, so people who do not read the script still find it. if (english is not null) { option.Keywords(english); } return option.Build(); } private static BaletteCommand ThemeOption(DemoState state, string theme, string subtitle) => BaletteCmd.New(Capitalize(theme)) .Id($"theme:{theme}") .Subtitle(subtitle) .Checked(state.Theme == theme) .Handler(async _ => { await state.SetThemeAsync(theme); return BaletteCommandResult.Back; }) .Build(); private static BaletteCommand Toggle(string title, string id, bool value, Action toggle, string? shortcut, string keywords) { var builder = BaletteCmd.New(title) .Id($"toggle:{id}") .Group("Preferences") .Keywords(keywords.Split(' ')) .Tags(CommandTag) .Checked(value) .Handler(_ => { toggle(); return Task.FromResult(BaletteCommandResult.KeepOpen); }); if (shortcut is not null) { builder.Shortcut(shortcut); } return builder.Build(); } private static string Capitalize(string s) => s.Length == 0 ? s : char.ToUpperInvariant(s[0]) + s[1..]; }