From 85732bc5817e54cf5ea19fcdd1bfb676189769e5 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Fri, 16 Jun 2023 12:04:15 +0200 Subject: [PATCH 01/28] Make sure Windows InputMethod deletes current selection before preeditText is set --- .../Avalonia.Win32/Input/Imm32InputMethod.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Windows/Avalonia.Win32/Input/Imm32InputMethod.cs b/src/Windows/Avalonia.Win32/Input/Imm32InputMethod.cs index 05074cc82e..8502f5b41a 100644 --- a/src/Windows/Avalonia.Win32/Input/Imm32InputMethod.cs +++ b/src/Windows/Avalonia.Win32/Input/Imm32InputMethod.cs @@ -324,6 +324,11 @@ namespace Avalonia.Win32.Input if (IsActive) { Client.SetPreeditText(null); + + if (Client.SupportsSurroundingText && Client.SurroundingText.AnchorOffset != Client.SurroundingText.CursorOffset) + { + KeyPress(Key.Delete); + } } IsComposing = true; @@ -393,6 +398,19 @@ namespace Avalonia.Win32.Input return (int)(ptr.ToInt64() & 0xffffffff); } + private void KeyPress(Key key) + { + if (_parent?.Input != null) + { + _parent.Input(new RawKeyEventArgs(KeyboardDevice.Instance!, (ulong)DateTime.Now.Ticks, _parent.Owner, + RawKeyEventType.KeyDown, key, RawInputModifiers.None)); + + _parent.Input(new RawKeyEventArgs(KeyboardDevice.Instance!, (ulong)DateTime.Now.Ticks, _parent.Owner, + RawKeyEventType.KeyUp, key, RawInputModifiers.None)); + + } + } + ~Imm32InputMethod() { _caretManager.TryDestroy(); From 87b8c63c1b7ce8e1753ee063da919819d6814434 Mon Sep 17 00:00:00 2001 From: robloo Date: Sun, 18 Jun 2023 12:16:58 -0400 Subject: [PATCH 02/28] Add Distance method to Point --- src/Avalonia.Base/Point.cs | 13 +++++++++++++ src/Avalonia.Base/Vector.cs | 14 ++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Base/Point.cs b/src/Avalonia.Base/Point.cs index 331cce4a76..e3ea21d1eb 100644 --- a/src/Avalonia.Base/Point.cs +++ b/src/Avalonia.Base/Point.cs @@ -164,6 +164,19 @@ namespace Avalonia /// The resulting point. public static Point operator *(Point point, Matrix matrix) => matrix.Transform(point); + /// + /// Computes the Euclidean distance between the two given points. + /// + /// The first point. + /// The second point. + /// The Euclidean distance. + public static double Distance(Point value1, Point value2) + { + double distanceSquared = ((value2.X - value1.X) * (value2.X - value1.X)) + + ((value2.Y - value1.Y) * (value2.Y - value1.Y)); + return Math.Sqrt(distanceSquared); + } + /// /// Parses a string. /// diff --git a/src/Avalonia.Base/Vector.cs b/src/Avalonia.Base/Vector.cs index 15722901a6..fffe206835 100644 --- a/src/Avalonia.Base/Vector.cs +++ b/src/Avalonia.Base/Vector.cs @@ -359,7 +359,7 @@ namespace Avalonia internal Vector(Vector2 v) : this(v.X, v.Y) { - + } /// @@ -379,21 +379,27 @@ namespace Avalonia /// public static Vector Max(Vector left, Vector right) => new(Math.Max(left.X, right.X), Math.Max(left.Y, right.Y)); - + /// /// Returns a vector whose elements are the minimum of each of the pairs of elements in two specified vectors /// public static Vector Min(Vector left, Vector right) => new(Math.Min(left.X, right.X), Math.Min(left.Y, right.Y)); - + /// /// Computes the Euclidean distance between the two given points. /// + /// The first point. + /// The second point. + /// The Euclidean distance. public static double Distance(Vector value1, Vector value2) => Math.Sqrt(DistanceSquared(value1, value2)); - + /// /// Returns the Euclidean distance squared between two specified points /// + /// The first point. + /// The second point. + /// The Euclidean distance squared. public static double DistanceSquared(Vector value1, Vector value2) { var difference = value1 - value2; From 84c2efde2264ea488c2830c6d41af817ec2b2149 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Mon, 19 Jun 2023 22:21:10 -0400 Subject: [PATCH 03/28] Minor app builder fix --- src/Avalonia.Controls/AppBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/AppBuilder.cs b/src/Avalonia.Controls/AppBuilder.cs index 77cc9d4dcb..1cac22c8ce 100644 --- a/src/Avalonia.Controls/AppBuilder.cs +++ b/src/Avalonia.Controls/AppBuilder.cs @@ -151,7 +151,7 @@ namespace Avalonia } throw new InvalidOperationException( - $"Unable to create AppBuilder from type {entryPointType.Name}." + + $"Unable to create AppBuilder from type \"{entryPointType.FullName}\". " + $"Input type either needs to have BuildAvaloniaApp -> AppBuilder method or inherit Application type."); } From 10941d7686b3e5dbbb18bead44cfc315235513e4 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 20 Jun 2023 05:20:46 -0400 Subject: [PATCH 04/28] Fix ColorPalette TryGetResource --- .../ColorPaletteResourcesCollection.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs b/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs index 261de5497d..f26f6d8660 100644 --- a/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs +++ b/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs @@ -30,11 +30,22 @@ internal class ColorPaletteResourcesCollection : AvaloniaDictionary Count > 0; public bool TryGetResource(object key, ThemeVariant? theme, out object? value) { - theme ??= ThemeVariant.Default; - if (base.TryGetValue(theme, out var paletteResources) - && paletteResources.TryGetResource(key, theme, out value)) + if (theme is not null) { - return true; + if (base.TryGetValue(theme, out var themePaletteResources) + && themePaletteResources.TryGetResource(key, theme, out value)) + { + return true; + } + } + + if (theme != ThemeVariant.Default) + { + if (base.TryGetValue(ThemeVariant.Default, out var defaultPaletteResources) + && defaultPaletteResources.TryGetResource(key, ThemeVariant.Default, out value)) + { + return true; + } } value = null; From 3c23f01839f27ad4d832281597550f41982d012e Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 20 Jun 2023 15:38:03 +0200 Subject: [PATCH 05/28] fix(ListBox): Regression space handling --- src/Avalonia.Controls/ListBox.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Controls/ListBox.cs b/src/Avalonia.Controls/ListBox.cs index 220903cc04..2f3ba33d8d 100644 --- a/src/Avalonia.Controls/ListBox.cs +++ b/src/Avalonia.Controls/ListBox.cs @@ -148,7 +148,7 @@ namespace Avalonia.Controls } else if (e.Key == Key.Space || e.Key == Key.Enter) { - e.Handled |= UpdateSelectionFromEventSource( + UpdateSelectionFromEventSource( e.Source, true, e.KeyModifiers.HasFlag(KeyModifiers.Shift), From c368048aefe8430c3b22cf228f481daffe2b5a24 Mon Sep 17 00:00:00 2001 From: Giuseppe Lippolis Date: Tue, 20 Jun 2023 15:38:31 +0200 Subject: [PATCH 06/28] test(LIstBox): Space handling --- .../ListBoxTests.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs index 03497c335d..a6a2a02479 100644 --- a/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ListBoxTests.cs @@ -1247,6 +1247,51 @@ namespace Avalonia.Controls.UnitTests data.GetRealizedItems()); } + [Fact] + public void Should_Not_Handle_Space_When_TextBox_Inside_ListBoxItem() + { + using (UnitTestApplication.Start(TestServices.RealFocus)) + { + var target = new TextBox + { + Focusable = true + }; + var listbox = new ListBox() + { + Template = ListBoxTemplate(), + Items = + { + new ListBoxItem() + { + Template = ListBoxItemTemplate(), + Content = target, + } + } + }; + + var nKeyDown = 0; + + var root = new TestRoot() + { + Width = 1000, + Height = 1000, + Child = listbox, + }; + + root.KeyDown += (s, e) => nKeyDown++; + + + listbox.ApplyTemplate(); + root.LayoutManager.ExecuteInitialLayoutPass(); + + target.Focus(); + + RaiseKeyEvent(target, Key.Space, KeyModifiers.None); + + Assert.Equal(1, nKeyDown); + } + } + private static void RaiseKeyEvent(Control target, Key key, KeyModifiers inputModifiers = 0) { target.RaiseEvent(new KeyEventArgs From 349d7e9e6763d9d477fc57945f992675b5c480de Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 23 Jun 2023 19:38:08 -0400 Subject: [PATCH 07/28] Fix compat fluent theme not being applied --- .../DensityStyles/Compact.xaml | 13 +++---- src/Avalonia.Themes.Fluent/FluentTheme.xaml | 2 +- .../FluentTheme.xaml.cs | 35 +++++++++---------- 3 files changed, 22 insertions(+), 28 deletions(-) diff --git a/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml b/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml index d335e6f4b9..f6efefa05a 100644 --- a/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml +++ b/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml @@ -1,10 +1,3 @@ - - - 14 14 24 @@ -18,5 +11,7 @@ 24 12,1,0,3 32 - - + + diff --git a/src/Avalonia.Themes.Fluent/FluentTheme.xaml b/src/Avalonia.Themes.Fluent/FluentTheme.xaml index c84772aa61..f4467e40ce 100644 --- a/src/Avalonia.Themes.Fluent/FluentTheme.xaml +++ b/src/Avalonia.Themes.Fluent/FluentTheme.xaml @@ -18,7 +18,7 @@ - + diff --git a/src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs b/src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs index 5af22dbd1d..378041356a 100644 --- a/src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs +++ b/src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs @@ -16,9 +16,10 @@ namespace Avalonia.Themes.Fluent /// /// Includes the fluent theme in an application. /// - public class FluentTheme : Styles + public class FluentTheme : Styles, IResourceNode { - private readonly Styles _compactStyles; + private readonly ResourceDictionary _compactStyles; + private DensityStyle _densityStyle; /// /// Initializes a new instance of the class. @@ -28,9 +29,7 @@ namespace Avalonia.Themes.Fluent { AvaloniaXamlLoader.Load(sp, this); - _compactStyles = (Styles)GetAndRemove("CompactStyles"); - - EnsureCompactStyles(); + _compactStyles = (ResourceDictionary)GetAndRemove("CompactStyles"); Palettes = Resources.MergedDictionaries.OfType().FirstOrDefault() ?? throw new InvalidOperationException("FluentTheme was initialized with missing ColorPaletteResourcesCollection."); @@ -43,17 +42,17 @@ namespace Avalonia.Themes.Fluent return val; } } - - public static readonly StyledProperty DensityStyleProperty = - AvaloniaProperty.Register(nameof(DensityStyle)); + + public static readonly DirectProperty DensityStyleProperty = AvaloniaProperty.RegisterDirect( + nameof(DensityStyle), o => o.DensityStyle, (o, v) => o.DensityStyle = v); /// /// Gets or sets the density style of the fluent theme (normal, compact). /// public DensityStyle DensityStyle { - get => GetValue(DensityStyleProperty); - set => SetValue(DensityStyleProperty, value); + get => _densityStyle; + set => SetAndRaise(DensityStyleProperty, ref _densityStyle, value); } public IDictionary Palettes { get; } @@ -64,20 +63,20 @@ namespace Avalonia.Themes.Fluent if (change.Property == DensityStyleProperty) { - EnsureCompactStyles(); + Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); } } - private void EnsureCompactStyles() + bool IResourceNode.TryGetResource(object key, ThemeVariant? theme, out object? value) { - if (DensityStyle == DensityStyle.Compact) + // DensityStyle dictionary should be checked first + if (_densityStyle == DensityStyle.Compact + && _compactStyles.TryGetResource(key, theme, out value)) { - Add(_compactStyles); - } - else - { - Remove(_compactStyles); + return true; } + + return base.TryGetResource(key, theme, out value); } } } From dcd1b59d3101a3d28d47fe3e888371a83b12d6b1 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 23 Jun 2023 19:38:55 -0400 Subject: [PATCH 08/28] Make compact resources better --- .../DensityStyles/Compact.xaml | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml b/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml index f6efefa05a..830198f6d1 100644 --- a/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml +++ b/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml @@ -1,17 +1,25 @@ - 14 - 14 - 24 - 2,2,6,1 - 32 - 24 - 0,1,0,2 - 0,1,0,2 - 9,0,0,1 - 10,0,30,0 - 24 - 12,1,0,3 - 32  + 14 + 14 + 24 + 32 + 4, 2 + 4,2 + 24 + 11,5,11,7 + 4 4 8 4 + 5, 2 + 24 + 0,1,0,2 + 0,1,0,2 + 9,0,0,1 + 10,0,30,0 + 24 + 12,1,0,3 + 32 + 28 + 6, 0 + 6,4 From 9b66943dd2d3cab3ecb30319d2ab26c75e778c7d Mon Sep 17 00:00:00 2001 From: Trym Lund Flogard Date: Sat, 24 Jun 2023 12:55:29 +0200 Subject: [PATCH 09/28] Use widechar variant for win32 pinvokes --- .../NativeControls/Win/WinApi.cs | 2 +- .../Interop/UnmanagedMethods.cs | 42 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/samples/ControlCatalog.NetCore/NativeControls/Win/WinApi.cs b/samples/ControlCatalog.NetCore/NativeControls/Win/WinApi.cs index 47d368f7a4..bd0c1bf98e 100644 --- a/samples/ControlCatalog.NetCore/NativeControls/Win/WinApi.cs +++ b/samples/ControlCatalog.NetCore/NativeControls/Win/WinApi.cs @@ -39,7 +39,7 @@ internal unsafe class WinApi [DllImport("user32.dll", SetLastError = true)] public static extern bool DestroyWindow(IntPtr hwnd); - [DllImport("kernel32.dll")] + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "LoadLibraryW", ExactSpelling = true)] public static extern IntPtr LoadLibrary(string lib); diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index 39363beae3..ce42227292 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -1161,7 +1161,7 @@ namespace Avalonia.Win32.Interop [DllImport("user32.dll")] public static extern bool ClientToScreen(IntPtr hWnd, ref POINT lpPoint); - [DllImport("user32.dll", SetLastError = true)] + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "CreateWindowExW", ExactSpelling = true)] public static extern IntPtr CreateWindowEx( int dwExStyle, uint lpClassName, @@ -1215,16 +1215,16 @@ namespace Avalonia.Win32.Interop [DllImport("user32.dll")] public static extern int GetMessageTime(); - [DllImport("kernel32.dll")] + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetModuleHandleW", ExactSpelling = true)] public static extern IntPtr GetModuleHandle(string? lpModuleName); [DllImport("user32.dll")] public static extern int GetSystemMetrics(SystemMetric smIndex); - [DllImport("user32.dll", SetLastError = true)] + [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetWindowLongPtrW", ExactSpelling = true)] public static extern uint GetWindowLongPtr(IntPtr hWnd, int nIndex); - [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetWindowLong")] + [DllImport("user32.dll", SetLastError = true, EntryPoint = "GetWindowLongW", ExactSpelling = true)] public static extern uint GetWindowLong32b(IntPtr hWnd, int nIndex); public static uint GetWindowLong(IntPtr hWnd, int nIndex) @@ -1239,10 +1239,10 @@ namespace Avalonia.Win32.Interop } } - [DllImport("user32.dll", SetLastError = true, EntryPoint = "SetWindowLong")] + [DllImport("user32.dll", SetLastError = true, EntryPoint = "SetWindowLongW", ExactSpelling = true)] private static extern uint SetWindowLong32b(IntPtr hWnd, int nIndex, uint value); - [DllImport("user32.dll", SetLastError = true, EntryPoint = "SetWindowLongPtr")] + [DllImport("user32.dll", SetLastError = true, EntryPoint = "SetWindowLongPtrW", ExactSpelling = true)] private static extern IntPtr SetWindowLong64b(IntPtr hWnd, int nIndex, IntPtr value); public static uint SetWindowLong(IntPtr hWnd, int nIndex, uint value) @@ -1310,7 +1310,7 @@ namespace Avalonia.Win32.Interop [DllImport("user32.dll")] public static extern bool KillTimer(IntPtr hWnd, IntPtr uIDEvent); - [DllImport("user32.dll")] + [DllImport("user32.dll", EntryPoint = "LoadCursorW", ExactSpelling = true)] public static extern IntPtr LoadCursor(IntPtr hInstance, IntPtr lpCursorName); [DllImport("user32.dll")] @@ -1319,7 +1319,7 @@ namespace Avalonia.Win32.Interop [DllImport("user32.dll")] public static extern bool DestroyIcon(IntPtr hIcon); - [DllImport("user32.dll")] + [DllImport("user32.dll", EntryPoint = "PeekMessageW", ExactSpelling = true)] public static extern bool PeekMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg); [DllImport("user32")] @@ -1334,7 +1334,7 @@ namespace Avalonia.Win32.Interop [DllImport("user32.dll")] public static extern bool ReleaseCapture(); - [DllImport("user32.dll", SetLastError = true)] + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "RegisterWindowMessageW", ExactSpelling = true)] public static extern uint RegisterWindowMessage(string lpString); [DllImport("user32.dll")] @@ -1415,7 +1415,7 @@ namespace Avalonia.Win32.Interop [DllImport("user32.dll")] public static extern bool TranslateMessage(ref MSG lpMsg); - [DllImport("user32.dll", CharSet = CharSet.Unicode)] + [DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "UnregisterClassW", ExactSpelling = true)] public static extern bool UnregisterClass(string lpClassName, IntPtr hInstance); [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "SetWindowTextW")] @@ -1439,10 +1439,10 @@ namespace Avalonia.Win32.Interop [DllImport("shell32", CharSet = CharSet.Auto)] public static extern int Shell_NotifyIcon(NIM dwMessage, NOTIFYICONDATA lpData); - [DllImport("user32.dll", EntryPoint = "SetClassLongPtr")] + [DllImport("user32.dll", EntryPoint = "SetClassLongPtrW", ExactSpelling = true)] private static extern IntPtr SetClassLong64(IntPtr hWnd, ClassLongIndex nIndex, IntPtr dwNewLong); - [DllImport("user32.dll", EntryPoint = "SetClassLong")] + [DllImport("user32.dll", EntryPoint = "SetClassLongW", ExactSpelling = true)] private static extern IntPtr SetClassLong32(IntPtr hWnd, ClassLongIndex nIndex, IntPtr dwNewLong); public static IntPtr SetClassLong(IntPtr hWnd, ClassLongIndex nIndex, IntPtr dwNewLong) @@ -1463,10 +1463,10 @@ namespace Avalonia.Win32.Interop return new IntPtr(GetClassLongPtr32(hWnd, nIndex)); } - [DllImport("user32.dll", EntryPoint = "GetClassLong")] + [DllImport("user32.dll", EntryPoint = "GetClassLongW", ExactSpelling = true)] public static extern uint GetClassLongPtr32(IntPtr hWnd, int nIndex); - [DllImport("user32.dll", EntryPoint = "GetClassLongPtr")] + [DllImport("user32.dll", EntryPoint = "GetClassLongPtrW", ExactSpelling = true)] public static extern IntPtr GetClassLongPtr64(IntPtr hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetCursor")] @@ -1527,10 +1527,10 @@ namespace Avalonia.Win32.Interop [DllImport("kernel32.dll", ExactSpelling = true)] public static extern IntPtr GlobalFree(IntPtr hMem); - [DllImport("kernel32.dll", SetLastError = true)] + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "LoadLibraryW", ExactSpelling = true)] public static extern IntPtr LoadLibrary(string fileName); - [DllImport("kernel32.dll", SetLastError = true)] + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode, EntryPoint = "LoadLibraryExW", ExactSpelling = true)] public static extern IntPtr LoadLibraryEx(string fileName, IntPtr hFile, int flags); [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)] @@ -1645,7 +1645,7 @@ namespace Avalonia.Win32.Interop [DllImport("opengl32.dll", CharSet = CharSet.Ansi)] public static extern IntPtr wglGetProcAddress(string name); - [DllImport("kernel32.dll", SetLastError = true)] + [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "CreateFileMappingW", ExactSpelling = true)] public static extern IntPtr CreateFileMapping(IntPtr hFile, IntPtr lpFileMappingAttributes, uint flProtect, @@ -1668,16 +1668,16 @@ namespace Avalonia.Win32.Interop [DllImport("ole32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] internal static extern void ReleaseStgMedium(ref STGMEDIUM medium); - [DllImport("user32.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)] + [DllImport("user32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "GetClipboardFormatNameW", ExactSpelling = true)] public static extern int GetClipboardFormatName(int format, StringBuilder lpString, int cchMax); - [DllImport("user32.dll", BestFitMapping = false, CharSet = CharSet.Auto, SetLastError = true)] + [DllImport("user32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "RegisterClipboardFormatW", ExactSpelling = true)] public static extern int RegisterClipboardFormat(string format); [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = true, SetLastError = true)] public static extern IntPtr GlobalSize(IntPtr hGlobal); - [DllImport("shell32.dll", BestFitMapping = false, CharSet = CharSet.Auto)] + [DllImport("shell32.dll", BestFitMapping = false, CharSet = CharSet.Unicode, EntryPoint = "DragQueryFileW", ExactSpelling = true)] public static extern int DragQueryFile(IntPtr hDrop, int iFile, StringBuilder? lpszFile, int cch); [DllImport("ole32.dll", CharSet = CharSet.Auto, ExactSpelling = true, PreserveSig = false)] @@ -1893,7 +1893,7 @@ namespace Avalonia.Win32.Interop [DllImport("imm32.dll")] public static extern bool ImmSetCompositionFont(IntPtr hIMC, ref LOGFONT lf); - [DllImport("imm32.dll", SetLastError = false, CharSet = CharSet.Unicode)] + [DllImport("imm32.dll", SetLastError = false, CharSet = CharSet.Unicode, EntryPoint = "ImmGetCompositionStringW", ExactSpelling = true)] public static extern int ImmGetCompositionString(IntPtr hIMC, GCS dwIndex, [Out, Optional] IntPtr lpBuf, uint dwBufLen); public static string? ImmGetCompositionString(IntPtr hIMC, GCS dwIndex) From 6a3925b91b67e2a3412a7d3e3e11ba24589dfeff Mon Sep 17 00:00:00 2001 From: Trym Lund Flogard Date: Sat, 24 Jun 2023 13:49:26 +0200 Subject: [PATCH 10/28] Remove support for undocumented blur #11855 --- .../Interop/UnmanagedMethods.cs | 3 -- src/Windows/Avalonia.Win32/WindowImpl.cs | 53 ++----------------- 2 files changed, 5 insertions(+), 51 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index ce42227292..e58abcadf0 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -1827,9 +1827,6 @@ namespace Avalonia.Win32.Interop return result; } - [DllImport("user32.dll")] - internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data); - [Flags] public enum GCS : uint { diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index 057cdb2db0..3fc2751e43 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -371,7 +371,7 @@ namespace Avalonia.Win32 if (level == WindowTransparencyLevel.Transparent) SetTransparencyTransparent(windowsVersion); else if (level == WindowTransparencyLevel.Blur) - SetTransparencyBlur(windowsVersion); + continue; // Unsupported on all versions (unreachable) else if (level == WindowTransparencyLevel.AcrylicBlur) SetTransparencyAcrylicBlur(windowsVersion); else if (level == WindowTransparencyLevel.Mica) @@ -409,9 +409,9 @@ namespace Avalonia.Win32 if (level == WindowTransparencyLevel.Transparent) return windowsVersion >= PlatformConstants.Windows8; - // Blur only supported on Windows 8 and lower. + // Support for legacy, undocumented DWM blur has been removed. if (level == WindowTransparencyLevel.Blur) - return windowsVersion < PlatformConstants.Windows10; + return false; // Acrylic is supported on Windows >= 10.0.15063. if (level == WindowTransparencyLevel.AcrylicBlur) @@ -426,37 +426,14 @@ namespace Avalonia.Win32 private void SetTransparencyTransparent(Version windowsVersion) { - // Transparent only supported with composition on Windows 8+. - if (!_isUsingComposition || windowsVersion < PlatformConstants.Windows8) + // Transparent only supported with composition on Windows 10+. + if (!_isUsingComposition || windowsVersion < PlatformConstants.Windows10) return; - if (windowsVersion < PlatformConstants.Windows10) - { - // Some of the AccentState Enum's values have different meanings on Windows 8.x than on - // Windows 10, hence using ACCENT_ENABLE_BLURBEHIND to disable blurbehind ¯\_(ツ)_/¯. - // Hey, I'm just porting what was here before. - SetAccentState(AccentState.ACCENT_ENABLE_BLURBEHIND); - var blurInfo = new DWM_BLURBEHIND(false); - DwmEnableBlurBehindWindow(_hwnd, ref blurInfo); - } - SetUseHostBackdropBrush(false); _blurHost?.SetBlur(BlurEffect.None); } - private void SetTransparencyBlur(Version windowsVersion) - { - // Blur only supported with composition on Windows 8 and lower. - if (!_isUsingComposition || windowsVersion >= PlatformConstants.Windows10) - return; - - // Some of the AccentState Enum's values have different meanings on Windows 8.x than on - // Windows 10. - SetAccentState(AccentState.ACCENT_DISABLED); - var blurInfo = new DWM_BLURBEHIND(true); - DwmEnableBlurBehindWindow(_hwnd, ref blurInfo); - } - private void SetTransparencyAcrylicBlur(Version windowsVersion) { // Acrylic blur only supported with composition on Windows >= 10.0.15063. @@ -477,26 +454,6 @@ namespace Avalonia.Win32 _blurHost?.SetBlur(BlurEffect.Mica); } - private void SetAccentState(AccentState state) - { - var accent = new AccentPolicy(); - var accentStructSize = Marshal.SizeOf(accent); - - //Some of the AccentState Enum's values have different meanings on Windows 8.x than on Windows 10 - accent.AccentState = state; - - var accentPtr = Marshal.AllocHGlobal(accentStructSize); - Marshal.StructureToPtr(accent, accentPtr, false); - - var data = new WindowCompositionAttributeData(); - data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY; - data.SizeOfData = accentStructSize; - data.Data = accentPtr; - - SetWindowCompositionAttribute(_hwnd, ref data); - Marshal.FreeHGlobal(accentPtr); - } - private void SetUseHostBackdropBrush(bool useHostBackdropBrush) { if (Win32Platform.WindowsVersion < WinUiCompositionShared.MinHostBackdropVersion) From 29d3c7670be216f704f001ee6e28efc0adbe2e83 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 25 Jun 2023 23:26:45 -0400 Subject: [PATCH 11/28] Restructure Win32PlatformOptions options --- samples/ControlCatalog.NetCore/Program.cs | 3 +- src/Windows/Avalonia.Win32/Win32GlManager.cs | 37 ++++-- src/Windows/Avalonia.Win32/Win32Platform.cs | 74 ------------ .../Avalonia.Win32/Win32PlatformOptions.cs | 114 ++++++++++++++++++ .../WinUiCompositedWindowSurface.cs | 2 +- .../Composition/WinUiCompositionShared.cs | 1 + .../Composition/WinUiCompositorConnection.cs | 31 ++--- 7 files changed, 160 insertions(+), 102 deletions(-) create mode 100644 src/Windows/Avalonia.Win32/Win32PlatformOptions.cs diff --git a/samples/ControlCatalog.NetCore/Program.cs b/samples/ControlCatalog.NetCore/Program.cs index 5249a4fb41..99d507c817 100644 --- a/samples/ControlCatalog.NetCore/Program.cs +++ b/samples/ControlCatalog.NetCore/Program.cs @@ -110,8 +110,7 @@ namespace ControlCatalog.NetCore { builder.With(new Win32PlatformOptions() { - UseLowLatencyDxgiSwapChain = true, - UseWindowsUIComposition = false + CompositionMode = Win32CompositionMode.LowLatencyDxgiSwapChain }); return builder.StartWithClassicDesktopLifetime(args); } diff --git a/src/Windows/Avalonia.Win32/Win32GlManager.cs b/src/Windows/Avalonia.Win32/Win32GlManager.cs index c26ce5fd45..49eb369149 100644 --- a/src/Windows/Avalonia.Win32/Win32GlManager.cs +++ b/src/Windows/Avalonia.Win32/Win32GlManager.cs @@ -1,3 +1,4 @@ +using System; using Avalonia.OpenGL; using Avalonia.Platform; using Avalonia.Win32.DirectX; @@ -23,15 +24,19 @@ namespace Avalonia.Win32 private static IPlatformGraphics? InitializeCore() { - var opts = AvaloniaLocator.Current.GetService() ?? new Win32PlatformOptions(); - if (opts.UseWgl) + + var winVersion = Win32Platform.WindowsVersion; + var renderingMode = opts.RenderingMode ?? + (winVersion > PlatformConstants.Windows7 ? Win32RenderingMode.AngleEgl : Win32RenderingMode.Software); + + if (renderingMode == Win32RenderingMode.Wgl) { var wgl = WglPlatformOpenGlInterface.TryCreate(); return wgl; } - if (opts.AllowEglInitialization ?? Win32Platform.WindowsVersion > PlatformConstants.Windows7) + if (renderingMode == Win32RenderingMode.AngleEgl) { var egl = AngleWin32PlatformGraphics.TryCreate(AvaloniaLocator.Current.GetService() ?? new()); @@ -40,14 +45,26 @@ namespace Avalonia.Win32 { AvaloniaLocator.CurrentMutable.Bind() .ToConstant(egl); - - if (opts.UseWindowsUIComposition) - { - WinUiCompositorConnection.TryCreateAndRegister(); - } - else if (opts.UseLowLatencyDxgiSwapChain) + + var compositionMode = opts.CompositionMode ?? + (WinUiCompositorConnection.IsSupported() ? Win32CompositionMode.WinUIComposition + //: DirectCompositionConnection.IsSupported() ? Win32CompositionMode.DirectComposition + : Win32CompositionMode.RedirectionSurface); + + switch (compositionMode) { - DxgiConnection.TryCreateAndRegister(); + case Win32CompositionMode.WinUIComposition: + if (!WinUiCompositorConnection.TryCreateAndRegister()) + { + //goto case Win32CompositionMode.DirectComposition; + } + break; + //case Win32CompositionMode.DirectComposition: + //DirectCompositionConnection.TryCreateAndRegister(); + //break; + case Win32CompositionMode.LowLatencyDxgiSwapChain: + DxgiConnection.TryCreateAndRegister(); + break; } } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index e9019803be..aaf6fed624 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.IO; @@ -10,7 +9,6 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Platform; using Avalonia.Input; using Avalonia.Input.Platform; -using Avalonia.OpenGL; using Avalonia.Platform; using Avalonia.Rendering; using Avalonia.Rendering.Composition; @@ -31,78 +29,6 @@ namespace Avalonia "Win32"); } } - - /// - /// Platform-specific options which apply to Windows. - /// - public class Win32PlatformOptions - { - /// - /// Enables ANGLE for Windows. For every Windows version that is above Windows 7, the default is true otherwise it's false. - /// - /// - /// GPU rendering will not be enabled if this is set to false. - /// - public bool? AllowEglInitialization { get; set; } - - /// - /// Embeds popups to the window when set to true. The default value is false. - /// - public bool OverlayPopups { get; set; } - - /// - /// Avalonia would try to use native Widows OpenGL when set to true. The default value is false. - /// - public bool UseWgl { get; set; } - - public IList WglProfiles { get; set; } = new List - { - new GlVersion(GlProfileType.OpenGL, 4, 0), - new GlVersion(GlProfileType.OpenGL, 3, 2), - }; - - /// - /// Render Avalonia to a Texture inside the Windows.UI.Composition tree. - /// This setting is true by default. - /// - /// - /// Supported on Windows 10 build 16299 and above. Ignored on other versions. - /// This is recommended if you need to use AcrylicBlur or acrylic in your applications. - /// - public bool UseWindowsUIComposition { get; set; } = true; - - /// - /// When enabled, create rounded corner blur brushes - /// If set to null the brushes will be created using default settings (sharp corners) - /// This can be useful when you need a rounded-corner blurred Windows 10 app, or borderless Windows 11 app - /// - public float? CompositionBackdropCornerRadius { get; set; } - - /// - /// When is active, renders Avalonia through a low-latency Dxgi Swapchain. - /// Requires Feature Level 11_3 to be active, Windows 8.1+ Any Subversion. - /// This is only recommended if low input latency is desirable, and there is no need for the transparency - /// and stylings / blurrings offered by
- /// This is mutually exclusive with - /// which if active will override this setting. - /// This setting is false by default. - ///
- public bool UseLowLatencyDxgiSwapChain { get; set; } - - /// - /// Render directly on the UI thread instead of using a dedicated render thread. - /// Only applicable if both and - /// are false. - /// This setting is only recommended for interop with systems that must render on the UI thread, such as WPF. - /// This setting is false by default. - /// - public bool ShouldRenderOnUIThread { get; set; } - - /// - /// Provides a way to use a custom-implemented graphics context such as a custom ISkiaGpu - /// - public IPlatformGraphics? CustomPlatformGraphics { get; set; } - } } namespace Avalonia.Win32 diff --git a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs new file mode 100644 index 0000000000..aaebedb8c5 --- /dev/null +++ b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs @@ -0,0 +1,114 @@ +using System.Collections.Generic; +using Avalonia.OpenGL; +using Avalonia.Platform; + +namespace Avalonia; + +public enum Win32RenderingMode +{ + /// + /// Avalonia is rendered into a framebuffer. + /// + Software = 1, + + /// + /// Enables ANGLE EGL for Windows with GPU rendering. + /// + AngleEgl = 2, + + /// + /// Avalonia would try to use native Widows OpenGL with GPU rendering. + /// + Wgl +} + +public enum Win32CompositionMode +{ + /// + /// Render Avalonia to a texture inside the Windows.UI.Composition tree. + /// + /// + /// Supported on Windows 10 build 17134 and above. Ignored on other versions. + /// This is recommended option, as it allows window acrylic effects and high refresh rate rendering. + /// + WinUIComposition = 1, + + // /// + // /// Render Avalonia to a texture inside the DirectComposition tree. + // /// + // /// + // /// Supported on Windows 8 and above. Ignored on other versions. + // /// + // DirectComposition = 2, + + /// + /// When is active, renders Avalonia through a low-latency Dxgi Swapchain. + /// Requires Feature Level 11_3 to be active, Windows 8.1+ Any Subversion. + /// This is only recommended if low input latency is desirable, and there is no need for the transparency + /// and styling / blurring offered by
. + ///
+ LowLatencyDxgiSwapChain = 3, + + /// + /// The window renders to a redirection surface. + /// + /// + /// This option is kept only for compatibility with older systems. Some Avalonia features might not work. + /// + RedirectionSurface, +} + +/// +/// Platform-specific options which apply to Windows. +/// +public class Win32PlatformOptions +{ + /// + /// Embeds popups to the window when set to true. The default value is false. + /// + public bool OverlayPopups { get; set; } + + /// + /// Avalonia window rendering mode. + /// On Windows 8 and newer default value is , + /// otherwise. + /// + public Win32RenderingMode? RenderingMode { get; set; } + + /// + /// Avalonia window composition mode. + /// On Windows 8 and newer default value is , + /// otherwise. + /// + public Win32CompositionMode? CompositionMode { get; set; } + + /// + /// When is set to , create rounded corner blur brushes + /// If set to null the brushes will be created using default settings (sharp corners) + /// This can be useful when you need a rounded-corner blurred Windows 10 app, or borderless Windows 11 app. + /// + public float? WinUICompositionBackdropCornerRadius { get; set; } + + /// + /// Render directly on the UI thread instead of using a dedicated render thread. + /// Only applicable if is set to . + /// This setting is only recommended for interop with systems that must render on the UI thread, such as WPF. + /// This setting is false by default. + /// + public bool ShouldRenderOnUIThread { get; set; } + + /// + /// Windows OpenGL profiles used when is set to . + /// This setting is 4.0 and 3.2 by default. + /// + public IList WglProfiles { get; set; } = new List + { + new(GlProfileType.OpenGL, 4, 0), new(GlProfileType.OpenGL, 3, 2) + }; + + /// + /// Provides a way to use a custom-implemented graphics context such as a custom ISkiaGpu. + /// When this property set and are completely ignored. + /// + public IPlatformGraphics? CustomPlatformGraphics { get; set; } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs index 95ef338f08..8da154dc83 100644 --- a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs @@ -23,7 +23,7 @@ namespace Avalonia.Win32.WinRT.Composition public IDirect3D11TextureRenderTarget CreateRenderTarget(IPlatformGraphicsContext context, IntPtr d3dDevice) { var cornerRadius = AvaloniaLocator.Current.GetService() - ?.CompositionBackdropCornerRadius; + ?.WinUICompositionBackdropCornerRadius; _window ??= new WinUiCompositedWindow(_info, _shared, cornerRadius); _window.SetBlur(_blurEffect); diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositionShared.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositionShared.cs index f17805fba3..b3a328d097 100644 --- a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositionShared.cs +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositionShared.cs @@ -12,6 +12,7 @@ internal class WinUiCompositionShared : IDisposable public ICompositionBrush? MicaBrush { get; } public object SyncRoot { get; } = new(); + public static readonly Version MinWinCompositionVersion = new(10, 0, 17134); public static readonly Version MinAcrylicVersion = new(10, 0, 15063); public static readonly Version MinHostBackdropVersion = new(10, 0, 22000); diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs index 754af86c06..3e5384715c 100644 --- a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs @@ -112,35 +112,36 @@ internal class WinUiCompositorConnection : IRenderTimer } } - public static void TryCreateAndRegister() + public static bool IsSupported() { - const int majorRequired = 10; - const int buildRequired = 17134; - - var majorInstalled = Win32Platform.WindowsVersion.Major; - var buildInstalled = Win32Platform.WindowsVersion.Build; - - if (majorInstalled >= majorRequired && - buildInstalled >= buildRequired) + return Win32Platform.WindowsVersion >= WinUiCompositionShared.MinWinCompositionVersion; + } + + public static bool TryCreateAndRegister() + { + if (Win32Platform.WindowsVersion >= WinUiCompositionShared.MinWinCompositionVersion) { try { TryCreateAndRegisterCore(); - return; + return true; } catch (Exception e) { Logger.TryGet(LogEventLevel.Error, "WinUIComposition") ?.Log(null, "Unable to initialize WinUI compositor: {0}", e); - } } + else + { + var osVersionNotice = + $"Windows {WinUiCompositionShared.MinWinCompositionVersion} is required. Your machine has Windows {Win32Platform.WindowsVersion} installed."; - var osVersionNotice = - $"Windows {majorRequired} Build {buildRequired} is required. Your machine has Windows {majorInstalled} Build {buildInstalled} installed."; + Logger.TryGet(LogEventLevel.Warning, "WinUIComposition")?.Log(null, + $"Unable to initialize WinUI compositor: {osVersionNotice}"); + } - Logger.TryGet(LogEventLevel.Warning, "WinUIComposition")?.Log(null, - $"Unable to initialize WinUI compositor: {osVersionNotice}"); + return false; } public WinUiCompositedWindowSurface CreateSurface(EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo info) => new(_shared, info); From effb12f9e04343437f2e2ac172c9951881f2bbfa Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 25 Jun 2023 23:43:07 -0400 Subject: [PATCH 12/28] Throw an exception when CustomPlatformGraphics is used with unsupported Win32CompositionMode --- src/Windows/Avalonia.Win32/Win32Platform.cs | 21 ++++++++++++++++--- .../Avalonia.Win32/Win32PlatformOptions.cs | 5 +++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index aaf6fed624..5082c76d0c 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -99,9 +99,24 @@ namespace Avalonia.Win32 .Bind().ToConstant(NonPumpingWaitHelperImpl.Instance) .Bind().ToConstant(new WindowsMountedVolumeInfoProvider()) .Bind().ToConstant(s_instance); - - var platformGraphics = options.CustomPlatformGraphics - ?? Win32GlManager.Initialize(); + + IPlatformGraphics? platformGraphics; + if (options.CustomPlatformGraphics is not null) + { + if (options.CompositionMode.HasValue + && options.CompositionMode != Win32CompositionMode.RedirectionSurface) + { + throw new InvalidOperationException( + $"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.CustomPlatformGraphics)} is only " + + $"compatible with {nameof(Win32CompositionMode)}.{nameof(Win32CompositionMode.RedirectionSurface)}"); + } + + platformGraphics = options.CustomPlatformGraphics; + } + else + { + platformGraphics = Win32GlManager.Initialize(); + } if (OleContext.Current != null) AvaloniaLocator.CurrentMutable.Bind().ToSingleton(); diff --git a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs index aaebedb8c5..3c7e32f774 100644 --- a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs +++ b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs @@ -91,7 +91,7 @@ public class Win32PlatformOptions /// /// Render directly on the UI thread instead of using a dedicated render thread. - /// Only applicable if is set to . + /// Only applicable if is set to . /// This setting is only recommended for interop with systems that must render on the UI thread, such as WPF. /// This setting is false by default. /// @@ -108,7 +108,8 @@ public class Win32PlatformOptions /// /// Provides a way to use a custom-implemented graphics context such as a custom ISkiaGpu. - /// When this property set and are completely ignored. + /// When this property set is ignored + /// and only accepts null or . /// public IPlatformGraphics? CustomPlatformGraphics { get; set; } } From 33f70d9fca7912510ba0d6708cabc19e5c15f50a Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sun, 25 Jun 2023 21:18:55 -0700 Subject: [PATCH 13/28] Update src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs Co-authored-by: jp2masa --- .../WinRT/Composition/WinUiCompositorConnection.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs index 3e5384715c..596c94d30b 100644 --- a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositorConnection.cs @@ -119,7 +119,7 @@ internal class WinUiCompositorConnection : IRenderTimer public static bool TryCreateAndRegister() { - if (Win32Platform.WindowsVersion >= WinUiCompositionShared.MinWinCompositionVersion) + if (IsSupported()) { try { From 5fb1cf9f829c437befd163ea9ab6b5657e03e1af Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Mon, 26 Jun 2023 11:43:44 +0200 Subject: [PATCH 14/28] Fix DataGridColumn.CellStyleClasses not settable from xaml --- src/Avalonia.Controls.DataGrid/DataGridColumn.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/Avalonia.Controls.DataGrid/DataGridColumn.cs b/src/Avalonia.Controls.DataGrid/DataGridColumn.cs index d28c0969c4..a5695afeb7 100644 --- a/src/Avalonia.Controls.DataGrid/DataGridColumn.cs +++ b/src/Avalonia.Controls.DataGrid/DataGridColumn.cs @@ -38,7 +38,7 @@ namespace Avalonia.Controls private ICellEditBinding _editBinding; private IBinding _clipboardContentBinding; private ControlTheme _cellTheme; - private readonly Classes _cellStyleClasses = new Classes(); + private Classes _cellStyleClasses; /// /// Initializes a new instance of the class. @@ -393,17 +393,7 @@ namespace Avalonia.Controls } } - public Classes CellStyleClasses - { - get => _cellStyleClasses; - set - { - if(_cellStyleClasses != value) - { - _cellStyleClasses.Replace(value); - } - } - } + public Classes CellStyleClasses => _cellStyleClasses ??= new(); /// /// Backing field for CellTheme property. From a390f9f50cbf1db5d670b23243ec6328c3e90658 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Mon, 26 Jun 2023 23:18:34 -0400 Subject: [PATCH 15/28] Make RenderingMode and CompositionMode lists with fallbacks --- samples/ControlCatalog.NetCore/Program.cs | 2 +- .../Avalonia.Win32/DirectX/DxgiConnection.cs | 4 +- src/Windows/Avalonia.Win32/Win32GlManager.cs | 112 ++++++++++-------- src/Windows/Avalonia.Win32/Win32Platform.cs | 4 +- .../Avalonia.Win32/Win32PlatformOptions.cs | 43 +++++-- 5 files changed, 102 insertions(+), 63 deletions(-) diff --git a/samples/ControlCatalog.NetCore/Program.cs b/samples/ControlCatalog.NetCore/Program.cs index 99d507c817..5e3e301461 100644 --- a/samples/ControlCatalog.NetCore/Program.cs +++ b/samples/ControlCatalog.NetCore/Program.cs @@ -110,7 +110,7 @@ namespace ControlCatalog.NetCore { builder.With(new Win32PlatformOptions() { - CompositionMode = Win32CompositionMode.LowLatencyDxgiSwapChain + CompositionMode = new [] { Win32CompositionMode.LowLatencyDxgiSwapChain } }); return builder.StartWithClassicDesktopLifetime(args); } diff --git a/src/Windows/Avalonia.Win32/DirectX/DxgiConnection.cs b/src/Windows/Avalonia.Win32/DirectX/DxgiConnection.cs index 07fb3169cb..bac2a73dd9 100644 --- a/src/Windows/Avalonia.Win32/DirectX/DxgiConnection.cs +++ b/src/Windows/Avalonia.Win32/DirectX/DxgiConnection.cs @@ -28,16 +28,18 @@ namespace Avalonia.Win32.DirectX _syncLock = syncLock; } - public static void TryCreateAndRegister() + public static bool TryCreateAndRegister() { try { TryCreateAndRegisterCore(); + return true; } catch (Exception ex) { Logger.TryGet(LogEventLevel.Error, LogArea) ?.Log(null, "Unable to establish Dxgi: {0}", ex); + return false; } } diff --git a/src/Windows/Avalonia.Win32/Win32GlManager.cs b/src/Windows/Avalonia.Win32/Win32GlManager.cs index 49eb369149..b3760a37e1 100644 --- a/src/Windows/Avalonia.Win32/Win32GlManager.cs +++ b/src/Windows/Avalonia.Win32/Win32GlManager.cs @@ -1,4 +1,6 @@ using System; +using System.Diagnostics.Tracing; +using System.Linq; using Avalonia.OpenGL; using Avalonia.Platform; using Avalonia.Win32.DirectX; @@ -6,72 +8,88 @@ using Avalonia.Win32.OpenGl; using Avalonia.Win32.OpenGl.Angle; using Avalonia.Win32.WinRT.Composition; -namespace Avalonia.Win32 +namespace Avalonia.Win32; + +static class Win32GlManager { - static class Win32GlManager + public static IPlatformGraphics? Initialize() { - public static IPlatformGraphics? Initialize() - { - var gl = InitializeCore(); - - if (gl is not null) - { - AvaloniaLocator.CurrentMutable.Bind().ToConstant(gl); - } + var gl = InitializeCore(); - return gl; + if (gl is not null) + { + AvaloniaLocator.CurrentMutable.Bind().ToConstant(gl); } + + return gl; + } - private static IPlatformGraphics? InitializeCore() + private static IPlatformGraphics? InitializeCore() + { + var opts = AvaloniaLocator.Current.GetService() ?? new Win32PlatformOptions(); + if (opts.RenderingMode is null || !opts.RenderingMode.Any()) { - var opts = AvaloniaLocator.Current.GetService() ?? new Win32PlatformOptions(); + throw new InvalidOperationException($"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.RenderingMode)} must not be empty or null"); + } - var winVersion = Win32Platform.WindowsVersion; - var renderingMode = opts.RenderingMode ?? - (winVersion > PlatformConstants.Windows7 ? Win32RenderingMode.AngleEgl : Win32RenderingMode.Software); - - if (renderingMode == Win32RenderingMode.Wgl) + foreach (var renderingMode in opts.RenderingMode) + { + if (renderingMode == Win32RenderingMode.Software) { - var wgl = WglPlatformOpenGlInterface.TryCreate(); - return wgl; + return null; } - + if (renderingMode == Win32RenderingMode.AngleEgl) { - var egl = AngleWin32PlatformGraphics.TryCreate(AvaloniaLocator.Current.GetService() ?? - new()); + var egl = AngleWin32PlatformGraphics.TryCreate(AvaloniaLocator.Current.GetService() ?? new()); if (egl != null && egl.PlatformApi == AngleOptions.PlatformApi.DirectX11) { - AvaloniaLocator.CurrentMutable.Bind() - .ToConstant(egl); - - var compositionMode = opts.CompositionMode ?? - (WinUiCompositorConnection.IsSupported() ? Win32CompositionMode.WinUIComposition - //: DirectCompositionConnection.IsSupported() ? Win32CompositionMode.DirectComposition - : Win32CompositionMode.RedirectionSurface); + TryRegisterComposition(opts); + return egl; + } + } - switch (compositionMode) - { - case Win32CompositionMode.WinUIComposition: - if (!WinUiCompositorConnection.TryCreateAndRegister()) - { - //goto case Win32CompositionMode.DirectComposition; - } - break; - //case Win32CompositionMode.DirectComposition: - //DirectCompositionConnection.TryCreateAndRegister(); - //break; - case Win32CompositionMode.LowLatencyDxgiSwapChain: - DxgiConnection.TryCreateAndRegister(); - break; - } + if (renderingMode == Win32RenderingMode.Wgl) + { + if (WglPlatformOpenGlInterface.TryCreate() is { } wgl) + { + return wgl; } + } + } + + throw new InvalidOperationException($"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.RenderingMode)} has a value of \"{string.Join(", ", opts.RenderingMode)}\", but no options were applied."); + } + + private static void TryRegisterComposition(Win32PlatformOptions opts) + { + if (opts.CompositionMode is null || !opts.CompositionMode.Any()) + { + throw new InvalidOperationException($"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.CompositionMode)} must not be empty or null"); + } - return egl; + foreach (var compositionMode in opts.CompositionMode) + { + if (compositionMode == Win32CompositionMode.RedirectionSurface) + { + return; } - return null; + if (compositionMode == Win32CompositionMode.WinUIComposition + && WinUiCompositorConnection.IsSupported() + && WinUiCompositorConnection.TryCreateAndRegister()) + { + return; + } + + if (compositionMode == Win32CompositionMode.LowLatencyDxgiSwapChain + && DxgiConnection.TryCreateAndRegister()) + { + return; + } } + + throw new InvalidOperationException($"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.CompositionMode)} has a value of \"{string.Join(", ", opts.CompositionMode)}\", but no options were applied."); } } diff --git a/src/Windows/Avalonia.Win32/Win32Platform.cs b/src/Windows/Avalonia.Win32/Win32Platform.cs index 5082c76d0c..6e727ba4d4 100644 --- a/src/Windows/Avalonia.Win32/Win32Platform.cs +++ b/src/Windows/Avalonia.Win32/Win32Platform.cs @@ -2,6 +2,7 @@ using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.IO; +using System.Linq; using Avalonia.Reactive; using System.Runtime.InteropServices; using System.Threading; @@ -103,8 +104,7 @@ namespace Avalonia.Win32 IPlatformGraphics? platformGraphics; if (options.CustomPlatformGraphics is not null) { - if (options.CompositionMode.HasValue - && options.CompositionMode != Win32CompositionMode.RedirectionSurface) + if (options.CompositionMode?.Contains(Win32CompositionMode.RedirectionSurface) == false) { throw new InvalidOperationException( $"{nameof(Win32PlatformOptions)}.{nameof(Win32PlatformOptions.CustomPlatformGraphics)} is only " + diff --git a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs index 3c7e32f774..9eeb36b7d1 100644 --- a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs +++ b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs @@ -29,7 +29,8 @@ public enum Win32CompositionMode /// /// /// Supported on Windows 10 build 17134 and above. Ignored on other versions. - /// This is recommended option, as it allows window acrylic effects and high refresh rate rendering. + /// This is recommended option, as it allows window acrylic effects and high refresh rate rendering.
+ /// Can only be applied with =. ///
WinUIComposition = 1, @@ -37,16 +38,20 @@ public enum Win32CompositionMode // /// Render Avalonia to a texture inside the DirectComposition tree. // ///
// /// - // /// Supported on Windows 8 and above. Ignored on other versions. + // /// Supported on Windows 8 and above. Ignored on other versions.
+ // /// Can only be applied with =. // ///
// DirectComposition = 2, /// /// When is active, renders Avalonia through a low-latency Dxgi Swapchain. + /// + /// /// Requires Feature Level 11_3 to be active, Windows 8.1+ Any Subversion. /// This is only recommended if low input latency is desirable, and there is no need for the transparency - /// and styling / blurring offered by
. - ///
+ /// and styling / blurring offered by .
+ /// Can only be applied with =. + /// LowLatencyDxgiSwapChain = 3, /// @@ -69,18 +74,32 @@ public class Win32PlatformOptions public bool OverlayPopups { get; set; } /// - /// Avalonia window rendering mode. - /// On Windows 8 and newer default value is , - /// otherwise. + /// Gets or sets Avalonia rendering modes with fallbacks. + /// The first element in the array has the highest priority. + /// The default value is: , . /// - public Win32RenderingMode? RenderingMode { get; set; } + /// + /// If application should work on as wide range of devices as possible, at least add as a fallback value. + /// + /// Thrown if no values were matched. + public IReadOnlyList RenderingMode { get; set; } = new[] + { + Win32RenderingMode.AngleEgl, Win32RenderingMode.Software + }; /// - /// Avalonia window composition mode. - /// On Windows 8 and newer default value is , - /// otherwise. + /// Gets or sets Avalonia composition modes with fallbacks. + /// The first element in the array has the highest priority. + /// The default value is: , . /// - public Win32CompositionMode? CompositionMode { get; set; } + /// + /// If application should work on as wide range of devices as possible, at least add as a fallback value. + /// + /// Thrown if no values were matched. + public IReadOnlyList CompositionMode { get; set; } = new[] + { + Win32CompositionMode.WinUIComposition, Win32CompositionMode.RedirectionSurface + }; /// /// When is set to , create rounded corner blur brushes From 6a50fc5a92648c8975d307b76be0adca4b5981b8 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Mon, 26 Jun 2023 23:43:33 -0400 Subject: [PATCH 16/28] Replace UseGpu and UseEgl parameters with consistent RenderingMode enum on every platform --- .../Avalonia.Android/AndroidPlatform.cs | 71 ++++++++++++++-- .../Egl/EglPlatformGraphics.cs | 7 -- src/Avalonia.X11/Glx/GlxPlatformFeature.cs | 12 --- src/Avalonia.X11/X11Platform.cs | 82 +++++++++++++++---- .../Avalonia.Win32/Win32PlatformOptions.cs | 2 +- 5 files changed, 130 insertions(+), 44 deletions(-) diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index d5d5f211e9..6ea067d8b7 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using Avalonia.Controls; using Avalonia.Android; using Avalonia.Android.Platform; @@ -22,6 +24,36 @@ namespace Avalonia .UseSkia(); } } + + public enum AndroidRenderingMode + { + /// + /// Avalonia is rendered into a framebuffer. + /// + Software = 1, + + /// + /// Enables android EGL rendering. + /// + Egl = 2 + } + + public sealed class AndroidPlatformOptions + { + /// + /// Gets or sets Avalonia rendering modes with fallbacks. + /// The first element in the array has the highest priority. + /// The default value is: , . + /// + /// + /// If application should work on as wide range of devices as possible, at least add as a fallback value. + /// + /// Thrown if no values were matched. + public IReadOnlyList RenderingMode { get; set; } = new[] + { + AndroidRenderingMode.Egl, AndroidRenderingMode.Software + }; + } } namespace Avalonia.Android @@ -47,18 +79,39 @@ namespace Avalonia.Android .Bind().ToConstant(new ChoreographerTimer()) .Bind().ToSingleton(); - if (Options.UseGpu) + var graphics = InitializeGraphics(Options); + if (graphics is not null) { - EglPlatformGraphics.TryInitialize(); + AvaloniaLocator.CurrentMutable.Bind().ToConstant(graphics); } - - Compositor = new Compositor(AvaloniaLocator.Current.GetService()); + + Compositor = new Compositor(graphics); } - } + + private static IPlatformGraphics InitializeGraphics(AndroidPlatformOptions opts) + { + if (opts.RenderingMode is null || !opts.RenderingMode.Any()) + { + throw new InvalidOperationException($"{nameof(AndroidPlatformOptions)}.{nameof(AndroidPlatformOptions.RenderingMode)} must not be empty or null"); + } - public sealed class AndroidPlatformOptions - { - public bool UseDeferredRendering { get; set; } = false; - public bool UseGpu { get; set; } = true; + foreach (var renderingMode in opts.RenderingMode) + { + if (renderingMode == AndroidRenderingMode.Software) + { + return null; + } + + if (renderingMode == AndroidRenderingMode.Egl) + { + if (EglPlatformGraphics.TryCreate() is { } egl) + { + return egl; + } + } + } + + throw new InvalidOperationException($"{nameof(AndroidPlatformOptions)}.{nameof(AndroidPlatformOptions.RenderingMode)} has a value of \"{string.Join(", ", opts.RenderingMode)}\", but no options were applied."); + } } } diff --git a/src/Avalonia.OpenGL/Egl/EglPlatformGraphics.cs b/src/Avalonia.OpenGL/Egl/EglPlatformGraphics.cs index 07e304febe..cf81999095 100644 --- a/src/Avalonia.OpenGL/Egl/EglPlatformGraphics.cs +++ b/src/Avalonia.OpenGL/Egl/EglPlatformGraphics.cs @@ -16,13 +16,6 @@ namespace Avalonia.OpenGL.Egl _display = display; } - public static void TryInitialize() - { - var feature = TryCreate(); - if (feature != null) - AvaloniaLocator.CurrentMutable.Bind().ToConstant(feature); - } - public static EglPlatformGraphics? TryCreate() => TryCreate(() => new EglDisplay(new EglDisplayCreationOptions { Egl = new EglInterface(), diff --git a/src/Avalonia.X11/Glx/GlxPlatformFeature.cs b/src/Avalonia.X11/Glx/GlxPlatformFeature.cs index 06766e0963..58336522c1 100644 --- a/src/Avalonia.X11/Glx/GlxPlatformFeature.cs +++ b/src/Avalonia.X11/Glx/GlxPlatformFeature.cs @@ -15,18 +15,6 @@ namespace Avalonia.X11.Glx IPlatformGraphicsContext IPlatformGraphics.CreateContext() => Display.CreateContext(); public IPlatformGraphicsContext GetSharedContext() => throw new NotSupportedException(); - - public static bool TryInitialize(X11Info x11, IList glProfiles) - { - var feature = TryCreate(x11, glProfiles); - if (feature != null) - { - AvaloniaLocator.CurrentMutable.Bind().ToConstant(feature); - return true; - } - - return false; - } public static GlxPlatformGraphics TryCreate(X11Info x11, IList glProfiles) { diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index a880e4ba1a..7324481aaa 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -93,17 +93,13 @@ namespace Avalonia.X11 XI2 = xi2; } - if (options.UseGpu) + var graphics = InitializeGraphics(options, Info); + if (graphics is not null) { - if (options.UseEGL) - EglPlatformGraphics.TryInitialize(); - else - GlxPlatformGraphics.TryInitialize(Info, Options.GlProfiles); + AvaloniaLocator.CurrentMutable.Bind().ToConstant(graphics); } - var gl = AvaloniaLocator.Current.GetService(); - - Compositor = new Compositor(gl); + Compositor = new Compositor(graphics); } public IntPtr DeferredDisplay { get; set; } @@ -185,25 +181,81 @@ namespace Avalonia.X11 return false; } + + private static IPlatformGraphics InitializeGraphics(X11PlatformOptions opts, X11Info info) + { + if (opts.RenderingMode is null || !opts.RenderingMode.Any()) + { + throw new InvalidOperationException($"{nameof(X11PlatformOptions)}.{nameof(X11PlatformOptions.RenderingMode)} must not be empty or null"); + } + + foreach (var renderingMode in opts.RenderingMode) + { + if (renderingMode == X11RenderingMode.Software) + { + return null; + } + + if (renderingMode == X11RenderingMode.Glx) + { + if (GlxPlatformGraphics.TryCreate(info, opts.GlProfiles) is { } glx) + { + return glx; + } + } + + if (renderingMode == X11RenderingMode.Egl) + { + if (EglPlatformGraphics.TryCreate() is { } egl) + { + return egl; + } + } + } + + throw new InvalidOperationException($"{nameof(X11PlatformOptions)}.{nameof(X11PlatformOptions.RenderingMode)} has a value of \"{string.Join(", ", opts.RenderingMode)}\", but no options were applied."); + } } } namespace Avalonia { + public enum X11RenderingMode + { + /// + /// Avalonia is rendered into a framebuffer. + /// + Software = 1, + + /// + /// Enables Glx rendering. + /// + Glx = 2, + + /// + /// Enables native Linux EGL rendering. + /// + Egl = 3 + } + /// /// Platform-specific options which apply to Linux. /// public class X11PlatformOptions { /// - /// Enables native Linux EGL when set to true. The default value is false. + /// Gets or sets Avalonia rendering modes with fallbacks. + /// The first element in the array has the highest priority. + /// The default value is: , . /// - public bool UseEGL { get; set; } - - /// - /// Determines whether to use GPU for rendering in your project. The default value is true. - /// - public bool UseGpu { get; set; } = true; + /// + /// If application should work on as wide range of devices as possible, at least add as a fallback value. + /// + /// Thrown if no values were matched. + public IReadOnlyList RenderingMode { get; set; } = new[] + { + X11RenderingMode.Glx, X11RenderingMode.Software + }; /// /// Embeds popups to the window when set to true. The default value is false. diff --git a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs index 9eeb36b7d1..f4765a558f 100644 --- a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs +++ b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs @@ -19,7 +19,7 @@ public enum Win32RenderingMode /// /// Avalonia would try to use native Widows OpenGL with GPU rendering. /// - Wgl + Wgl = 3 } public enum Win32CompositionMode From d42a40fda7d47d6f88e9819068626f8c2f54945c Mon Sep 17 00:00:00 2001 From: Max Katz Date: Mon, 26 Jun 2023 23:43:50 -0400 Subject: [PATCH 17/28] Remove AvaloniaNativePlatformOptions.UseGpu as it's not supported in 11.0 (always uses GPU) --- src/Avalonia.Native/AvaloniaNativePlatform.cs | 24 +++++++++---------- .../AvaloniaNativePlatformExtensions.cs | 6 +---- src/Avalonia.Native/WindowImpl.cs | 2 +- src/Avalonia.Native/WindowImplBase.cs | 5 ++-- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/Avalonia.Native/AvaloniaNativePlatform.cs b/src/Avalonia.Native/AvaloniaNativePlatform.cs index 5d5e17839f..03f0129200 100644 --- a/src/Avalonia.Native/AvaloniaNativePlatform.cs +++ b/src/Avalonia.Native/AvaloniaNativePlatform.cs @@ -123,20 +123,18 @@ namespace Avalonia.Native hotkeys.MoveCursorToTheEndOfLineWithSelection.Add(new KeyGesture(Key.Right, hotkeys.CommandModifiers | hotkeys.SelectionModifiers)); AvaloniaLocator.CurrentMutable.Bind().ToConstant(hotkeys); - - if (_options.UseGpu) + + // TODO: add software and metal support via RenderingMode options param + try + { + _platformGl = new AvaloniaNativeGlPlatformGraphics(_factory.ObtainGlDisplay()); + AvaloniaLocator.CurrentMutable + .Bind().ToConstant(_platformGl); + + } + catch (Exception) { - try - { - _platformGl = new AvaloniaNativeGlPlatformGraphics(_factory.ObtainGlDisplay()); - AvaloniaLocator.CurrentMutable - .Bind().ToConstant(_platformGl); - - } - catch (Exception) - { - // ignored - } + // ignored } Compositor = new Compositor(_platformGl, true); diff --git a/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs b/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs index 2b989ce733..7e01e03c9c 100644 --- a/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs +++ b/src/Avalonia.Native/AvaloniaNativePlatformExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Avalonia.Controls; using Avalonia.Native; @@ -30,11 +31,6 @@ namespace Avalonia /// public class AvaloniaNativePlatformOptions { - /// - /// Determines whether to use GPU for rendering in your project. The default value is true. - /// - public bool UseGpu { get; set; } = true; - /// /// Embeds popups to the window when set to true. The default value is false. /// diff --git a/src/Avalonia.Native/WindowImpl.cs b/src/Avalonia.Native/WindowImpl.cs index 817fe3d080..ba96dd401a 100644 --- a/src/Avalonia.Native/WindowImpl.cs +++ b/src/Avalonia.Native/WindowImpl.cs @@ -24,7 +24,7 @@ namespace Avalonia.Native private bool _canResize = true; internal WindowImpl(IAvaloniaNativeFactory factory, AvaloniaNativePlatformOptions opts, - AvaloniaNativeGlPlatformGraphics glFeature) : base(factory, opts, glFeature) + AvaloniaNativeGlPlatformGraphics glFeature) : base(factory, glFeature) { _opts = opts; _glFeature = glFeature; diff --git a/src/Avalonia.Native/WindowImplBase.cs b/src/Avalonia.Native/WindowImplBase.cs index 760816643e..053957f89f 100644 --- a/src/Avalonia.Native/WindowImplBase.cs +++ b/src/Avalonia.Native/WindowImplBase.cs @@ -67,11 +67,10 @@ namespace Avalonia.Native private PlatformBehaviorInhibition _platformBehaviorInhibition; private WindowTransparencyLevel _transparencyLevel = WindowTransparencyLevel.None; - internal WindowBaseImpl(IAvaloniaNativeFactory factory, AvaloniaNativePlatformOptions opts, - AvaloniaNativeGlPlatformGraphics glFeature) + internal WindowBaseImpl(IAvaloniaNativeFactory factory, AvaloniaNativeGlPlatformGraphics glFeature) { _factory = factory; - _gpu = opts.UseGpu && glFeature != null; + _gpu = glFeature != null; _keyboard = AvaloniaLocator.Current.GetService(); _mouse = new MouseDevice(); From 290fd5caf38b140c976bcc2cd20edf7cffb9a9d7 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Mon, 26 Jun 2023 23:46:33 -0400 Subject: [PATCH 18/28] Add missing docs --- src/Android/Avalonia.Android/AndroidPlatform.cs | 3 +++ src/Avalonia.X11/X11Platform.cs | 3 +++ src/Windows/Avalonia.Win32/Win32PlatformOptions.cs | 6 ++++++ 3 files changed, 12 insertions(+) diff --git a/src/Android/Avalonia.Android/AndroidPlatform.cs b/src/Android/Avalonia.Android/AndroidPlatform.cs index 6ea067d8b7..144909db4b 100644 --- a/src/Android/Avalonia.Android/AndroidPlatform.cs +++ b/src/Android/Avalonia.Android/AndroidPlatform.cs @@ -25,6 +25,9 @@ namespace Avalonia } } + /// + /// Represents the rendering mode for platform graphics. + /// public enum AndroidRenderingMode { /// diff --git a/src/Avalonia.X11/X11Platform.cs b/src/Avalonia.X11/X11Platform.cs index 7324481aaa..c70c17d523 100644 --- a/src/Avalonia.X11/X11Platform.cs +++ b/src/Avalonia.X11/X11Platform.cs @@ -220,6 +220,9 @@ namespace Avalonia.X11 namespace Avalonia { + /// + /// Represents the rendering mode for platform graphics. + /// public enum X11RenderingMode { /// diff --git a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs index f4765a558f..bbb4c37d7e 100644 --- a/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs +++ b/src/Windows/Avalonia.Win32/Win32PlatformOptions.cs @@ -4,6 +4,9 @@ using Avalonia.Platform; namespace Avalonia; +/// +/// Represents the rendering mode for platform graphics. +/// public enum Win32RenderingMode { /// @@ -22,6 +25,9 @@ public enum Win32RenderingMode Wgl = 3 } +/// +/// Represents the Win32 window composition mode. +/// public enum Win32CompositionMode { /// From e5098cfd6211672cbc2841056bd1b50ea4136af5 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Tue, 27 Jun 2023 00:00:21 -0400 Subject: [PATCH 19/28] Fix build --- src/Avalonia.Native/PopupImpl.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Native/PopupImpl.cs b/src/Avalonia.Native/PopupImpl.cs index 6b7f7e8883..6cef7ea578 100644 --- a/src/Avalonia.Native/PopupImpl.cs +++ b/src/Avalonia.Native/PopupImpl.cs @@ -15,7 +15,7 @@ namespace Avalonia.Native public PopupImpl(IAvaloniaNativeFactory factory, AvaloniaNativePlatformOptions opts, AvaloniaNativeGlPlatformGraphics glFeature, - IWindowBaseImpl parent) : base(factory, opts, glFeature) + IWindowBaseImpl parent) : base(factory, glFeature) { _opts = opts; _glFeature = glFeature; From 862c175725bfb3978a32d6d03e94b2a8b1f9308c Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Tue, 27 Jun 2023 12:27:30 +0200 Subject: [PATCH 20/28] Fix ScrollContentPresenter's child margin with layout rounding --- .../Presenters/ScrollContentPresenter.cs | 12 +++++-- .../Presenters/ScrollContentPresenterTests.cs | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs index bd694b200c..bd3d2b5171 100644 --- a/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs +++ b/src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs @@ -7,7 +7,7 @@ using Avalonia.Input.GestureRecognizers; using Avalonia.Utilities; using Avalonia.VisualTree; using System.Linq; -using Avalonia.Interactivity; +using Avalonia.Layout; namespace Avalonia.Controls.Presenters { @@ -473,7 +473,15 @@ namespace Avalonia.Controls.Presenters } Viewport = finalSize; - Extent = Child!.Bounds.Size.Inflate(Child.Margin); + + var childMargin = Child!.Margin; + if (Child.UseLayoutRounding) + { + var scale = LayoutHelper.GetLayoutScale(Child); + childMargin = LayoutHelper.RoundLayoutThickness(childMargin, scale, scale); + } + + Extent = Child!.Bounds.Size.Inflate(childMargin); _isAnchorElementDirty = true; return finalSize; diff --git a/tests/Avalonia.Controls.UnitTests/Presenters/ScrollContentPresenterTests.cs b/tests/Avalonia.Controls.UnitTests/Presenters/ScrollContentPresenterTests.cs index f6130d49d6..c7ea5c1b69 100644 --- a/tests/Avalonia.Controls.UnitTests/Presenters/ScrollContentPresenterTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Presenters/ScrollContentPresenterTests.cs @@ -4,6 +4,7 @@ using System.Reactive.Linq; using Avalonia.Controls.Presenters; using Avalonia.Controls.Primitives; using Avalonia.Layout; +using Avalonia.UnitTests; using Xunit; namespace Avalonia.Controls.UnitTests.Presenters @@ -244,6 +245,36 @@ namespace Avalonia.Controls.UnitTests.Presenters Assert.Equal(new Size(110, 110), target.Extent); } + [Fact] + public void Extent_Should_Include_Content_Margin_Scaled_With_Layout_Rounding() + { + var root = new TestRoot + { + LayoutScaling = 1.25, + UseLayoutRounding = true + }; + + var target = new ScrollContentPresenter + { + HorizontalAlignment = HorizontalAlignment.Center, + VerticalAlignment = VerticalAlignment.Center, + Content = new Border + { + Width = 200, + Height = 200, + Margin = new Thickness(2) + } + }; + + root.Child = target; + target.UpdateChild(); + target.Measure(new Size(1000, 1000)); + target.Arrange(new Rect(0, 0, 1000, 1000)); + + Assert.Equal(new Size(203.2, 203.2), target.Viewport); + Assert.Equal(new Size(203.2, 203.2), target.Extent); + } + [Fact] public void Extent_Width_Should_Be_Arrange_Width_When_CanScrollHorizontally_False() { From b488afaf799eb50bcc1de5f6728024557a3373e3 Mon Sep 17 00:00:00 2001 From: Valters Melnalksnis Date: Tue, 27 Jun 2023 13:49:20 +0300 Subject: [PATCH 21/28] Fix non-configurable minimum width for DataGridColumnHeader --- src/Avalonia.Controls.DataGrid/Themes/Fluent.xaml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Avalonia.Controls.DataGrid/Themes/Fluent.xaml b/src/Avalonia.Controls.DataGrid/Themes/Fluent.xaml index 082eac60be..0cc620dae9 100644 --- a/src/Avalonia.Controls.DataGrid/Themes/Fluent.xaml +++ b/src/Avalonia.Controls.DataGrid/Themes/Fluent.xaml @@ -42,9 +42,10 @@ - + 0.6 0.8 + 32 M1875 1011l-787 787v-1798h-128v1798l-787 -787l-90 90l941 941l941 -941z M1965 947l-941 -941l-941 941l90 90l787 -787v1798h128v-1798l787 787z @@ -174,7 +175,7 @@ VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> - + - Date: Wed, 28 Jun 2023 01:27:12 +0300 Subject: [PATCH 22/28] fix SendDataRequest re-run task --- src/Avalonia.X11/X11Clipboard.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.X11/X11Clipboard.cs b/src/Avalonia.X11/X11Clipboard.cs index 04d1aae194..6e18151fae 100644 --- a/src/Avalonia.X11/X11Clipboard.cs +++ b/src/Avalonia.X11/X11Clipboard.cs @@ -224,7 +224,7 @@ namespace Avalonia.X11 private Task SendDataRequest(IntPtr format) { - if (_requestedDataTcs == null || _requestedFormatsTcs.Task.IsCompleted) + if (_requestedDataTcs == null || _requestedDataTcs.Task.IsCompleted) _requestedDataTcs = new TaskCompletionSource(); XConvertSelection(_x11.Display, _x11.Atoms.CLIPBOARD, format, format, _handle, IntPtr.Zero); return _requestedDataTcs.Task; From 7ac63a593b9abcaa5dc8c8833a78a285329c749f Mon Sep 17 00:00:00 2001 From: Max Katz Date: Wed, 28 Jun 2023 15:22:33 -0400 Subject: [PATCH 23/28] Seal geometry class --- src/Avalonia.Base/Media/Geometry.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Avalonia.Base/Media/Geometry.cs b/src/Avalonia.Base/Media/Geometry.cs index 0d2311eafc..3e0e6018de 100644 --- a/src/Avalonia.Base/Media/Geometry.cs +++ b/src/Avalonia.Base/Media/Geometry.cs @@ -28,6 +28,11 @@ namespace Avalonia.Media TransformProperty.Changed.AddClassHandler((x,e) => x.TransformChanged(e)); } + internal Geometry() + { + + } + /// /// Raised when the geometry changes. /// From fbca526f85b64cd9b26fe03213d3d0c96344a534 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Wed, 28 Jun 2023 15:31:29 -0400 Subject: [PATCH 24/28] Seal CreateDefiningGeometry --- src/Avalonia.Base/CombinedGeometry.cs | 4 +--- src/Avalonia.Base/Media/EllipseGeometry.cs | 2 +- src/Avalonia.Base/Media/Geometry.cs | 2 +- src/Avalonia.Base/Media/GeometryGroup.cs | 4 +--- src/Avalonia.Base/Media/LineGeometry.cs | 2 +- src/Avalonia.Base/Media/PathGeometry.cs | 4 ++-- src/Avalonia.Base/Media/PlatformGeometry.cs | 4 ++-- src/Avalonia.Base/Media/PolylineGeometry.cs | 2 +- src/Avalonia.Base/Media/RectangleGeometry.cs | 2 +- src/Avalonia.Base/Media/StreamGeometry.cs | 4 ++-- tests/Avalonia.Base.UnitTests/Media/GeometryTests.cs | 2 +- 11 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Avalonia.Base/CombinedGeometry.cs b/src/Avalonia.Base/CombinedGeometry.cs index 4b5866519b..a47e756c88 100644 --- a/src/Avalonia.Base/CombinedGeometry.cs +++ b/src/Avalonia.Base/CombinedGeometry.cs @@ -3,8 +3,6 @@ using System.Collections.Generic; using System.Text; using Avalonia.Platform; -#nullable enable - namespace Avalonia.Media { public enum GeometryCombineMode @@ -147,7 +145,7 @@ namespace Avalonia.Media return new CombinedGeometry(GeometryCombineMode, Geometry1, Geometry2, Transform); } - protected override IGeometryImpl? CreateDefiningGeometry() + private protected sealed override IGeometryImpl? CreateDefiningGeometry() { var g1 = Geometry1; var g2 = Geometry2; diff --git a/src/Avalonia.Base/Media/EllipseGeometry.cs b/src/Avalonia.Base/Media/EllipseGeometry.cs index 84d74e888e..bb1263d8d5 100644 --- a/src/Avalonia.Base/Media/EllipseGeometry.cs +++ b/src/Avalonia.Base/Media/EllipseGeometry.cs @@ -135,7 +135,7 @@ namespace Avalonia.Media } /// - protected override IGeometryImpl? CreateDefiningGeometry() + private protected sealed override IGeometryImpl? CreateDefiningGeometry() { var factory = AvaloniaLocator.Current.GetRequiredService(); diff --git a/src/Avalonia.Base/Media/Geometry.cs b/src/Avalonia.Base/Media/Geometry.cs index 3e0e6018de..a66cd616a3 100644 --- a/src/Avalonia.Base/Media/Geometry.cs +++ b/src/Avalonia.Base/Media/Geometry.cs @@ -139,7 +139,7 @@ namespace Avalonia.Media /// Creates the platform implementation of the geometry, without the transform applied. /// /// - protected abstract IGeometryImpl? CreateDefiningGeometry(); + private protected abstract IGeometryImpl? CreateDefiningGeometry(); /// /// Invalidates the platform implementation of the geometry. diff --git a/src/Avalonia.Base/Media/GeometryGroup.cs b/src/Avalonia.Base/Media/GeometryGroup.cs index 3e61413919..20bd297fc1 100644 --- a/src/Avalonia.Base/Media/GeometryGroup.cs +++ b/src/Avalonia.Base/Media/GeometryGroup.cs @@ -1,8 +1,6 @@ using Avalonia.Metadata; using Avalonia.Platform; -#nullable enable - namespace Avalonia.Media { /// @@ -72,7 +70,7 @@ namespace Avalonia.Media newChildren.Parent = this; } - protected override IGeometryImpl? CreateDefiningGeometry() + private protected sealed override IGeometryImpl? CreateDefiningGeometry() { if (_children.Count > 0) { diff --git a/src/Avalonia.Base/Media/LineGeometry.cs b/src/Avalonia.Base/Media/LineGeometry.cs index 6ac92ea33b..ced208e30b 100644 --- a/src/Avalonia.Base/Media/LineGeometry.cs +++ b/src/Avalonia.Base/Media/LineGeometry.cs @@ -68,7 +68,7 @@ namespace Avalonia.Media } /// - protected override IGeometryImpl? CreateDefiningGeometry() + private protected sealed override IGeometryImpl? CreateDefiningGeometry() { var factory = AvaloniaLocator.Current.GetRequiredService(); diff --git a/src/Avalonia.Base/Media/PathGeometry.cs b/src/Avalonia.Base/Media/PathGeometry.cs index 8292afde7e..bdfbfadce4 100644 --- a/src/Avalonia.Base/Media/PathGeometry.cs +++ b/src/Avalonia.Base/Media/PathGeometry.cs @@ -43,7 +43,7 @@ namespace Avalonia.Media /// /// The s. /// - public static new PathGeometry Parse(string pathData) + public new static PathGeometry Parse(string pathData) { var pathGeometry = new PathGeometry(); @@ -81,7 +81,7 @@ namespace Avalonia.Media set { SetValue(FillRuleProperty, value); } } - protected override IGeometryImpl? CreateDefiningGeometry() + private protected sealed override IGeometryImpl? CreateDefiningGeometry() { var figures = Figures; diff --git a/src/Avalonia.Base/Media/PlatformGeometry.cs b/src/Avalonia.Base/Media/PlatformGeometry.cs index f25a14540f..e1488a8229 100644 --- a/src/Avalonia.Base/Media/PlatformGeometry.cs +++ b/src/Avalonia.Base/Media/PlatformGeometry.cs @@ -2,7 +2,7 @@ namespace Avalonia.Media { - internal class PlatformGeometry : Geometry + internal sealed class PlatformGeometry : Geometry { private readonly IGeometryImpl _geometryImpl; @@ -16,7 +16,7 @@ namespace Avalonia.Media return new PlatformGeometry(_geometryImpl); } - protected override IGeometryImpl? CreateDefiningGeometry() + private protected override IGeometryImpl? CreateDefiningGeometry() { return _geometryImpl; } diff --git a/src/Avalonia.Base/Media/PolylineGeometry.cs b/src/Avalonia.Base/Media/PolylineGeometry.cs index b0229b6455..47cf2f48a4 100644 --- a/src/Avalonia.Base/Media/PolylineGeometry.cs +++ b/src/Avalonia.Base/Media/PolylineGeometry.cs @@ -74,7 +74,7 @@ namespace Avalonia.Media return new PolylineGeometry(Points, IsFilled); } - protected override IGeometryImpl? CreateDefiningGeometry() + private protected sealed override IGeometryImpl? CreateDefiningGeometry() { var factory = AvaloniaLocator.Current.GetRequiredService(); var geometry = factory.CreateStreamGeometry(); diff --git a/src/Avalonia.Base/Media/RectangleGeometry.cs b/src/Avalonia.Base/Media/RectangleGeometry.cs index 0bf9eb5664..01771242a7 100644 --- a/src/Avalonia.Base/Media/RectangleGeometry.cs +++ b/src/Avalonia.Base/Media/RectangleGeometry.cs @@ -47,7 +47,7 @@ namespace Avalonia.Media /// public override Geometry Clone() => new RectangleGeometry(Rect); - protected override IGeometryImpl? CreateDefiningGeometry() + private protected sealed override IGeometryImpl? CreateDefiningGeometry() { var factory = AvaloniaLocator.Current.GetRequiredService(); diff --git a/src/Avalonia.Base/Media/StreamGeometry.cs b/src/Avalonia.Base/Media/StreamGeometry.cs index fb79488e0f..9969376896 100644 --- a/src/Avalonia.Base/Media/StreamGeometry.cs +++ b/src/Avalonia.Base/Media/StreamGeometry.cs @@ -31,7 +31,7 @@ namespace Avalonia.Media /// /// The string. /// A . - public static new StreamGeometry Parse(string s) + public new static StreamGeometry Parse(string s) { var streamGeometry = new StreamGeometry(); @@ -62,7 +62,7 @@ namespace Avalonia.Media } /// - protected override IGeometryImpl? CreateDefiningGeometry() + private protected override IGeometryImpl? CreateDefiningGeometry() { if (_impl == null) { diff --git a/tests/Avalonia.Base.UnitTests/Media/GeometryTests.cs b/tests/Avalonia.Base.UnitTests/Media/GeometryTests.cs index ae8dc9dad2..6d724c86b5 100644 --- a/tests/Avalonia.Base.UnitTests/Media/GeometryTests.cs +++ b/tests/Avalonia.Base.UnitTests/Media/GeometryTests.cs @@ -103,7 +103,7 @@ namespace Avalonia.Base.UnitTests.Media throw new NotImplementedException(); } - protected override IGeometryImpl CreateDefiningGeometry() + private protected sealed override IGeometryImpl CreateDefiningGeometry() { return Mock.Of( x => x.WithTransform(It.IsAny()) == From 544cf0fdabd2c9f44219d79513caf690f4dd5135 Mon Sep 17 00:00:00 2001 From: Trym Lund Flogard Date: Wed, 28 Jun 2023 22:36:50 +0200 Subject: [PATCH 25/28] Revert "Remove support for undocumented blur #11855" This reverts commit 6a3925b91b67e2a3412a7d3e3e11ba24589dfeff. --- .../Interop/UnmanagedMethods.cs | 3 ++ src/Windows/Avalonia.Win32/WindowImpl.cs | 53 +++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index e58abcadf0..ce42227292 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -1827,6 +1827,9 @@ namespace Avalonia.Win32.Interop return result; } + [DllImport("user32.dll")] + internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data); + [Flags] public enum GCS : uint { diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index 3fc2751e43..057cdb2db0 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -371,7 +371,7 @@ namespace Avalonia.Win32 if (level == WindowTransparencyLevel.Transparent) SetTransparencyTransparent(windowsVersion); else if (level == WindowTransparencyLevel.Blur) - continue; // Unsupported on all versions (unreachable) + SetTransparencyBlur(windowsVersion); else if (level == WindowTransparencyLevel.AcrylicBlur) SetTransparencyAcrylicBlur(windowsVersion); else if (level == WindowTransparencyLevel.Mica) @@ -409,9 +409,9 @@ namespace Avalonia.Win32 if (level == WindowTransparencyLevel.Transparent) return windowsVersion >= PlatformConstants.Windows8; - // Support for legacy, undocumented DWM blur has been removed. + // Blur only supported on Windows 8 and lower. if (level == WindowTransparencyLevel.Blur) - return false; + return windowsVersion < PlatformConstants.Windows10; // Acrylic is supported on Windows >= 10.0.15063. if (level == WindowTransparencyLevel.AcrylicBlur) @@ -426,14 +426,37 @@ namespace Avalonia.Win32 private void SetTransparencyTransparent(Version windowsVersion) { - // Transparent only supported with composition on Windows 10+. - if (!_isUsingComposition || windowsVersion < PlatformConstants.Windows10) + // Transparent only supported with composition on Windows 8+. + if (!_isUsingComposition || windowsVersion < PlatformConstants.Windows8) return; + if (windowsVersion < PlatformConstants.Windows10) + { + // Some of the AccentState Enum's values have different meanings on Windows 8.x than on + // Windows 10, hence using ACCENT_ENABLE_BLURBEHIND to disable blurbehind ¯\_(ツ)_/¯. + // Hey, I'm just porting what was here before. + SetAccentState(AccentState.ACCENT_ENABLE_BLURBEHIND); + var blurInfo = new DWM_BLURBEHIND(false); + DwmEnableBlurBehindWindow(_hwnd, ref blurInfo); + } + SetUseHostBackdropBrush(false); _blurHost?.SetBlur(BlurEffect.None); } + private void SetTransparencyBlur(Version windowsVersion) + { + // Blur only supported with composition on Windows 8 and lower. + if (!_isUsingComposition || windowsVersion >= PlatformConstants.Windows10) + return; + + // Some of the AccentState Enum's values have different meanings on Windows 8.x than on + // Windows 10. + SetAccentState(AccentState.ACCENT_DISABLED); + var blurInfo = new DWM_BLURBEHIND(true); + DwmEnableBlurBehindWindow(_hwnd, ref blurInfo); + } + private void SetTransparencyAcrylicBlur(Version windowsVersion) { // Acrylic blur only supported with composition on Windows >= 10.0.15063. @@ -454,6 +477,26 @@ namespace Avalonia.Win32 _blurHost?.SetBlur(BlurEffect.Mica); } + private void SetAccentState(AccentState state) + { + var accent = new AccentPolicy(); + var accentStructSize = Marshal.SizeOf(accent); + + //Some of the AccentState Enum's values have different meanings on Windows 8.x than on Windows 10 + accent.AccentState = state; + + var accentPtr = Marshal.AllocHGlobal(accentStructSize); + Marshal.StructureToPtr(accent, accentPtr, false); + + var data = new WindowCompositionAttributeData(); + data.Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY; + data.SizeOfData = accentStructSize; + data.Data = accentPtr; + + SetWindowCompositionAttribute(_hwnd, ref data); + Marshal.FreeHGlobal(accentPtr); + } + private void SetUseHostBackdropBrush(bool useHostBackdropBrush) { if (Win32Platform.WindowsVersion < WinUiCompositionShared.MinHostBackdropVersion) From 8f7cf7cc4a8d8d5f937f3d20b61f026ec007696f Mon Sep 17 00:00:00 2001 From: Trym Lund Flogard Date: Wed, 28 Jun 2023 22:47:11 +0200 Subject: [PATCH 26/28] Invoke SetWindowCompositionAttribute dynamically without exposing it to ILC --- .../Avalonia.Win32/Interop/UnmanagedMethods.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index ce42227292..68f3e3c670 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -1827,8 +1827,21 @@ namespace Avalonia.Win32.Interop return result; } - [DllImport("user32.dll")] - internal static extern int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data); + internal static int SetWindowCompositionAttribute(IntPtr hwnd, ref WindowCompositionAttributeData data) + { + var user32 = LoadLibrary("user32.dll"); + var pfnSetWindowCompositionAttribute = (delegate* unmanaged[Stdcall])GetProcAddress(user32, nameof(SetWindowCompositionAttribute)); + if (pfnSetWindowCompositionAttribute == null) + { + // This preserves the same behavior as using the DllImport attribute. + throw new EntryPointNotFoundException("The unsupported SetWindowCompositionAttribute-function has been removed from the operating system."); + } + + fixed (WindowCompositionAttributeData* pData = &data) + { + return pfnSetWindowCompositionAttribute(hwnd, pData); + } + } [Flags] public enum GCS : uint From d76c06d4c4e77bd29577b0671b69ba13d3dcc9d5 Mon Sep 17 00:00:00 2001 From: Benedikt Stebner Date: Thu, 29 Jun 2023 10:55:29 +0200 Subject: [PATCH 27/28] Make TextInputEventArgs.Text settable --- src/Avalonia.Base/Input/TextInputEventArgs.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Avalonia.Base/Input/TextInputEventArgs.cs b/src/Avalonia.Base/Input/TextInputEventArgs.cs index f4acd694a0..305b4d81f8 100644 --- a/src/Avalonia.Base/Input/TextInputEventArgs.cs +++ b/src/Avalonia.Base/Input/TextInputEventArgs.cs @@ -4,6 +4,6 @@ namespace Avalonia.Input { public class TextInputEventArgs : RoutedEventArgs { - public string? Text { get; init; } + public string? Text { get; set; } } } From 1381ec28be5202139fca91ac6c7f2539401171bc Mon Sep 17 00:00:00 2001 From: Max Katz Date: Thu, 29 Jun 2023 17:04:24 -0400 Subject: [PATCH 28/28] Allow only Light and Dark in the ColorPaletteResourcesCollection --- samples/ControlCatalog/App.xaml | 3 +++ samples/ControlCatalog/App.xaml.cs | 4 +-- .../ColorPaletteResourcesCollection.cs | 25 +++++++++---------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/samples/ControlCatalog/App.xaml b/samples/ControlCatalog/App.xaml index 64bf3e53b3..02b1242471 100644 --- a/samples/ControlCatalog/App.xaml +++ b/samples/ControlCatalog/App.xaml @@ -28,6 +28,9 @@ + + + diff --git a/samples/ControlCatalog/App.xaml.cs b/samples/ControlCatalog/App.xaml.cs index 246fe4385f..605254c995 100644 --- a/samples/ControlCatalog/App.xaml.cs +++ b/samples/ControlCatalog/App.xaml.cs @@ -30,8 +30,8 @@ namespace ControlCatalog AvaloniaXamlLoader.Load(this); - _fluentTheme = new FluentTheme(); - _simpleTheme = new SimpleTheme(); + _fluentTheme = (FluentTheme)Resources["FluentTheme"]!; + _simpleTheme = (SimpleTheme)Resources["SimpleTheme"]!; _colorPickerFluent = (IStyle)Resources["ColorPickerFluent"]!; _colorPickerSimple = (IStyle)Resources["ColorPickerSimple"]!; _dataGridFluent = (IStyle)Resources["DataGridFluent"]!; diff --git a/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs b/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs index f26f6d8660..6eb465d399 100644 --- a/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs +++ b/src/Avalonia.Themes.Fluent/ColorPaletteResourcesCollection.cs @@ -10,12 +10,18 @@ internal class ColorPaletteResourcesCollection : AvaloniaDictionary + (key, x) => { if (Owner is not null) { x.PropertyChanged += Palette_PropertyChanged; } + + if (key != ThemeVariant.Dark && key != ThemeVariant.Light) + { + throw new InvalidOperationException( + $"{nameof(FluentTheme)}.{nameof(FluentTheme.Palettes)} only supports Light and Dark variants."); + } }, (_, x) => { @@ -30,22 +36,15 @@ internal class ColorPaletteResourcesCollection : AvaloniaDictionary Count > 0; public bool TryGetResource(object key, ThemeVariant? theme, out object? value) { - if (theme is not null) + if (theme == null || theme == ThemeVariant.Default) { - if (base.TryGetValue(theme, out var themePaletteResources) - && themePaletteResources.TryGetResource(key, theme, out value)) - { - return true; - } + theme = ThemeVariant.Light; } - if (theme != ThemeVariant.Default) + if (base.TryGetValue(theme, out var themePaletteResources) + && themePaletteResources.TryGetResource(key, theme, out value)) { - if (base.TryGetValue(ThemeVariant.Default, out var defaultPaletteResources) - && defaultPaletteResources.TryGetResource(key, ThemeVariant.Default, out value)) - { - return true; - } + return true; } value = null;