1+ using System ;
2+ using System . Runtime . InteropServices ;
3+ using Avalonia . Controls ;
4+ using Avalonia . Platform ;
5+ using FrameExtractor . Services ;
6+
17namespace FrameExtractor . Helpers ;
28
3- public class RoundedWindowHelper
9+ public static class RoundedWindowHelper
410{
11+ #region Windows 11 API
12+ [ DllImport ( "dwmapi.dll" ) ]
13+ private static extern int DwmSetWindowAttribute ( IntPtr hwnd , int attr , ref int attrValue , int attrSize ) ;
14+
15+ private const int DwmwaWindowCornerPreference = 33 ;
16+ private const int DwmwcpRound = 2 ;
17+ private const int DwmwcpRoundsmall = 3 ;
18+ #endregion
19+
20+ #region macOS API
21+ [ DllImport ( "/System/Library/Frameworks/AppKit.framework/AppKit" ) ]
22+ private static extern IntPtr objc_getClass ( string className ) ;
23+
24+ [ DllImport ( "/System/Library/Frameworks/AppKit.framework/AppKit" ) ]
25+ private static extern IntPtr objc_msgSend ( IntPtr receiver , IntPtr selector ) ;
26+
27+ [ DllImport ( "/System/Library/Frameworks/AppKit.framework/AppKit" ) ]
28+ private static extern void objc_msgSend_double ( IntPtr receiver , IntPtr selector , double value ) ;
29+
30+ [ DllImport ( "/System/Library/Frameworks/AppKit.framework/AppKit" ) ]
31+ private static extern IntPtr sel_registerName ( string name ) ;
32+ #endregion
33+
34+ private static bool TryEnableWindowsRoundedCorners ( IntPtr hwnd , bool smallRadius = false )
35+ {
36+ try
37+ {
38+ int preference = smallRadius ? DwmwcpRoundsmall : DwmwcpRound ;
39+ int result = DwmSetWindowAttribute (
40+ hwnd ,
41+ DwmwaWindowCornerPreference ,
42+ ref preference ,
43+ sizeof ( int )
44+ ) ;
45+
46+ bool success = result == 0 ;
47+ if ( success )
48+ {
49+ Logger . Info ( "[Windows 11] Native rounded corners enabled successfully" ) ;
50+ }
51+ return success ;
52+ }
53+ catch ( Exception ex )
54+ {
55+ Logger . Info ( $ "[Windows 11] Failed to enable rounded corners: { ex . Message } ") ;
56+ return false ;
57+ }
58+ }
59+
60+ private static bool TryEnableMacOsRoundedCorners ( IntPtr nsWindow , double radius = 10.0 )
61+ {
62+ try
63+ {
64+ // NSWindow.contentView.layer.cornerRadius = radius
65+ var contentView = objc_msgSend ( nsWindow , sel_registerName ( "contentView" ) ) ;
66+ if ( contentView == IntPtr . Zero ) return false ;
67+
68+ var layer = objc_msgSend ( contentView , sel_registerName ( "layer" ) ) ;
69+ if ( layer == IntPtr . Zero ) return false ;
70+
71+ // Set corner radius
72+ objc_msgSend_double ( layer , sel_registerName ( "setCornerRadius:" ) , radius ) ;
73+
74+ // Enable masksToBounds
75+ objc_msgSend ( layer , sel_registerName ( "setMasksToBounds:" ) ) ;
76+
77+ Logger . Info ( $ "[macOS] Native rounded corners enabled (radius: { radius } )") ;
78+ return true ;
79+ }
80+ catch ( Exception ex )
81+ {
82+ Logger . Info ( $ "[macOS] Failed to enable rounded corners: { ex . Message } ") ;
83+ return false ;
84+ }
85+ }
86+
87+ public static void SetupRoundedWindow ( Window window , bool useNativeChrome = false , double cornerRadius = 16.0 )
88+ {
89+ window . ExtendClientAreaToDecorationsHint = true ;
90+ window . ExtendClientAreaChromeHints = useNativeChrome
91+ ? ExtendClientAreaChromeHints . PreferSystemChrome
92+ : ExtendClientAreaChromeHints . NoChrome ;
93+
94+ window . Opened += ( s , e ) =>
95+ {
96+ var handle = window . TryGetPlatformHandle ( ) ;
97+ if ( handle == null )
98+ {
99+ Logger . Info ( "[Platform] Could not get platform handle" ) ;
100+ return ;
101+ }
102+
103+ bool roundedEnabled = false ;
104+
105+ // Windows 11
106+ if ( OperatingSystem . IsWindowsVersionAtLeast ( 10 , 0 , 22000 ) &&
107+ handle . HandleDescriptor == "HWND" )
108+ {
109+ Logger . Info ( "[Platform] Detected Windows 11" ) ;
110+ roundedEnabled = TryEnableWindowsRoundedCorners ( handle . Handle , smallRadius : cornerRadius < 10 ) ;
111+
112+ if ( roundedEnabled && ! useNativeChrome )
113+ {
114+ window . SystemDecorations = SystemDecorations . BorderOnly ;
115+ }
116+ }
117+ // macOS
118+ else if ( OperatingSystem . IsMacOS ( ) && handle . HandleDescriptor == "NSWindow" )
119+ {
120+ Logger . Info ( "[Platform] Detected macOS" ) ;
121+ roundedEnabled = TryEnableMacOsRoundedCorners ( handle . Handle , cornerRadius ) ;
122+
123+ if ( ! useNativeChrome )
124+ {
125+ window . SystemDecorations = SystemDecorations . None ;
126+ }
127+ }
128+ // Other platforms
129+ else
130+ {
131+ var platform = OperatingSystem . IsWindows ( ) ? "Windows 10 or older" :
132+ OperatingSystem . IsLinux ( ) ? "Linux" : "Unknown" ;
133+ Logger . Info ( $ "[Platform] Detected { platform } - native rounded corners not supported, keeping square corners") ;
134+
135+ if ( ! useNativeChrome )
136+ {
137+ window . SystemDecorations = SystemDecorations . None ;
138+ }
139+ }
140+
141+ if ( ! roundedEnabled && ! useNativeChrome )
142+ {
143+ Logger . Info ( "[Platform] Window will have square corners with custom chrome" ) ;
144+ }
145+ } ;
146+ }
147+
148+ public static bool IsNativeRoundedCornersSupported ( )
149+ {
150+ // Windows 11
151+ if ( OperatingSystem . IsWindowsVersionAtLeast ( 10 , 0 , 22000 ) )
152+ {
153+ Logger . Info ( "[Check] Windows 11 detected - native rounded corners supported" ) ;
154+ return true ;
155+ }
156+
157+ // macOS
158+ if ( OperatingSystem . IsMacOS ( ) )
159+ {
160+ Logger . Info ( "[Check] macOS detected - native rounded corners supported" ) ;
161+ return true ;
162+ }
163+
164+ // Other platforms
165+ var platform = OperatingSystem . IsWindows ( ) ? "Windows 10 or older" :
166+ OperatingSystem . IsLinux ( ) ? "Linux" : "Unknown platform" ;
167+ Logger . Info ( $ "[Check] { platform } - native rounded corners not supported") ;
168+ return false ;
169+ }
5170
171+ public static string GetPlatformName ( )
172+ {
173+ if ( OperatingSystem . IsWindowsVersionAtLeast ( 10 , 0 , 22000 ) )
174+ return "Windows 11" ;
175+ if ( OperatingSystem . IsWindows ( ) )
176+ return "Windows 10 or older" ;
177+ if ( OperatingSystem . IsMacOS ( ) )
178+ return "macOS" ;
179+ if ( OperatingSystem . IsLinux ( ) )
180+ return "Linux" ;
181+ return "Unknown" ;
182+ }
6183}
0 commit comments