/// <inheritdoc/> public IObservable <bool> GetActivationForView(IActivatableView view) { if (view is not FrameworkElement fe) { return(Observable <bool> .Empty); } var viewLoaded = Observable.FromEvent <TypedEventHandler <FrameworkElement, object>, bool>( eventHandler => { void Handler(FrameworkElement sender, object e) => eventHandler(true); return(Handler); }, x => fe.Loading += x, x => fe.Loading -= x); var viewUnloaded = Observable.FromEvent <RoutedEventHandler, bool>( eventHandler => { void Handler(object sender, RoutedEventArgs e) => eventHandler(false); return(Handler); }, x => fe.Unloaded += x, x => fe.Unloaded -= x); return(viewLoaded .Merge(viewUnloaded) .Select(b => b ? fe.WhenAnyValue(x => x.IsHitTestVisible).SkipWhile(x => !x) : Observables.False) .Switch() .DistinctUntilChanged()); }
/// <inheritdoc/> public IObservable <bool> GetActivationForView(IActivatableView view) { if (view is not FrameworkElement fe) { return(Observable <bool> .Empty); } #pragma warning disable SA1114 // Parameter list after. #if NETSTANDARD var viewLoaded = Observable.FromEvent <RoutedEventHandler, bool>( #else var viewLoaded = Observable.FromEvent <TypedEventHandler <DependencyObject, object>, bool>( #endif eventHandler => (_, _) => eventHandler(true), x => fe.Loading += x, x => fe.Loading -= x); var viewUnloaded = Observable.FromEvent <RoutedEventHandler, bool>( handler => { void EventHandler(object sender, RoutedEventArgs e) => handler(false); return(EventHandler); }, x => fe.Unloaded += x, x => fe.Unloaded -= x); return(viewLoaded .Merge(viewUnloaded) .Select(b => b ? fe.WhenAnyValue(x => x.IsHitTestVisible).SkipWhile(x => !x) : Observables.False) .Switch() .DistinctUntilChanged()); }
/// <summary> /// Get an observable defining whether the view is active. /// </summary> /// <param name="view">The view to observe.</param> /// <returns>An observable tracking whether the view is active.</returns> public IObservable <bool> GetActivationForView(IActivatableView view) { if (view is not ICanActivate canActivate) { return(Observable.Return(false)); } return(canActivate.Activated.Select(_ => true).Merge(canActivate.Deactivated.Select(_ => false))); }
public IObservable <bool> GetActivationForView(IActivatableView view) { if (!(view is ActivatingView av)) { throw new ArgumentNullException(nameof(view)); } return(av.Loaded.Select(_ => true).Merge(av.Unloaded.Select(_ => false))); }
/// <inheritdoc/> public IObservable <bool> GetActivationForView(IActivatableView view) { // Startup: Control.HandleCreated > Control.BindingContextChanged > Form.Load > Control.VisibleChanged > Form.Activated > Form.Shown // Shutdown: Form.Closing > Form.FormClosing > Form.Closed > Form.FormClosed > Form.Deactivate // https://docs.microsoft.com/en-us/dotnet/framework/winforms/order-of-events-in-windows-forms if (view is Control control) { if (GetCachedIsDesignMode(control)) { return(Observable <bool> .Empty); } var handleDestroyed = Observable.FromEvent <EventHandler, bool>( eventHandler => (sender, e) => eventHandler(false), h => control.HandleDestroyed += h, h => control.HandleDestroyed -= h); var handleCreated = Observable.FromEvent <EventHandler, bool>( eventHandler => (sender, e) => eventHandler(true), h => control.HandleCreated += h, h => control.HandleCreated -= h); var visibleChanged = Observable.FromEvent <EventHandler, bool>( eventHandler => (sender, e) => eventHandler(control.Visible), h => control.VisibleChanged += h, h => control.VisibleChanged -= h); var controlActivation = Observable.Merge(handleDestroyed, handleCreated, visibleChanged) .DistinctUntilChanged(); if (view is Form form) { var formClosed = Observable.FromEvent <FormClosedEventHandler, bool>( eventHandler => { void Handler(object sender, FormClosedEventArgs e) => eventHandler(control.Visible); return(Handler); }, h => form.FormClosed += h, h => form.FormClosed -= h); controlActivation = controlActivation.Merge(formClosed) .DistinctUntilChanged(); } return(controlActivation); } // Show a friendly warning in the log that this view will never be activated this.Log().Warn( CultureInfo.InvariantCulture, "Expected a view of type System.Windows.Forms.Control but it is {0}.\r\nYou need to implement your own IActivationForViewFetcher for {0}.", view?.GetType()); return(Observable <bool> .Empty); }
/// <summary> /// Get an observable defining whether the view is active. /// </summary> /// <param name="view">The view to observe.</param> /// <returns>An observable tracking whether the view is active.</returns> public IObservable <bool> GetActivationForView(IActivatableView view) { var canActivate = view as ICanActivate; if (canActivate == null) { return(Observable.Empty(false)); } return(canActivate.Activated.Select(_ => true).Merge(canActivate.Deactivated.Select(_ => false))); }
/// <inheritdoc/> public IObservable <bool> GetActivationForView(IActivatableView view) { var activation = GetActivationFor(view as ICanActivate) ?? GetActivationFor(view as Page) ?? GetActivationFor(view as View) ?? GetActivationFor(view as Cell) ?? Observable <bool> .Never; return(activation.DistinctUntilChanged()); }
/// <summary> /// Returns activation observable for activatable Avalonia view. /// </summary> public IObservable <bool> GetActivationForView(IActivatableView view) { if (!(view is IVisual visual)) { return(Observable.Return(false)); } if (view is WindowBase window) { return(GetActivationForWindowBase(window)); } return(GetActivationForVisual(visual)); }
/// <summary> /// Returns activation observable for activatable Avalonia view. /// </summary> public IObservable <bool> GetActivationForView(IActivatableView view) { if (!(view is IVisual visual)) { return(Observable.Return(false)); } if (view is Control control) { return(GetActivationForControl(control)); } return(GetActivationForVisual(visual)); }
/// <inheritdoc/> public IObservable <bool> GetActivationForView(IActivatableView view) { if (!(view is FrameworkElement fe)) { return(Observable <bool> .Empty); } var viewLoaded = Observable.FromEvent <RoutedEventHandler, bool>( eventHandler => { void Handler(object sender, RoutedEventArgs e) => eventHandler(true); return(Handler); }, x => fe.Loaded += x, x => fe.Loaded -= x); var hitTestVisible = Observable.FromEvent <DependencyPropertyChangedEventHandler, bool>( eventHandler => { void Handler(object sender, DependencyPropertyChangedEventArgs e) => eventHandler((bool)e.NewValue); return(Handler); }, x => fe.IsHitTestVisibleChanged += x, x => fe.IsHitTestVisibleChanged -= x); var viewUnloaded = Observable.FromEvent <RoutedEventHandler, bool>( eventHandler => { void Handler(object sender, RoutedEventArgs e) => eventHandler(false); return(Handler); }, x => fe.Unloaded += x, x => fe.Unloaded -= x); var windowActivation = GetActivationForWindow(view); var dispatcherActivation = GetActivationForDispatcher(fe); return(viewLoaded .Merge(viewUnloaded) .Merge(hitTestVisible) .Merge(windowActivation) .Merge(dispatcherActivation) .DistinctUntilChanged()); }
private static IObservable <bool> GetActivationForWindow(IActivatableView view) { if (view is not Window window) { return(Observable <bool> .Empty); } var viewClosed = Observable.FromEvent <EventHandler, bool>( eventHandler => { void Handler(object?sender, EventArgs e) => eventHandler(false); return(Handler); }, x => window.Closed += x, x => window.Closed -= x); return(viewClosed); }
public IObservable <bool> GetActivationForView(IActivatableView view) { var av = view as ActivatingView; return(av.Loaded.Select(_ => true).Merge(av.Unloaded.Select(_ => false))); }