Skip to content

Commit cb83879

Browse files
committed
Fixed nullability errors
1 parent 2cb787e commit cb83879

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

components/TitleBar/src/TitleBar.WASDK.cs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
#if WINAPPSDK
6+
using Windows.Graphics;
67
using Microsoft.UI;
78
using Microsoft.UI.Input;
89
using Microsoft.UI.Windowing;
@@ -15,8 +16,8 @@ namespace CommunityToolkit.WinUI.Controls;
1516

1617
public partial class TitleBar : Control
1718
{
18-
WndProcHelper WndProcHelper;
19-
MenuFlyout MenuFlyout;
19+
WndProcHelper? WndProcHelper;
20+
MenuFlyout? MenuFlyout;
2021
ContentPresenter? PART_ContentPresenter;
2122
ContentPresenter? PART_FooterPresenter;
2223

@@ -128,10 +129,13 @@ private void Window_Activated(object sender, WindowActivatedEventArgs args)
128129

129130
public void SetDragRegionForCustomTitleBar()
130131
{
131-
if (AutoConfigureCustomTitleBar && Window != null)
132+
if (AutoConfigureCustomTitleBar && Window is not null)
132133
{
133134
ClearDragRegions(NonClientRegionKind.Passthrough);
134-
SetDragRegion(NonClientRegionKind.Passthrough, PART_ContentPresenter, PART_FooterPresenter, PART_ButtonHolder);
135+
var items = new FrameworkElement?[] { PART_ContentPresenter, PART_FooterPresenter, PART_ButtonHolder };
136+
var validItems = items.Where(x => x is not null).Select(x => x!).ToArray(); // Prune null items
137+
138+
SetDragRegion(NonClientRegionKind.Passthrough, validItems);
135139
}
136140
}
137141

@@ -149,7 +153,7 @@ public void SetDragRegion(NonClientRegionKind nonClientRegionKind, params Framew
149153
var nonClientInputSrc = InputNonClientPointerSource.GetForWindowId(Window.AppWindow.Id);
150154
List<Windows.Graphics.RectInt32> rects = new List<Windows.Graphics.RectInt32>();
151155
var scale = GetRasterizationScaleForElement(this);
152-
156+
153157
foreach (var frameworkElement in frameworkElements)
154158
{
155159
if (frameworkElement == null)
@@ -183,26 +187,32 @@ private IntPtr InputNonClientPointerSourceWndProc(IntPtr hWnd, NativeMethods.Win
183187
switch (Msg)
184188
{
185189
case NativeMethods.WindowMessage.WM_NCLBUTTONDOWN:
190+
{
191+
if (MenuFlyout?.IsOpen ?? false)
186192
{
187-
if (MenuFlyout.IsOpen)
188-
{
189-
MenuFlyout.Hide();
190-
}
191-
break;
193+
MenuFlyout.Hide();
192194
}
195+
break;
196+
}
193197
case NativeMethods.WindowMessage.WM_NCRBUTTONDOWN:
194-
{
195-
PointInt32 pt = new PointInt32(lParam.ToInt32() & 0xFFFF, lParam.ToInt32() >> 16);
196-
FlyoutShowOptions options = new FlyoutShowOptions();
197-
options.ShowMode = FlyoutShowMode.Standard;
198-
options.Position = InfoHelper.SystemVersion.Build >= 22000 ?
199-
new Windows.Foundation.Point((pt.X - this.Window.AppWindow.Position.X - 8) / XamlRoot.RasterizationScale, (pt.Y - this.Window.AppWindow.Position.Y) / XamlRoot.RasterizationScale) :
200-
new Windows.Foundation.Point(pt.X - this.Window.AppWindow.Position.X - 8, pt.Y - this.Window.AppWindow.Position.Y);
198+
{
199+
PointInt32 pt = new PointInt32(lParam.ToInt32() & 0xFFFF, lParam.ToInt32() >> 16);
200+
FlyoutShowOptions options = new FlyoutShowOptions();
201+
options.ShowMode = FlyoutShowMode.Standard;
202+
options.Position = InfoHelper.SystemVersion.Build >= 22000 ?
203+
new Windows.Foundation.Point((pt.X - this.Window.AppWindow.Position.X - 8) / XamlRoot.RasterizationScale, (pt.Y - this.Window.AppWindow.Position.Y) / XamlRoot.RasterizationScale) :
204+
new Windows.Foundation.Point(pt.X - this.Window.AppWindow.Position.X - 8, pt.Y - this.Window.AppWindow.Position.Y);
201205

202-
MenuFlyout.ShowAt(this, options);
203-
return (IntPtr)0;
204-
}
206+
MenuFlyout?.ShowAt(this, options);
207+
return (IntPtr)0;
208+
}
209+
}
210+
211+
if (WndProcHelper is null)
212+
{
213+
throw new InvalidOperationException($"Internal error: {nameof(WndProcHelper)} is missing.");
205214
}
215+
206216
return WndProcHelper.CallInputNonClientPointerSourceWindowProc(hWnd, Msg, wParam, lParam);
207217
}
208218

@@ -224,20 +234,26 @@ private IntPtr WindowWndProc(IntPtr hWnd, NativeMethods.WindowMessage Msg, IntPt
224234
FlyoutShowOptions options = new FlyoutShowOptions();
225235
options.Position = new Windows.Foundation.Point(0, 15);
226236
options.ShowMode = FlyoutShowMode.Standard;
227-
MenuFlyout.ShowAt(null, options);
237+
MenuFlyout?.ShowAt(null, options);
228238
return (IntPtr)0;
229239
}
230240
else if (sysCommand is NativeMethods.SystemCommand.SC_KEYMENU)
231241
{
232242
FlyoutShowOptions options = new FlyoutShowOptions();
233243
options.Position = new Windows.Foundation.Point(0, 45);
234244
options.ShowMode = FlyoutShowMode.Standard;
235-
MenuFlyout.ShowAt(null, options);
245+
MenuFlyout?.ShowAt(null, options);
236246
return (IntPtr)0;
237247
}
238248
break;
239249
}
240250
}
251+
252+
if (WndProcHelper is null)
253+
{
254+
throw new InvalidOperationException($"Internal error: {nameof(WndProcHelper)} is missing.");
255+
}
256+
241257
return WndProcHelper.CallWindowProc(hWnd, Msg, wParam, lParam);
242258
}
243259
}

components/TitleBar/src/WndProcHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if WINAPPSDK
1+
#if WINAPPSDK
22
using System.Runtime.InteropServices;
33
using WinRT.Interop;
44

@@ -8,10 +8,10 @@ internal class WndProcHelper
88
public delegate IntPtr WNDPROC(IntPtr hWnd, NativeMethods.WindowMessage Msg, IntPtr wParam, IntPtr lParam);
99

1010
private IntPtr Handle { get; set; }
11-
private WNDPROC newMainWindowWndProc = null;
11+
private WNDPROC? newMainWindowWndProc = null;
1212
private IntPtr oldMainWindowWndProc = IntPtr.Zero;
1313

14-
private WNDPROC newInputNonClientPointerSourceWndProc = null;
14+
private WNDPROC? newInputNonClientPointerSourceWndProc = null;
1515
private IntPtr oldInputNonClientPointerSourceWndProc = IntPtr.Zero;
1616

1717
public WndProcHelper(Window window)
@@ -36,7 +36,7 @@ public void RegisterWndProc(WNDPROC wndProc)
3636

3737
public void RegisterInputNonClientPointerSourceWndProc(WNDPROC wndProc)
3838
{
39-
IntPtr inputNonClientPointerSourceHandle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "InputNonClientPointerSource", null);
39+
IntPtr inputNonClientPointerSourceHandle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "InputNonClientPointerSource", string.Empty);
4040

4141
if (inputNonClientPointerSourceHandle != IntPtr.Zero)
4242
{
@@ -48,4 +48,4 @@ public void RegisterInputNonClientPointerSourceWndProc(WNDPROC wndProc)
4848
}
4949
}
5050
}
51-
#endif
51+
#endif

0 commit comments

Comments
 (0)