-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SystemBackdrop samples for Mile.Xaml (#6)
Add SystemBackdrop samples for Mile.Xaml
- Loading branch information
1 parent
8f64f57
commit 9a4a0c8
Showing
41 changed files
with
5,016 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.