* Reminder for future me.
* Move compiled bindings to Avalonia base.
* Update suppressions.
* Tweaked `BindingExpressionVisitor`.
And documented public (internal) API.
* Fix ncrunch config.
* Add comprehensive unit tests for BindingExpressionVisitor.
Tests cover all supported features including property access, indexers,
AvaloniaProperty access, logical NOT, stream bindings, and type operators.
Also includes tests for unsupported operations that should throw exceptions.
Discovered bug: IsAssignableFrom check at line 139 is backwards, causing
upcasts to incorrectly throw and downcasts to be incorrectly ignored.
Bug is documented with skipped tests showing expected behavior and passing
tests documenting current broken behavior.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Fix backwards IsAssignableFrom check in BindingExpressionVisitor.
Fixed the inheritance cast check which was inverted, causing:
- Upcasts (derived→base) to incorrectly throw exceptions
- Downcasts (base→derived) to be incorrectly ignored
Changed line 139 from:
node.Operand.Type.IsAssignableFrom(node.Type)
to:
node.Type.IsAssignableFrom(node.Operand.Type)
This correctly identifies safe upcasts (which are ignored) vs unsafe
downcasts (which throw exceptions).
Updated tests to remove skip attributes and removed the temporary tests
that documented the broken behavior. All 33 tests now pass.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Allow downcasts and all reference type casts in binding expressions.
Changed the cast handling to allow both upcasts and downcasts for reference
types. This makes casts actually useful in binding expressions while still
rejecting value type conversions that would require actual conversion logic.
The logic now checks:
- Both types must be reference types (not value types)
- One type must be assignable to/from the other (either direction)
This allows practical scenarios like:
- Upcasts: (BaseClass)derived
- Downcasts: (DerivedClass)baseInstance
- Casting through object: (TargetType)(object)source
The binding system will gracefully handle any runtime type mismatches.
Updated tests to verify downcasts are allowed and added test for casting
through object pattern.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Add clarifying test for cast transparency in binding expressions.
Added test showing that casts are transparent in property chains.
For example, ((TestClass)x).Property produces just one node for the
property access - the cast doesn't create additional nodes.
This clarifies that empty nodes for (TestClass)x is correct behavior:
- Empty nodes = bind to source directly
- The cast is just a type annotation, transparent to the binding path
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Fix: Casts should create ReflectionTypeCastNode, not be transparent.
Casts were incorrectly being treated as transparent (not creating nodes),
but CompiledBindingPath uses TypeCastPathElement which creates FuncTransformNode.
For consistency and correctness, BindingExpressionVisitor should also create
cast nodes using ReflectionTypeCastNode.
Changes:
- Convert expressions now create ReflectionTypeCastNode
- TypeAs expressions now create ReflectionTypeCastNode
- Both upcasts and downcasts create nodes (runtime checks handle failures)
Examples:
- x => (TestClass)x → 1 node (cast)
- x => ((TestClass)x).Prop → 2 nodes (cast + property)
- x => x.Child as object → 2 nodes (property + cast)
This matches the behavior of CompiledBindingPathBuilder.TypeCast<T>().
Updated all related tests to verify cast nodes are created.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Use compiled cast functions instead of reflection-based type checks.
Changed from ReflectionTypeCastNode (which uses Type.IsInstanceOfType) to
FuncTransformNode with a compiled cast function. This matches how
CompiledBindingPath handles TypeCastPathElement and provides better
performance by avoiding reflection.
The CreateCastFunc method compiles an expression `(object? obj) => obj as T`
which generates efficient IL similar to the 'is T' pattern used in
TypeCastPathElement<T>, rather than using reflection-based type checks.
Performance improvement:
- Before: Type.IsInstanceOfType() reflection call for each cast
- After: Compiled IL using 'as' operator (same as TypeCastPathElement<T>)
Updated tests to expect FuncTransformNode instead of ReflectionTypeCastNode.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Reuse TypeCastPathElement<T> cast function directly.
Instead of compiling our own cast expression, extract the Cast delegate
directly from TypeCastPathElement<T>. This ensures we use the exact same
code path as CompiledBindingPath, avoiding any potential behavioral
differences and code duplication.
Benefits:
- Code reuse - single implementation of cast logic
- Consistency - same behavior as CompiledBindingPathBuilder.TypeCast<T>()
- No duplicate expression compilation logic
Implementation uses reflection to create the closed generic type and
extract the pre-compiled Cast delegate, which is still more efficient
than reflection-based type checks at runtime.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Revert to lambda compilation approach for cast functions.
Reverted from using TypeCastPathElement<T> back to compiling lambda
expressions directly. The lambda approach is cleaner and more straightforward:
Benefits of lambda compilation:
- No Activator.CreateInstance call (avoids reflection for construction)
- More direct - creates exactly what we need
- No coupling to TypeCastPathElement internal implementation
- Simpler code flow
The compiled lambda generates the same efficient IL code (using 'as' operator)
as TypeCastPathElement<T> does, just without the indirection.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Code cleanup: Fix XML docs and remove unused usings.
- Changed CreateCastFunc XML docs to use <remarks> tag for better formatting
- Removed unused 'using Avalonia.Data;' from tests
- Removed redundant '#nullable enable' from tests
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Refactor BindingExpressionVisitor to use CompiledBindingPathBuilder.
Changes BindingExpressionVisitor to use CompiledBindingPathBuilder instead of
directly creating ExpressionNode instances, unifying the approach with
compile-time XAML bindings. Adds new BuildPath() method that returns
CompiledBindingPath, while maintaining backwards compatibility through the
existing BuildNodes() wrapper method.
Key changes:
- Replace internal List<ExpressionNode> with CompiledBindingPathBuilder
- Refactor all visitor methods to call builder methods
- Add accessor factory methods and implementations for property access
- Support AvaloniaProperty, CLR properties, arrays, indexers, streams, casts
- Update tests to expect PropertyAccessorNode, StreamNode, ArrayIndexerNode
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Remove CompiledBindingPathFromExpressionBuilder in favor of BindingExpressionVisitor.
Now that BindingExpressionVisitor has been refactored to use
CompiledBindingPathBuilder and provides a BuildPath() method, the test-only
CompiledBindingPathFromExpressionBuilder class is redundant and can be removed.
Changes:
- Replace CompiledBindingPathFromExpressionBuilder.Build() with
BindingExpressionVisitor<TIn>.BuildPath() in BindingExpressionTests
- Delete CompiledBindingPathFromExpressionBuilder.cs test file
- All 122 tests in BindingExpressionTests.Compiled continue to pass
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Move test-only methods from production code to test extensions.
Removes BindingExpression.Create and BindingExpressionVisitor.BuildNodes
from production code since they were only used by unit tests.
Changes:
- Remove BindingExpression.Create<TIn, TOut> method
- Remove BindingExpressionVisitor.BuildNodes method
- Add BindingExpressionVisitorExtensions in Base.UnitTests
- Add BindingExpressionExtensions in LeakTests
- Add static helper methods in test classes to reduce noise
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Add public CompiledBinding.Create factory methods from LINQ expressions.
Adds two static factory methods to create CompiledBinding instances from
lambda expressions using BindingExpressionVisitor.BuildPath():
- Create<TIn, TOut>(expression, converter, mode)
Creates binding without explicit source (uses DataContext)
- Create<TIn, TOut>(source, expression, converter, mode)
Creates binding with explicit source
This provides a type-safe, ergonomic API for creating compiled bindings
from code without string-based paths.
Usage:
var binding = CompiledBinding.Create(viewModel, vm => vm.Title);
textBlock.Bind(TextBlock.TextProperty, binding);
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* Merge CompiledBinding.Create overloads and add all binding property parameters.
Consolidates the two Create method overloads into a single method with source
as an optional parameter. Adds optional parameters for all CompiledBinding
properties (priority, converterCulture, converterParameter, fallbackValue,
stringFormat, targetNullValue, updateSourceTrigger, delay) per PR feedback.
Properties that default to AvaloniaProperty.UnsetValue (source, fallbackValue,
targetNullValue) use null-coalescing to convert null parameter values.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
* (Re-)update suppressions.
* Add missing using.
* Fix ncrunch build.
* Remove file I committed by accident.
* PR feedback.
* Store static members outside generic class.
Based on PR feedback.
---------
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
* Introduce a universal IGlyphTypeface implementation that does not rely on any platform implementation
* Revert changes
* Fix Android
* Make the test happy
* Fix build
* Update baseline
* Fix naming
* Fix headless
* Move interfaces to dedicated files
Make GlyphTypeface.GlyphCount an integer
* Fix GlyphCount
* Make IGlyphTypeface NotClientImplementable
* Make sure we cache platform typefaces by their desired name, style, weight and stretch
* Update baseline
* Only use IGlyphTypeface
* Fix Android
* Try to clear the buffer before we encode somethimg
* Add needed test font
* Add more unit tests
* Reduce allocations
* Remove Direct2D1 test files
* More tests
* More complete table implementations
* More adjustments
* Use batch APIs
* Handle invalid timestamps
* Update baseline
* Introduce a CharacterToGlyphMap struct for faster access
* Remove AggressiveInlining
* Remove AggressiveInlining
* Make the head table optional for legacy fonts
* Remove Load method. Fix TextBlockTests
* Fix nullables
* Remove redundant folder
* Update Api baseline
* revert diff helper changes
* revert changes
* Use bare minimum font for Headless platform and introduce a test font manager that uses the Inter font for testing.
* Add missing font file for Headless platform
---------
Co-authored-by: Gillibald <stebner@avaloniaui.net>
Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
Make LeafNode property nullable and return null when the binding expression
has no nodes (e.g., when using a constant source with a converter but no path).
This fixes an ArgumentOutOfRangeException that occurred when accessing
LeafNode or Description properties on such bindings.
Fixes#20441
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
* Update to xunit.v3
* Little more progress
* More fixes
* Keep VSTest supported
* Adjust Nuke
* Few fixes
* Fix for xunit 2
* Fix GetData override
* Adjust
* Use MTP for xunit 2
* Fix test
* Better fix
* --no-progress
* Few more fixes
* no progress
* Fix test
* Better fix
* TRX
* Move to Directory.Build.props
* Unify on MTP v2
* Update
* Update to stable
* 1.0.1
* 1.0.2
* Fix some warnings
* Fix more warnings
* Fix more warnings
* Update ncrunch config.
* Tidy up reflection and multi-binding APIs:
- Move `BindingBase` and `MultiBinding` into Avalonia.Base
- `BindingBase` becomes a true base class for all bindings, and contains only the `Instance` method
- Properties common between reflection and compiled bindings are moved into `StandardBindingBase`
- `Binding` is moved to Avalonia.Base and renamed to `ReflectionBinding`
- A compatibility shim for `Binding` remains in Avalonia.Markup
- Remove `IBinding` and `IBinding2`
- Remove `ITreeDataTemplate's usage of `InstancedBinding`
- Remove `NativeMenuBarPresenter`s usage of `InstancedBinding`
- Remove `InstancedBinding` as it is now unused
This required an update to the DataGrid submodule: cell data validation has been temporarily removed as this used `InstancedBinding`.
* `Instance()` => `CreateInstance()`.
The use of "Instance" as a verb is quite unusual apparently ;)
* Seal classes where appropriate.
* Seal classes where appropriate.
* Remove `StandardBindingBase`.
Simply duplicate the members in reflection and compiled binding classes.
* Delete deleted submodule directory.
* Add missing attribute.
Fixes compile error.
* Fix reference to removed class.
* Update suppressions.
* Enable nullability in UnitTests
* Enable nullability in Base.UnitTests
* Enable nullability in Markup.UnitTests
* Enable nullability in Markup.Xaml.UnitTests
* Add support for parsing BoxShadows with colors expressions with parentheses
* Added BoxShadowsTests
* BoxShadow parsing should also respect parentheses
* Add StringSplitter
* Fix test
* Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* StringSplitter should not accept same bracket pairs
* Returns empty array only when input is null
* Add StringSplitterTests
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Reduce allocations for FontFamily.Parse
* Turn Trie data into static data
* Use static lambda in s_cachedLanguage.GetOrAdd
* Make LineBreakEnumerator+LineBreakState a ref struct to avoid allocations
* Use backing field for storing trie data in debug builds
* Fixed invalid renering of controls with non-zero bounds position.
Fixed text clipping of text elements inside LayoutTransformControl.
* Fixed rendering of VisualBrush.
* Fixed control clipping.
* Fixed opacity mask state.
* Reworked to skip rendering of elements out of clip.
* Code cleanup.
* Added unit test.
* Parameters order.
---------
Co-authored-by: PleshakovDV <pleshakovdv@ugpa.ru>
* Improve FontCollection user story
* Make adjustments after review
* Refactor IsFontFile
* Make FontFamilyLoader internal
Make tests happy again
* Update baseline
* Adjust modifier
---------
Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
* Fixed Selector.ValidateNestingSelector not calling overrides when iterating up through parent selectors
Changed ValidateNestingSelector to call recursively, walking up
the hierarchy of parent selectors. This means function overrides
are taken into account when walking up the tree. Now if a
selector has an OrSelector as its parent, it doesn't throw an
exception.
* Updated ToString methods to surround OrSelectors with parenthesis where useful
* Fix ClassBindingManager property dictionary insert
* Prepend ClassPropertyPrefix to key when inserting into registered properties dictionary to be consistent with lookup.
* Added ClassBindingManager unit tests
* Add unit test for all color format Brush parsing
* Support parsing all known color formats in Brush.Parse()
* Update property syntax
* Remove unnecessary usings
* Use an actual hex color
* Fix new brush test
* Switch order of brush parsing so known color optimizations are used
* Add more documentation comments to KnownColors
* Fix displayed text in BorderPage
InnerBorderEdge not InnerBorder
* Capture ExecutionContext for Dispatcher.InvokeAsync
(cherry picked from commit e41c9272ac45afc31007a8fe26f9f56291b063ef)
* Implement CulturePreservingExecutionContext
* Add IsFlowSuppressed checking
* Add NET6_0_OR_GREATER because only the Restore need it.
* Use `ExecutionContext.Run` instead of `ExecutionContext.Restore`.
* Pass this to avoid lambda capture.
* Use ExecutionContext directly on NET6_0_OR_GREATER
* on NET6_0_OR_GREATER, use Restore so we can get a simple stack trace.
* Add unit tests.
* All test code must run inside Task.Run to avoid interfering with the test
* First, test Task.Run to ensure that the preceding validation always passes, serving as a baseline for the subsequent Invoke/InvokeAsync tests.
This way, if a later test fails, we have the .NET framework's baseline behavior for reference.
* update mouse test to better simulate clicks on captured controls
* add tap failing test
* use captured element if available as source for tap gestures
* implement pre events for focus change
* add cancelling api to focus change
* add overload for SetFocusedElement
* add focus redirection
* update with api change requests
* fix height queries not matching when container name is set, remove unused query grammer
* add unit test for height container queries with name
---------
Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
* Update ncrunch config.
* Fix test naming.
* Add BindingExpressionGrammar tests.
We only had tests for the error states, and that was named incorrectly.
* Parse null-conditionals in bindings.
* Initial impl of null conditionals in binding path.
Fixes#17029.
* Ensure that nothing is logged.
* Make "?." work when binding to methods.
* Don't add a new public API.
And add a comment reminding us to make this class internal for 12.0.
* Use existing method.
* search for visual parents when hittesting
* Add unit test for hit testing on disabled visual
---------
Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
* Improve ToString() output in case when Key is set to Key.None
* Update KeyGesture.Parse method to support only modifiers combinations
* Update tests with empty combination and modifiers-only combinations
* Send dispose command for CompositionTarget as an OOB batch
* Make Close_Should_Remove_PointerOver to provide some render interface stub
* why ins't reactive stuff using our headless testing?
* fix?
---------
Co-authored-by: Dan Walmsley <dan@walms.co.uk>
* Fix SlicedStream.Seek(offset, SeekOrigin.End)
This fixes https://github.com/AvaloniaUI/Avalonia/issues/13604
The offset _from is applied in set_Position, so applying it also in Seek mispositions the stream. ZipArchive exposes the problem by seeking to the end to read the table of contents.
* Add tests for SlicedStream
---------
Co-authored-by: Julien Lebosquain <julien@lebosquain.net>