Browse Source

Fix WhenActivated ignoring WindowBase

pull/1968/head
artyom 8 years ago
parent
commit
965057a7e2
  1. 34
      src/Avalonia.ReactiveUI/AvaloniaActivationForViewFetcher.cs
  2. 35
      tests/Avalonia.ReactiveUI.UnitTests/AvaloniaActivationForViewFetcherTest.cs

34
src/Avalonia.ReactiveUI/AvaloniaActivationForViewFetcher.cs

@ -6,6 +6,7 @@ using System.Reflection;
using System.Reactive.Linq; using System.Reactive.Linq;
using Avalonia; using Avalonia;
using Avalonia.VisualTree; using Avalonia.VisualTree;
using Avalonia.Controls;
using ReactiveUI; using ReactiveUI;
namespace Avalonia namespace Avalonia
@ -20,18 +21,41 @@ namespace Avalonia
public IObservable<bool> GetActivationForView(IActivatable view) public IObservable<bool> GetActivationForView(IActivatable view)
{ {
if (!(view is IVisual visual)) return Observable.Return(false); if (!(view is IVisual visual)) return Observable.Return(false);
var viewLoaded = Observable if (view is WindowBase window) return GetActivationForWindowBase(window);
return GetActivationForVisual(visual);
}
private IObservable<bool> GetActivationForWindowBase(WindowBase window)
{
var windowLoaded = Observable
.FromEventPattern(
x => window.Initialized += x,
x => window.Initialized -= x)
.Select(args => true);
var windowUnloaded = Observable
.FromEventPattern(
x => window.Closed += x,
x => window.Closed -= x)
.Select(args => false);
return windowLoaded
.Merge(windowUnloaded)
.DistinctUntilChanged();
}
private IObservable<bool> GetActivationForVisual(IVisual visual)
{
var visualLoaded = Observable
.FromEventPattern<VisualTreeAttachmentEventArgs>( .FromEventPattern<VisualTreeAttachmentEventArgs>(
x => visual.AttachedToVisualTree += x, x => visual.AttachedToVisualTree += x,
x => visual.DetachedFromVisualTree -= x) x => visual.AttachedToVisualTree -= x)
.Select(args => true); .Select(args => true);
var viewUnloaded = Observable var visualUnloaded = Observable
.FromEventPattern<VisualTreeAttachmentEventArgs>( .FromEventPattern<VisualTreeAttachmentEventArgs>(
x => visual.DetachedFromVisualTree += x, x => visual.DetachedFromVisualTree += x,
x => visual.DetachedFromVisualTree -= x) x => visual.DetachedFromVisualTree -= x)
.Select(args => false); .Select(args => false);
return viewLoaded return visualLoaded
.Merge(viewUnloaded) .Merge(visualUnloaded)
.DistinctUntilChanged(); .DistinctUntilChanged();
} }
} }

35
tests/Avalonia.ReactiveUI.UnitTests/AvaloniaActivationForViewFetcherTest.cs

@ -32,6 +32,21 @@ namespace Avalonia
} }
} }
public class TestWindowWithWhenActivated : Window, IActivatable
{
public bool Active { get; private set; }
public TestWindowWithWhenActivated()
{
this.WhenActivated(disposables => {
Active = true;
Disposable
.Create(() => Active = false)
.DisposeWith(disposables);
});
}
}
[Fact] [Fact]
public void Visual_Element_Is_Activated_And_Deactivated() public void Visual_Element_Is_Activated_And_Deactivated()
{ {
@ -85,5 +100,25 @@ namespace Avalonia
fakeRenderedDecorator.Child = null; fakeRenderedDecorator.Child = null;
Assert.False(userControl.Active); Assert.False(userControl.Active);
} }
[Fact]
public void Activation_For_View_Fetcher_Should_Support_Windows()
{
Locator.CurrentMutable.RegisterConstant(
new AvaloniaActivationForViewFetcher(),
typeof(IActivationForViewFetcher));
using (var application = UnitTestApplication.Start(TestServices.MockWindowingPlatform))
{
var window = new TestWindowWithWhenActivated();
Assert.False(window.Active);
window.Show();
Assert.True(window.Active);
window.Close();
Assert.False(window.Active);
}
}
} }
} }
Loading…
Cancel
Save