Skip to content

NsTextView.ShouldChangeText throws ArgumentNullExceptionΒ #24098

Description

@tipa

Apple platform

macOS

Framework version

net10.0-*

Affected platform version

macOS 26.0.1 - net10-rc2

Description

When subclassing ShouldChangeText of NSTextView, sometimes null is provided as replacementString. The Apple docs say: ... If only text attributes are being changed, replacementString is nil.

The .NET bindings appear to null-check the parameter and throw an ArgumentNullException when calling the base-method in a subclass:

System.ArgumentNullException: Value cannot be null. (Parameter 'replacementString')

Steps to Reproduce

public partial class ViewController : NSViewController
{
    public override void LoadView()
    {
        var tv = new DelegateDrivenUndoTextView()
        {
            AutoresizingMask = NSViewResizingMask.WidthSizable | NSViewResizingMask.HeightSizable,
            Value = "test"
        };
        View = new NSScrollView { DocumentView = tv };

        tv.SelectAll(this);
        var delay = new CoreFoundation.DispatchTime(CoreFoundation.DispatchTime.Now, TimeSpan.FromMilliseconds(100));
        CoreFoundation.DispatchQueue.MainQueue.DispatchAfter(delay, () =>
        {
            NSFontManager.SharedFontManager.PerformSelector(new Selector("addFontTrait:"),
                NSNumber.FromInt32((int)NSFontTraitMask.Bold));
        });
    }
}

sealed class DelegateDrivenUndoTextView : NSTextView
{
    public override bool ShouldChangeText(NSRange affectedCharRange, string replacementString)
    {
        return base.ShouldChangeText(affectedCharRange, replacementString);
    }
}

Example project: test_nstextview.zip

Did you find any workaround?

I can check for this case, but it shows a warning:

Image

Additionally, doing this causes the NSTextView's undo manager not register the trait change and it cannot be undone without further customization

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions