Skip to content

Commit d6119a0

Browse files
committed
Add WinUI 3 support for Windows message dialogs
This commit adds WinUI 3 and Windows App SDK detection and configuration to CMakeLists.txt, enabling modern XAML-based dialogs on Windows if available. The message_dialog_windows.cpp implementation now uses WinUI 3 ContentDialog for message dialogs when enabled, with fallback to MessageBox if unavailable. This improves dialog appearance and integration on supported Windows platforms.
1 parent c27b91d commit d6119a0

File tree

2 files changed

+483
-10
lines changed

2 files changed

+483
-10
lines changed

src/CMakeLists.txt

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,109 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "OHOS")
4040
file(GLOB PLATFORM_SOURCES "platform/ohos/*.cpp")
4141
elseif(WIN32)
4242
file(GLOB PLATFORM_SOURCES "platform/windows/*.cpp")
43+
44+
# WinUI3 / Windows App SDK support - find SDK paths before target creation
45+
option(ENABLE_WINUI3 "Enable WinUI3 support" ON)
46+
47+
set(WINDOWS_APP_SDK_FOUND FALSE)
48+
set(WINDOWS_APP_SDK_INCLUDE_DIRS "")
49+
set(WINDOWS_APP_SDK_LIB_DIRS "")
50+
51+
if(ENABLE_WINUI3)
52+
# Try to find Windows App SDK
53+
# Check common installation paths
54+
set(WINDOWS_APP_SDK_PATHS "")
55+
56+
# Get ProgramFiles environment variable
57+
if(DEFINED ENV{ProgramFiles})
58+
list(APPEND WINDOWS_APP_SDK_PATHS "$ENV{ProgramFiles}\\Windows Kits\\10\\App SDKs")
59+
list(APPEND WINDOWS_APP_SDK_PATHS "C:\\Program Files\\Windows Kits\\10\\App SDKs")
60+
endif()
61+
62+
# Get ProgramFiles(x86) environment variable (may not exist on 32-bit systems)
63+
if(DEFINED ENV{ProgramFiles})
64+
# Try to get ProgramFiles(x86) via CMake's get_filename_component or check common path
65+
set(PROGRAMFILES_X86 "$ENV{ProgramFiles}")
66+
string(REPLACE "Program Files" "Program Files (x86)" PROGRAMFILES_X86 "${PROGRAMFILES_X86}")
67+
list(APPEND WINDOWS_APP_SDK_PATHS "${PROGRAMFILES_X86}\\Windows Kits\\10\\App SDKs")
68+
list(APPEND WINDOWS_APP_SDK_PATHS "C:\\Program Files (x86)\\Windows Kits\\10\\App SDKs")
69+
endif()
70+
71+
# Add user-specific path
72+
if(DEFINED ENV{LOCALAPPDATA})
73+
list(APPEND WINDOWS_APP_SDK_PATHS "$ENV{LOCALAPPDATA}\\Microsoft\\WindowsAppSDK")
74+
endif()
75+
76+
# Find the latest Windows App SDK version
77+
set(WINDOWS_APP_SDK_VERSION "")
78+
set(WINDOWS_APP_SDK_ROOT "")
79+
80+
foreach(SDK_PATH ${WINDOWS_APP_SDK_PATHS})
81+
if(EXISTS "${SDK_PATH}")
82+
file(GLOB SDK_VERSIONS "${SDK_PATH}/*")
83+
list(SORT SDK_VERSIONS)
84+
list(REVERSE SDK_VERSIONS)
85+
if(SDK_VERSIONS)
86+
list(GET SDK_VERSIONS 0 WINDOWS_APP_SDK_ROOT)
87+
get_filename_component(WINDOWS_APP_SDK_VERSION ${WINDOWS_APP_SDK_ROOT} NAME)
88+
break()
89+
endif()
90+
endif()
91+
endforeach()
92+
93+
# If not found in standard locations, try to find via NuGet packages
94+
if(NOT WINDOWS_APP_SDK_ROOT OR NOT EXISTS "${WINDOWS_APP_SDK_ROOT}")
95+
# Check for NuGet package location
96+
set(NUGET_PACKAGES_PATH "$ENV{USERPROFILE}\\.nuget\\packages")
97+
if(EXISTS "${NUGET_PACKAGES_PATH}")
98+
file(GLOB WINAPP_SDK_PACKAGES "${NUGET_PACKAGES_PATH}\\microsoft.windowsappsdk\\*")
99+
if(WINAPP_SDK_PACKAGES)
100+
list(SORT WINAPP_SDK_PACKAGES)
101+
list(REVERSE WINAPP_SDK_PACKAGES)
102+
list(GET WINAPP_SDK_PACKAGES 0 WINDOWS_APP_SDK_ROOT)
103+
if(EXISTS "${WINDOWS_APP_SDK_ROOT}\\c\\include")
104+
set(WINDOWS_APP_SDK_ROOT "${WINDOWS_APP_SDK_ROOT}\\c")
105+
endif()
106+
endif()
107+
endif()
108+
endif()
109+
110+
# Set Windows App SDK paths
111+
if(WINDOWS_APP_SDK_ROOT AND EXISTS "${WINDOWS_APP_SDK_ROOT}")
112+
message(STATUS "Found Windows App SDK at: ${WINDOWS_APP_SDK_ROOT}")
113+
set(WINDOWS_APP_SDK_FOUND TRUE)
114+
115+
# Collect include directories
116+
if(EXISTS "${WINDOWS_APP_SDK_ROOT}\\include")
117+
list(APPEND WINDOWS_APP_SDK_INCLUDE_DIRS "${WINDOWS_APP_SDK_ROOT}\\include")
118+
endif()
119+
120+
# Collect library paths
121+
if(EXISTS "${WINDOWS_APP_SDK_ROOT}\\lib")
122+
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
123+
# 64-bit
124+
if(EXISTS "${WINDOWS_APP_SDK_ROOT}\\lib\\x64")
125+
list(APPEND WINDOWS_APP_SDK_LIB_DIRS "${WINDOWS_APP_SDK_ROOT}\\lib\\x64")
126+
endif()
127+
if(EXISTS "${WINDOWS_APP_SDK_ROOT}\\lib\\win10-x64")
128+
list(APPEND WINDOWS_APP_SDK_LIB_DIRS "${WINDOWS_APP_SDK_ROOT}\\lib\\win10-x64")
129+
endif()
130+
else()
131+
# 32-bit
132+
if(EXISTS "${WINDOWS_APP_SDK_ROOT}\\lib\\x86")
133+
list(APPEND WINDOWS_APP_SDK_LIB_DIRS "${WINDOWS_APP_SDK_ROOT}\\lib\\x86")
134+
endif()
135+
if(EXISTS "${WINDOWS_APP_SDK_ROOT}\\lib\\win10-x86")
136+
list(APPEND WINDOWS_APP_SDK_LIB_DIRS "${WINDOWS_APP_SDK_ROOT}\\lib\\win10-x86")
137+
endif()
138+
endif()
139+
endif()
140+
else()
141+
# Don't assume Windows SDK has WinUI 3 - check for headers first
142+
message(STATUS "Windows App SDK not found in standard locations")
143+
set(WINDOWS_APP_SDK_FOUND FALSE)
144+
endif()
145+
endif()
43146
else()
44147
set(PLATFORM_SOURCES "")
45148
endif()
@@ -81,4 +184,71 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "OHOS")
81184
target_link_libraries(nativeapi PUBLIC hilog_ndk.z)
82185
elseif(WIN32)
83186
target_link_libraries(nativeapi PUBLIC user32 shell32 dwmapi gdiplus crypt32)
187+
188+
# Apply WinUI3 configuration after target creation
189+
if(ENABLE_WINUI3 AND WINDOWS_APP_SDK_FOUND)
190+
# Check if WinUI 3 headers actually exist
191+
set(WINUI3_HEADERS_FOUND FALSE)
192+
193+
# Check for Microsoft.UI.Xaml.h header
194+
foreach(INCLUDE_DIR ${WINDOWS_APP_SDK_INCLUDE_DIRS})
195+
if(EXISTS "${INCLUDE_DIR}/winrt/Microsoft.UI.Xaml.h")
196+
set(WINUI3_HEADERS_FOUND TRUE)
197+
break()
198+
endif()
199+
endforeach()
200+
201+
# Also check in standard Windows SDK locations
202+
if(NOT WINUI3_HEADERS_FOUND)
203+
set(WINRT_CHECK_PATHS "")
204+
if(DEFINED ENV{ProgramFiles})
205+
list(APPEND WINRT_CHECK_PATHS "$ENV{ProgramFiles}\\Windows Kits\\10\\Include")
206+
endif()
207+
list(APPEND WINRT_CHECK_PATHS "C:\\Program Files\\Windows Kits\\10\\Include")
208+
list(APPEND WINRT_CHECK_PATHS "C:\\Program Files (x86)\\Windows Kits\\10\\Include")
209+
210+
foreach(BASE_PATH ${WINRT_CHECK_PATHS})
211+
if(EXISTS "${BASE_PATH}")
212+
file(GLOB SDK_VERSIONS "${BASE_PATH}/*")
213+
foreach(SDK_VERSION ${SDK_VERSIONS})
214+
if(IS_DIRECTORY "${SDK_VERSION}" AND EXISTS "${SDK_VERSION}/winrt/Microsoft.UI.Xaml.h")
215+
set(WINUI3_HEADERS_FOUND TRUE)
216+
list(APPEND WINDOWS_APP_SDK_INCLUDE_DIRS "${SDK_VERSION}")
217+
break()
218+
endif()
219+
endforeach()
220+
if(WINUI3_HEADERS_FOUND)
221+
break()
222+
endif()
223+
endif()
224+
endforeach()
225+
endif()
226+
227+
if(WINUI3_HEADERS_FOUND)
228+
# Add include directories
229+
if(WINDOWS_APP_SDK_INCLUDE_DIRS)
230+
target_include_directories(nativeapi PUBLIC ${WINDOWS_APP_SDK_INCLUDE_DIRS})
231+
endif()
232+
233+
# Add library directories
234+
if(WINDOWS_APP_SDK_LIB_DIRS)
235+
target_link_directories(nativeapi PUBLIC ${WINDOWS_APP_SDK_LIB_DIRS})
236+
endif()
237+
238+
# Define WinUI3 preprocessor macros
239+
target_compile_definitions(nativeapi PRIVATE
240+
WINUI3_ENABLED=1
241+
WINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP
242+
_WINRT_DLL
243+
)
244+
245+
# Link WinUI3 libraries
246+
# Try to link windowsapp.lib (available in Windows 11 SDK or Windows App SDK)
247+
target_link_libraries(nativeapi PUBLIC windowsapp)
248+
249+
message(STATUS "WinUI3 support enabled")
250+
else()
251+
message(STATUS "WinUI3 headers not found - WinUI3 support disabled")
252+
endif()
253+
endif()
84254
endif ()

0 commit comments

Comments
 (0)