Skip to content

Commit

Permalink
Fix For Maui WinUI Registrations (#3577)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPulman committed Jul 8, 2023
1 parent 2ef3d99 commit 3467c36
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 45 deletions.
39 changes: 23 additions & 16 deletions src/ReactiveUI.Maui/ActivationForViewFetcher.cs
Expand Up @@ -6,13 +6,15 @@
using System;
using System.Reactive.Linq;
using System.Reflection;
#if HAS_WINUI
#if WINUI_TARGET
using Microsoft.UI.Xaml;
using Windows.Foundation;
#endif

#if IS_WINUI
namespace ReactiveUI.WinUI;
#endif
#if HAS_MAUI
#if IS_MAUI
using System.ComponentModel;
using Microsoft.Maui.Controls;

Expand All @@ -27,13 +29,15 @@ public class ActivationForViewFetcher : IActivationForViewFetcher
{
/// <inheritdoc/>
public int GetAffinityForView(Type view) =>
#if HAS_WINUI
typeof(FrameworkElement).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo())
#if WINUI_TARGET
#if IS_MAUI
typeof(Page).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ||
#endif
#if HAS_MAUI
typeof(Page).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ||
typeof(FrameworkElement).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo())
#else
typeof(Page).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ||
typeof(View).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo()) ||
typeof(Cell).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo())
typeof(Cell).GetTypeInfo().IsAssignableFrom(view.GetTypeInfo())
#endif
? 10 : 0;

Expand All @@ -42,10 +46,12 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
{
var activation =
GetActivationFor(view as ICanActivate) ??
#if HAS_WINUI
#if WINUI_TARGET
GetActivationFor(view as FrameworkElement) ??
#if IS_MAUI
GetActivationFor(view as Page) ??
#endif
#if HAS_MAUI
#else
GetActivationFor(view as Page) ??
GetActivationFor(view as View) ??
GetActivationFor(view as Cell) ??
Expand All @@ -55,9 +61,10 @@ public IObservable<bool> GetActivationForView(IActivatableView view)
return activation.DistinctUntilChanged();
}

private static IObservable<bool>? GetActivationFor(ICanActivate? canActivate) => canActivate?.Activated.Select(_ => true).Merge(canActivate.Deactivated.Select(_ => false));
private static IObservable<bool>? GetActivationFor(ICanActivate? canActivate) =>
canActivate?.Activated.Select(_ => true).Merge(canActivate.Deactivated.Select(_ => false));

#if HAS_MAUI
#if !WINUI_TARGET || (WINUI_TARGET && IS_MAUI)
private static IObservable<bool>? GetActivationFor(Page? page)
{
if (page is null)
Expand Down Expand Up @@ -85,7 +92,9 @@ public IObservable<bool> GetActivationForView(IActivatableView view)

return appearing.Merge(disappearing);
}
#endif

#if !WINUI_TARGET
private static IObservable<bool>? GetActivationFor(View? view)
{
if (view is null)
Expand Down Expand Up @@ -135,14 +144,12 @@ public IObservable<bool> GetActivationForView(IActivatableView view)

return appearing.Merge(disappearing);
}
#endif

#if HAS_WINUI
private static IObservable<bool> GetActivationFor(FrameworkElement? view)
#else
private static IObservable<bool>? GetActivationFor(FrameworkElement? view)
{
if (view is null)
{
return Observable<bool>.Empty;
return null;
}

var viewLoaded = Observable.FromEvent<TypedEventHandler<FrameworkElement, object>, bool>(
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Maui/Common/AutoDataTemplateBindingHook.cs
Expand Up @@ -3,7 +3,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#if HAS_WINUI
#if WINUI_TARGET
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand Down
Expand Up @@ -4,11 +4,10 @@
// See the LICENSE file in the project root for full license information.

using System;
#if HAS_MAUI
using Microsoft.Maui;
#endif
#if HAS_WINUI
#if WINUI_TARGET
using Microsoft.UI.Xaml;
#else
using Microsoft.Maui;
#endif

namespace ReactiveUI
Expand Down Expand Up @@ -46,7 +45,7 @@ public bool TryConvert(object? from, Type toType, object? conversionHint, out ob
{
var fromAsBool = (hint & BooleanToVisibilityHint.Inverse) != 0 ? !fromBool : fromBool;

#if !NETFX_CORE && !HAS_UNO && !HAS_WINUI
#if !WINUI_TARGET
var notVisible = (hint & BooleanToVisibilityHint.UseHidden) != 0 ? Visibility.Hidden : Visibility.Collapsed;
#else
var notVisible = Visibility.Collapsed;
Expand Down
11 changes: 5 additions & 6 deletions src/ReactiveUI.Maui/Common/ReactivePage.cs
Expand Up @@ -3,12 +3,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#if HAS_MAUI
using Microsoft.Maui.Controls;
#endif
#if HAS_WINUI
#if WINUI_TARGET
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
#else
using Microsoft.Maui.Controls;
#endif

namespace ReactiveUI
Expand Down Expand Up @@ -81,7 +80,7 @@ class ReactivePage<TViewModel> :
Page, IViewFor<TViewModel>
where TViewModel : class
{
#if HAS_WINUI
#if WINUI_TARGET
/// <summary>
/// The view model dependency property.
/// </summary>
Expand Down Expand Up @@ -123,7 +122,7 @@ class ReactivePage<TViewModel> :
set => ViewModel = (TViewModel?)value;
}

#if HAS_MAUI
#if !WINUI_TARGET
/// <inheritdoc/>
protected override void OnBindingContextChanged()
{
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Maui/Common/ReactiveUserControl.cs
Expand Up @@ -3,7 +3,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#if HAS_WINUI
#if WINUI_TARGET
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.UI.Xaml;
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Maui/Common/RoutedViewHost.cs
Expand Up @@ -3,7 +3,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#if HAS_WINUI
#if WINUI_TARGET
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Maui/Common/ViewModelViewHost.cs
Expand Up @@ -3,7 +3,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#if HAS_WINUI
#if WINUI_TARGET
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reactive;
Expand Down
9 changes: 2 additions & 7 deletions src/ReactiveUI.Maui/ReactiveUI.Maui.csproj
Expand Up @@ -6,13 +6,11 @@
<PackageDescription>Contains the ReactiveUI platform specific extensions for Microsoft Maui</PackageDescription>
<PackageTags>mvvm;reactiveui;rx;reactive extensions;observable;LINQ;events;frp;maui;android;ios;mac;forms;net</PackageTags>
<UseMaui>true</UseMaui>
<DefineConstants>IS_MAUI</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="$(TargetFramework.EndsWith('-windows10.0.19041.0'))">
<DefineConstants>HAS_WINUI</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="!$(TargetFramework.EndsWith('-windows10.0.19041.0'))">
<DefineConstants>HAS_MAUI</DefineConstants>
<DefineConstants>$(DefineConstants);WINUI_TARGET;</DefineConstants>
</PropertyGroup>

<ItemGroup Condition="$(TargetFramework.EndsWith('-windows10.0.19041.0'))">
Expand All @@ -26,8 +24,5 @@
<ItemGroup>
<Compile Remove="Common\RoutedViewHost.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ReactiveUI\ReactiveUI.csproj" />
</ItemGroup>

</Project>
8 changes: 5 additions & 3 deletions src/ReactiveUI.Maui/Registrations.cs
Expand Up @@ -5,13 +5,15 @@

using System;

#if HAS_WINUI
#if WINUI_TARGET
using System.Reactive.Concurrency;
using Splat;
#endif

#if IS_WINUI
namespace ReactiveUI.WinUI;
#endif
#if HAS_MAUI
#if IS_MAUI
namespace ReactiveUI.Maui;
#endif

Expand All @@ -37,7 +39,7 @@ public void Register(Action<Func<object>, Type> registerFunction)
registerFunction(() => new ActivationForViewFetcher(), typeof(IActivationForViewFetcher));
registerFunction(() => new BooleanToVisibilityTypeConverter(), typeof(IBindingTypeConverter));

#if HAS_WINUI
#if WINUI_TARGET
registerFunction(() => new PlatformOperations(), typeof(IPlatformOperations));
registerFunction(() => new DependencyObjectObservableForProperty(), typeof(ICreatesObservableForProperty));
registerFunction(() => new AutoDataTemplateBindingHook(), typeof(IPropertyBindingHook));
Expand Down
Expand Up @@ -3,7 +3,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#if HAS_WINUI
#if WINUI_TARGET
using System;
using System.Globalization;
using System.Linq.Expressions;
Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.Maui/WinUI/DispatcherQueueScheduler.cs
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information.
// <auto-generated />

#if HAS_WINUI
#if WINUI_TARGET
using System.Reactive.Disposables;
using System.Threading;
using Microsoft.UI.Dispatching;
Expand Down
Expand Up @@ -3,7 +3,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

#if HAS_WINUI
#if WINUI_TARGET
using System.Diagnostics.CodeAnalysis;
using Microsoft.UI.Xaml.Controls;

Expand Down
2 changes: 1 addition & 1 deletion src/ReactiveUI.WinUI/ReactiveUI.WinUI.csproj
Expand Up @@ -4,9 +4,9 @@
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<PackageDescription>Contains the ReactiveUI platform specific extensions for WinUI Desktop</PackageDescription>
<RootNamespace>ReactiveUI.WinUI.Desktop</RootNamespace>
<DefineConstants>HAS_WINUI</DefineConstants>
<PackageTags>mvvm;reactiveui;rx;reactive extensions;observable;LINQ;events;winui</PackageTags>
<UseWinUI>true</UseWinUI>
<DefineConstants>IS_WINUI;WINUI_TARGET;</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 3467c36

Please sign in to comment.