From 7625c51ef426b73a8bda5853a4bd2e2d9d3aeb55 Mon Sep 17 00:00:00 2001 From: Andrew Knight Date: Fri, 7 Mar 2014 12:39:47 +0200 Subject: Windows Phone: Handle back-button press When the back button is pressed, send an immediate key event to the topmost window (or the application if there is no window). If it is accepted, mark the native event as accepted and send a key release event. This way, the application may call accept() on the KeyPress event for Qt::Key_Back in order to create backstepping behaviors within the app. This is in line with Android, which quits the app when the event is ignored. On Windows Phone, the default behavior occurs when the event is ignored, which is to back out of the application and leave it running in the background. Task-number: QTBUG-35951 Change-Id: I46d15478f441f73d3660370370689b2f9fa11f25 Reviewed-by: Oliver Wolff --- src/plugins/platforms/winrt/qwinrtscreen.cpp | 37 ++++++++++++++++++++++++++++ src/plugins/platforms/winrt/qwinrtscreen.h | 13 ++++++++++ 2 files changed, 50 insertions(+) (limited to 'src/plugins') diff --git a/src/plugins/platforms/winrt/qwinrtscreen.cpp b/src/plugins/platforms/winrt/qwinrtscreen.cpp index d3ede89865..2e38f81499 100644 --- a/src/plugins/platforms/winrt/qwinrtscreen.cpp +++ b/src/plugins/platforms/winrt/qwinrtscreen.cpp @@ -63,6 +63,9 @@ #include #include #include +#ifdef Q_OS_WINPHONE +#include +#endif using namespace Microsoft::WRL; using namespace Microsoft::WRL::Wrappers; @@ -75,6 +78,9 @@ using namespace ABI::Windows::UI::Input; using namespace ABI::Windows::UI::ViewManagement; using namespace ABI::Windows::Devices::Input; using namespace ABI::Windows::Graphics::Display; +#ifdef Q_OS_WINPHONE +using namespace ABI::Windows::Phone::UI::Input; +#endif typedef IEventHandler ResumeHandler; typedef IEventHandler SuspendHandler; @@ -87,6 +93,9 @@ typedef ITypedEventHandler PointerHandler; typedef ITypedEventHandler SizeChangedHandler; typedef ITypedEventHandler VisibilityChangedHandler; typedef ITypedEventHandler AutomationProviderRequestedHandler; +#ifdef Q_OS_WINPHONE +typedef IEventHandler BackPressedHandler; +#endif QT_BEGIN_NAMESPACE @@ -465,6 +474,11 @@ QWinRTScreen::QWinRTScreen(ICoreWindow *window) m_coreWindow->add_PointerReleased(Callback(this, &QWinRTScreen::onPointerUpdated).Get(), &m_tokens[QEvent::MouseButtonRelease]); m_coreWindow->add_PointerWheelChanged(Callback(this, &QWinRTScreen::onPointerUpdated).Get(), &m_tokens[QEvent::Wheel]); m_coreWindow->add_SizeChanged(Callback(this, &QWinRTScreen::onSizeChanged).Get(), &m_tokens[QEvent::Resize]); +#ifdef Q_OS_WINPHONE + ComPtr hardwareButtons; + if (SUCCEEDED(GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Phone_UI_Input_HardwareButtons).Get(), &hardwareButtons))) + hardwareButtons->add_BackPressed(Callback(this, &QWinRTScreen::onBackButtonPressed).Get(), &m_tokens[QEvent::User]); +#endif // Q_OS_WINPHONE // Window event handlers m_coreWindow->add_Activated(Callback(this, &QWinRTScreen::onActivated).Get(), &m_tokens[QEvent::WindowActivate]); @@ -1002,4 +1016,27 @@ HRESULT QWinRTScreen::onOrientationChanged(IInspectable *) return S_OK; } +#ifdef Q_OS_WINPHONE +HRESULT QWinRTScreen::onBackButtonPressed(IInspectable *, IBackPressedEventArgs *args) +{ + QKeyEvent backPress(QEvent::KeyPress, Qt::Key_Back, Qt::NoModifier); + backPress.setAccepted(false); + + QObject *receiver = m_visibleWindows.isEmpty() + ? static_cast(QGuiApplication::instance()) + : static_cast(m_visibleWindows.first()); + + // If the event is ignored, the app will suspend + QGuiApplication::sendEvent(receiver, &backPress); + if (backPress.isAccepted()) { + args->put_Handled(true); + // If the app accepts the event, send the release for symmetry + QKeyEvent backRelease(QEvent::KeyRelease, Qt::Key_Back, Qt::NoModifier); + QGuiApplication::sendEvent(receiver, &backRelease); + } + + return S_OK; +} +#endif // Q_OS_WINPHONE + QT_END_NAMESPACE diff --git a/src/plugins/platforms/winrt/qwinrtscreen.h b/src/plugins/platforms/winrt/qwinrtscreen.h index 3131f879b5..c6511e9446 100644 --- a/src/plugins/platforms/winrt/qwinrtscreen.h +++ b/src/plugins/platforms/winrt/qwinrtscreen.h @@ -80,6 +80,15 @@ namespace ABI { struct IDisplayPropertiesStatics; } } +#ifdef Q_OS_WINPHONE + namespace Phone { + namespace UI { + namespace Input { + struct IBackPressedEventArgs; + } + } + } +#endif } } struct IInspectable; @@ -149,6 +158,10 @@ private: HRESULT onOrientationChanged(IInspectable *); +#ifdef Q_OS_WINPHONE + HRESULT onBackButtonPressed(IInspectable *, ABI::Windows::Phone::UI::Input::IBackPressedEventArgs *args); +#endif + ABI::Windows::UI::Core::ICoreWindow *m_coreWindow; ABI::Windows::UI::ViewManagement::IApplicationViewStatics *m_applicationView; ABI::Windows::ApplicationModel::Core::ICoreApplication *m_application; -- cgit v1.2.3