Revamp mica application and fallback to acrylic on win10

This commit is contained in:
Difegue 2021-10-14 15:39:53 +02:00
parent b1e002803f
commit c1188a6d92
5 changed files with 49 additions and 22 deletions

View File

@ -16,7 +16,19 @@
<!-- Light theme by default -->
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:ThemeResources />
<ui:ThemeResources>
<!-- Theme-Aware resources -->
<ui:ThemeResources.ThemeDictionaries>
<ResourceDictionary x:Key="Light" ui:ThemeDictionary.Key="Light">
<SolidColorBrush x:Key="SystemControlPageBackgroundAltHighBrush" Color="#F3F3F3"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark" ui:ThemeDictionary.Key="Dark">
<SolidColorBrush x:Key="SystemControlPageBackgroundAltHighBrush" Color="#202020"/>
</ResourceDictionary>
</ui:ThemeResources.ThemeDictionaries>
</ui:ThemeResources>
<ui:XamlControlsResources />
</ResourceDictionary.MergedDictionaries>

View File

@ -19,6 +19,8 @@ namespace Karen.Interop
[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute dwAttribute, ref int pvAttribute, int cbAttribute);
private static bool IsWin11 = Environment.OSVersion.Version >= new Version(10, 0, 22000, 0);
[Flags]
public enum DwmWindowAttribute : uint
{
@ -54,31 +56,31 @@ namespace Karen.Interop
{
int trueValue = 0x01;
int falseValue = 0x00;
DwmSetWindowAttribute(source.Handle, DwmWindowAttribute.DWMWA_MICA_EFFECT, ref trueValue, Marshal.SizeOf(typeof(int)));
// Set dark mode before applying the material, otherwise you'll get an ugly flash when displaying the window.
if (darkThemeEnabled)
DwmSetWindowAttribute(source.Handle, DwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE, ref trueValue, Marshal.SizeOf(typeof(int)));
else
DwmSetWindowAttribute(source.Handle, DwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE, ref falseValue, Marshal.SizeOf(typeof(int)));
var hr = DwmSetWindowAttribute(source.Handle, DwmWindowAttribute.DWMWA_MICA_EFFECT, ref trueValue, Marshal.SizeOf(typeof(int)));
}
public static void UpdateStyleAttributes(HwndSource hwnd, bool enableAcrylic)
public static void UpdateStyleAttributes(HwndSource hwnd)
{
var _blurBackgroundColor = 0x99FFFFFF;
var darkThemeEnabled = ModernWpf.ThemeManager.Current.ActualApplicationTheme == ModernWpf.ApplicationTheme.Dark;
if (darkThemeEnabled)
{
_blurBackgroundColor = 0xAA000000;
}
if (IsWin11)
EnableMica(hwnd, darkThemeEnabled);
else
{
_blurBackgroundColor = 0x99FFFFFF;
if (darkThemeEnabled)
EnableBlur(hwnd, 0xFF202020);
else
EnableBlur(hwnd, 0xFFF3F3F3);
}
if (enableAcrylic)
EnableBlur(hwnd, _blurBackgroundColor);
EnableMica(hwnd, darkThemeEnabled);
}
}

View File

@ -44,7 +44,7 @@ namespace Karen
void KarenPopup_ContentRendered(object sender, EventArgs e)
{
UpdateStyleAttributes((HwndSource)sender, false);
UpdateStyleAttributes((HwndSource)sender);
// Round off corners for the popup
int value = 0x02;
@ -52,7 +52,7 @@ namespace Karen
ThemeManager.Current.ActualApplicationThemeChanged += (s, ev) =>
{
UpdateStyleAttributes((HwndSource)sender, false);
UpdateStyleAttributes((HwndSource)sender);
// Update theme manually since we're not in a window
if (ThemeManager.Current.ActualApplicationTheme == ApplicationTheme.Dark)

View File

@ -3,17 +3,30 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:emoji="clr-namespace:Emoji.Wpf;assembly=Emoji.Wpf"
xmlns:ui="http://schemas.modernwpf.com/2019"
ui:WindowHelper.UseModernWindowStyle="True"
x:Class="Karen.MainWindow"
xmlns:p="clr-namespace:Karen.Properties"
ResizeMode="CanResizeWithGrip"
Title="LANraragi Windows Settings"
MinWidth="500" MinHeight="480"
Width="500" Height="680"
Loaded="Window_Loaded"
Background="Transparent"
Foreground="{DynamicResource SystemControlPageTextBaseHighBrush}"
ui:ThemeManager.IsThemeAware="True"
Closing="OnClosing">
<ScrollViewer Margin="0,0,0,16">
<WindowChrome.WindowChrome>
<WindowChrome
CaptionHeight="32"
ResizeBorderThickness="8"
CornerRadius="0"
GlassFrameThickness="-1"
NonClientFrameEdges="Right"
UseAeroCaptionButtons="True" />
</WindowChrome.WindowChrome>
<Grid>
<TextBlock Text="LANraragi Windows Settings" Margin="12"/>
<ScrollViewer Margin="0,40,0,16">
<ui:SimpleStackPanel VerticalAlignment="Top" Margin="16">
<emoji:TextBlock Style="{StaticResource BaseTextBlockStyle}" Text="🗃 Content Folder Location" Margin="0,0,0,5"/>
<TextBlock Style="{StaticResource CaptionTextBlockStyle}">Where LANraragi looks for archives on your machine.</TextBlock>
@ -69,5 +82,5 @@
</ui:SimpleStackPanel>
</ScrollViewer>
</Grid>
</Window>

View File

@ -91,8 +91,8 @@ namespace Karen
private void Window_ContentRendered(object sender, System.EventArgs e)
{
WCAUtils.UpdateStyleAttributes((HwndSource)sender, true);
ModernWpf.ThemeManager.Current.ActualApplicationThemeChanged += (s, ev) => WCAUtils.UpdateStyleAttributes((HwndSource)sender, true);
WCAUtils.UpdateStyleAttributes((HwndSource)sender);
ModernWpf.ThemeManager.Current.ActualApplicationThemeChanged += (s, ev) => WCAUtils.UpdateStyleAttributes((HwndSource)sender);
}
private void Window_Loaded(object sender, RoutedEventArgs e)