Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit a6d42fc

Browse files
committed
Reduce boilerplate in DirectManipulation
fix some comments
1 parent 03505f6 commit a6d42fc

File tree

3 files changed

+45
-126
lines changed

3 files changed

+45
-126
lines changed

lib/web_ui/lib/src/engine/pointer_converter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ class PointerDataConverter {
616616
case ui.PointerChange.panZoomStart:
617617
case ui.PointerChange.panZoomUpdate:
618618
case ui.PointerChange.panZoomEnd:
619-
// Pointer pan/zoom events are not generated on web
619+
// Pointer pan/zoom events are not generated on web.
620620
assert(false);
621621
break;
622622
}

shell/platform/windows/direct_manipulation.cc

Lines changed: 41 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88
#include "flutter/shell/platform/windows/window_binding_handler_delegate.h"
99
#include "flutter/shell/platform/windows/window_win32.h"
1010

11+
#define VERIFY_HR(operation) \
12+
if (FAILED(operation)) { \
13+
FML_LOG(ERROR) << #operation << " failed"; \
14+
manager_ = nullptr; \
15+
updateManager_ = nullptr; \
16+
viewport_ = nullptr; \
17+
return -1; \
18+
}
19+
20+
#define WARN_HR(operation) \
21+
if (FAILED(operation)) { \
22+
FML_LOG(ERROR) << #operation << " failed"; \
23+
}
24+
1125
namespace flutter {
1226

1327
STDMETHODIMP DirectManipulationEventHandler::QueryInterface(REFIID iid,
@@ -31,20 +45,23 @@ HRESULT DirectManipulationEventHandler::OnViewportStatusChanged(
3145
DIRECTMANIPULATION_STATUS previous) {
3246
if (current == DIRECTMANIPULATION_RUNNING) {
3347
if (!resetting_) {
48+
// Not a false event
3449
if (owner_->binding_handler_delegate) {
3550
owner_->binding_handler_delegate->OnPointerPanZoomStart(
3651
(int32_t) reinterpret_cast<int64_t>(this));
3752
}
3853
}
3954
} else if (previous == DIRECTMANIPULATION_RUNNING) {
4055
if (resetting_) {
56+
// The resetting transition has concluded
4157
resetting_ = false;
4258
} else {
4359
if (owner_->binding_handler_delegate) {
4460
owner_->binding_handler_delegate->OnPointerPanZoomEnd(
4561
(int32_t) reinterpret_cast<int64_t>(this));
4662
}
47-
// Need to reset the content transform
63+
// Need to reset the content transform to its original position
64+
// so that we are ready for the next gesture
4865
// Use resetting_ flag to prevent sending reset also to the framework
4966
resetting_ = true;
5067
RECT rect;
@@ -117,120 +134,38 @@ DirectManipulationOwner::DirectManipulationOwner(WindowWin32* window)
117134
: window_(window) {}
118135

119136
int DirectManipulationOwner::Init(unsigned int width, unsigned int height) {
120-
HRESULT hr = CoCreateInstance(CLSID_DirectManipulationManager, nullptr,
121-
CLSCTX_INPROC_SERVER,
122-
IID_IDirectManipulationManager, &manager_);
123-
if (FAILED(hr)) {
124-
FML_LOG(ERROR)
125-
<< "CoCreateInstance(CLSID_DirectManipulationManager) failed";
126-
manager_ = nullptr;
127-
return -1;
128-
}
129-
130-
hr = manager_->GetUpdateManager(IID_IDirectManipulationUpdateManager,
131-
&updateManager_);
132-
if (FAILED(hr)) {
133-
FML_LOG(ERROR) << "GetUpdateManager failed";
134-
manager_ = nullptr;
135-
updateManager_ = nullptr;
136-
return -1;
137-
}
138-
139-
hr = manager_->CreateViewport(nullptr, window_->GetWindowHandle(),
140-
IID_IDirectManipulationViewport, &viewport_);
141-
if (FAILED(hr)) {
142-
FML_LOG(ERROR) << "CreateViewport failed";
143-
manager_ = nullptr;
144-
updateManager_ = nullptr;
145-
viewport_ = nullptr;
146-
return -1;
147-
}
148-
137+
VERIFY_HR(CoCreateInstance(CLSID_DirectManipulationManager, nullptr,
138+
CLSCTX_INPROC_SERVER,
139+
IID_IDirectManipulationManager, &manager_));
140+
VERIFY_HR(manager_->GetUpdateManager(IID_IDirectManipulationUpdateManager,
141+
&updateManager_));
142+
VERIFY_HR(manager_->CreateViewport(nullptr, window_->GetWindowHandle(),
143+
IID_IDirectManipulationViewport,
144+
&viewport_));
149145
DIRECTMANIPULATION_CONFIGURATION configuration =
150146
DIRECTMANIPULATION_CONFIGURATION_INTERACTION |
151147
DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_X |
152148
DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_Y |
153149
DIRECTMANIPULATION_CONFIGURATION_SCALING;
154-
155-
hr = viewport_->ActivateConfiguration(configuration);
156-
if (FAILED(hr)) {
157-
FML_LOG(ERROR) << "ActivateConfiguration failed";
158-
manager_ = nullptr;
159-
updateManager_ = nullptr;
160-
viewport_ = nullptr;
161-
return -1;
162-
}
163-
164-
hr = viewport_->SetViewportOptions(
165-
DIRECTMANIPULATION_VIEWPORT_OPTIONS_MANUALUPDATE);
166-
if (FAILED(hr)) {
167-
FML_LOG(ERROR) << "SetViewportOptions failed";
168-
manager_ = nullptr;
169-
updateManager_ = nullptr;
170-
viewport_ = nullptr;
171-
return -1;
172-
}
173-
150+
VERIFY_HR(viewport_->ActivateConfiguration(configuration));
151+
VERIFY_HR(viewport_->SetViewportOptions(
152+
DIRECTMANIPULATION_VIEWPORT_OPTIONS_MANUALUPDATE));
174153
handler_ = fml::MakeRefCounted<DirectManipulationEventHandler>(window_, this);
175-
176-
hr = viewport_->AddEventHandler(window_->GetWindowHandle(), handler_.get(),
177-
&viewportHandlerCookie_);
178-
if (FAILED(hr)) {
179-
FML_LOG(ERROR) << "AddEventHandler failed";
180-
manager_ = nullptr;
181-
updateManager_ = nullptr;
182-
viewport_ = nullptr;
183-
return -1;
184-
}
185-
154+
VERIFY_HR(viewport_->AddEventHandler(
155+
window_->GetWindowHandle(), handler_.get(), &viewportHandlerCookie_));
186156
RECT rect = {0, 0, (LONG)width, (LONG)height};
187-
hr = viewport_->SetViewportRect(&rect);
188-
if (FAILED(hr)) {
189-
FML_LOG(ERROR) << "SetViewportRect failed";
190-
manager_ = nullptr;
191-
updateManager_ = nullptr;
192-
viewport_ = nullptr;
193-
return -1;
194-
}
195-
196-
hr = manager_->Activate(window_->GetWindowHandle());
197-
if (FAILED(hr)) {
198-
FML_LOG(ERROR) << "manager_->Activate failed";
199-
manager_ = nullptr;
200-
updateManager_ = nullptr;
201-
viewport_ = nullptr;
202-
return -1;
203-
}
204-
205-
hr = viewport_->Enable();
206-
if (FAILED(hr)) {
207-
FML_LOG(ERROR) << "viewport_->Enable failed";
208-
manager_ = nullptr;
209-
updateManager_ = nullptr;
210-
viewport_ = nullptr;
211-
return -1;
212-
}
213-
214-
hr = updateManager_->Update(nullptr);
215-
if (FAILED(hr)) {
216-
FML_LOG(ERROR) << "updateManager_->Update failed";
217-
manager_ = nullptr;
218-
updateManager_ = nullptr;
219-
viewport_ = nullptr;
220-
return -1;
221-
}
222-
157+
VERIFY_HR(viewport_->SetViewportRect(&rect));
158+
VERIFY_HR(manager_->Activate(window_->GetWindowHandle()));
159+
VERIFY_HR(viewport_->Enable());
160+
VERIFY_HR(updateManager_->Update(nullptr));
223161
return 0;
224162
}
225163

226164
void DirectManipulationOwner::ResizeViewport(unsigned int width,
227165
unsigned int height) {
228166
if (viewport_) {
229167
RECT rect = {0, 0, (LONG)width, (LONG)height};
230-
HRESULT hr = viewport_->SetViewportRect(&rect);
231-
if (FAILED(hr)) {
232-
FML_LOG(ERROR) << "SetViewportRect failed";
233-
}
168+
WARN_HR(viewport_->SetViewportRect(&rect));
234169
}
235170
}
236171

@@ -240,34 +175,15 @@ void DirectManipulationOwner::Destroy() {
240175
handler_->owner_ = nullptr;
241176
}
242177

243-
HRESULT hr;
244178
if (viewport_) {
245-
hr = viewport_->Disable();
246-
if (FAILED(hr)) {
247-
FML_LOG(ERROR) << "viewport_->Stop failed";
248-
}
249-
250-
hr = viewport_->Disable();
251-
if (FAILED(hr)) {
252-
FML_LOG(ERROR) << "viewport_->Disable failed";
253-
}
254-
255-
hr = viewport_->RemoveEventHandler(viewportHandlerCookie_);
256-
if (FAILED(hr)) {
257-
FML_LOG(ERROR) << "viewport_->RemoveEventHandler failed";
258-
}
259-
260-
hr = viewport_->Abandon();
261-
if (FAILED(hr)) {
262-
FML_LOG(ERROR) << "viewport_->Abandon failed";
263-
}
179+
WARN_HR(viewport_->Disable());
180+
WARN_HR(viewport_->Disable());
181+
WARN_HR(viewport_->RemoveEventHandler(viewportHandlerCookie_));
182+
WARN_HR(viewport_->Abandon());
264183
}
265184

266185
if (window_ && manager_) {
267-
hr = manager_->Deactivate(window_->GetWindowHandle());
268-
if (FAILED(hr)) {
269-
FML_LOG(ERROR) << "manager_->Deactivate failed";
270-
}
186+
WARN_HR(manager_->Deactivate(window_->GetWindowHandle()));
271187
}
272188

273189
handler_ = nullptr;

shell/platform/windows/direct_manipulation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ class DirectManipulationEventHandler
7575
private:
7676
WindowWin32* window_;
7777
DirectManipulationOwner* owner_;
78+
// We need to reset some parts of DirectManipulation after each gesture
79+
// A flag is needed to ensure that false events created as the reset occurs
80+
// are not sent to the flutter framework.
7881
bool resetting_ = false;
7982
};
8083

0 commit comments

Comments
 (0)