using System;
using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.Immutable;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Media;
using ReactiveUI;
namespace Artemis.UI.Shared;
///
/// A ReactiveUI that implements the interface and will
/// activate your ViewModel automatically if the view model implements . When
/// the DataContext property changes, this class will update the ViewModel property with the new DataContext value,
/// and vice versa.
///
/// ViewModel type.
public class ReactiveCoreWindow : CoreWindow, IViewFor where TViewModel : class
{
///
/// The ViewModel.
///
public static readonly StyledProperty ViewModelProperty = AvaloniaProperty
.Register, TViewModel?>(nameof(ViewModel));
///
/// Initializes a new instance of the class.
///
public ReactiveCoreWindow()
{
// This WhenActivated block calls ViewModel's WhenActivated
// block if the ViewModel implements IActivatableViewModel.
this.WhenActivated(disposables => { });
this.GetObservable(DataContextProperty).Subscribe(OnDataContextChanged);
this.GetObservable(ViewModelProperty).Subscribe(OnViewModelChanged);
}
///
protected override void OnOpened(EventArgs e)
{
// TODO: Move to a style and remove opacity on focus loss
base.OnOpened(e);
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || !IsWindows11)
return;
// Enable Mica on Windows 11, based on the FluentAvalonia sample application
TransparencyBackgroundFallback = Brushes.Transparent;
TransparencyLevelHint = WindowTransparencyLevel.Mica;
Color2 color = this.TryFindResource("SolidBackgroundFillColorBase", out object? value) ? (Color) value! : new Color2(32, 32, 32);
color = color.LightenPercent(-0.5f);
Background = new ImmutableSolidColorBrush(color, 0.82);
}
private void OnDataContextChanged(object? value)
{
if (value is TViewModel viewModel)
ViewModel = viewModel;
else
ViewModel = null;
}
private void OnViewModelChanged(object? value)
{
if (value == null)
ClearValue(DataContextProperty);
else if (DataContext != value) DataContext = value;
}
///
/// The ViewModel.
///
public TViewModel? ViewModel
{
get => GetValue(ViewModelProperty);
set => SetValue(ViewModelProperty, value);
}
object? IViewFor.ViewModel
{
get => ViewModel;
set => ViewModel = (TViewModel?) value;
}
}