Skip to content

Commit

Permalink
fix: Fix UWP activating too soon (#1930)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennawatson committed Jan 25, 2019
1 parent 829e702 commit 49de639
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
Expand Up @@ -9,10 +9,6 @@
using System.Reflection;
using System.Windows;

#if NETFX_CORE
using Windows.UI.Xaml;
#endif

namespace ReactiveUI
{
/// <summary>
Expand All @@ -37,14 +33,7 @@ public IObservable<bool> GetActivationForView(IActivatable view)
{
return Observable<bool>.Empty;
}
#if WINDOWS_UWP
var viewLoaded = WindowsObservable.FromEventPattern<FrameworkElement, object>(
x => fe.Loading += x,
x => fe.Loading -= x)
.Select(_ => true);

var hitTestVisible = fe.WhenAnyValue(x => x.IsHitTestVisible);
#else
var viewLoaded = Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>(
x => fe.Loaded += x,
x => fe.Loaded -= x)
Expand All @@ -54,7 +43,6 @@ public IObservable<bool> GetActivationForView(IActivatable view)
x => fe.IsHitTestVisibleChanged += x,
x => fe.IsHitTestVisibleChanged -= x)
.Select(x => (bool)x.EventArgs.NewValue);
#endif

var viewUnloaded = Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>(
x => fe.Unloaded += x,
Expand Down
54 changes: 54 additions & 0 deletions src/ReactiveUI/Platforms/uap10.0.16299/ActivationForViewFetcher.cs
@@ -0,0 +1,54 @@
// Copyright (c) 2019 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;
using System.Linq;
using System.Reactive.Linq;
using System.Reflection;
using System.Windows;

using Windows.UI.Xaml;

namespace ReactiveUI
{
/// <summary>
/// ActiveationForViewFetcher is how ReactiveUI determine when a
/// View is activated or deactivated. This is usually only used when porting
/// ReactiveUI to a new UI framework.
/// </summary>
public class ActivationForViewFetcher : IActivationForViewFetcher
{
/// <inheritdoc/>
public int GetAffinityForView(Type view)
{
return typeof(FrameworkElement).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ? 10 : 0;
}

/// <inheritdoc/>
public IObservable<bool> GetActivationForView(IActivatable view)
{
var fe = view as FrameworkElement;

if (fe == null)
{
return Observable<bool>.Empty;
}

var viewLoaded = WindowsObservable.FromEventPattern<FrameworkElement, object>(
x => fe.Loading += x,
x => fe.Loading -= x).Select(_ => true);

var viewUnloaded = Observable.FromEventPattern<RoutedEventHandler, RoutedEventArgs>(
x => fe.Unloaded += x,
x => fe.Unloaded -= x).Select(_ => false);

return viewLoaded
.Merge(viewUnloaded)
.Select(b => b ? fe.WhenAnyValue(x => x.IsHitTestVisible).SkipWhile(x => !x) : Observables.False)
.Switch()
.DistinctUntilChanged();
}
}
}

0 comments on commit 49de639

Please sign in to comment.