Skip to content

Commit 9c96ca0

Browse files
committed
Move the Direct2D factory to Application
Also use the new calling convention syntax for the CsWin32 workaround
1 parent 8cdacef commit 9c96ca0

File tree

4 files changed

+13
-18
lines changed

4 files changed

+13
-18
lines changed

src/samples/DirectDraw/Direct2dDemo/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ protected override LRESULT WindowProcedure(HWND window, MessageType message, WPA
7575
renderTarget.FillRectangle(rectangle1, _lightSlateGrayBrush!);
7676
renderTarget.DrawRectangle(rectangle2, _cornflowerBlueBrush!);
7777
}
78+
7879
return (LRESULT)0;
7980
}
8081

src/thirtytwo/Application.cs

+5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
using System.Drawing;
55
using System.Runtime.InteropServices;
66
using Windows.Support;
7+
using Windows.Win32.Graphics.Direct2D;
78

89
namespace Windows;
910

1011
public static unsafe class Application
1112
{
1213
private static ActivationContext? s_visualStylesContext;
14+
private static Factory? s_direct2dFactory;
15+
1316
internal static ActivationScope ThemingScope => new(GetStylesContext());
1417

1518
internal static void EnsureDpiAwareness()
@@ -203,4 +206,6 @@ public static void EnumerateThreadWindows(
203206
{
204207
using var enumerator = new ThreadWindowEnumerator(threadId, callback);
205208
}
209+
210+
public static Factory Direct2dFactory => s_direct2dFactory ??= new();
206211
}

src/thirtytwo/Win32/Graphics/Direct2D/ID2D1RenderTarget.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public unsafe partial struct ID2D1RenderTarget
1010
{
1111
public D2D_SIZE_F GetSizeHack()
1212
{
13-
D2D_SIZE_F result;
14-
return *((delegate* unmanaged<ID2D1RenderTarget*, D2D_SIZE_F*, D2D_SIZE_F*>)lpVtbl[53])((ID2D1RenderTarget*)Unsafe.AsPointer(ref this), &result);
13+
return ((delegate* unmanaged[Stdcall, MemberFunction]<ID2D1RenderTarget*, D2D_SIZE_F>)lpVtbl[53])((ID2D1RenderTarget*)Unsafe.AsPointer(ref this));
1514
}
1615
}

src/thirtytwo/Window.cs

+6-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public unsafe class Window : ComponentBase, IHandle<HWND>, ILayoutHandler
1515
private static readonly object s_lock = new();
1616
private static readonly ConcurrentDictionary<HWND, WeakReference<Window>> s_windows = new();
1717
private static readonly WindowClass s_defaultWindowClass = new(className: $"DefaultWindowClass_{Guid.NewGuid()}");
18-
protected static Factory? Direct2dFactory { get; private set; }
18+
1919
public static Rectangle DefaultBounds { get; }
2020
= new(Interop.CW_USEDEFAULT, Interop.CW_USEDEFAULT, Interop.CW_USEDEFAULT, Interop.CW_USEDEFAULT);
2121

@@ -72,10 +72,10 @@ protected bool IsDirect2dEnabled([NotNullWhen(true)] out HwndRenderTarget? rende
7272
return IsDirect2dEnabled();
7373
}
7474

75-
/// <summary>
76-
/// The window handle. This will be <see cref="HWND.Null"/> after the window is destroyed.
77-
/// </summary>
78-
public HWND Handle => _handle;
75+
/// <summary>
76+
/// The window handle. This will be <see cref="HWND.Null"/> after the window is destroyed.
77+
/// </summary>
78+
public HWND Handle => _handle;
7979

8080
public event WindowsMessageEvent? MessageHandler;
8181

@@ -101,11 +101,6 @@ public Window(
101101
_text = text;
102102
_features = features;
103103

104-
if (_features.AreFlagsSet(Features.EnableDirect2d))
105-
{
106-
Direct2dFactory ??= new();
107-
}
108-
109104
try
110105
{
111106
_handle = _windowClass.CreateWindow(
@@ -194,14 +189,9 @@ public void SetFont(string typeFace, int pointSize)
194189
[MemberNotNull(nameof(Direct2dRenderTarget))]
195190
private void UpdateRenderTarget(HWND window, Size size)
196191
{
197-
if (Direct2dFactory is null)
198-
{
199-
throw new InvalidOperationException("Should never call UpdateRenderTarget without a factory.");
200-
}
201-
202192
if (Direct2dRenderTarget is null)
203193
{
204-
Direct2dRenderTarget = HwndRenderTarget.CreateForWindow(Direct2dFactory, window, size);
194+
Direct2dRenderTarget = HwndRenderTarget.CreateForWindow(Application.Direct2dFactory, window, size);
205195
RenderTargetCreated(Direct2dRenderTarget);
206196
}
207197
else

0 commit comments

Comments
 (0)