-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathScenarioDragDropOverride.cpp
More file actions
134 lines (117 loc) · 4.99 KB
/
ScenarioDragDropOverride.cpp
File metadata and controls
134 lines (117 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "CheckFailure.h"
#include "ScenarioDragDropOverride.h"
using namespace Microsoft::WRL;
static constexpr WCHAR c_samplePath[] = L"ScenarioDragDropOverride.html";
static constexpr WCHAR c_webmessageDefault[] = L"default";
static constexpr WCHAR c_webmessageOverride[] = L"override";
static constexpr WCHAR c_webmessageNoop[] = L"noop";
ScenarioDragDropOverride::ScenarioDragDropOverride(AppWindow* appWindow)
: m_appWindow(appWindow)
{
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
m_webView = m_appWindow->GetWebView();
wil::com_ptr<ICoreWebView2Controller> controller = appWindow->GetWebViewController();
wil::com_ptr<ICoreWebView2CompositionController> compController =
controller.try_query<ICoreWebView2CompositionController>();
if (!compController)
{
return;
}
m_compController5 = compController.try_query<ICoreWebView2CompositionController5>();
if (!m_compController5)
{
return;
}
CHECK_FAILURE(m_webView->add_WebMessageReceived(
Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
{
wil::unique_cotaskmem_string uri;
CHECK_FAILURE(args->get_Source(&uri));
// Always validate that the origin of the message is what you expect.
if (uri.get() != m_sampleUri)
{
return S_OK;
}
wil::unique_cotaskmem_string messageRaw;
CHECK_FAILURE(args->TryGetWebMessageAsString(&messageRaw));
std::wstring message = messageRaw.get();
if (message == c_webmessageDefault)
{
m_dragOverrideMode = DragOverrideMode::DEFAULT;
}
else if (message == c_webmessageOverride)
{
m_dragOverrideMode = DragOverrideMode::OVERRIDE;
}
else if (message == c_webmessageNoop)
{
m_dragOverrideMode = DragOverrideMode::NOOP;
}
return S_OK;
})
.Get(),
&m_webMessageReceivedToken));
//! [DragStarting]
// Using DragStarting to simply make a synchronous DoDragDrop call instead of
// having WebView2 do it.
CHECK_FAILURE(m_compController5->add_DragStarting(
Callback<ICoreWebView2DragStartingEventHandler>(
[this](
ICoreWebView2CompositionController* sender,
ICoreWebView2DragStartingEventArgs* args)
{
if (m_dragOverrideMode != DragOverrideMode::OVERRIDE)
{
// If the event is marked handled, WebView2 will not execute its drag logic.
args->put_Handled(m_dragOverrideMode == DragOverrideMode::NOOP);
return S_OK;
}
wil::com_ptr<IDataObject> dragData;
DWORD okEffects = DROPEFFECT_NONE;
CHECK_FAILURE(args->get_AllowedDropEffects(&okEffects));
CHECK_FAILURE(args->get_Data(&dragData));
// This member refers to an implementation of IDropSource. It is an
// OLE interface that is necessary to initiate drag in an application.
// https://learn.microsoft.com/en-us/windows/win32/api/oleidl/nn-oleidl-idropsource
if (!m_dropSource)
{
m_dropSource = Make<ScenarioDragDropOverrideDropSource>();
}
DWORD effect = DROPEFFECT_NONE;
HRESULT hr = DoDragDrop(dragData.get(), m_dropSource.get(), okEffects, &effect);
args->put_Handled(TRUE);
return hr;
})
.Get(),
&m_dragStartingToken));
//! [DragStarting]
CHECK_FAILURE(m_webView->add_ContentLoading(
Callback<ICoreWebView2ContentLoadingEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT
{
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (uri.get() != m_sampleUri)
{
m_appWindow->DeleteComponent(this);
}
return S_OK;
})
.Get(),
&m_contentLoadingToken));
CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str()));
}
ScenarioDragDropOverride::~ScenarioDragDropOverride()
{
if (m_compController5)
{
CHECK_FAILURE(m_compController5->remove_DragStarting(m_dragStartingToken));
}
CHECK_FAILURE(m_webView->remove_WebMessageReceived(m_webMessageReceivedToken));
CHECK_FAILURE(m_webView->remove_ContentLoading(m_contentLoadingToken));
}