@@ -42,6 +42,7 @@ static constexpr int s_minNewWindowSize = 100;
4242AppWindow::AppWindow (
4343 UINT creationModeId,
4444 std::wstring initialUri,
45+ bool isMainWindow,
4546 std::function<void ()> webviewCreatedCallback,
4647 bool customWindowRect,
4748 RECT windowRect,
@@ -73,13 +74,15 @@ AppWindow::AppWindow(
7374
7475 SetWindowLongPtr (m_mainWindow, GWLP_USERDATA, (LONG_PTR)this );
7576
77+ #ifdef USE_WEBVIEW2_WIN10
7678 // ! [TextScaleChanged1]
7779 if (winrt::try_get_activation_factory<winrt::Windows::UI::ViewManagement::UISettings>())
7880 {
7981 m_uiSettings = winrt::Windows::UI::ViewManagement::UISettings ();
8082 m_uiSettings.TextScaleFactorChanged ({ this , &AppWindow::OnTextScaleChanged });
8183 }
8284 // ! [TextScaleChanged1]
85+ #endif
8386
8487 if (shouldHaveToolbar)
8588 {
@@ -89,12 +92,10 @@ AppWindow::AppWindow(
8992 UpdateCreationModeMenu ();
9093 ShowWindow (m_mainWindow, g_nCmdShow);
9194 UpdateWindow (m_mainWindow);
92-
93- RunAsync ([this ] {
94- InitializeWebView ();
95- });
95+ RunAsync ([this ] {
96+ InitializeWebView ();
97+ });
9698}
97-
9899// Register the Win32 window class for the app window.
99100PCWSTR AppWindow::GetWindowClass ()
100101{
@@ -199,7 +200,7 @@ bool AppWindow::HandleWindowMessage(
199200 {
200201 int retValue = 0 ;
201202 SetWindowLongPtr (hWnd, GWLP_USERDATA, NULL );
202- delete this ;
203+ NotifyClosed () ;
203204 if (--s_appInstances == 0 )
204205 {
205206 PostQuitMessage (retValue);
@@ -334,7 +335,9 @@ bool AppWindow::ExecuteAppCommands(WPARAM wParam, LPARAM lParam)
334335 case IDM_CREATION_MODE_WINDOWED:
335336 case IDM_CREATION_MODE_VISUAL_DCOMP:
336337 case IDM_CREATION_MODE_TARGET_DCOMP:
338+ #ifdef USE_WEBVIEW2_WIN10
337339 case IDM_CREATION_MODE_VISUAL_WINCOMP:
340+ #endif
338341 m_creationModeId = LOWORD (wParam);
339342 UpdateCreationModeMenu ();
340343 return true ;
@@ -452,7 +455,9 @@ void AppWindow::InitializeWebView()
452455 // getting created which will apply the browser switches.
453456 CloseWebView ();
454457 m_dcompDevice = nullptr ;
458+ #ifdef USE_WEBVIEW2_WIN10
455459 m_wincompCompositor = nullptr ;
460+ #endif
456461 LPCWSTR subFolder = nullptr ;
457462
458463 if (m_creationModeId == IDM_CREATION_MODE_VISUAL_DCOMP ||
@@ -470,6 +475,7 @@ void AppWindow::InitializeWebView()
470475 return ;
471476 }
472477 }
478+ #ifdef USE_WEBVIEW2_WIN10
473479 else if (m_creationModeId == IDM_CREATION_MODE_VISUAL_WINCOMP)
474480 {
475481 HRESULT hr = TryCreateDispatcherQueue ();
@@ -485,6 +491,7 @@ void AppWindow::InitializeWebView()
485491 }
486492 m_wincompCompositor = winrtComp::Compositor ();
487493 }
494+ #endif
488495 // ! [CreateCoreWebView2EnvironmentWithOptions]
489496 auto options = Microsoft::WRL::Make<CoreWebView2EnvironmentOptions>();
490497 CHECK_FAILURE (options->put_AllowSingleSignOnUsingOSPrimaryAccount (
@@ -524,7 +531,11 @@ HRESULT AppWindow::OnCreateEnvironmentCompleted(
524531
525532 auto webViewExperimentalEnvironment =
526533 m_webViewEnvironment.try_query <ICoreWebView2ExperimentalEnvironment>();
534+ #ifdef USE_WEBVIEW2_WIN10
527535 if (webViewExperimentalEnvironment && (m_dcompDevice || m_wincompCompositor))
536+ #else
537+ if (webViewExperimentalEnvironment && m_dcompDevice)
538+ #endif
528539 {
529540 CHECK_FAILURE (webViewExperimentalEnvironment->CreateCoreWebView2CompositionController (
530541 m_mainWindow,
@@ -574,7 +585,10 @@ HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(HRESULT result, ICore
574585 this , m_webViewEnvironment.get (), m_oldSettingsComponent.get ());
575586 m_oldSettingsComponent = nullptr ;
576587 NewComponent<ViewComponent>(
577- this , m_dcompDevice.get (), m_wincompCompositor,
588+ this , m_dcompDevice.get (),
589+ #ifdef USE_WEBVIEW2_WIN10
590+ m_wincompCompositor,
591+ #endif
578592 m_creationModeId == IDM_CREATION_MODE_TARGET_DCOMP);
579593 NewComponent<ControlComponent>(this , &m_toolbar);
580594
@@ -738,7 +752,7 @@ void AppWindow::RegisterEventHandlers()
738752
739753 if (!useDefaultWindow)
740754 {
741- newAppWindow = new AppWindow (m_creationModeId, L" " , nullptr , true , windowRect, !!shouldHaveToolbar);
755+ newAppWindow = new AppWindow (m_creationModeId, L" " , false , nullptr , true , windowRect, !!shouldHaveToolbar);
742756 }
743757 else
744758 {
@@ -1080,6 +1094,7 @@ HRESULT AppWindow::TryCreateDispatcherQueue()
10801094 return hr;
10811095}
10821096
1097+ #ifdef USE_WEBVIEW2_WIN10
10831098// ! [TextScaleChanged2]
10841099void AppWindow::OnTextScaleChanged (
10851100 winrt::Windows::UI::ViewManagement::UISettings const & settings,
@@ -1090,13 +1105,18 @@ void AppWindow::OnTextScaleChanged(
10901105 });
10911106}
10921107// ! [TextScaleChanged2]
1108+ #endif
10931109void AppWindow::UpdateCreationModeMenu ()
10941110{
10951111 HMENU hMenu = GetMenu (m_mainWindow);
10961112 CheckMenuRadioItem (
10971113 hMenu,
10981114 IDM_CREATION_MODE_WINDOWED,
1115+ #ifdef USE_WEBVIEW2_WIN10
10991116 IDM_CREATION_MODE_VISUAL_WINCOMP,
1117+ #else
1118+ IDM_CREATION_MODE_TARGET_DCOMP,
1119+ #endif
11001120 m_creationModeId,
11011121 MF_BYCOMMAND);
11021122}
@@ -1106,7 +1126,28 @@ double AppWindow::GetDpiScale()
11061126 return DpiUtil::GetDpiForWindow (m_mainWindow) * 1 .0f / USER_DEFAULT_SCREEN_DPI;
11071127}
11081128
1129+ #ifdef USE_WEBVIEW2_WIN10
11091130double AppWindow::GetTextScale ()
11101131{
11111132 return m_uiSettings ? m_uiSettings.TextScaleFactor () : 1 .0f ;
11121133}
1134+ #endif
1135+
1136+ void AppWindow::AddRef ()
1137+ {
1138+ InterlockedIncrement ((LONG *)&m_refCount);
1139+ }
1140+
1141+ void AppWindow::Release ()
1142+ {
1143+ uint32_t refCount = InterlockedDecrement ((LONG *)&m_refCount);
1144+ if (refCount == 0 )
1145+ {
1146+ delete this ;
1147+ }
1148+ }
1149+
1150+ void AppWindow::NotifyClosed ()
1151+ {
1152+ m_isClosed = true ;
1153+ }
0 commit comments