Cysharp / R3

The new future of dotnet/reactive and UniRx.
MIT License
1.72k stars 70 forks source link

wpf observable extensions #153

Closed angelofb closed 3 months ago

angelofb commented 4 months ago

thanks to your suggestion I have put together some extensions like you did for stride/unity. if you are interested I can prepare a pull request

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace R3;

public static class WpfExtension
{
    //// Button
    public static Observable<RoutedEventArgs> ClickAsObservable(this Button button, CancellationToken token = default)
    {
        return Observable.FromEvent<RoutedEventHandler, RoutedEventArgs>(h => (sender, e) => h(e), e => button.Click += e, e => button.Click -= e, token);
    }

    //// Slider
    public static Observable<RoutedPropertyChangedEventArgs<double>> ValueChangedAsObservable(this Slider slider, CancellationToken token = default)
    {
        return Observable.FromEvent<RoutedPropertyChangedEventHandler<double>, RoutedPropertyChangedEventArgs<double>>(h => (sender, e) => h(e), h => slider.ValueChanged += h, h => slider.ValueChanged -= h, token);
    }

    //// EditText
    public static Observable<TextChangedEventArgs> TextChangedAsObservable(this TextBox editText, CancellationToken token = default)
    {
        return Observable.FromEvent<TextChangedEventHandler, TextChangedEventArgs>(h => (sender, e) => h(e), h => editText.TextChanged += h, h => editText.TextChanged -= h, token);
    }

    //// CheckBox
    public static Observable<RoutedEventArgs> CheckedAsObservable(this CheckBox checkBox, CancellationToken token = default)
    {
        return Observable.FromEvent<RoutedEventHandler, RoutedEventArgs>(h => (sender, e) => h(e), h => checkBox.Checked += h, h => checkBox.Checked -= h, token);
    }

    public static Observable<RoutedEventArgs> IndeterminateAsObservable(this CheckBox checkBox, CancellationToken token = default)
    {
        return Observable.FromEvent<RoutedEventHandler, RoutedEventArgs>(h => (sender, e) => h(e), h => checkBox.Indeterminate += h, h => checkBox.Indeterminate -= h, token);
    }

    public static Observable<RoutedEventArgs> UncheckedAsObservable(this CheckBox checkBox, CancellationToken token = default)
    {
        return Observable.FromEvent<RoutedEventHandler, RoutedEventArgs>(h => (sender, e) => h(e), h => checkBox.Unchecked += h, h => checkBox.Unchecked -= h, token);
    }

    //// ToggleButton
    public static Observable<RoutedEventArgs> CheckedAsObservable(this ToggleButton checkBox, CancellationToken token = default)
    {
        return Observable.FromEvent<RoutedEventHandler, RoutedEventArgs>(h => (sender, e) => h(e), h => checkBox.Checked += h, h => checkBox.Checked -= h, token);
    }

    public static Observable<RoutedEventArgs> IndeterminateAsObservable(this ToggleButton checkBox, CancellationToken token = default)
    {
        return Observable.FromEvent<RoutedEventHandler, RoutedEventArgs>(h => (sender, e) => h(e), h => checkBox.Indeterminate += h, h => checkBox.Indeterminate -= h, token);
    }

    public static Observable<RoutedEventArgs> UncheckedAsObservable(this ToggleButton checkBox, CancellationToken token = default)
    {
        return Observable.FromEvent<RoutedEventHandler, RoutedEventArgs>(h => (sender, e) => h(e), h => checkBox.Unchecked += h, h => checkBox.Unchecked -= h, token);
    }
}
angelofb commented 4 months ago
//// ComboBox
public static Observable<SelectionChangedEventArgs> SelectionChangedAsObservable(this ComboBox comboBox, CancellationToken token = default)
{
    return Observable.FromEvent<SelectionChangedEventHandler, SelectionChangedEventArgs>(h => (sender, e) => h(e), h => comboBox.SelectionChanged += h, h => comboBox.SelectionChanged -= h, token);
}
neuecc commented 4 months ago

Some UI events are useful if the current value is sent at Subscribe time. https://github.com/Cysharp/R3/blob/main/src/R3.Unity/Assets/R3.Unity/Runtime/UnityUIComponentExtensions.cs It is important to be able to properly coordinate such details and to properly cover the components, not just some of them.