From f2f2016539fe54fba1a22a9660ba577e9c2a29e6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 6 Aug 2024 22:51:36 -0700 Subject: [PATCH 1/4] Switch to 'CanvasSwapChain.CreateForWindowId' --- .../ICanvasFactoryNative.cs | 80 ---------------- .../Backend/Win32Application.cs | 94 ++----------------- .../ComputeSharp.SwapChain.D2D1.Cli.csproj | 7 ++ 3 files changed, 14 insertions(+), 167 deletions(-) delete mode 100644 samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs deleted file mode 100644 index 49bfa283..00000000 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/ABI.Microsoft.Graphics.Canvas/ICanvasFactoryNative.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using TerraFX.Interop.Windows; -using TerraFX.Interop; - -#pragma warning disable CS0649, IDE0055, IDE1006 - -namespace ABI.Microsoft.Graphics.Canvas; - -/// -/// Raw bindings for the interop interface for the activation factory. -/// -internal unsafe struct ICanvasFactoryNative : IUnknown.Interface -{ - public void** lpVtbl; - - /// - static Guid* INativeGuid.NativeGuid - { - get - { - ReadOnlySpan data = - [ - 0x0D, 0x44, 0x5C, 0x69, - 0xB3, 0x04, - 0xDD, 0x4E, - 0xBF, 0xD9, - 0x63, - 0xE5, - 0x1E, - 0x9F, - 0x72, - 0x02 - ]; - - return (Guid*)Unsafe.AsPointer(ref MemoryMarshal.GetReference(data)); - } - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public HRESULT QueryInterface(Guid* riid, void** ppvObject) - { - return ((delegate* unmanaged[MemberFunction])this.lpVtbl[0])((ICanvasFactoryNative*)Unsafe.AsPointer(ref this), riid, ppvObject); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public uint AddRef() - { - return ((delegate* unmanaged[MemberFunction])this.lpVtbl[1])((ICanvasFactoryNative*)Unsafe.AsPointer(ref this)); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public uint Release() - { - return ((delegate* unmanaged[MemberFunction])this.lpVtbl[2])((ICanvasFactoryNative*)Unsafe.AsPointer(ref this)); - } - - /// - /// Gets or creates an IInspectable Win2D wrapper for a given native D2D resource. - /// - /// The input canvas device (as a marshalled ). - /// The input native resource to create a wrapper for. - /// The realization DPIs for - /// The resulting wrapper for . - /// The HRESULT for the operation. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public HRESULT GetOrCreate(IUnknown* device, IUnknown* resource, float dpi, void** wrapper) - { - return ((delegate* unmanaged[MemberFunction])this.lpVtbl[6])( - (ICanvasFactoryNative*)Unsafe.AsPointer(ref this), - device, - resource, - dpi, - wrapper); - } -} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs index 4eef52a2..0a6a4be3 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs @@ -1,10 +1,7 @@ using System; using System.Diagnostics.CodeAnalysis; -using ABI.Microsoft.Graphics.Canvas; -using TerraFX.Interop.DirectX; +using Microsoft.UI; using TerraFX.Interop.Windows; -using TerraFX.Interop.WinRT; -using WinRT; namespace ComputeSharp.SwapChain.D2D1.Backend; @@ -13,7 +10,6 @@ using CanvasDevice = Microsoft.Graphics.Canvas.CanvasDevice; using CanvasDrawingSession = Microsoft.Graphics.Canvas.CanvasDrawingSession; using CanvasSwapChain = Microsoft.Graphics.Canvas.CanvasSwapChain; using DirectXPixelFormat = Windows.Graphics.DirectX.DirectXPixelFormat; -using Windows = TerraFX.Interop.Windows.Windows; /// /// A simple Win32 application handling a D2D swapchain. @@ -60,88 +56,12 @@ internal sealed class Win32Application { // Create a new canvas device, which will handle DX11/D2D initialization CanvasDevice canvasDevice = new(); - CanvasSwapChain canvasSwapChain; - - // Create the swap chain to display frames - using (ComPtr direct3DDeviceUnknown = default) - using (ComPtr dxgiFactory2 = default) - using (ComPtr dxgiSwapChain1 = default) - { - HRESULT hresult; - - // Extract the underlying Direct3D device from the canvas device. - // This is needed for CreateSwapChainForHwnd, as the owning device. - using (ComPtr canvasDeviceUnknown = default) - using (ComPtr direct3DDxgiInterfaceAccess = default) - { - canvasDeviceUnknown.Attach((IUnknown*)MarshalInspectable.FromManaged(canvasDevice)); - - hresult = canvasDeviceUnknown.CopyTo(direct3DDxgiInterfaceAccess.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - - hresult = direct3DDxgiInterfaceAccess.Get()->GetInterface(Windows.__uuidof(), (void**)direct3DDeviceUnknown.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - } - - // Create the DXGIFactory2 instance to create the swapchain with - hresult = DirectX.CreateDXGIFactory2(DXGI.DXGI_CREATE_FACTORY_DEBUG, Windows.__uuidof(), (void**)dxgiFactory2.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - - DXGI_SWAP_CHAIN_DESC1 dxgiSwapChainDesc1 = default; - dxgiSwapChainDesc1.AlphaMode = DXGI_ALPHA_MODE.DXGI_ALPHA_MODE_IGNORE; - dxgiSwapChainDesc1.BufferCount = 2; - dxgiSwapChainDesc1.BufferUsage = DXGI.DXGI_USAGE_RENDER_TARGET_OUTPUT; - dxgiSwapChainDesc1.Flags = 0; - dxgiSwapChainDesc1.Format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; - dxgiSwapChainDesc1.Width = this.screenWidth = 1; - dxgiSwapChainDesc1.Height = this.screenHeight = 1; - dxgiSwapChainDesc1.SampleDesc = new DXGI_SAMPLE_DESC(count: 1, quality: 0); - dxgiSwapChainDesc1.Scaling = DXGI_SCALING.DXGI_SCALING_STRETCH; - dxgiSwapChainDesc1.Stereo = 0; - dxgiSwapChainDesc1.SwapEffect = DXGI_SWAP_EFFECT.DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; - - // Create the native DXGI swapchain object to wrap - hresult = dxgiFactory2.Get()->CreateSwapChainForHwnd( - direct3DDeviceUnknown.Get(), - hwnd, - &dxgiSwapChainDesc1, - null, - null, - dxgiSwapChain1.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - - using ComPtr canvasFactoryNative = default; - - // Get the activation factory for CanvasDevice - using (IObjectReference canvasDeviceActivationFactory = ActivationFactory.Get( - typeName: "Microsoft.Graphics.Canvas.CanvasDevice", - iid: Windows.__uuidof())) - { - canvasFactoryNative.Attach((ICanvasFactoryNative*)canvasDeviceActivationFactory.GetRef()); - } - - // Create a Win2D wrapper for the swapchain - using (ComPtr canvasDeviceUnknown = default) - using (ComPtr canvasSwapChainUnknown = default) - { - canvasDeviceUnknown.Attach((IUnknown*)MarshalInspectable.FromManaged(canvasDevice)); - - hresult = canvasFactoryNative.Get()->GetOrCreate( - device: canvasDeviceUnknown.Get(), - resource: (IUnknown*)dxgiSwapChain1.Get(), - dpi: 96.0f, - wrapper: (void**)canvasSwapChainUnknown.GetAddressOf()); - - ExceptionHelpers.ThrowExceptionForHR(hresult); - - // Marshal to a WinRT managed object - canvasSwapChain = MarshalInspectable.FromAbi((IntPtr)canvasSwapChainUnknown.Get()); - } - } + CanvasSwapChain canvasSwapChain = CanvasSwapChain.CreateForWindowId( + resourceCreator: canvasDevice, + windowId: Win32Interop.GetWindowIdFromWindow(hwnd), + width: this.screenWidth = 1, + height: this.screenHeight = 1, + dpi: 96.0f); // Save the Win2D objects for later use this.canvasDevice = canvasDevice; diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj b/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj index d91eeb26..ebb4d442 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj @@ -9,6 +9,13 @@ win-x64;win-arm64 computesharp.d2d1.cli ..\..\assets\icon.ico + + + true + None From 5cb2512fab78fd70372d98c915c5000062397ffe Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 28 Aug 2024 18:44:08 -0700 Subject: [PATCH 2/4] Switch to 'AppWindow' and remove interop code --- .../Backend/Win32Application.cs | 12 +- .../Backend/Win32ApplicationRunner.cs | 319 ++---------------- .../Program.cs | 4 +- 3 files changed, 32 insertions(+), 303 deletions(-) diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs index 0a6a4be3..1ac88d0c 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32Application.cs @@ -1,7 +1,6 @@ using System; using System.Diagnostics.CodeAnalysis; -using Microsoft.UI; -using TerraFX.Interop.Windows; +using Microsoft.UI.Windowing; namespace ComputeSharp.SwapChain.D2D1.Backend; @@ -49,16 +48,16 @@ internal sealed class Win32Application /// /// Initializes the current application. /// - /// The handle for the window. + /// The window to show. [MemberNotNull(nameof(canvasDevice))] [MemberNotNull(nameof(canvasSwapChain))] - public unsafe void OnInitialize(HWND hwnd) + public unsafe void OnInitialize(AppWindow appWindow) { // Create a new canvas device, which will handle DX11/D2D initialization CanvasDevice canvasDevice = new(); CanvasSwapChain canvasSwapChain = CanvasSwapChain.CreateForWindowId( resourceCreator: canvasDevice, - windowId: Win32Interop.GetWindowIdFromWindow(hwnd), + windowId: appWindow.Id, width: this.screenWidth = 1, height: this.screenHeight = 1, dpi: 96.0f); @@ -66,6 +65,9 @@ internal sealed class Win32Application // Save the Win2D objects for later use this.canvasDevice = canvasDevice; this.canvasSwapChain = canvasSwapChain; + + // Request a resize before the first draw operation + this.isResizePending = true; } /// diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs index a8431b6e..599f1f8a 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs @@ -1,118 +1,34 @@ -using System; using System.Diagnostics; -using System.Drawing; -using System.Runtime.InteropServices; using System.Threading; -using TerraFX.Interop.Windows; +using Microsoft.UI.Dispatching; +using Microsoft.UI.Windowing; namespace ComputeSharp.SwapChain.D2D1.Backend; -using Windows = TerraFX.Interop.Windows.Windows; - /// /// A helper class to manage the creation and execution of Win32 applications. /// internal static unsafe class Win32ApplicationRunner { - /// - /// Whether or not a resize operation is in progress. - /// - private static bool isResizing = false; - - /// - /// Whether or not the application is currently paused. - /// - private static bool isPaused = false; - - /// - /// The for the application window. - /// - private static HWND hwnd; - - /// - /// The application being run. - /// - private static Win32Application application = null!; - - /// - /// The thread running the render loop. - /// - private static Thread renderThread = null!; - /// /// Runs a specified application and starts the main loop to update its state. This is the entry point for a given application, /// and it should be called as soon as the process is launched, excluding any other additional initialization needed. /// /// The input application instance to run. - /// Name of the input application instance to run. /// The title to use for the window being opened. - /// The exit code for the application. - public static int Run(Win32Application application, string applicationName, string windowTitle) + public static void Run(Win32Application application, string windowTitle) { - Win32ApplicationRunner.application = application; + // Create the 'AppWindow' instance (a thin wrapper for an 'HWND') + AppWindow appWindow = AppWindow.Create(); - HMODULE hInstance = Windows.GetModuleHandleW(null); - - fixed (char* name = applicationName) - fixed (char* title = windowTitle) - { - // Initialize the window class - WNDCLASSEXW windowClassEx = new() - { - cbSize = (uint)sizeof(WNDCLASSEXW), - style = CS.CS_HREDRAW | CS.CS_VREDRAW, - lpfnWndProc = &WindowProc, - hInstance = hInstance, - hCursor = Windows.LoadCursorW(HINSTANCE.NULL, IDC.IDC_ARROW), - lpszClassName = name - }; - - // Register the window class - _ = Windows.RegisterClassExW(&windowClassEx); - - Rectangle windowRect = new(0, 0, 1280, 720); - - // Set the target window size - _ = Windows.AdjustWindowRect((RECT*)&windowRect, WS.WS_OVERLAPPEDWINDOW, Windows.FALSE); - - uint height = (uint)(windowRect.Bottom - windowRect.Top); - uint width = (uint)(windowRect.Right - windowRect.Left); - - // Create the window and store a handle to it - hwnd = Windows.CreateWindowExW( - 0, - windowClassEx.lpszClassName, - title, - WS.WS_OVERLAPPEDWINDOW, - Windows.CW_USEDEFAULT, - Windows.CW_USEDEFAULT, - (int)width, - (int)height, - HWND.NULL, - HMENU.NULL, - hInstance, - (void*)GCHandle.ToIntPtr(GCHandle.Alloc(application)) - ); - - MARGINS margins = default; - margins.cxLeftWidth = -1; - margins.cxRightWidth = -1; - margins.cyTopHeight = -1; - margins.cyBottomHeight = -1; - - _ = Windows.DwmExtendFrameIntoClientArea(hwnd, &margins); - } + appWindow.Title = windowTitle; + appWindow.TitleBar.ExtendsContentIntoTitleBar = true; // Initialize the application - application.OnInitialize(hwnd); - - // Display the window - _ = Windows.ShowWindow(hwnd, SW.SW_SHOWDEFAULT); - - MSG msg = default; + application.OnInitialize(appWindow); // Setup the render thread that enables smooth resizing of the window - renderThread = new Thread(static args => + Thread renderThread = new(static args => { (Win32Application application, CancellationToken token) = ((Win32Application, CancellationToken))args!; @@ -126,219 +42,28 @@ internal static unsafe class Win32ApplicationRunner CancellationTokenSource tokenSource = new(); + // Start the render thread before activating the window. + // This allows the app to show up while already drawing. renderThread.Start((application, tokenSource.Token)); - // Process any messages in the queue - while (msg.message != WM.WM_QUIT) + // Register a callback to monitor resize operations + appWindow.Changed += (s, e) => { - if (Windows.PeekMessageW(&msg, HWND.NULL, 0, 0, PM.PM_REMOVE) != 0) + if (e.DidSizeChange) { - _ = Windows.TranslateMessage(&msg); - _ = Windows.DispatchMessageW(&msg); - } - else if (isPaused) - { - Thread.Sleep(100); + application.OnResize(); } + }; - // Also listen to Escape and 'Q' to quit - if (msg.message == WM.WM_KEYUP && (msg.wParam == (WPARAM)VK.VK_ESCAPE || msg.wParam == 'Q')) - { - _ = Windows.DestroyWindow(hwnd); - } - } + // Display the window + appWindow.Show(activateWindow: true); + + // Process any messages in the queue + DispatcherQueueController.CreateOnCurrentThread().DispatcherQueue.RunEventLoop(DispatcherRunOptions.ContinueOnQuit, deferral: null); tokenSource.Cancel(); + // Wait for the render thread to stop before terminating renderThread.Join(); - - // Return this part of the WM_QUIT message to Windows - return (int)msg.wParam; - } - - /// - /// Processes incoming messages for a window. - /// - /// A handle to the window. - /// The message. - /// Additional message information (the contents depend on the value of ). - /// Additional message information (the contents depend on the value of ). - /// The result of the message processing for the input message. - [UnmanagedCallersOnly] - private static LRESULT WindowProc(HWND hwnd, uint uMsg, WPARAM wParam, LPARAM lParam) - { - switch (uMsg) - { - // Change the paused state on window activation - case WM.WM_ACTIVATE: - { - if (Windows.LOWORD(wParam) == Windows.WA_INACTIVE) - { - isPaused = true; - } - else - { - isPaused = false; - } - - return 0; - } - - // Change the paused state when ESC is pressed - case WM.WM_KEYDOWN: - { - if (wParam == (byte)ConsoleKey.Escape) - { - - if (isPaused) - { - _ = Windows.SetCapture(hwnd); - } - else - { - _ = Windows.ReleaseCapture(); - } - - isPaused = !isPaused; - } - - return 0; - } - - // Window resize started - case WM.WM_ENTERSIZEMOVE: - { - isResizing = true; - - return 0; - } - - // Window resize completed - case WM.WM_EXITSIZEMOVE: - { - isResizing = false; - application.OnResize(); - - return 0; - } - - // Size update - case WM.WM_SIZE: - { - if (!isResizing && wParam != Windows.SIZE_MINIMIZED) - { - application.OnResize(); - } - - return 0; - } - - // Size and position of the window (needed to enable the borderless mode) - case WM.WM_NCCALCSIZE: - { - return 0; - } - - // Enable dragging the window - case WM.WM_NCHITTEST: - { - POINT point; - RECT rect; - - _ = Windows.GetCursorPos(&point); - _ = Windows.GetWindowRect(hwnd, &rect); - - bool isAtTop = Math.Abs(point.y - rect.top) < 12; - bool isAtRight = Math.Abs(point.x - rect.right) < 12; - bool isAtBottom = Math.Abs(point.y - rect.bottom) < 12; - bool isAtLeft = Math.Abs(point.x - rect.left) < 12; - - if (isAtTop) - { - if (isAtRight) - { - return Windows.HTTOPRIGHT; - } - - if (isAtLeft) - { - return Windows.HTTOPLEFT; - } - - return Windows.HTTOP; - } - - if (isAtRight) - { - if (isAtTop) - { - return Windows.HTTOPRIGHT; - } - - if (isAtBottom) - { - return Windows.HTBOTTOMRIGHT; - } - - return Windows.HTRIGHT; - } - - if (isAtBottom) - { - if (isAtRight) - { - return Windows.HTBOTTOMRIGHT; - } - - if (isAtLeft) - { - return Windows.HTBOTTOMLEFT; - } - - return Windows.HTBOTTOM; - } - - if (isAtLeft) - { - if (isAtTop) - { - return Windows.HTTOPLEFT; - } - - if (isAtBottom) - { - return Windows.HTBOTTOMLEFT; - } - - return Windows.HTLEFT; - } - - return Windows.HTCAPTION; - } - - // Restore the drop shadow - case WM.WM_DWMCOMPOSITIONCHANGED: - { - MARGINS margins = default; - margins.cxLeftWidth = -1; - margins.cxRightWidth = -1; - margins.cyTopHeight = -1; - margins.cyBottomHeight = -1; - - _ = Windows.DwmExtendFrameIntoClientArea(hwnd, &margins); - - return 0; - } - - // Shutdown - case WM.WM_DESTROY: - { - Windows.PostQuitMessage(0); - - return 0; - } - } - - return Windows.DefWindowProcW(hwnd, uMsg, wParam, lParam); } } \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Program.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Program.cs index 25467b03..fddaba8f 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Program.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Program.cs @@ -46,7 +46,9 @@ if (args is [] or [_]) e.DrawingSession.DrawImage(effect); }; - return Win32ApplicationRunner.Run(win32Application, "ComputeSharp.D2D1", "ComputeSharp.D2D1"); + Win32ApplicationRunner.Run(win32Application, "ComputeSharp.D2D1"); + + return S.S_OK; } // If no shader matches, check if help was requested From d6105a346f5f467216458d44bb78b5e3e338fd7f Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 28 Aug 2024 18:46:57 -0700 Subject: [PATCH 3/4] Style 'AppWindow' title bar --- .../Backend/Win32ApplicationRunner.cs | 17 +++++++++++++++++ .../ComputeSharp.SwapChain.D2D1.Cli.csproj | 1 - 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs index 599f1f8a..960432e0 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Threading; using Microsoft.UI.Dispatching; using Microsoft.UI.Windowing; +using Windows.UI; namespace ComputeSharp.SwapChain.D2D1.Backend; @@ -24,6 +25,22 @@ internal static unsafe class Win32ApplicationRunner appWindow.Title = windowTitle; appWindow.TitleBar.ExtendsContentIntoTitleBar = true; + // Transparent colors for the title bar + appWindow.TitleBar.ForegroundColor = Colors.Transparent; + appWindow.TitleBar.BackgroundColor = Colors.Transparent; + appWindow.TitleBar.ButtonBackgroundColor = Colors.Transparent; + appWindow.TitleBar.InactiveBackgroundColor = Colors.Transparent; + appWindow.TitleBar.ButtonInactiveBackgroundColor = Colors.Transparent; + + // Theme aware colors for the title bar + appWindow.TitleBar.ButtonForegroundColor = Colors.White; + appWindow.TitleBar.ButtonHoverForegroundColor = Colors.White; + appWindow.TitleBar.ButtonPressedForegroundColor = Colors.White; + appWindow.TitleBar.ButtonHoverBackgroundColor = Color.FromArgb(0x20, 0xFF, 0xFF, 0xFF); + appWindow.TitleBar.ButtonPressedBackgroundColor = Color.FromArgb(0x40, 0xFF, 0xFF, 0xFF); + appWindow.TitleBar.ButtonInactiveForegroundColor = Color.FromArgb(0xC0, 0xFF, 0xFF, 0xFF); + appWindow.TitleBar.InactiveForegroundColor = Color.FromArgb(0xA0, 0xA0, 0xA0, 0xA0); + // Initialize the application application.OnInitialize(appWindow); diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj b/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj index ebb4d442..f96c74d2 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/ComputeSharp.SwapChain.D2D1.Cli.csproj @@ -36,7 +36,6 @@ true false false - false false From 3b9bbffacd0c06593a341587e7d771a9eedb4ed6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 28 Aug 2024 19:49:38 -0700 Subject: [PATCH 4/4] Wire up window close events to dispatcher queue --- .../Backend/Win32ApplicationRunner.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs index 960432e0..7b33e450 100644 --- a/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs +++ b/samples/ComputeSharp.SwapChain.D2D1.Cli/Backend/Win32ApplicationRunner.cs @@ -72,11 +72,20 @@ internal static unsafe class Win32ApplicationRunner } }; + DispatcherQueueController dispatcherQueueController = DispatcherQueueController.CreateOnCurrentThread(); + + // Bind the window to a 'DispatcherQueue' instance, so it can flow the exit message + appWindow.AssociateWithDispatcherQueue(dispatcherQueueController.DispatcherQueue); + + // Enqueue the exit message when the window is closed. This ensures that + // the process actually terminates after closing the window, as expected. + appWindow.Closing += static (s, e) => s.DispatcherQueue.EnqueueEventLoopExit(); + // Display the window appWindow.Show(activateWindow: true); // Process any messages in the queue - DispatcherQueueController.CreateOnCurrentThread().DispatcherQueue.RunEventLoop(DispatcherRunOptions.ContinueOnQuit, deferral: null); + dispatcherQueueController.DispatcherQueue.RunEventLoop(DispatcherRunOptions.QuitOnlyLocalLoop, deferral: null); tokenSource.Cancel();