This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Win32 trackpad gestures #31594
Merged
fluttergithubbot
merged 5 commits into
flutter:main
from
moffatman:trackpad_gestures_win32
May 20, 2022
Merged
Win32 trackpad gestures #31594
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/fml/logging.h" | ||
|
|
||
| #include "flutter/shell/platform/windows/direct_manipulation.h" | ||
| #include "flutter/shell/platform/windows/window_binding_handler_delegate.h" | ||
| #include "flutter/shell/platform/windows/window_win32.h" | ||
|
|
||
| #define RETURN_IF_FAILED(operation) \ | ||
| if (FAILED(operation)) { \ | ||
| FML_LOG(ERROR) << #operation << " failed"; \ | ||
| manager_ = nullptr; \ | ||
| updateManager_ = nullptr; \ | ||
| viewport_ = nullptr; \ | ||
| return -1; \ | ||
| } | ||
|
|
||
| #define WARN_IF_FAILED(operation) \ | ||
| if (FAILED(operation)) { \ | ||
| FML_LOG(ERROR) << #operation << " failed"; \ | ||
| } | ||
|
|
||
| namespace flutter { | ||
|
|
||
| STDMETHODIMP DirectManipulationEventHandler::QueryInterface(REFIID iid, | ||
| void** ppv) { | ||
| if ((iid == IID_IUnknown) || | ||
| (iid == IID_IDirectManipulationViewportEventHandler)) { | ||
| *ppv = static_cast<IDirectManipulationViewportEventHandler*>(this); | ||
| AddRef(); | ||
| return S_OK; | ||
| } else if (iid == IID_IDirectManipulationInteractionEventHandler) { | ||
| *ppv = static_cast<IDirectManipulationInteractionEventHandler*>(this); | ||
| AddRef(); | ||
| return S_OK; | ||
| } | ||
| return E_NOINTERFACE; | ||
| } | ||
|
|
||
| HRESULT DirectManipulationEventHandler::OnViewportStatusChanged( | ||
| IDirectManipulationViewport* viewport, | ||
| DIRECTMANIPULATION_STATUS current, | ||
| DIRECTMANIPULATION_STATUS previous) { | ||
| if (during_synthesized_reset_ && previous == DIRECTMANIPULATION_RUNNING) { | ||
| during_synthesized_reset_ = false; | ||
| } else if (current == DIRECTMANIPULATION_RUNNING) { | ||
| if (!during_synthesized_reset_) { | ||
| // Not a false event. | ||
| if (owner_->binding_handler_delegate) { | ||
| owner_->binding_handler_delegate->OnPointerPanZoomStart( | ||
| (int32_t) reinterpret_cast<int64_t>(this)); | ||
| } | ||
| } | ||
| } else if (previous == DIRECTMANIPULATION_RUNNING) { | ||
| if (owner_->binding_handler_delegate) { | ||
| owner_->binding_handler_delegate->OnPointerPanZoomEnd( | ||
| (int32_t) reinterpret_cast<int64_t>(this)); | ||
| } | ||
| // Need to reset the content transform to its original position | ||
| // so that we are ready for the next gesture. | ||
| // Use during_synthesized_reset_ flag to prevent sending reset also to the | ||
| // framework. | ||
| during_synthesized_reset_ = true; | ||
| RECT rect; | ||
| HRESULT hr = viewport->GetViewportRect(&rect); | ||
| if (FAILED(hr)) { | ||
| FML_LOG(ERROR) << "Failed to get the current viewport rect"; | ||
| return E_FAIL; | ||
| } | ||
| hr = viewport->ZoomToRect(rect.left, rect.top, rect.right, rect.bottom, | ||
| false); | ||
| if (FAILED(hr)) { | ||
| FML_LOG(ERROR) << "Failed to reset the gesture using ZoomToRect"; | ||
| return E_FAIL; | ||
| } | ||
| } | ||
| return S_OK; | ||
| } | ||
|
|
||
| HRESULT DirectManipulationEventHandler::OnViewportUpdated( | ||
| IDirectManipulationViewport* viewport) { | ||
| return S_OK; | ||
| } | ||
|
|
||
| HRESULT DirectManipulationEventHandler::OnContentUpdated( | ||
| IDirectManipulationViewport* viewport, | ||
| IDirectManipulationContent* content) { | ||
| float transform[6]; | ||
| HRESULT hr = content->GetContentTransform(transform, ARRAYSIZE(transform)); | ||
| if (FAILED(hr)) { | ||
| FML_LOG(ERROR) << "GetContentTransform failed"; | ||
| return S_OK; | ||
| } | ||
| if (!during_synthesized_reset_) { | ||
| // DirectManipulation provides updates with very high precision. If the user | ||
| // holds their fingers steady on a trackpad, DirectManipulation sends | ||
| // jittery updates. This calculation will reduce the precision of the scale | ||
| // value of the event to avoid jitter. | ||
| const int mantissa_bits_chop = 2; | ||
| const float factor = (1 << mantissa_bits_chop) + 1; | ||
| float c = factor * transform[0]; | ||
| float scale = c - (c - transform[0]); | ||
| float pan_x = transform[4]; | ||
| float pan_y = transform[5]; | ||
| if (owner_->binding_handler_delegate) { | ||
| owner_->binding_handler_delegate->OnPointerPanZoomUpdate( | ||
| (int32_t) reinterpret_cast<int64_t>(this), pan_x, pan_y, scale, 0); | ||
| } | ||
| } | ||
| return S_OK; | ||
| } | ||
|
|
||
| HRESULT DirectManipulationEventHandler::OnInteraction( | ||
| IDirectManipulationViewport2* viewport, | ||
| DIRECTMANIPULATION_INTERACTION_TYPE interaction) { | ||
| return S_OK; | ||
| } | ||
|
|
||
| ULONG STDMETHODCALLTYPE DirectManipulationEventHandler::AddRef() { | ||
| RefCountedThreadSafe::AddRef(); | ||
| return 0; | ||
| } | ||
|
|
||
| ULONG STDMETHODCALLTYPE DirectManipulationEventHandler::Release() { | ||
| RefCountedThreadSafe::Release(); | ||
| return 0; | ||
| } | ||
|
|
||
| DirectManipulationOwner::DirectManipulationOwner(WindowWin32* window) | ||
| : window_(window) {} | ||
|
|
||
| int DirectManipulationOwner::Init(unsigned int width, unsigned int height) { | ||
| RETURN_IF_FAILED(CoCreateInstance(CLSID_DirectManipulationManager, nullptr, | ||
| CLSCTX_INPROC_SERVER, | ||
| IID_IDirectManipulationManager, &manager_)); | ||
| RETURN_IF_FAILED(manager_->GetUpdateManager( | ||
| IID_IDirectManipulationUpdateManager, &updateManager_)); | ||
| RETURN_IF_FAILED(manager_->CreateViewport(nullptr, window_->GetWindowHandle(), | ||
| IID_IDirectManipulationViewport, | ||
| &viewport_)); | ||
| DIRECTMANIPULATION_CONFIGURATION configuration = | ||
| DIRECTMANIPULATION_CONFIGURATION_INTERACTION | | ||
| DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_X | | ||
| DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_Y | | ||
| DIRECTMANIPULATION_CONFIGURATION_SCALING; | ||
| RETURN_IF_FAILED(viewport_->ActivateConfiguration(configuration)); | ||
| RETURN_IF_FAILED(viewport_->SetViewportOptions( | ||
| DIRECTMANIPULATION_VIEWPORT_OPTIONS_MANUALUPDATE)); | ||
| handler_ = fml::MakeRefCounted<DirectManipulationEventHandler>(this); | ||
| RETURN_IF_FAILED(viewport_->AddEventHandler( | ||
| window_->GetWindowHandle(), handler_.get(), &viewportHandlerCookie_)); | ||
| RECT rect = {0, 0, (LONG)width, (LONG)height}; | ||
| RETURN_IF_FAILED(viewport_->SetViewportRect(&rect)); | ||
| RETURN_IF_FAILED(manager_->Activate(window_->GetWindowHandle())); | ||
| RETURN_IF_FAILED(viewport_->Enable()); | ||
| RETURN_IF_FAILED(updateManager_->Update(nullptr)); | ||
| return 0; | ||
| } | ||
|
|
||
| void DirectManipulationOwner::ResizeViewport(unsigned int width, | ||
| unsigned int height) { | ||
| if (viewport_) { | ||
| RECT rect = {0, 0, (LONG)width, (LONG)height}; | ||
| WARN_IF_FAILED(viewport_->SetViewportRect(&rect)); | ||
| } | ||
| } | ||
|
|
||
| void DirectManipulationOwner::Destroy() { | ||
| if (handler_) { | ||
| handler_->owner_ = nullptr; | ||
| } | ||
|
|
||
| if (viewport_) { | ||
| WARN_IF_FAILED(viewport_->Disable()); | ||
| WARN_IF_FAILED(viewport_->Disable()); | ||
| WARN_IF_FAILED(viewport_->RemoveEventHandler(viewportHandlerCookie_)); | ||
| WARN_IF_FAILED(viewport_->Abandon()); | ||
| } | ||
|
|
||
| if (window_ && manager_) { | ||
| WARN_IF_FAILED(manager_->Deactivate(window_->GetWindowHandle())); | ||
| } | ||
|
|
||
| handler_ = nullptr; | ||
| viewport_ = nullptr; | ||
| updateManager_ = nullptr; | ||
| manager_ = nullptr; | ||
| window_ = nullptr; | ||
| } | ||
|
|
||
| void DirectManipulationOwner::SetContact(UINT contactId) { | ||
| if (viewport_) { | ||
| viewport_->SetContact(contactId); | ||
| } | ||
| } | ||
|
|
||
| void DirectManipulationOwner::SetBindingHandlerDelegate( | ||
| WindowBindingHandlerDelegate* delegate) { | ||
| binding_handler_delegate = delegate; | ||
| } | ||
|
|
||
| void DirectManipulationOwner::Update() { | ||
| if (updateManager_) { | ||
| HRESULT hr = updateManager_->Update(nullptr); | ||
| if (FAILED(hr)) { | ||
| FML_LOG(ERROR) << "updateManager_->Update failed"; | ||
| auto error = GetLastError(); | ||
| FML_LOG(ERROR) << error; | ||
| LPWSTR message = nullptr; | ||
| size_t size = FormatMessageW( | ||
| FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | | ||
| FORMAT_MESSAGE_IGNORE_INSERTS, | ||
| NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | ||
| reinterpret_cast<LPWSTR>(&message), 0, NULL); | ||
| FML_LOG(ERROR) << message; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace flutter | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_DIRECT_MANIPULATION_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_WINDOWS_DIRECT_MANIPULATION_H_ | ||
|
|
||
| #include "flutter/fml/memory/ref_counted.h" | ||
|
|
||
| #include <wrl/client.h> | ||
| #include "directmanipulation.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| class WindowWin32; | ||
| class WindowBindingHandlerDelegate; | ||
|
|
||
| class DirectManipulationEventHandler; | ||
|
|
||
| // Owner for a DirectManipulation event handler, contains the link between | ||
| // DirectManipulation and WindowBindingHandlerDelegate. | ||
| class DirectManipulationOwner { | ||
moffatman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public: | ||
| explicit DirectManipulationOwner(WindowWin32* window); | ||
| // Initialize a DirectManipulation viewport with specified width and height. | ||
| // These should match the width and height of the application window. | ||
| int Init(unsigned int width, unsigned int height); | ||
| // Resize the DirectManipulation viewport. Should be called when the | ||
| // application window is resized. | ||
| void ResizeViewport(unsigned int width, unsigned int height); | ||
| // Set the WindowBindingHandlerDelegate which will receive callbacks based on | ||
| // DirectManipulation updates. | ||
| void SetBindingHandlerDelegate( | ||
| WindowBindingHandlerDelegate* binding_handler_delegate); | ||
| // Called when DM_POINTERHITTEST occurs with an acceptable pointer type. Will | ||
| // start DirectManipulation for that interaction. | ||
| void SetContact(UINT contactId); | ||
| // Called to get updates from DirectManipulation. Should be called frequently | ||
| // to provide smooth updates. | ||
| void Update(); | ||
| // Release child event handler and OS resources. | ||
| void Destroy(); | ||
| // The target that should be updated when DirectManipulation provides a new | ||
| // pan/zoom transformation. | ||
| WindowBindingHandlerDelegate* binding_handler_delegate; | ||
|
|
||
| private: | ||
| // The window gesture input is occuring on. | ||
| WindowWin32* window_; | ||
| // Cookie needed to register child event handler with viewport. | ||
| DWORD viewportHandlerCookie_; | ||
| // Object needed for operation of the DirectManipulation API. | ||
| Microsoft::WRL::ComPtr<IDirectManipulationManager> manager_; | ||
| // Object needed for operation of the DirectManipulation API. | ||
| Microsoft::WRL::ComPtr<IDirectManipulationUpdateManager> updateManager_; | ||
| // Object needed for operation of the DirectManipulation API. | ||
| Microsoft::WRL::ComPtr<IDirectManipulationViewport> viewport_; | ||
| // Child needed for operation of the DirectManipulation API. | ||
| fml::RefPtr<DirectManipulationEventHandler> handler_; | ||
| }; | ||
|
|
||
| // Implements DirectManipulation event handling interfaces, receives calls from | ||
| // system when gesture events occur. | ||
| class DirectManipulationEventHandler | ||
| : public fml::RefCountedThreadSafe<DirectManipulationEventHandler>, | ||
| public IDirectManipulationViewportEventHandler, | ||
| public IDirectManipulationInteractionEventHandler { | ||
| friend class DirectManipulationOwner; | ||
| FML_FRIEND_REF_COUNTED_THREAD_SAFE(DirectManipulationEventHandler); | ||
| FML_FRIEND_MAKE_REF_COUNTED(DirectManipulationEventHandler); | ||
|
|
||
| public: | ||
| explicit DirectManipulationEventHandler(DirectManipulationOwner* owner) | ||
| : owner_(owner) {} | ||
|
|
||
| // |IUnknown| | ||
| STDMETHODIMP QueryInterface(REFIID iid, void** ppv) override; | ||
|
|
||
| // |IUnknown| | ||
| ULONG STDMETHODCALLTYPE AddRef() override; | ||
|
|
||
| // |IUnknown| | ||
| ULONG STDMETHODCALLTYPE Release() override; | ||
|
|
||
| // |IDirectManipulationViewportEventHandler| | ||
| HRESULT STDMETHODCALLTYPE | ||
| OnViewportStatusChanged(IDirectManipulationViewport* viewport, | ||
| DIRECTMANIPULATION_STATUS current, | ||
| DIRECTMANIPULATION_STATUS previous) override; | ||
|
|
||
| // |IDirectManipulationViewportEventHandler| | ||
| HRESULT STDMETHODCALLTYPE | ||
| OnViewportUpdated(IDirectManipulationViewport* viewport) override; | ||
|
|
||
| // |IDirectManipulationViewportEventHandler| | ||
| HRESULT STDMETHODCALLTYPE | ||
| OnContentUpdated(IDirectManipulationViewport* viewport, | ||
| IDirectManipulationContent* content) override; | ||
|
|
||
| // |IDirectManipulationInteractionEventHandler| | ||
| HRESULT STDMETHODCALLTYPE | ||
| OnInteraction(IDirectManipulationViewport2* viewport, | ||
| DIRECTMANIPULATION_INTERACTION_TYPE interaction) override; | ||
|
|
||
| private: | ||
| // Parent object, used to store the target for gesture event updates. | ||
| DirectManipulationOwner* owner_; | ||
| // We need to reset some parts of DirectManipulation after each gesture | ||
| // A flag is needed to ensure that false events created as the reset occurs | ||
| // are not sent to the flutter framework. | ||
| bool during_synthesized_reset_ = false; | ||
| }; | ||
|
|
||
| } // namespace flutter | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_DIRECT_MANIPULATION_H_ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we tested this part yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test for it