Skip to content

Commit

Permalink
Add SystemBackdrop samples for Mile.Xaml (#6)
Browse files Browse the repository at this point in the history
Add SystemBackdrop samples for Mile.Xaml
  • Loading branch information
Gaoyifei1011 authored Jul 25, 2024
1 parent 8f64f57 commit 9a4a0c8
Show file tree
Hide file tree
Showing 41 changed files with 5,016 additions and 16 deletions.
2 changes: 1 addition & 1 deletion MileXamlBlankApp/MileXamlBlankApp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Mile.Xaml">
<Version>2.2.890</Version>
<Version>2.2.1017</Version>
</PackageReference>
</ItemGroup>
<Import Project="..\Mile.Project.Windows\Mile.Project.Cpp.targets" />
Expand Down
45 changes: 45 additions & 0 deletions MileXamlBlankAppNetFramework/Helpers/BackdropHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Windows.System;
using Windows.UI.Composition.Desktop;
using Windows.UI.Xaml;
using MileXamlBlankAppNetFrameworkModern.WindowsAPI.ComTypes;
using MileXamlBlankAppNetFrameworkModern.WindowsAPI.PInvoke.Combase;

namespace MileXamlBlankAppNetFrameworkModern.Backdrop
{
public static class BackdropHelper
{
public static Lazy<IPropertyValueStatics> PropertyValueStatics { get; } = new(() => GetActivationFactory<IPropertyValueStatics>("Windows.Foundation.PropertyValue", typeof(IPropertyValueStatics).GUID));

public static T GetActivationFactory<T>(string activatableClassId, Guid iid)
{
if (!string.IsNullOrEmpty(activatableClassId))
{
CombaseLibrary.RoGetActivationFactory(activatableClassId, iid, out IntPtr comp);
return (T)Marshal.GetObjectForIUnknown(comp);
}
else
{
return default;
}
}

public static DesktopWindowTarget InitializeDesktopWindowTarget(Form form, bool isTopMost)
{
if (form.Handle == IntPtr.Zero)
{
throw new NullReferenceException("Form window not initialized");
}

DesktopWindowTarget desktopWindowTarget = null;
if (DispatcherQueue.GetForCurrentThread() is not null)
{
ICompositorDesktopInterop interop = Window.Current.Compositor as object as ICompositorDesktopInterop;
interop.CreateDesktopWindowTarget(form.Handle, isTopMost, out desktopWindowTarget);
}
return desktopWindowTarget;
}
}
}
68 changes: 61 additions & 7 deletions MileXamlBlankAppNetFramework/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,70 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Loaded="OnLoaded"
ActualThemeChanged="OnActualThemeChanged"
mc:Ignorable="d">
<Page.Content>
<Grid
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Grid.Children>
<Button
Content="Click Me"
Click="Button_Click" />
</Grid.Children>
HorizontalAlignment="Center"
VerticalAlignment="Center"
ColumnSpacing="10"
RowSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<StackPanel
Grid.Row="0"
Grid.Column="0"
Orientation="Horizontal">

<TextBlock Text="Current SystemBackdrop: " />
<TextBlock x:Name="SystemBackdropNameText" />
</StackPanel>

<Button
Grid.Row="0"
Grid.Column="1"
Click="SwitchSystemBackdropClick"
Content="Switch SystemBackdrop" />

<StackPanel
Grid.Row="1"
Grid.Column="0"
Orientation="Horizontal">

<TextBlock Text="Current Theme: " />
<TextBlock x:Name="ThemeNameText" />
</StackPanel>

<Button
Grid.Row="1"
Grid.Column="1"
Click="SwitchThemeClick"
Content="Switch Theme" />

<StackPanel
Grid.Row="2"
Grid.Column="0"
Orientation="Horizontal">

<TextBlock Text="Current InputActive State: " />
<TextBlock x:Name="InputActiveStateText" />
</StackPanel>

<Button
Grid.Row="2"
Grid.Column="1"
Click="SwitchInputActiveStateClick"
Content="Switch InputActive State" />
</Grid>
</Page.Content>
</Page>
239 changes: 235 additions & 4 deletions MileXamlBlankAppNetFramework/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,244 @@
using Windows.UI.Xaml;
using MileXamlBlankAppNetFrameworkModern.Backdrop;
using MileXamlBlankAppNetFrameworkModern.UI.Backdrop;
using Windows.UI;
using Windows.UI.Composition.Desktop;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace MileXamlBlankAppNetFrameworkModern
{
public sealed partial class MainPage : Page
{
public MainPage() => this.InitializeComponent();
private DesktopWindowTarget DesktopWindowTarget { get; set; }

private void Button_Click(object sender, RoutedEventArgs _)
=> (sender as Button).Content = "Clicked";
private ElementTheme currentTheme = ElementTheme.Default;
private bool currentInputActiveState = false;
private SystemBackdrop currentSystemBackdrop = null;

public MainPage()
{
InitializeComponent();
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
DesktopWindowTarget = BackdropHelper.InitializeDesktopWindowTarget(Program.MainForm, false);

currentSystemBackdrop = new MicaBackdrop(DesktopWindowTarget, this, Program.MainForm)
{
Kind = MicaKind.Base,
RequestedTheme = currentTheme,
IsInputActive = currentInputActiveState
};
currentSystemBackdrop.InitializeBackdrop();

currentTheme = ElementTheme.Default;
currentInputActiveState = false;
RequestedTheme = currentTheme;

SystemBackdropNameText.Text = "MicaBase";
ThemeNameText.Text = currentTheme.ToString();
InputActiveStateText.Text = currentInputActiveState.ToString();
}

private void SwitchSystemBackdropClick(object sender, RoutedEventArgs args)
{
if (SystemBackdropNameText.Text == "None")
{
Background = new SolidColorBrush(Colors.Transparent);
currentSystemBackdrop?.Dispose();
currentSystemBackdrop = null;
currentSystemBackdrop = new MicaBackdrop(DesktopWindowTarget, this, Program.MainForm)
{
Kind = MicaKind.Base,
RequestedTheme = currentTheme,
IsInputActive = currentInputActiveState
};
currentSystemBackdrop.InitializeBackdrop();

SystemBackdropNameText.Text = "MicaBase";
ThemeNameText.Text = currentTheme.ToString();
InputActiveStateText.Text = currentInputActiveState.ToString();
}
else if (SystemBackdropNameText.Text == "MicaBase")
{
Background = new SolidColorBrush(Colors.Transparent);
currentSystemBackdrop?.Dispose();
currentSystemBackdrop = null;
currentSystemBackdrop = new MicaBackdrop(DesktopWindowTarget, this, Program.MainForm)
{
Kind = MicaKind.BaseAlt,
RequestedTheme = currentTheme,
IsInputActive = currentInputActiveState
};
currentSystemBackdrop.InitializeBackdrop();

SystemBackdropNameText.Text = "MicaAlt";
ThemeNameText.Text = currentTheme.ToString();
InputActiveStateText.Text = currentInputActiveState.ToString();
}
else if (SystemBackdropNameText.Text == "MicaAlt")
{
Background = new SolidColorBrush(Colors.Transparent);
currentSystemBackdrop?.Dispose();
currentSystemBackdrop = null;
currentSystemBackdrop = new DesktopAcrylicBackdrop(DesktopWindowTarget, this, Program.MainForm)
{
Kind = DesktopAcrylicKind.Default,
RequestedTheme = currentTheme,
IsInputActive = currentInputActiveState,
UseHostBackdropBrush = true
};
currentSystemBackdrop.InitializeBackdrop();

SystemBackdropNameText.Text = "DesktopAcrylicDefault";
ThemeNameText.Text = currentTheme.ToString();
InputActiveStateText.Text = currentInputActiveState.ToString();
}
else if (SystemBackdropNameText.Text == "DesktopAcrylicDefault")
{
Background = new SolidColorBrush(Colors.Transparent);
currentSystemBackdrop?.Dispose();
currentSystemBackdrop = null;
currentSystemBackdrop = new DesktopAcrylicBackdrop(DesktopWindowTarget, this, Program.MainForm)
{
Kind = DesktopAcrylicKind.Base,
RequestedTheme = currentTheme,
IsInputActive = currentInputActiveState,
UseHostBackdropBrush = true
};
currentSystemBackdrop.InitializeBackdrop();

SystemBackdropNameText.Text = "DesktopAcrylicBase";
ThemeNameText.Text = currentTheme.ToString();
InputActiveStateText.Text = currentInputActiveState.ToString();
}
else if (SystemBackdropNameText.Text == "DesktopAcrylicBase")
{
Background = new SolidColorBrush(Colors.Transparent);
currentSystemBackdrop?.Dispose();
currentSystemBackdrop = null;
currentSystemBackdrop = new DesktopAcrylicBackdrop(DesktopWindowTarget, this, Program.MainForm)
{
Kind = DesktopAcrylicKind.Thin,
RequestedTheme = currentTheme,
IsInputActive = currentInputActiveState,
UseHostBackdropBrush = true
};
currentSystemBackdrop.InitializeBackdrop();

SystemBackdropNameText.Text = "DesktopAcrylicThin";
ThemeNameText.Text = currentTheme.ToString();
InputActiveStateText.Text = currentInputActiveState.ToString();
}
else if (SystemBackdropNameText.Text == "DesktopAcrylicThin")
{
SystemBackdropNameText.Text = "None";
currentSystemBackdrop?.Dispose();
currentSystemBackdrop = null;
ThemeNameText.Text = currentTheme.ToString();
InputActiveStateText.Text = currentInputActiveState.ToString();

if (ActualTheme is ElementTheme.Light)
{
Background = new SolidColorBrush(Color.FromArgb(255, 243, 243, 243));
}
else
{
Background = new SolidColorBrush(Color.FromArgb(255, 32, 32, 32));
}
}
}

private void SwitchThemeClick(object sender, RoutedEventArgs args)
{
if (currentTheme is ElementTheme.Default)
{
RequestedTheme = ElementTheme.Light;
currentTheme = ElementTheme.Light;
ThemeNameText.Text = ElementTheme.Light.ToString();
if (currentSystemBackdrop is not null)
{
currentSystemBackdrop.RequestedTheme = ElementTheme.Light;
}
else
{
Background = new SolidColorBrush(Color.FromArgb(255, 243, 243, 243));
}
}
else if (currentTheme is ElementTheme.Light)
{
RequestedTheme = ElementTheme.Dark;
currentTheme = ElementTheme.Dark;
ThemeNameText.Text = ElementTheme.Dark.ToString();
if (currentSystemBackdrop is not null)
{
currentSystemBackdrop.RequestedTheme = ElementTheme.Dark;
}
else
{
Background = new SolidColorBrush(Color.FromArgb(255, 32, 32, 32));
}
}
else
{
RequestedTheme = ElementTheme.Default;
currentTheme = ElementTheme.Default;
ThemeNameText.Text = ElementTheme.Default.ToString();
if (currentSystemBackdrop is not null)
{
currentSystemBackdrop.RequestedTheme = ElementTheme.Default;
}
else
{
if (ActualTheme is ElementTheme.Light)
{
Background = new SolidColorBrush(Color.FromArgb(255, 243, 243, 243));
}
else
{
Background = new SolidColorBrush(Color.FromArgb(255, 32, 32, 32));
}
}
}
}

private void SwitchInputActiveStateClick(object sender, RoutedEventArgs args)
{
if (currentInputActiveState)
{
currentInputActiveState = false;
InputActiveStateText.Text = currentInputActiveState.ToString();
if (currentSystemBackdrop is not null)
{
currentSystemBackdrop.IsInputActive = false;
}
}
else
{
currentInputActiveState = true;
InputActiveStateText.Text = currentInputActiveState.ToString();
if (currentSystemBackdrop is not null)
{
currentSystemBackdrop.IsInputActive = true;
}
}
}

private void OnActualThemeChanged(FrameworkElement sender, object args)
{
if (currentSystemBackdrop is not null)
{
if (ActualTheme is ElementTheme.Light)
{
Background = new SolidColorBrush(Color.FromArgb(255, 243, 243, 243));
}
else
{
Background = new SolidColorBrush(Color.FromArgb(255, 32, 32, 32));
}
}
}
}
}
Loading

0 comments on commit 9a4a0c8

Please sign in to comment.