Skip to content

Commit

Permalink
Move the Direct2D factory to Application
Browse files Browse the repository at this point in the history
Also use the new calling convention syntax for the CsWin32 workaround
  • Loading branch information
JeremyKuhne committed Feb 6, 2024
1 parent 8cdacef commit 9c96ca0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/samples/DirectDraw/Direct2dDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ protected override LRESULT WindowProcedure(HWND window, MessageType message, WPA
renderTarget.FillRectangle(rectangle1, _lightSlateGrayBrush!);
renderTarget.DrawRectangle(rectangle2, _cornflowerBlueBrush!);
}

return (LRESULT)0;
}

Expand Down
5 changes: 5 additions & 0 deletions src/thirtytwo/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
using System.Drawing;
using System.Runtime.InteropServices;
using Windows.Support;
using Windows.Win32.Graphics.Direct2D;

namespace Windows;

public static unsafe class Application
{
private static ActivationContext? s_visualStylesContext;
private static Factory? s_direct2dFactory;

internal static ActivationScope ThemingScope => new(GetStylesContext());

internal static void EnsureDpiAwareness()
Expand Down Expand Up @@ -203,4 +206,6 @@ public static void EnumerateThreadWindows(
{
using var enumerator = new ThreadWindowEnumerator(threadId, callback);
}

public static Factory Direct2dFactory => s_direct2dFactory ??= new();
}
3 changes: 1 addition & 2 deletions src/thirtytwo/Win32/Graphics/Direct2D/ID2D1RenderTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public unsafe partial struct ID2D1RenderTarget
{
public D2D_SIZE_F GetSizeHack()
{
D2D_SIZE_F result;
return *((delegate* unmanaged<ID2D1RenderTarget*, D2D_SIZE_F*, D2D_SIZE_F*>)lpVtbl[53])((ID2D1RenderTarget*)Unsafe.AsPointer(ref this), &result);
return ((delegate* unmanaged[Stdcall, MemberFunction]<ID2D1RenderTarget*, D2D_SIZE_F>)lpVtbl[53])((ID2D1RenderTarget*)Unsafe.AsPointer(ref this));
}
}
22 changes: 6 additions & 16 deletions src/thirtytwo/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public unsafe class Window : ComponentBase, IHandle<HWND>, ILayoutHandler
private static readonly object s_lock = new();
private static readonly ConcurrentDictionary<HWND, WeakReference<Window>> s_windows = new();
private static readonly WindowClass s_defaultWindowClass = new(className: $"DefaultWindowClass_{Guid.NewGuid()}");
protected static Factory? Direct2dFactory { get; private set; }

public static Rectangle DefaultBounds { get; }
= new(Interop.CW_USEDEFAULT, Interop.CW_USEDEFAULT, Interop.CW_USEDEFAULT, Interop.CW_USEDEFAULT);

Expand Down Expand Up @@ -72,10 +72,10 @@ protected bool IsDirect2dEnabled([NotNullWhen(true)] out HwndRenderTarget? rende
return IsDirect2dEnabled();
}

/// <summary>
/// The window handle. This will be <see cref="HWND.Null"/> after the window is destroyed.
/// </summary>
public HWND Handle => _handle;
/// <summary>
/// The window handle. This will be <see cref="HWND.Null"/> after the window is destroyed.
/// </summary>
public HWND Handle => _handle;

public event WindowsMessageEvent? MessageHandler;

Expand All @@ -101,11 +101,6 @@ public Window(
_text = text;
_features = features;

if (_features.AreFlagsSet(Features.EnableDirect2d))
{
Direct2dFactory ??= new();
}

try
{
_handle = _windowClass.CreateWindow(
Expand Down Expand Up @@ -194,14 +189,9 @@ public void SetFont(string typeFace, int pointSize)
[MemberNotNull(nameof(Direct2dRenderTarget))]
private void UpdateRenderTarget(HWND window, Size size)
{
if (Direct2dFactory is null)
{
throw new InvalidOperationException("Should never call UpdateRenderTarget without a factory.");
}

if (Direct2dRenderTarget is null)
{
Direct2dRenderTarget = HwndRenderTarget.CreateForWindow(Direct2dFactory, window, size);
Direct2dRenderTarget = HwndRenderTarget.CreateForWindow(Application.Direct2dFactory, window, size);
RenderTargetCreated(Direct2dRenderTarget);
}
else
Expand Down

0 comments on commit 9c96ca0

Please sign in to comment.