From a9c060299d84a52fcdad7454a54a5562de7d59ee Mon Sep 17 00:00:00 2001 From: token <61819790+239573049@users.noreply.github.com> Date: Tue, 1 Dec 2026 04:05:23 -0500 Subject: [PATCH 1/3] feat: CodeEditor programmatic API, toggle selected colors, public SegmentedItem, rectangle caption buttons - CodeEditor (#228, #233): public cursor and selection API (Get/SetPosition, Get/SetSelection(s), Reveal*), CursorPositionChanged / CursorSelectionChanged, CanUndo / CanRedo / Undo / Redo / PushUndoStop / UndoRedoStateChanged, and ShowFind / ShowFindReplace / CloseFind. - ToggleGroup and SegmentedControl (#227): SelectedItemBackground and SelectedItemForeground. - SegmentedItem is public (#231), with its own token (identity SegmentedControlItem); Mobile shares the class name. - ArcWindow / TitleBar / WindowCaptionButtons (#230): CaptionButtonVariant (Round by default, Rectangle for Windows-style buttons). - Token baselines regenerated for the new token entries. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- CHANGELOG.md | 14 + .../CodeEditor/CodeEditor.Api.cs | 75 ++++- .../CodeEditor/CodeEditor.Commands.cs | 7 +- .../CodeEditor/CodeEditor.FindApi.cs | 58 ++++ .../CodeEditor/CodeEditor.History.cs | 40 +++ .../CodeEditor/CodeEditor.PublicEvents.cs | 120 ++++++++ .../CodeEditor/CodeEditor.cs | 2 + .../CodeEditor/CodeEditorCursorEventArgs.cs | 88 ++++++ .../PublicAPI.Unshipped.txt | 48 +++ src/TKCode.Arc/Content/ArcWindow/ArcWindow.cs | 16 + .../ArcWindow/Themes/TitleBarTheme.axaml | 16 + .../Themes/WindowCaptionButtonsTheme.axaml | 55 +++- src/TKCode.Arc/Content/ArcWindow/TitleBar.cs | 25 +- .../Content/ArcWindow/TitleBarToken.cs | 8 + .../Content/ArcWindow/WindowCaptionButton.cs | 26 +- .../Content/ArcWindow/WindowCaptionButtons.cs | 83 ++++- .../ArcWindow/WindowCaptionButtonsToken.cs | 11 + src/TKCode.Arc/PublicAPI.Unshipped.txt | 46 +++ .../SegmentedControl/SegmentedControl.cs | 21 ++ .../Toggles/SegmentedControl/SegmentedItem.cs | 25 +- .../SegmentedControl/SegmentedItemToken.cs | 27 ++ .../Themes/SegmentedControlTheme.axaml | 5 +- .../Toggles/ToggleGroup/ToggleGroup.cs | 31 +- .../Toggles/ToggleGroup/ToggleGroupItem.cs | 11 +- src/TKCode.Arc/Toggles/_Shared/TogglesItem.cs | 13 + tests/Baselines/tokens/dark-amber.json | 6 + tests/Baselines/tokens/dark-blue.json | 6 + tests/Baselines/tokens/dark-coral.json | 6 + .../Baselines/tokens/dark-corners-square.json | 6 + tests/Baselines/tokens/dark-default.json | 6 + tests/Baselines/tokens/dark-green.json | 6 + tests/Baselines/tokens/dark-neutral.json | 6 + tests/Baselines/tokens/dark-orange.json | 6 + tests/Baselines/tokens/dark-pixel.json | 6 + tests/Baselines/tokens/dark-rose.json | 6 + tests/Baselines/tokens/dark-seed-0562EF.json | 6 + tests/Baselines/tokens/dark-seed-E5484D.json | 6 + tests/Baselines/tokens/dark-violet.json | 6 + tests/Baselines/tokens/light-amber.json | 6 + tests/Baselines/tokens/light-blue.json | 6 + tests/Baselines/tokens/light-coral.json | 6 + .../tokens/light-corners-scale-0.5.json | 6 + .../tokens/light-corners-scale-2.json | 6 + .../tokens/light-corners-square.json | 6 + .../tokens/light-default-motion-off.json | 6 + .../tokens/light-default-motion-reduced.json | 6 + tests/Baselines/tokens/light-default.json | 6 + tests/Baselines/tokens/light-green.json | 6 + tests/Baselines/tokens/light-neutral.json | 6 + tests/Baselines/tokens/light-orange.json | 6 + tests/Baselines/tokens/light-pixel.json | 6 + tests/Baselines/tokens/light-rose.json | 6 + tests/Baselines/tokens/light-seed-0562EF.json | 6 + tests/Baselines/tokens/light-seed-E5484D.json | 6 + tests/Baselines/tokens/light-violet.json | 6 + .../Editor/CodeEditorPublicApiTests.cs | 290 ++++++++++++++++++ .../Architecture/MobileClassNameTests.cs | 2 +- .../Content/ArcWindowTests.cs | 75 +++++ .../Content/WindowCaptionButtonsTests.cs | 121 ++++++++ .../Toggles/SegmentedControlTests.cs | 113 +++++++ .../Toggles/ToggleGroupTests.cs | 100 ++++++ 62 files changed, 1719 insertions(+), 35 deletions(-) create mode 100644 src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.FindApi.cs create mode 100644 src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.History.cs create mode 100644 src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.PublicEvents.cs create mode 100644 src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditorCursorEventArgs.cs create mode 100644 src/TKCode.Arc/Toggles/SegmentedControl/SegmentedItemToken.cs create mode 100644 tests/TKCode.Arc.Extras.Tests/Editor/CodeEditorPublicApiTests.cs diff --git a/AGENTS.md b/AGENTS.md index d2137850c..9a2208344 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -23,7 +23,7 @@ Read `design/engineering/implementation-guide.md` before writing a component: fi ## Non-negotiable rules - Controls derive from Avalonia base types; only public Avalonia APIs are used. No AtomUI code or packages, and none of its source is consulted (clean room; design/decisions/0031-replace-atomui.md). -- Class names have no prefix; flat namespace `TKCode.Arc`; xmlns `https://github.com/AIDotNet/TKCode.Arc`. `TKCode.Arc.Mobile` is the exception: namespace `TKCode.Arc.Mobile`, xmlns `https://github.com/AIDotNet/TKCode.Arc.Mobile` (prefix `arcm`), and a few of its types share a desktop name (`Button`, `ButtonVariant`, `Checkbox`, `Dialog`, `RadioCard`, `RadioCardOption`, `RadioCards`, `Switch`, `Toast`; pinned by `MobileClassNameTests`, #152). New Mobile types do not reuse desktop names; a C# file that imports both namespaces aliases one side (`using MobileButton = TKCode.Arc.Mobile.Button;`). +- Class names have no prefix; flat namespace `TKCode.Arc`; xmlns `https://github.com/AIDotNet/TKCode.Arc`. `TKCode.Arc.Mobile` is the exception: namespace `TKCode.Arc.Mobile`, xmlns `https://github.com/AIDotNet/TKCode.Arc.Mobile` (prefix `arcm`), and a few of its types share a desktop name (`Button`, `ButtonVariant`, `Checkbox`, `Dialog`, `RadioCard`, `RadioCardOption`, `RadioCards`, `SegmentedItem`, `Switch`, `Toast`; pinned by `MobileClassNameTests`, #152). New Mobile types do not reuse desktop names; a C# file that imports both namespaces aliases one side (`using MobileButton = TKCode.Arc.Mobile.Button;`). - `TKCode.Arc.Terminal` is a standalone terminal library published with Arc (#150, ADR 0052): it depends on Avalonia alone, keeps its `TKCode.Terminal.*` namespaces, needs no `UseArc` entry, and takes its colors from the terminal core's palette (the host sets it), so the token, theme and registration rules below do not apply to it. Its default font starts with Geist Mono. - The `TKCode.Arc.StreamMark*` packages are standalone libraries published with Arc, like Terminal: they depend on Avalonia (plus Markdig, TextMate, Mermaider / Svg.Skia, the RaTeX native library) and never on Foundation, Core or the controls, keep their `StreamMark.*` namespaces, need no `UseArc` entry, and take their look from the `Md.*` resources a host maps (Arc's `Markdown` maps them onto tokens), so the token, theme and registration rules below do not apply to them. They keep StreamMark's stricter gate: trimming and AOT warnings are errors (build/ProjectDefaults.props). They follow the rest: braces, no reflection, PublicAPI files, CHANGELOG, the deprecation rule. - Every public control has a `[ControlDesignToken]` `Token` in the same namespace with at least one property (ARCF003). Its identity (class name minus `Token`, or `[ControlDesignToken(Identity = "...")]`) is unique across all Arc packages, Mobile included (Mobile uses `MobileButton`, `MobileSwitch`, ...; design/engineering/reserved-token-names.md). Token property types are limited to those in foundation.md section 3.4 (ARCF001). diff --git a/CHANGELOG.md b/CHANGELOG.md index acd385ae5..bc086fcf6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,20 @@ NuGet packages published with each release: `TKCode.Arc`, `TKCode.Arc.Core`, `TK Also, as previews that follow the main version: `TKCode.Arc.Graphics3D`, `TKCode.Arc.Graphics3D.WebGpu`, `TKCode.Arc.Graphics3D.Controls` and `TKCode.Arc.Graphics3D.Browser` (from 1.2.0-preview.3). +## Unreleased + +### Added + +- `CodeEditor` (`TKCode.Arc.Editor.Controls`): the cursor and selection API a host needs to drive the editor from code (#228), named after Monaco's `ICodeEditor`, with positions and selections as the public `TextPosition` / `TextSelection` / `TextRange` (1-based line and column in UTF-16 code units): + - `GetPosition()`, `GetSelection()`, `GetSelections()`, `SetPosition(...)`, `SetSelection(...)`, `SetSelections(...)`; + - `RevealLine`, `RevealPosition` and `RevealRange`, each also as `...InCenter` and `...InCenterIfOutsideViewport` (`RevealRangeAtTop` for ranges); + - the `CursorPositionChanged` and `CursorSelectionChanged` events, with `CodeEditorCursorPositionChangedEventArgs` / `CodeEditorCursorSelectionChangedEventArgs` (new position or selection, the old selections, `Source` such as `keyboard`, `mouse` or `api`, and a `CodeEditorCursorChangeReason`). +- `CodeEditor` (`TKCode.Arc.Editor.Controls`): undo and redo from code (#233): `CanUndo` and `CanRedo` (read-only Avalonia properties, as on `RichTextEditor`), `Undo()` and `Redo()` (they return whether a step ran), `PushUndoStop()` to end the current undo step, and the `UndoRedoStateChanged` event. +- `CodeEditor` (`TKCode.Arc.Editor.Controls`): `ShowFind()`, `ShowFindReplace()` and `CloseFind()` open and close the built-in find and find-and-replace widget from code (#233). +- `ToggleGroup` and `SegmentedControl` (`TKCode.Arc`): `SelectedItemBackground` and `SelectedItemForeground` brush properties set the fill behind the selected option (the gliding fill, or each pressed option's fill in multiple mode; the segmented pill) and its text and icon color, per instance or from a style, for backgrounds where the default selected look is hard to tell from the other options. Null (cleared) keeps the theme's colors (#227). +- `SegmentedItem` (`TKCode.Arc`) is now public (it was internal), so that XAML styles and Rider's checks can address `arc|SegmentedItem` (#231). It has a token of its own, `SegmentedItemToken` (identity `SegmentedControlItem`, because `TKCode.Arc.Mobile` already has a `SegmentedItem` token). `TKCode.Arc.Mobile` also has a `SegmentedItem` in its own namespace; a C# file that imports both namespaces aliases one side. +- `ArcWindow` / `TitleBar` / `WindowCaptionButtons` (`TKCode.Arc`): `CaptionButtonVariant` (`Round` | `Rectangle`, #230) picks the shape of the drawn caption buttons. `Round` is the default and the look is unchanged; `Rectangle` draws Windows-style flat buttons that are as tall as the title bar, 46 wide (`WindowCaptionButtonsToken.RectangleButtonWidth`), touch each other and the window's right edge (`TitleBarToken.RectangleButtonsPadding`), with square corners, and the close button turns solid danger under the pointer. Inherited, so setting it on the `ArcWindow` is enough; it applies only while the bar draws the buttons (`CaptionButtons` `Drawn`, or `Auto` outside macOS). New token entries: `WindowCaptionButtonsTokenKind.RectangleButtonWidth` and `RectangleCloseHoverForeground`, `TitleBarTokenKind.RectangleButtonsPadding`. + ## 1.3.3-preview.2 - 2026-12-01 A patch preview fixing spurious `HoverCard` openings, with no public API changes. Install it with `--version 1.3.3-preview.2` (or `--prerelease`). diff --git a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.Api.cs b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.Api.cs index afc097def..526ee3253 100644 --- a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.Api.cs +++ b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.Api.cs @@ -14,28 +14,77 @@ namespace TKCode.Arc; public sealed partial class CodeEditor { - /// The primary cursor's model position, or null without a view model (upstream's getPosition). - internal TextPosition? GetPosition() => ViewModel?.GetPosition(); + /// + /// The primary cursor's position in the document (1-based line and column, UTF-16 code units), or null while the + /// editor has no view yet (upstream's getPosition). + /// + public TextPosition? GetPosition() => ViewModel?.GetPosition(); - /// The primary selection (upstream's getSelection). - internal TextSelection? GetSelection() => ViewModel?.GetSelection(); + /// The primary selection, or null while the editor has no view yet (upstream's getSelection). + public TextSelection? GetSelection() => ViewModel?.GetSelection(); - /// Every selection, the primary first (upstream's getSelections). - internal IReadOnlyList GetSelections() => ViewModel?.GetSelections() ?? []; + /// Every selection, the primary first; empty while the editor has no view yet (upstream's getSelections). + public IReadOnlyList GetSelections() => ViewModel?.GetSelections() ?? []; - /// Moves the cursor to a model position, selecting nothing (upstream's setPosition). - internal void SetPosition(TextPosition position, string source = "api") => + /// + /// Moves the cursor to a position, selecting nothing; the position is clamped into the document and the cursor + /// is revealed (upstream's setPosition). tells listeners where the change came from. + /// + public void SetPosition(TextPosition position, string source = "api") => SetSelections([new TextSelection(position.LineNumber, position.Column, position.LineNumber, position.Column)], source); - /// Sets the primary selection, removing the others (upstream's setSelection). - internal void SetSelection(TextSelection selection, string source = "api") => SetSelections([selection], source); + /// + /// Sets the primary selection, removing the other cursors (upstream's setSelection). + /// tells listeners where the change came from. + /// + public void SetSelection(TextSelection selection, string source = "api") => SetSelections([selection], source); + + /// + /// Sets one or more selections, the first being the primary; an empty list changes nothing (upstream's + /// setSelections). tells listeners where the change came from. + /// + public void SetSelections(IReadOnlyList selections, string source = "api") => + SetSelections(selections, source, CursorChangeReason.NotSet); /// Sets the selections (upstream's setSelections). - internal void SetSelections(IReadOnlyList selections, string source = "api", CursorChangeReason reason = CursorChangeReason.NotSet) => + internal void SetSelections(IReadOnlyList selections, string source, CursorChangeReason reason) => ViewModel?.SetSelections(source, selections, reason); + /// Scrolls a line into view with the least scrolling (upstream's revealLine); the cursor does not move. + public void RevealLine(int lineNumber) => RevealLine(lineNumber, VerticalRevealType.Simple, ScrollType.Smooth); + + /// Scrolls a line into the center of the view (upstream's revealLineInCenter). + public void RevealLineInCenter(int lineNumber) => RevealLine(lineNumber, VerticalRevealType.Center, ScrollType.Smooth); + + /// Scrolls a line into the center of the view unless it is already visible (upstream's revealLineInCenterIfOutsideViewport). + public void RevealLineInCenterIfOutsideViewport(int lineNumber) => + RevealLine(lineNumber, VerticalRevealType.CenterIfOutsideViewport, ScrollType.Smooth); + + /// Scrolls a position into view with the least scrolling, also horizontally (upstream's revealPosition); the cursor does not move. + public void RevealPosition(TextPosition position) => RevealRange(new TextRange(position), VerticalRevealType.Simple, ScrollType.Smooth); + + /// Scrolls a position into the center of the view (upstream's revealPositionInCenter). + public void RevealPositionInCenter(TextPosition position) => RevealRange(new TextRange(position), VerticalRevealType.Center, ScrollType.Smooth); + + /// Scrolls a position into the center of the view unless it is already visible (upstream's revealPositionInCenterIfOutsideViewport). + public void RevealPositionInCenterIfOutsideViewport(TextPosition position) => + RevealRange(new TextRange(position), VerticalRevealType.CenterIfOutsideViewport, ScrollType.Smooth); + + /// Scrolls a range into view with the least scrolling (upstream's revealRange); the cursor does not move. + public void RevealRange(TextRange range) => RevealRange(range, VerticalRevealType.Simple, ScrollType.Smooth, revealHorizontal: true); + + /// Scrolls a range into the center of the view (upstream's revealRangeInCenter). + public void RevealRangeInCenter(TextRange range) => RevealRange(range, VerticalRevealType.Center, ScrollType.Smooth, revealHorizontal: true); + + /// Scrolls a range into the center of the view unless it is already visible (upstream's revealRangeInCenterIfOutsideViewport). + public void RevealRangeInCenterIfOutsideViewport(TextRange range) => + RevealRange(range, VerticalRevealType.CenterIfOutsideViewport, ScrollType.Smooth, revealHorizontal: true); + + /// Scrolls a range to the top of the view (upstream's revealRangeAtTop). + public void RevealRangeAtTop(TextRange range) => RevealRange(range, VerticalRevealType.Top, ScrollType.Smooth, revealHorizontal: true); + /// Scrolls a model range into view (upstream's revealRange and its InCenter / AtTop variants). - internal void RevealRange(TextRange range, VerticalRevealType verticalType = VerticalRevealType.Simple, ScrollType scrollType = ScrollType.Smooth, bool revealHorizontal = true) + internal void RevealRange(TextRange range, VerticalRevealType verticalType, ScrollType scrollType = ScrollType.Smooth, bool revealHorizontal = true) { if (ViewModel is not { } viewModel) { @@ -47,7 +96,7 @@ internal void RevealRange(TextRange range, VerticalRevealType verticalType = Ver } /// Scrolls a model line into view (upstream's revealLine). - internal void RevealLine(int lineNumber, VerticalRevealType verticalType = VerticalRevealType.Simple, ScrollType scrollType = ScrollType.Smooth) => + internal void RevealLine(int lineNumber, VerticalRevealType verticalType, ScrollType scrollType = ScrollType.Smooth) => RevealRange(new TextRange(lineNumber, 1, lineNumber, 1), verticalType, scrollType, revealHorizontal: false); /// diff --git a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.Commands.cs b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.Commands.cs index bcae83ec1..4a44c97fd 100644 --- a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.Commands.cs +++ b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.Commands.cs @@ -34,8 +34,11 @@ void ICoreCommandsCodeEditor.ExecuteCommands(string? source, IReadOnlyList() => this as T ?? throw new InvalidOperationException($"[invokeFunction] unknown service '{typeof(T).Name}'"); - /// Adds an undo stop (upstream's pushUndoStop); false in a read-only editor. - internal bool PushUndoStop() + /// + /// Ends the current undo step, so that the edits after it undo separately from the ones before (upstream's + /// pushUndoStop); false in a read-only editor. + /// + public bool PushUndoStop() { if (IsReadOnly) { diff --git a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.FindApi.cs b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.FindApi.cs new file mode 100644 index 000000000..78e40fa46 --- /dev/null +++ b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.FindApi.cs @@ -0,0 +1,58 @@ +// The find and replace widget as method calls (upstream's trigger("actions.find"), trigger("editor.action.startFindReplace") +// and the find controller's closeFindWidget). + +using TKCode.Arc.Editor.Contrib.Find; + +namespace TKCode.Arc; + +public sealed partial class CodeEditor +{ + /// + /// Shows the find widget and focuses its search box, as Ctrl+F (Cmd+F) does: the search box is filled from the + /// selection when the options say so. Returns false when the editor cannot show it yet (it has not been + /// attached to a window). + /// + public bool ShowFind() + { + if (FindController.Get(this) is not { } controller) + { + return false; + } + + Finish(FindActions.StartFind(controller)); + return true; + } + + /// + /// Shows the find widget with its replace row and focuses the search box, as Ctrl+H (Cmd+Alt+F) does. Returns + /// false when the editor is read-only or cannot show the widget yet (it has not been attached to a window). + /// + public bool ShowFindReplace() + { + if (FindController.Get(this) is not { } controller) + { + return false; + } + + var started = FindActions.StartFindReplace(controller); + return started.IsCompleted ? started.GetAwaiter().GetResult() : !IsReadOnly; + } + + /// Hides the find widget and puts the focus back into the text, as Escape does. Does nothing when it is not shown. + public void CloseFind() + { + if (FindController.Get(this) is { } controller && controller.State.IsRevealed) + { + controller.CloseFindWidget(); + } + } + + // find starts synchronously unless the clipboard has to be read; a fault of a start that goes on later is not lost + private static void Finish(Task task) + { + if (task.IsCompleted) + { + task.GetAwaiter().GetResult(); + } + } +} diff --git a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.History.cs b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.History.cs new file mode 100644 index 000000000..e45f3f89f --- /dev/null +++ b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.History.cs @@ -0,0 +1,40 @@ +// Undo and redo as method calls (upstream's trigger("undo") / the model's undo and redo behind the core commands). + +namespace TKCode.Arc; + +public sealed partial class CodeEditor +{ + /// + /// Undoes the last edit step, as Ctrl+Z does: the text and the cursors go back to before it. Returns false, and + /// changes nothing, when there is nothing to undo or the editor is read-only. + /// + public bool Undo() => RunHistoryStep(redo: false); + + /// + /// Redoes the last undone edit step, as Ctrl+Y does. Returns false, and changes nothing, when there is nothing to + /// redo or the editor is read-only. + /// + public bool Redo() => RunHistoryStep(redo: true); + + private bool RunHistoryStep(bool redo) + { + if (IsReadOnly || !(redo ? Model.CanRedo() : Model.CanUndo())) + { + return false; + } + + var step = redo ? Model.Redo() : Model.Undo(); + if (step.IsCompleted) + { + // the model's undo stack runs synchronously: no prompts here, so there is no way to be cancelled + step.GetAwaiter().GetResult(); + } + else + { + _ = step.AsTask(); + } + + UpdateUndoRedoState(); + return true; + } +} diff --git a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.PublicEvents.cs b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.PublicEvents.cs new file mode 100644 index 000000000..eef4f0144 --- /dev/null +++ b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.PublicEvents.cs @@ -0,0 +1,120 @@ +// The editor's public events: the cursor events (upstream's onDidChangeCursorPosition and onDidChangeCursorSelection +// in codeEditorWidget.ts, which the view model reports as one event here, see CodeEditor.Events.cs) and the undo and +// redo state (no upstream event: the host asks the model's undo stack, upstream's canUndo / canRedo). + +using Avalonia; +using TKCode.Arc.Editor; +using TKCode.Arc.Editor.ViewModel; + +namespace TKCode.Arc; + +public sealed partial class CodeEditor +{ + /// Identifies the property. + public static readonly DirectProperty CanUndoProperty = + AvaloniaProperty.RegisterDirect(nameof(CanUndo), o => o.CanUndo); + + /// Identifies the property. + public static readonly DirectProperty CanRedoProperty = + AvaloniaProperty.RegisterDirect(nameof(CanRedo), o => o.CanRedo); + + private bool _canUndo; + private bool _canRedo; + + /// True when there is an edit to undo. + public bool CanUndo + { + get => _canUndo; + private set => SetAndRaise(CanUndoProperty, ref _canUndo, value); + } + + /// True when there is an undone edit to redo. + public bool CanRedo + { + get => _canRedo; + private set => SetAndRaise(CanRedoProperty, ref _canRedo, value); + } + + /// + /// Raised once the answer of or has changed: after an edit, an undo, a + /// redo, or the text being replaced (which clears the history). + /// + public event EventHandler? UndoRedoStateChanged; + + /// + /// Raised when the cursor moved: the position of the primary cursor or of a secondary one changed (upstream's + /// onDidChangeCursorPosition). It is raised once per change, whether the user typed, clicked or the code called + /// , and not when only the anchor of a selection moved. + /// + public event EventHandler? CursorPositionChanged; + + /// + /// Raised when the selections changed (upstream's onDidChangeCursorSelection), including a plain cursor move: an + /// empty selection is a cursor. + /// + public event EventHandler? CursorSelectionChanged; + + private void RaiseCursorEvents(CursorStateChangedEvent e) + { + if (e.Selections.Count == 0) + { + return; + } + + var reason = CodeEditorCursorPositionChangedEventArgs.Convert(e.Reason); + if (CursorPositionChanged is { } positionChanged && PositionsChanged(e.OldSelections, e.Selections)) + { + var secondary = new List(e.Selections.Count - 1); + for (var i = 1; i < e.Selections.Count; i++) + { + secondary.Add(e.Selections[i].Position); + } + + positionChanged(this, new CodeEditorCursorPositionChangedEventArgs(e.Selections[0].Position, secondary, e.Source, reason)); + } + + if (CursorSelectionChanged is { } selectionChanged) + { + var secondary = new List(e.Selections.Count - 1); + for (var i = 1; i < e.Selections.Count; i++) + { + secondary.Add(e.Selections[i]); + } + + selectionChanged(this, new CodeEditorCursorSelectionChangedEventArgs(e.Selections[0], secondary, e.OldSelections, e.Source, reason)); + } + } + + private static bool PositionsChanged(IReadOnlyList? oldSelections, IReadOnlyList selections) + { + if (oldSelections is null || oldSelections.Count != selections.Count) + { + return true; + } + + for (var i = 0; i < selections.Count; i++) + { + if (oldSelections[i].Position != selections[i].Position) + { + return true; + } + } + + return false; + } + + // Reads the model's undo stack and raises UndoRedoStateChanged if an answer changed. + private void UpdateUndoRedoState() + { + var canUndo = Model.CanUndo(); + var canRedo = Model.CanRedo(); + if (canUndo == _canUndo && canRedo == _canRedo) + { + return; + } + + CanUndo = canUndo; + CanRedo = canRedo; + UndoRedoStateChanged?.Invoke(this, EventArgs.Empty); + } +} diff --git a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.cs b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.cs index e42f88fad..8769919f0 100644 --- a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.cs +++ b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditor.cs @@ -120,6 +120,7 @@ internal CodeEditor(IReadOnlyList contributions, ContextKeyService = new ContextKeyService(); Model = CreateModel(string.Empty, LanguageIds.PlaintextLanguageId); AttachModel(Model); + _onDidChangeCursorSelection.Add(RaiseCursorEvents); MarkerService = new MarkerService(); MarkerDecorationsService = new MarkerDecorationsService(MarkerService, LanguageConfigurationService); _markerDecorationsRegistration = MarkerDecorationsService.AddModel(Model); @@ -403,6 +404,7 @@ private void OnModelContentChanged(ModelContentChangedEvent e) _syncingText = false; } + UpdateUndoRedoState(); TextChanged?.Invoke(this, EventArgs.Empty); } diff --git a/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditorCursorEventArgs.cs b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditorCursorEventArgs.cs new file mode 100644 index 000000000..7a2306d26 --- /dev/null +++ b/src/TKCode.Arc.Editor.Controls/CodeEditor/CodeEditorCursorEventArgs.cs @@ -0,0 +1,88 @@ +using TKCode.Arc.Editor; +using TKCode.Arc.Editor.Cursor; + +namespace TKCode.Arc; + +/// Why the cursor of a moved (upstream's CursorChangeReason). +public enum CodeEditorCursorChangeReason +{ + /// Unknown, or not set by whoever moved it (the keyboard and mouse use ). + NotSet = 0, + + /// The whole text was replaced ( was set). + ContentFlush = 1, + + /// The text changed elsewhere and the cursor recovered its place from its markers. + RecoverFromMarkers = 2, + + /// An explicit user gesture. + Explicit = 3, + + /// A paste. + Paste = 4, + + /// An undo. + Undo = 5, + + /// A redo. + Redo = 6, +} + +/// Data of (upstream's ICursorPositionChangedEvent). +public sealed class CodeEditorCursorPositionChangedEventArgs : EventArgs +{ + internal CodeEditorCursorPositionChangedEventArgs(TextPosition position, IReadOnlyList secondaryPositions, string source, CodeEditorCursorChangeReason reason) + { + Position = position; + SecondaryPositions = secondaryPositions; + Source = source; + Reason = reason; + } + + /// The position of the primary cursor (1-based line and column). + public TextPosition Position { get; } + + /// The positions of the other cursors, if the editor has several. + public IReadOnlyList SecondaryPositions { get; } + + /// What moved the cursor: keyboard, mouse, api () and so on. + public string Source { get; } + + /// Why the cursor moved. + public CodeEditorCursorChangeReason Reason { get; } + + internal static CodeEditorCursorChangeReason Convert(CursorChangeReason reason) => (CodeEditorCursorChangeReason)(int)reason; +} + +/// Data of (upstream's ICursorSelectionChangedEvent). +public sealed class CodeEditorCursorSelectionChangedEventArgs : EventArgs +{ + internal CodeEditorCursorSelectionChangedEventArgs( + TextSelection selection, + IReadOnlyList secondarySelections, + IReadOnlyList? oldSelections, + string source, + CodeEditorCursorChangeReason reason) + { + Selection = selection; + SecondarySelections = secondarySelections; + OldSelections = oldSelections; + Source = source; + Reason = reason; + } + + /// The primary selection. + public TextSelection Selection { get; } + + /// The other selections, if the editor has several cursors. + public IReadOnlyList SecondarySelections { get; } + + /// The selections before the change (primary first), or null when not known. + public IReadOnlyList? OldSelections { get; } + + /// What changed the selection: keyboard, mouse, api () and so on. + public string Source { get; } + + /// Why the selection changed. + public CodeEditorCursorChangeReason Reason { get; } +} diff --git a/src/TKCode.Arc.Editor.Controls/PublicAPI.Unshipped.txt b/src/TKCode.Arc.Editor.Controls/PublicAPI.Unshipped.txt index 7dc5c5811..f4e870666 100644 --- a/src/TKCode.Arc.Editor.Controls/PublicAPI.Unshipped.txt +++ b/src/TKCode.Arc.Editor.Controls/PublicAPI.Unshipped.txt @@ -1 +1,49 @@ #nullable enable +static readonly TKCode.Arc.CodeEditor.CanRedoProperty -> Avalonia.DirectProperty! +static readonly TKCode.Arc.CodeEditor.CanUndoProperty -> Avalonia.DirectProperty! +TKCode.Arc.CodeEditor.CanRedo.get -> bool +TKCode.Arc.CodeEditor.CanUndo.get -> bool +TKCode.Arc.CodeEditor.CloseFind() -> void +TKCode.Arc.CodeEditor.CursorPositionChanged -> System.EventHandler? +TKCode.Arc.CodeEditor.CursorSelectionChanged -> System.EventHandler? +TKCode.Arc.CodeEditor.GetPosition() -> TKCode.Arc.Editor.TextPosition? +TKCode.Arc.CodeEditor.GetSelection() -> TKCode.Arc.Editor.TextSelection? +TKCode.Arc.CodeEditor.GetSelections() -> System.Collections.Generic.IReadOnlyList! +TKCode.Arc.CodeEditor.PushUndoStop() -> bool +TKCode.Arc.CodeEditor.Redo() -> bool +TKCode.Arc.CodeEditor.RevealLine(int lineNumber) -> void +TKCode.Arc.CodeEditor.RevealLineInCenter(int lineNumber) -> void +TKCode.Arc.CodeEditor.RevealLineInCenterIfOutsideViewport(int lineNumber) -> void +TKCode.Arc.CodeEditor.RevealPosition(TKCode.Arc.Editor.TextPosition position) -> void +TKCode.Arc.CodeEditor.RevealPositionInCenter(TKCode.Arc.Editor.TextPosition position) -> void +TKCode.Arc.CodeEditor.RevealPositionInCenterIfOutsideViewport(TKCode.Arc.Editor.TextPosition position) -> void +TKCode.Arc.CodeEditor.RevealRange(TKCode.Arc.Editor.TextRange range) -> void +TKCode.Arc.CodeEditor.RevealRangeAtTop(TKCode.Arc.Editor.TextRange range) -> void +TKCode.Arc.CodeEditor.RevealRangeInCenter(TKCode.Arc.Editor.TextRange range) -> void +TKCode.Arc.CodeEditor.RevealRangeInCenterIfOutsideViewport(TKCode.Arc.Editor.TextRange range) -> void +TKCode.Arc.CodeEditor.SetPosition(TKCode.Arc.Editor.TextPosition position, string! source = "api") -> void +TKCode.Arc.CodeEditor.SetSelection(TKCode.Arc.Editor.TextSelection selection, string! source = "api") -> void +TKCode.Arc.CodeEditor.SetSelections(System.Collections.Generic.IReadOnlyList! selections, string! source = "api") -> void +TKCode.Arc.CodeEditor.ShowFind() -> bool +TKCode.Arc.CodeEditor.ShowFindReplace() -> bool +TKCode.Arc.CodeEditor.Undo() -> bool +TKCode.Arc.CodeEditor.UndoRedoStateChanged -> System.EventHandler? +TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorChangeReason.ContentFlush = 1 -> TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorChangeReason.Explicit = 3 -> TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorChangeReason.NotSet = 0 -> TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorChangeReason.Paste = 4 -> TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorChangeReason.RecoverFromMarkers = 2 -> TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorChangeReason.Redo = 6 -> TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorChangeReason.Undo = 5 -> TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorPositionChangedEventArgs +TKCode.Arc.CodeEditorCursorPositionChangedEventArgs.Position.get -> TKCode.Arc.Editor.TextPosition +TKCode.Arc.CodeEditorCursorPositionChangedEventArgs.Reason.get -> TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorPositionChangedEventArgs.SecondaryPositions.get -> System.Collections.Generic.IReadOnlyList! +TKCode.Arc.CodeEditorCursorPositionChangedEventArgs.Source.get -> string! +TKCode.Arc.CodeEditorCursorSelectionChangedEventArgs +TKCode.Arc.CodeEditorCursorSelectionChangedEventArgs.OldSelections.get -> System.Collections.Generic.IReadOnlyList? +TKCode.Arc.CodeEditorCursorSelectionChangedEventArgs.Reason.get -> TKCode.Arc.CodeEditorCursorChangeReason +TKCode.Arc.CodeEditorCursorSelectionChangedEventArgs.SecondarySelections.get -> System.Collections.Generic.IReadOnlyList! +TKCode.Arc.CodeEditorCursorSelectionChangedEventArgs.Selection.get -> TKCode.Arc.Editor.TextSelection +TKCode.Arc.CodeEditorCursorSelectionChangedEventArgs.Source.get -> string! diff --git a/src/TKCode.Arc/Content/ArcWindow/ArcWindow.cs b/src/TKCode.Arc/Content/ArcWindow/ArcWindow.cs index 1fc241aa7..5a31933d0 100644 --- a/src/TKCode.Arc/Content/ArcWindow/ArcWindow.cs +++ b/src/TKCode.Arc/Content/ArcWindow/ArcWindow.cs @@ -83,6 +83,15 @@ public class ArcWindow : AvaloniaWindow, IArcContentOverlayWindow public static readonly StyledProperty UnavailableCaptionButtonsProperty = WindowCaptionButtons.UnavailableCaptionButtonsProperty.AddOwner(); + /// + /// The shape of the drawn caption buttons: Round (the default) or Rectangle, flat buttons as tall as the + /// title bar that touch the window's right edge, as on Windows (#230). Inherited by the window's + /// and any in it. The macOS traffic lights are the + /// system's and stay as they are. + /// + public static readonly StyledProperty CaptionButtonVariantProperty = + WindowCaptionButtons.CaptionButtonVariantProperty.AddOwner(); + /// /// An empty theme for the decorations Avalonia draws where the system leaves them to the app (Windows, Linux): it draws /// no title bar of its own, so the Arc bar is the only one. The resize grips are not part of the theme and stay. @@ -137,6 +146,13 @@ public UnavailableCaptionButtonMode UnavailableCaptionButtons set => SetValue(UnavailableCaptionButtonsProperty, value); } + /// + public CaptionButtonVariant CaptionButtonVariant + { + get => GetValue(CaptionButtonVariantProperty); + set => SetValue(CaptionButtonVariantProperty, value); + } + ArcOverlayHost? IArcContentOverlayWindow.ActiveContentOverlayHost => ShouldHostModalsInContent ? _contentOverlayHost : null; diff --git a/src/TKCode.Arc/Content/ArcWindow/Themes/TitleBarTheme.axaml b/src/TKCode.Arc/Content/ArcWindow/Themes/TitleBarTheme.axaml index 93a362258..62156d0f0 100644 --- a/src/TKCode.Arc/Content/ArcWindow/Themes/TitleBarTheme.axaml +++ b/src/TKCode.Arc/Content/ArcWindow/Themes/TitleBarTheme.axaml @@ -178,6 +178,22 @@ + + + + + diff --git a/src/TKCode.Arc/Content/ArcWindow/Themes/WindowCaptionButtonsTheme.axaml b/src/TKCode.Arc/Content/ArcWindow/Themes/WindowCaptionButtonsTheme.axaml index da2d7d70a..0e56d3403 100644 --- a/src/TKCode.Arc/Content/ArcWindow/Themes/WindowCaptionButtonsTheme.axaml +++ b/src/TKCode.Arc/Content/ArcWindow/Themes/WindowCaptionButtonsTheme.axaml @@ -4,6 +4,8 @@ themed here: ghost squares, close turns danger under the pointer. Sizes come from the control (ButtonSize, GlyphSize, ...) so a TitleBar's tokens reach them; the maximize glyph and its size follow the window state in code, and so do whether minimize and maximize show (ShowMinimizeButton / ShowMaximizeButton and UnavailableCaptionButtons, issue #130). + CaptionButtonVariant="Rectangle" (#230) makes them flat, bar-high rectangles; the control computes the sizes the template + binds (ButtonWidth / ButtonHeight / ButtonGap) and the buttons' own theme handles the colors. --> + + + + + + + + + + + + + + + + """); + var window = TestHelpers.Show(control); + try + { + var first = (SegmentedItem)control.ContainerFromIndex(0)!; + var second = (SegmentedItem)control.ContainerFromIndex(1)!; + first.Tag.ShouldBe("styled"); + second.Tag.ShouldBe("styled"); + second.Opacity.ShouldBe(0.5); + first.Opacity.ShouldBe(1); + } + finally + { + window.Close(); + } + } + + [AvaloniaFact] + public void The_Selected_Brushes_Can_Be_Set_In_Xaml() + { + var control = AvaloniaRuntimeXamlLoader.Parse(""" + + + + + """); + var window = TestHelpers.Show(control); + try + { + TestHelpers.ColorOf(TestHelpers.Descendant(control).Fill).ShouldBe(Color.Parse("#5B6B8C")); + TestHelpers.ColorOf(((SegmentedItem)control.ContainerFromIndex(0)!).Foreground).ShouldBe(Color.Parse("#FFD166")); + } + finally + { + window.Close(); + } + } } diff --git a/tests/TKCode.Arc.Tests/Toggles/ToggleGroupTests.cs b/tests/TKCode.Arc.Tests/Toggles/ToggleGroupTests.cs index 47ca6d5c0..88a181bf2 100644 --- a/tests/TKCode.Arc.Tests/Toggles/ToggleGroupTests.cs +++ b/tests/TKCode.Arc.Tests/Toggles/ToggleGroupTests.cs @@ -656,4 +656,104 @@ public void The_Press_Comes_From_The_Item_Tokens() } public sealed record ViewChoice(string Key, string Title, Geometry Glyph); + + [AvaloniaFact] + public void SelectedItemBackground_Fills_The_Gliding_Indicator_And_Falls_Back_When_Cleared() + { + var group = Group(); + group.SelectedValue = "b"; + var window = TestHelpers.Show(group); + try + { + var indicator = TestHelpers.Descendant(group, "PART_Selection"); + var themed = indicator.Fill; + themed.ShouldBeAssignableTo(); + + var custom = new SolidColorBrush(Color.Parse("#5B6B8C")); + group.SelectedItemBackground = custom; + Dispatcher.UIThread.RunJobs(); + indicator.Fill.ShouldBeSameAs(custom); + + group.ClearValue(ToggleGroup.SelectedItemBackgroundProperty); + Dispatcher.UIThread.RunJobs(); + ((ISolidColorBrush)indicator.Fill!).Color.ShouldBe(((ISolidColorBrush)themed!).Color); + } + finally + { + window.Close(); + } + } + + [AvaloniaFact] + public void SelectedItemBackground_Fills_Every_Pressed_Option_In_Multiple_Mode() + { + var group = Group(ToggleGroupMode.Multiple); + var custom = new SolidColorBrush(Color.Parse("#5B6B8C")); + group.SelectedItemBackground = custom; + group.SelectedValues = ["a", "c"]; + var window = TestHelpers.Show(group); + try + { + TestHelpers.Descendant(Item(group, 0), "PART_Fill").PressedFill.ShouldBeSameAs(custom); + TestHelpers.Descendant(Item(group, 2), "PART_Fill").PressedFill.ShouldBeSameAs(custom); + } + finally + { + window.Close(); + } + } + + [AvaloniaFact] + public void SelectedItemForeground_Colors_Only_The_Selected_Option_And_Holds_Over_Hover() + { + var group = Group(); + group.SelectedValue = "b"; + var window = TestHelpers.Show(group); + try + { + var themedSelected = Item(group, 1).Foreground; + var themedOther = Item(group, 0).Foreground; + var custom = new SolidColorBrush(Color.Parse("#FFD166")); + group.SelectedItemForeground = custom; + Dispatcher.UIThread.RunJobs(); + Item(group, 1).Foreground.ShouldBeSameAs(custom); + Item(group, 0).Foreground.ShouldBeSameAs(themedOther); + + group.SelectedValue = "c"; + Dispatcher.UIThread.RunJobs(); + Item(group, 2).Foreground.ShouldBeSameAs(custom); + Item(group, 1).Foreground.ShouldNotBeSameAs(custom); + + group.ClearValue(ToggleGroup.SelectedItemForegroundProperty); + Dispatcher.UIThread.RunJobs(); + Item(group, 2).Foreground.ShouldNotBeSameAs(custom); + ((ISolidColorBrush)Item(group, 2).Foreground!).Color.ShouldBe(((ISolidColorBrush)themedSelected!).Color); + } + finally + { + window.Close(); + } + } + + [AvaloniaFact] + public void The_Selected_Brushes_Can_Be_Set_In_Xaml() + { + var group = AvaloniaRuntimeXamlLoader.Parse(""" + + + + + """); + var window = TestHelpers.Show(group); + try + { + TestHelpers.ColorOf(TestHelpers.Descendant(group, "PART_Selection").Fill).ShouldBe(Color.Parse("#5B6B8C")); + TestHelpers.ColorOf(Item(group, 0).Foreground).ShouldBe(Color.Parse("#FFD166")); + } + finally + { + window.Close(); + } + } } From 0b3a73fd844b4ba87aa0ee9639ebea006968c036 Mon Sep 17 00:00:00 2001 From: token <61819790+239573049@users.noreply.github.com> Date: Tue, 1 Dec 2026 04:20:07 -0500 Subject: [PATCH 2/3] feat(selects): Ghost variant for Select, Combobox, MultiSelect and DropdownMenu; DropdownMenu.Size - FieldVariant (Outline default | Ghost) in Core; Variant on Select (defined there), Combobox, MultiSelect and DropdownMenu (#229). Ghost: no border, fill or minimum width at rest, SurfaceMuted on hover / press / open, focus ring kept, GhostTriggerPadding token (8, compact 6). - DropdownMenu.Size for its built-in trigger, defaulting to Small so the look does not change; Ghost uses ButtonVariant.Ghost for that trigger. - Fix: the select triggers' resting fill, border and padding were local values on the template parts, so the theme's hover, pressed, open and focus styles never applied; they are style setters now. - ControlSize docs: heights depend on density. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 8 + src/TKCode.Arc.Core/Primitives/ControlSize.cs | 5 +- .../Primitives/FieldVariant.cs | 14 + src/TKCode.Arc.Core/PublicAPI.Unshipped.txt | 3 + .../Menus/DropdownMenu/DropdownMenu.cs | 49 ++- .../Themes/DropdownMenuTheme.axaml | 15 +- src/TKCode.Arc/PublicAPI.Unshipped.txt | 25 ++ src/TKCode.Arc/Selects/Combobox/Combobox.cs | 25 +- .../Selects/Combobox/ComboboxToken.cs | 5 + .../Combobox/Themes/ComboboxTheme.axaml | 44 ++- .../Selects/MultiSelect/MultiSelect.cs | 25 +- .../Selects/MultiSelect/MultiSelectToken.cs | 5 + .../MultiSelect/Themes/MultiSelectTheme.axaml | 52 ++- src/TKCode.Arc/Selects/Select/Select.cs | 28 +- src/TKCode.Arc/Selects/Select/SelectToken.cs | 5 + .../Selects/Select/Themes/SelectTheme.axaml | 40 +- tests/Baselines/tokens/dark-amber.json | 3 + tests/Baselines/tokens/dark-blue.json | 3 + tests/Baselines/tokens/dark-coral.json | 3 + .../Baselines/tokens/dark-corners-square.json | 3 + tests/Baselines/tokens/dark-default.json | 3 + tests/Baselines/tokens/dark-green.json | 3 + tests/Baselines/tokens/dark-neutral.json | 3 + tests/Baselines/tokens/dark-orange.json | 3 + tests/Baselines/tokens/dark-pixel.json | 3 + tests/Baselines/tokens/dark-rose.json | 3 + tests/Baselines/tokens/dark-seed-0562EF.json | 3 + tests/Baselines/tokens/dark-seed-E5484D.json | 3 + tests/Baselines/tokens/dark-violet.json | 3 + tests/Baselines/tokens/light-amber.json | 3 + tests/Baselines/tokens/light-blue.json | 3 + tests/Baselines/tokens/light-coral.json | 3 + .../tokens/light-corners-scale-0.5.json | 3 + .../tokens/light-corners-scale-2.json | 3 + .../tokens/light-corners-square.json | 3 + .../tokens/light-default-motion-off.json | 3 + .../tokens/light-default-motion-reduced.json | 3 + tests/Baselines/tokens/light-default.json | 3 + tests/Baselines/tokens/light-green.json | 3 + tests/Baselines/tokens/light-neutral.json | 3 + tests/Baselines/tokens/light-orange.json | 3 + tests/Baselines/tokens/light-pixel.json | 3 + tests/Baselines/tokens/light-rose.json | 3 + tests/Baselines/tokens/light-seed-0562EF.json | 3 + tests/Baselines/tokens/light-seed-E5484D.json | 3 + tests/Baselines/tokens/light-violet.json | 3 + .../Selects/SelectVariantTests.cs | 342 ++++++++++++++++++ 47 files changed, 759 insertions(+), 21 deletions(-) create mode 100644 src/TKCode.Arc.Core/Primitives/FieldVariant.cs create mode 100644 tests/TKCode.Arc.Tests/Selects/SelectVariantTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index bc086fcf6..30a934e56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,14 @@ Also, as previews that follow the main version: `TKCode.Arc.Graphics3D`, `TKCode - `ToggleGroup` and `SegmentedControl` (`TKCode.Arc`): `SelectedItemBackground` and `SelectedItemForeground` brush properties set the fill behind the selected option (the gliding fill, or each pressed option's fill in multiple mode; the segmented pill) and its text and icon color, per instance or from a style, for backgrounds where the default selected look is hard to tell from the other options. Null (cleared) keeps the theme's colors (#227). - `SegmentedItem` (`TKCode.Arc`) is now public (it was internal), so that XAML styles and Rider's checks can address `arc|SegmentedItem` (#231). It has a token of its own, `SegmentedItemToken` (identity `SegmentedControlItem`, because `TKCode.Arc.Mobile` already has a `SegmentedItem` token). `TKCode.Arc.Mobile` also has a `SegmentedItem` in its own namespace; a C# file that imports both namespaces aliases one side. - `ArcWindow` / `TitleBar` / `WindowCaptionButtons` (`TKCode.Arc`): `CaptionButtonVariant` (`Round` | `Rectangle`, #230) picks the shape of the drawn caption buttons. `Round` is the default and the look is unchanged; `Rectangle` draws Windows-style flat buttons that are as tall as the title bar, 46 wide (`WindowCaptionButtonsToken.RectangleButtonWidth`), touch each other and the window's right edge (`TitleBarToken.RectangleButtonsPadding`), with square corners, and the close button turns solid danger under the pointer. Inherited, so setting it on the `ArcWindow` is enough; it applies only while the bar draws the buttons (`CaptionButtons` `Drawn`, or `Auto` outside macOS). New token entries: `WindowCaptionButtonsTokenKind.RectangleButtonWidth` and `RectangleCloseHoverForeground`, `TitleBarTokenKind.RectangleButtonsPadding`. +- `FieldVariant` (`TKCode.Arc.Core`, `Outline` default | `Ghost`) and a `Variant` property on `Select`, `Combobox`, `MultiSelect` and `DropdownMenu` ([#229](https://github.com/AIDotNet/TKCode.Arc/issues/229)). `Ghost` drops the border, the fill and the minimum width of the trigger at rest (the border keeps its thickness, so nothing shifts) and fills with `SurfaceMuted` on hover, press and open; the keyboard focus ring stays. It has a tighter horizontal padding from the new token property `GhostTriggerPadding` (8, compact 6) on `SelectToken`, `ComboboxToken` and `MultiSelectToken`. On `DropdownMenu` the built-in trigger button uses `ButtonVariant.Ghost` instead of `Secondary`. The defaults are unchanged (`Outline`). +- `DropdownMenu.Size` (`ControlSize`) for the built-in trigger button. Its default is `Small`, not `Medium` as on most controls, because the trigger was always a small button, so the default look does not change. +- The select triggers get the `:ghost` and `:raised` pseudo-classes (`Select.GhostPseudoClass`, `Select.RaisedPseudoClass`, and the same constants on `Combobox`, `MultiSelect`; `DropdownMenu.GhostPseudoClass`). + +### Fixed + +- `Select`, `Combobox` and `MultiSelect`: the resting fill, border (and padding, chevron margin and chip fill on `MultiSelect`) of the trigger were written as local values on the template, so the hover, pressed, open, focus and `:has-selection` styles of the theme could not override them. They are style setters now and those states show as the theme declares them (a `SurfaceMuted` fill and a stronger border while hovered, pressed or open; room for the clear button on a `MultiSelect` with a selection). +- The `ControlSize` documentation said the heights are 36 / 44 / 50; they depend on the density (36 / 44 / 50, or 28 / 34 / 40 with `ArcDensity.Compact`). Documentation only. ## 1.3.3-preview.2 - 2026-12-01 diff --git a/src/TKCode.Arc.Core/Primitives/ControlSize.cs b/src/TKCode.Arc.Core/Primitives/ControlSize.cs index 4505983f7..98bac6060 100644 --- a/src/TKCode.Arc.Core/Primitives/ControlSize.cs +++ b/src/TKCode.Arc.Core/Primitives/ControlSize.cs @@ -1,6 +1,9 @@ namespace TKCode.Arc; -/// Arc control sizes: heights 36 / 44 / 50 (ControlSizeSM / MD / LG). +/// +/// Arc control sizes (ControlSizeSM / MD / LG). The heights depend on the density: 36 / 44 / 50 by default and +/// 28 / 34 / 40 with ArcDensity.Compact. +/// public enum ControlSize { Small, diff --git a/src/TKCode.Arc.Core/Primitives/FieldVariant.cs b/src/TKCode.Arc.Core/Primitives/FieldVariant.cs new file mode 100644 index 000000000..3fa8be20c --- /dev/null +++ b/src/TKCode.Arc.Core/Primitives/FieldVariant.cs @@ -0,0 +1,14 @@ +namespace TKCode.Arc; + +/// Chrome of a select-like field's trigger (Select, Combobox, MultiSelect, DropdownMenu). +public enum FieldVariant +{ + /// A bordered box on the surface color: Arc's look (default). + Outline, + + /// + /// No border and no fill at rest: the value and the chevron only, as wide as the content needs. Hover, press and open show + /// a muted fill; the keyboard focus ring stays visible. + /// + Ghost +} diff --git a/src/TKCode.Arc.Core/PublicAPI.Unshipped.txt b/src/TKCode.Arc.Core/PublicAPI.Unshipped.txt index 7dc5c5811..4589f615e 100644 --- a/src/TKCode.Arc.Core/PublicAPI.Unshipped.txt +++ b/src/TKCode.Arc.Core/PublicAPI.Unshipped.txt @@ -1 +1,4 @@ #nullable enable +TKCode.Arc.FieldVariant +TKCode.Arc.FieldVariant.Ghost = 1 -> TKCode.Arc.FieldVariant +TKCode.Arc.FieldVariant.Outline = 0 -> TKCode.Arc.FieldVariant diff --git a/src/TKCode.Arc/Menus/DropdownMenu/DropdownMenu.cs b/src/TKCode.Arc/Menus/DropdownMenu/DropdownMenu.cs index 0831c3d42..c4e8231e2 100644 --- a/src/TKCode.Arc/Menus/DropdownMenu/DropdownMenu.cs +++ b/src/TKCode.Arc/Menus/DropdownMenu/DropdownMenu.cs @@ -33,13 +33,14 @@ namespace TKCode.Arc; /// [TemplatePart(TriggerPart, typeof(AvaloniaButton))] [TemplatePart(TriggerPresenterPart, typeof(ContentPresenter))] -[PseudoClasses(OpenPseudoClass, CustomTriggerPseudoClass)] +[PseudoClasses(OpenPseudoClass, CustomTriggerPseudoClass, GhostPseudoClass)] public class DropdownMenu : TemplatedControl { public const string TriggerPart = "PART_Trigger"; public const string TriggerPresenterPart = "PART_TriggerPresenter"; public const string OpenPseudoClass = ":open"; public const string CustomTriggerPseudoClass = ":custom-trigger"; + public const string GhostPseudoClass = ":ghost"; #region 公共属性定义 @@ -68,6 +69,21 @@ public class DropdownMenu : TemplatedControl public static readonly StyledProperty TriggerProperty = AvaloniaProperty.Register(nameof(Trigger)); + /// + /// Chrome of the built-in trigger button: Outline (default) keeps the bordered ButtonVariant.Secondary look, Ghost + /// uses ButtonVariant.Ghost (no border or fill at rest). A custom is not affected. + /// + public static readonly StyledProperty VariantProperty = + Select.VariantProperty.AddOwner(); + + /// + /// Size of the built-in trigger button. The default is (not Medium as for most controls), + /// which is what the trigger always used, so the look does not change; set Medium or Large for a taller trigger. + /// A custom is not affected. + /// + public static readonly StyledProperty SizeProperty = + Field.SizeProperty.AddOwner(new StyledPropertyMetadata(ControlSize.Small)); + /// Whether the menu is open (two-way). public static readonly StyledProperty IsOpenProperty = AvaloniaProperty.Register(nameof(IsOpen), defaultBindingMode: BindingMode.TwoWay); @@ -131,6 +147,20 @@ public object? Trigger set => SetValue(TriggerProperty, value); } + /// + public FieldVariant Variant + { + get => GetValue(VariantProperty); + set => SetValue(VariantProperty, value); + } + + /// + public ControlSize Size + { + get => GetValue(SizeProperty); + set => SetValue(SizeProperty, value); + } + /// public bool IsOpen { @@ -274,6 +304,7 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { SetMenu(_trigger, Menu); AutomationProperties.SetName(_trigger, Label); + ApplyTriggerVariant(); } _triggerPresenter?.PropertyChanged += OnTriggerPresenterPropertyChanged; AttachCustomTrigger(_triggerPresenter?.Child); @@ -289,10 +320,24 @@ protected override void OnLoaded(RoutedEventArgs e) } } + private void ApplyTriggerVariant() + { + // The built-in trigger is a template part, so its variant is synced here: a style setter would lose to the template's value. + if (_trigger is Button button) + { + button.Variant = Variant == FieldVariant.Ghost ? ButtonVariant.Ghost : ButtonVariant.Secondary; + } + } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); - if (change.Property == IsOpenProperty) + if (change.Property == VariantProperty) + { + PseudoClasses.Set(GhostPseudoClass, change.GetNewValue() == FieldVariant.Ghost); + ApplyTriggerVariant(); + } + else if (change.Property == IsOpenProperty) { var open = change.GetNewValue(); PseudoClasses.Set(OpenPseudoClass, open); diff --git a/src/TKCode.Arc/Menus/DropdownMenu/Themes/DropdownMenuTheme.axaml b/src/TKCode.Arc/Menus/DropdownMenu/Themes/DropdownMenuTheme.axaml index 8722934e9..c06038ce9 100644 --- a/src/TKCode.Arc/Menus/DropdownMenu/Themes/DropdownMenuTheme.axaml +++ b/src/TKCode.Arc/Menus/DropdownMenu/Themes/DropdownMenuTheme.axaml @@ -10,7 +10,7 @@ @@ -68,6 +68,19 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +