MCPcopy Create free account
hub / github.com/MicrosoftEdge/WebView2Samples / ViewComponent

Method ViewComponent

SampleApps/WebView2APISample/ViewComponent.cpp:37–214  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35};
36
37ViewComponent::ViewComponent(
38 AppWindow* appWindow, IDCompositionDevice* dcompDevice,
39 winrtComp::Compositor wincompCompositor, bool isDcompTargetMode)
40 : m_appWindow(appWindow), m_controller(appWindow->GetWebViewController()),
41 m_webView(appWindow->GetWebView()), m_dcompDevice(dcompDevice),
42 m_wincompCompositor(wincompCompositor), m_isDcompTargetMode(isDcompTargetMode)
43{
44 //! [ZoomFactorChanged]
45 // Register a handler for the ZoomFactorChanged event.
46 // This handler just announces the new level of zoom on the window's title bar.
47 CHECK_FAILURE(m_controller->add_ZoomFactorChanged(
48 Callback<ICoreWebView2ZoomFactorChangedEventHandler>(
49 [this](ICoreWebView2Controller* sender, IUnknown* args) -> HRESULT {
50 double zoomFactor;
51 CHECK_FAILURE(sender->get_ZoomFactor(&zoomFactor));
52
53 UpdateDocumentTitle(m_appWindow, L" (Zoom: ", zoomFactor);
54 return S_OK;
55 })
56 .Get(),
57 &m_zoomFactorChangedToken));
58 //! [ZoomFactorChanged]
59
60 CHECK_FAILURE(m_webView->add_NavigationStarting(
61 Callback<ICoreWebView2NavigationStartingEventHandler>(
62 [this](ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args)
63 -> HRESULT {
64 wil::unique_cotaskmem_string newUri;
65 CHECK_FAILURE(args->get_Uri(&newUri));
66
67 wil::unique_cotaskmem_string oldUri;
68 CHECK_FAILURE(m_webView->get_Source(&oldUri));
69
70 std::wstring appStartPage = AppStartPage::GetUri(m_appWindow);
71 size_t queryIndex = appStartPage.find('?');
72
73 if (appStartPage.compare(0, queryIndex, newUri.get(), queryIndex) == 0)
74 {
75 // When navigating to the app start page, make the background of
76 // the html be the WebView2 logo. We do this by making
77 // the background color transparent so the WebView2 logo in the AppWindow
78 // shows through.
79 COREWEBVIEW2_COLOR transparentColor = {0, 0, 0, 0};
80 wil::com_ptr<ICoreWebView2Controller2> controller2 =
81 m_controller.query<ICoreWebView2Controller2>();
82 // Save the previous background color to restore when navigating away.
83 CHECK_FAILURE(controller2->get_DefaultBackgroundColor(&m_webViewColor));
84 CHECK_FAILURE(controller2->put_DefaultBackgroundColor(transparentColor));
85 }
86 else if (appStartPage.compare(0, queryIndex, oldUri.get(), queryIndex) == 0)
87 {
88 // When navigating away from the app start page, set the background color
89 // back to the previous value. If the user changed the background color,
90 // m_webViewColor will have changed.
91 wil::com_ptr<ICoreWebView2Controller2> controller2 =
92 m_controller.query<ICoreWebView2Controller2>();
93 CHECK_FAILURE(controller2->put_DefaultBackgroundColor(m_webViewColor));
94 }

Callers

nothing calls this directly

Calls 6

UpdateDocumentTitleFunction · 0.85
GetUriFunction · 0.85
InitMethod · 0.80
GetWebViewControllerMethod · 0.45
GetWebViewMethod · 0.45
GetMainWindowMethod · 0.45

Tested by

no test coverage detected