Set mouse cursor to "Wait" on macOS using C#

Viewed 112

I'm porting an application from WPF to AvaloniaUI/.Net 6 to be able to support macOS.

In WPF I used this code to change the cursor:

public class WaitCursor : IDisposable { private readonly Cursor _previousCursor;

    public WaitCursor()
    {
        _previousCursor = Mouse.OverrideCursor;

        Mouse.OverrideCursor = Cursors.Wait;
    }

    public void Dispose()
    {
        Mouse.OverrideCursor = _previousCursor;
    }
}

How can I get this to work on macOS?

2 Answers

The original question was also asked on the AvaloniaUI GitHub Discussion board, and has this answer there:

public abstract class BaseWindow<T> : ReactiveWindow<T> where T : ViewModelBase
{
    protected BaseWindow()
    {
        this.WhenActivated((CompositeDisposable disposable) =>
        {
            this.ViewModel.WhenAnyValue(x => x.IsBusy)
                .Do(UpdateCursor)
                .Subscribe()
                .DisposeWith(disposable);
        });
    }

    private void UpdateCursor(bool show)
    {
        this.Cursor = show ? new Cursor(StandardCursorType.Wait) : Cursor.Default;
    }
}

I just use this on an element.

Cursor="Hand"

Which is also a StyledProperty ... meaning you can data bind it. Which I don't think you could in WPF.

public static readonly StyledProperty<Cursor?> CursorProperty =
    AvaloniaProperty.Register<InputElement, Cursor?>(nameof(Cursor), null, true);

Which is on InputElement so pretty low in the inhertance stack.

Related