33// See the LICENSE file in the project root for more information.
44
55#if WINAPPSDK
6+ using Windows . Graphics ;
67using Microsoft . UI ;
78using Microsoft . UI . Input ;
89using Microsoft . UI . Windowing ;
@@ -15,8 +16,8 @@ namespace CommunityToolkit.WinUI.Controls;
1516
1617public 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}
0 commit comments