using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Layout; using Avalonia.Media; using Avalonia.Styling; using Avalonia.Controls.Presenters; using Avalonia.Controls.Templates; using Avalonia.Markup.Xaml.MarkupExtensions; using Avalonia.Themes.Fluent; using FinalToken.Core; namespace FinalToken.App; public static class Ui { public static IBrush Colour(string role, bool dark) => Brush.Parse(role switch { "Page" => dark ? "#0d0d0d" : "#f9f9f7", "Surface" => dark ? "#1a1a19" : "#fcfcfb", "Border" => dark ? "#1affffff" : "#1a0b0b0b", "Ink" => dark ? "#f0efec" : "#0b0b0b", "Secondary" => dark ? "#c3c2b7" : "#52514e", "Grid" => dark ? "#2c2c2a" : "#e1e0d9", "Axis" => dark ? "#383835" : "#c3c2b7", "Selection" => dark ? "#383835" : "#e1e0d9", "Claude" => dark ? "#d95926" : "#eb6834", "Codex" => dark ? "#3987e5" : "#2a78d6", "Cache" => dark ? "#5c5b57" : "#c3c2b7", _ => "#898781" }); public static bool Dark(StyledElement element) => element.ActualThemeVariant == ThemeVariant.Dark; public static IBrush ProviderColour(Provider provider, bool dark) => Colour(provider.ToString(), dark); public static void Theme(Window window) { foreach (var theme in new[] { ThemeVariant.Dark, ThemeVariant.Light }) { var resources = new ResourceDictionary(); foreach (var role in new[] { "Page", "Surface", "Border", "Ink", "Secondary", "Muted", "Grid", "Axis", "Selection", "Claude", "Codex", "Cache" }) resources[role] = Colour(role, theme == ThemeVariant.Dark); window.Resources.ThemeDictionaries[theme] = resources; } window.Bind(TemplatedControl.BackgroundProperty, window.GetResourceObservable("Page")); window.Bind(TemplatedControl.ForegroundProperty, window.GetResourceObservable("Ink")); window.FontSize = 13; var segments = new Style(s => s.OfType().Class("segment")); segments.Setters.Add(new Setter(TemplatedControl.BackgroundProperty, new DynamicResourceExtension("Surface"))); segments.Setters.Add(new Setter(TemplatedControl.ForegroundProperty, new DynamicResourceExtension("Ink"))); segments.Setters.Add(new Setter(TemplatedControl.TemplateProperty, new FuncControlTemplate((button, _) => { var presenter = new ContentPresenter { HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center }; presenter.Bind(ContentPresenter.ContentProperty, button.GetObservable(ContentControl.ContentProperty)); presenter.Bind(ContentPresenter.ForegroundProperty, button.GetObservable(TemplatedControl.ForegroundProperty)); presenter.Bind(ContentPresenter.FontWeightProperty, button.GetObservable(TemplatedControl.FontWeightProperty)); var border = new Border { Child = presenter, Padding = new Thickness(8, 6), CornerRadius = new CornerRadius(4) }; border.Bind(Border.BackgroundProperty, button.GetObservable(TemplatedControl.BackgroundProperty)); border.BorderThickness = new Thickness(1); border.Bind(Border.BorderBrushProperty, button.GetObservable(TemplatedControl.BorderBrushProperty)); return border; }))); window.Styles.Add(segments); var focusSegment = new Style(s => s.OfType().Class("segment").Class(":focus-visible")); focusSegment.Setters.Add(new Setter(TemplatedControl.BorderBrushProperty, new DynamicResourceExtension("Ink"))); window.Styles.Add(focusSegment); var selected = new Style(s => s.OfType().Class("segment").Class(":checked")); selected.Setters.Add(new Setter(TemplatedControl.BackgroundProperty, new DynamicResourceExtension("Selection"))); selected.Setters.Add(new Setter(TemplatedControl.FontWeightProperty, FontWeight.SemiBold)); window.Styles.Add(selected); var checks = new Style(s => s.OfType()); checks.Setters.Add(new Setter(TemplatedControl.BorderBrushProperty, Brushes.Transparent)); checks.Setters.Add(new Setter(TemplatedControl.TemplateProperty, new FuncControlTemplate((check, _) => { var glyph = Text("✓", 14); glyph.Name = "NeutralCheckGlyph"; glyph.HorizontalAlignment = HorizontalAlignment.Center; var box = new Border { Name = "NeutralCheckSurface", Child = glyph, Width = 20, Height = 20, CornerRadius = new CornerRadius(4), BorderThickness = new Thickness(1), VerticalAlignment = VerticalAlignment.Center }; box.Bind(Border.BackgroundProperty, box.GetResourceObservable("Selection")); box.Bind(Border.BorderBrushProperty, box.GetResourceObservable("Axis")); glyph.Bind(Visual.IsVisibleProperty, check.GetObservable(NeutralCheckBox.MarkVisibleProperty)); var label = new ContentPresenter { Margin = new Thickness(8, 0, 0, 0), VerticalAlignment = VerticalAlignment.Center }; label.Bind(ContentPresenter.ContentProperty, check.GetObservable(ContentControl.ContentProperty)); label.Bind(ContentPresenter.ForegroundProperty, label.GetResourceObservable("Ink")); var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("20,*"), MinHeight = 32 }; grid.Children.Add(box); Grid.SetColumn(label, 1); grid.Children.Add(label); var focus = new Border { Child = grid, BorderThickness = new Thickness(1), CornerRadius = new CornerRadius(4) }; focus.Bind(Border.BorderBrushProperty, check.GetObservable(TemplatedControl.BorderBrushProperty)); return focus; }))); window.Styles.Add(checks); var focusCheck = new Style(s => s.OfType().Class(":focus-visible")); focusCheck.Setters.Add(new Setter(TemplatedControl.BorderBrushProperty, new DynamicResourceExtension("Ink"))); window.Styles.Add(focusCheck); } // Explicit palettes keep every remaining Fluent control independent of the OS accent. public static FluentTheme NeutralTheme() { var theme = new FluentTheme(); theme.Palettes[ThemeVariant.Dark] = new ColorPaletteResources { Accent = Color.Parse("#383835") }; theme.Palettes[ThemeVariant.Light] = new ColorPaletteResources { Accent = Color.Parse("#e1e0d9") }; return theme; } public static TextBlock Text(string text, double size = 13, string role = "Ink") { var block = new TextBlock { Text = text, FontSize = size, TextWrapping = TextWrapping.Wrap }; block.Bind(TextBlock.ForegroundProperty, block.GetResourceObservable(role)); return block; } public static TextBlock Mark(string text, IBrush colour, double size = 12) => new() { Text = text, FontSize = size, Foreground = colour }; public static TextBlock TableNumber(string text, double size = 13, string role = "Ink") { var block = Text(text, size, role); block.FontFeatures = FontFeatureCollection.Parse("tnum"); return block; } public static StackPanel Stack(params Control[] children) { var stack = new StackPanel { Spacing = 8 }; foreach (var child in children) stack.Children.Add(child); return stack; } public static Border Card(Control content) { var card = new Border { Child = content, Padding = new Thickness(16), CornerRadius = new CornerRadius(10), BorderThickness = new Thickness(1) }; card.Bind(Border.BackgroundProperty, card.GetResourceObservable("Surface")); card.Bind(Border.BorderBrushProperty, card.GetResourceObservable("Border")); return card; } public static Button Button(string text, Action action) { var button = new Button { Content = text, Padding = new Thickness(12, 6) }; button.Click += (_, _) => action(); return button; } public static Control Columns(IEnumerable children, bool wide) => Columns(children, wide ? 2 : 1); public static Control Columns(IEnumerable children, int columns) { var grid = new Grid { ColumnDefinitions = new ColumnDefinitions(string.Join(',', Enumerable.Repeat("*", columns))), ColumnSpacing = 16, RowSpacing = 16 }; var index = 0; foreach (var child in children) { child.VerticalAlignment = VerticalAlignment.Top; if (index % columns == 0) grid.RowDefinitions.Add(new RowDefinition(GridLength.Auto)); Grid.SetColumn(child, index % columns); Grid.SetRow(child, index / columns); grid.Children.Add(child); index++; } return grid; } }