Skip to content

Commit f957296

Browse files
committed
Added USE_WEBVIEW2_WIN10
1 parent 8df5be9 commit f957296

File tree

15 files changed

+384
-22
lines changed

15 files changed

+384
-22
lines changed

SampleApps/WebView2APISample/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Debug/
22
Release/
33
S Debug/
44
S Release/
5+
Win7 Debug/
6+
Win7 Release/
57
ipch/
68
x64/
79
x86/
@@ -25,4 +27,4 @@ packages/
2527
/HostObjectSample_i.c
2628

2729
# Make sure script debug config is checked in for vendor testing
28-
!.vscode
30+
!.vscode

SampleApps/WebView2APISample/App.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,12 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
106106
{
107107
creationModeId = IDM_CREATION_MODE_TARGET_DCOMP;
108108
}
109+
#ifdef USE_WEBVIEW2_WIN10
109110
else if (NEXT_PARAM_CONTAINS(L"visualwincomp"))
110111
{
111112
creationModeId = IDM_CREATION_MODE_VISUAL_WINCOMP;
112113
}
114+
#endif
113115
}
114116
}
115117
LocalFree(params);
@@ -118,7 +120,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
118120

119121
DpiUtil::SetProcessDpiAwarenessContext(dpiAwarenessContext);
120122

121-
new AppWindow(creationModeId, initialUri);
123+
new AppWindow(creationModeId, initialUri, true);
122124

123125
int retVal = RunMessagePump();
124126

SampleApps/WebView2APISample/AppWindow.cpp

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static constexpr int s_minNewWindowSize = 100;
4242
AppWindow::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.
99100
PCWSTR 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]
10841099
void AppWindow::OnTextScaleChanged(
10851100
winrt::Windows::UI::ViewManagement::UISettings const& settings,
@@ -1090,13 +1105,18 @@ void AppWindow::OnTextScaleChanged(
10901105
});
10911106
}
10921107
//! [TextScaleChanged2]
1108+
#endif
10931109
void 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
11091130
double 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+
}

SampleApps/WebView2APISample/AppWindow.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
#include <string>
1717
#include <vector>
1818
#include <winnt.h>
19+
#ifdef USE_WEBVIEW2_WIN10
1920
#include <winrt/Windows.UI.Composition.h>
2021
#include <winrt/Windows.UI.ViewManagement.h>
2122

2223
namespace winrtComp = winrt::Windows::UI::Composition;
24+
#endif
2325

2426
class SettingsComponent;
2527

@@ -29,6 +31,7 @@ class AppWindow
2931
AppWindow(
3032
UINT creationModeId,
3133
std::wstring initialUri = L"https://www.bing.com/",
34+
bool isMainWindow = false,
3235
std::function<void()> webviewCreatedCallback = nullptr,
3336
bool customWindowRect = false,
3437
RECT windowRect = { 0 },
@@ -51,7 +54,9 @@ class AppWindow
5154
std::wstring GetLocalUri(std::wstring path);
5255
std::function<void()> GetAcceleratorKeyFunction(UINT key);
5356
double GetDpiScale();
57+
#ifdef USE_WEBVIEW2_WIN10
5458
double GetTextScale();
59+
#endif
5560

5661
void ReinitializeWebView();
5762

@@ -63,6 +68,10 @@ class AppWindow
6368

6469
void RunAsync(std::function<void(void)> callback);
6570

71+
void AddRef();
72+
void Release();
73+
void NotifyClosed();
74+
6675
private:
6776
static PCWSTR GetWindowClass();
6877

@@ -89,9 +98,11 @@ class AppWindow
8998
void ChangeLanguage();
9099
void UpdateCreationModeMenu();
91100
void ToggleAADSSO();
101+
#ifdef USE_WEBVIEW2_WIN10
92102
void OnTextScaleChanged(
93103
winrt::Windows::UI::ViewManagement::UISettings const& uiSettings,
94104
winrt::Windows::Foundation::IInspectable const& args);
105+
#endif
95106
std::wstring GetLocalPath(std::wstring path, bool keep_exe_path);
96107
void DeleteAllComponents();
97108

@@ -102,6 +113,8 @@ class AppWindow
102113
Toolbar m_toolbar;
103114
std::function<void()> m_onWebViewFirstInitialized;
104115
DWORD m_creationModeId = 0;
116+
int m_refCount = 1;
117+
bool m_isClosed = false;
105118

106119
// The following is state that belongs with the webview, and should
107120
// be reinitialized along with it. Everything here is undefined when
@@ -132,8 +145,10 @@ class AppWindow
132145
HRESULT TryCreateDispatcherQueue();
133146

134147
wil::com_ptr<IDCompositionDevice> m_dcompDevice;
148+
#ifdef USE_WEBVIEW2_WIN10
135149
winrtComp::Compositor m_wincompCompositor{ nullptr };
136150
winrt::Windows::UI::ViewManagement::UISettings m_uiSettings{ nullptr };
151+
#endif
137152
};
138153

139154
template <class ComponentType, class... Args> void AppWindow::NewComponent(Args&&... args)

SampleApps/WebView2APISample/ScenarioWebViewEventMonitor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ScenarioWebViewEventMonitor::ScenarioWebViewEventMonitor(AppWindow* appWindowEve
2828
m_appWindowEventView = new AppWindow(
2929
IDM_CREATION_MODE_WINDOWED,
3030
m_sampleUri,
31+
false,
3132
[this]() -> void {
3233
InitializeEventView(m_appWindowEventView->GetWebView());
3334
});

SampleApps/WebView2APISample/Toolbar.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,14 @@ void Toolbar::UpdateFont()
137137
LOGFONT logFont;
138138
GetObject(GetStockObject(SYSTEM_FONT), sizeof(LOGFONT), &logFont);
139139
double dpiScale = m_appWindow->GetDpiScale();
140+
#ifdef USE_WEBVIEW2_WIN10
140141
double textScale = m_appWindow->GetTextScale();
141142
logFont.lfHeight *= dpiScale * textScale;
142143
logFont.lfWidth *= dpiScale * textScale;
144+
#else
145+
logFont.lfHeight *= dpiScale;
146+
logFont.lfWidth *= dpiScale;
147+
#endif
143148
StringCchCopy(logFont.lfFaceName, ARRAYSIZE(logFont.lfFaceName), s_fontName);
144149
m_font = CreateFontIndirect(&logFont);
145150
}

0 commit comments

Comments
 (0)