From dc724ea5d9dd8155478a47471ce2fc1cd37bde54 Mon Sep 17 00:00:00 2001 From: James Clarke Date: Thu, 13 May 2021 17:18:23 -0700 Subject: [PATCH] Windows: UWP ViewController accepts a CoreApplicationView and exposes to plugins (flutter/engine#25879) --- .../client_wrapper/flutter_view_controller.cc | 6 ++--- .../include/flutter/flutter_view.h | 7 +++++- .../include/flutter/flutter_view_controller.h | 3 ++- .../platform/windows/flutter_window_win32.cc | 4 ++++ .../platform/windows/flutter_window_win32.h | 3 +++ .../platform/windows/flutter_window_winuwp.cc | 15 ++++++++---- .../platform/windows/flutter_window_winuwp.h | 23 +++++++++++++------ .../shell/platform/windows/flutter_windows.cc | 15 ++++++++++-- .../platform/windows/flutter_windows_view.cc | 4 ++++ .../platform/windows/flutter_windows_view.h | 3 +++ .../windows/flutter_windows_winuwp.cc | 6 ++--- .../platform/windows/public/flutter_windows.h | 15 ++++++++---- .../testing/mock_window_binding_handler.h | 1 + .../platform/windows/window_binding_handler.h | 19 +++++++++++++-- 14 files changed, 96 insertions(+), 28 deletions(-) diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc index 5e47d8c382..b3bd963c9c 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/flutter_view_controller.cc @@ -11,12 +11,12 @@ namespace flutter { #ifdef WINUWP FlutterViewController::FlutterViewController( - ABI::Windows::UI::Core::CoreWindow* window, + ABI::Windows::ApplicationModel::Core::CoreApplicationView* applicationview, ABI::Windows::ApplicationModel::Activation::IActivatedEventArgs* args, const DartProject& project) { engine_ = std::make_unique(project); - controller_ = FlutterDesktopViewControllerCreateFromCoreWindow( - window, args, engine_->RelinquishEngine()); + controller_ = FlutterDesktopViewControllerCreateFromCoreApplicationView( + applicationview, args, engine_->RelinquishEngine()); if (!controller_) { std::cerr << "Failed to create view controller." << std::endl; return; diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h index 057d905308..52945676d7 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view.h @@ -20,7 +20,12 @@ class FlutterView { FlutterView(FlutterView const&) = delete; FlutterView& operator=(FlutterView const&) = delete; -#ifndef WINUWP +#ifdef WINUWP + // Returns the backing CoreApplicationView for the view. + ABI::Windows::ApplicationModel::Core::CoreApplicationView* GetNativeWindow() { + return FlutterDesktopViewGetCoreApplicationView(view_); + } +#else // Returns the backing HWND for the view. HWND GetNativeWindow() { return FlutterDesktopViewGetHWND(view_); } #endif diff --git a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h index 66808fc135..b9fce9ec31 100644 --- a/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h +++ b/engine/src/flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h @@ -47,7 +47,8 @@ class FlutterViewController { // |IActivatedEventArgs| will be used to configure the engine switches. Can // be set to nullptr. explicit FlutterViewController( - ABI::Windows::UI::Core::CoreWindow* window, + ABI::Windows::ApplicationModel::Core::CoreApplicationView* + applicationview, ABI::Windows::ApplicationModel::Activation::IActivatedEventArgs* args, const DartProject& project); #endif diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc b/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc index b0e5d88a1e..aa75f30ac0 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_win32.cc @@ -81,6 +81,10 @@ WindowsRenderTarget FlutterWindowWin32::GetRenderTarget() { return WindowsRenderTarget(GetWindowHandle()); } +PlatformWindow FlutterWindowWin32::GetPlatformWindow() { + return GetWindowHandle(); +} + float FlutterWindowWin32::GetDpiScale() { return static_cast(GetCurrentDPI()) / static_cast(base_dpi); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_win32.h b/engine/src/flutter/shell/platform/windows/flutter_window_win32.h index a73ba88112..d4fe0f5623 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_win32.h +++ b/engine/src/flutter/shell/platform/windows/flutter_window_win32.h @@ -86,6 +86,9 @@ class FlutterWindowWin32 : public WindowWin32, public WindowBindingHandler { // |FlutterWindowBindingHandler| WindowsRenderTarget GetRenderTarget() override; + // |FlutterWindowBindingHandler| + PlatformWindow GetPlatformWindow() override; + // |FlutterWindowBindingHandler| float GetDpiScale() override; diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc index 447477fc3d..bace4d7529 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.cc @@ -10,10 +10,13 @@ namespace flutter { static constexpr double kControllerScrollMultiplier = 3; FlutterWindowWinUWP::FlutterWindowWinUWP( - ABI::Windows::UI::Core::CoreWindow* window) { - winrt::Windows::UI::Core::CoreWindow cw{nullptr}; - winrt::copy_from_abi(cw, window); - window_ = cw; + ABI::Windows::ApplicationModel::Core::CoreApplicationView* + applicationview) { + winrt::Windows::ApplicationModel::Core::CoreApplicationView cav{nullptr}; + winrt::copy_from_abi(cav, applicationview); + + application_view_ = cav; + window_ = application_view_.CoreWindow(); SetEventHandlers(); @@ -46,6 +49,10 @@ WindowsRenderTarget FlutterWindowWinUWP::GetRenderTarget() { #endif } +PlatformWindow FlutterWindowWinUWP::GetPlatformWindow() { + return application_view_; +} + void FlutterWindowWinUWP::ApplyInverseDpiScalingTransform() { // Apply inverse transform to negate built in DPI scaling in order to render // at native scale. diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h index 9870b96d75..8cb4455568 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h +++ b/engine/src/flutter/shell/platform/windows/flutter_window_winuwp.h @@ -5,12 +5,13 @@ #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_UWP_FLUTTER_WINDOW_H_ #define FLUTTER_SHELL_PLATFORM_WINDOWS_UWP_FLUTTER_WINDOW_H_ -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #include "flutter/shell/platform/embedder/embedder.h" #include "flutter/shell/platform/windows/display_helper_winuwp.h" @@ -25,7 +26,8 @@ namespace flutter { // Specifically handles window events within windows. class FlutterWindowWinUWP : public WindowBindingHandler { public: - explicit FlutterWindowWinUWP(ABI::Windows::UI::Core::CoreWindow* window); + explicit FlutterWindowWinUWP( + ABI::Windows::ApplicationModel::Core::CoreApplicationView* window); virtual ~FlutterWindowWinUWP(); @@ -35,6 +37,9 @@ class FlutterWindowWinUWP : public WindowBindingHandler { // |WindowBindingHandler| WindowsRenderTarget GetRenderTarget() override; + // |FlutterWindowBindingHandler| + PlatformWindow GetPlatformWindow() override; + // |WindowBindingHandler| float GetDpiScale() override; @@ -119,6 +124,10 @@ class FlutterWindowWinUWP : public WindowBindingHandler { // Backing CoreWindow. nullptr if not set. winrt::Windows::UI::Core::CoreWindow window_{nullptr}; + // CoreApplicationView that owns window_. nullptr if not set. + winrt::Windows::ApplicationModel::Core::CoreApplicationView application_view_{ + nullptr}; + // Pointer to a FlutterWindowsView that can be // used to update engine windowing and input state. WindowBindingHandlerDelegate* binding_handler_delegate_; diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index 9babba5ce3..9b8b7d8047 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -128,9 +128,20 @@ FlutterDesktopTextureRegistrarRef FlutterDesktopEngineGetTextureRegistrar( EngineFromHandle(engine)->texture_registrar()); } -HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) { - return std::get(*ViewFromHandle(view)->GetRenderTarget()); +#ifdef WINUWP +ABI::Windows::ApplicationModel::Core::CoreApplicationView* +FlutterDesktopViewGetCoreApplicationView(FlutterDesktopViewRef view) { + return static_cast< + ABI::Windows::ApplicationModel::Core::CoreApplicationView*>( + winrt::get_abi(ViewFromHandle(view)->GetPlatformWindow())); } +#endif + +#ifndef WINUWP +HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) { + return ViewFromHandle(view)->GetPlatformWindow(); +} +#endif FlutterDesktopViewRef FlutterDesktopPluginRegistrarGetView( FlutterDesktopPluginRegistrarRef registrar) { diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc index aeac73f9d0..d4b2f056af 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc @@ -487,6 +487,10 @@ WindowsRenderTarget* FlutterWindowsView::GetRenderTarget() const { return render_target_.get(); } +PlatformWindow FlutterWindowsView::GetPlatformWindow() const { + return binding_handler_->GetPlatformWindow(); +} + FlutterWindowsEngine* FlutterWindowsView::GetEngine() { return engine_.get(); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h index 8b3ef7153a..f70d1b1aa6 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h @@ -59,6 +59,9 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate, // Return the currently configured WindowsRenderTarget. WindowsRenderTarget* GetRenderTarget() const; + // Return the currently configured PlatformWindow. + PlatformWindow GetPlatformWindow() const; + // Returns the engine backing this view. FlutterWindowsEngine* GetEngine(); diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_winuwp.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_winuwp.cc index 90363154fc..274eb48cde 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_winuwp.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_winuwp.cc @@ -41,12 +41,12 @@ std::vector SplitCommaSeparatedString(const std::string& s) { } FlutterDesktopViewControllerRef -FlutterDesktopViewControllerCreateFromCoreWindow( - ABI::Windows::UI::Core::CoreWindow* window, +FlutterDesktopViewControllerCreateFromCoreApplicationView( + ABI::Windows::ApplicationModel::Core::CoreApplicationView* application_view, ABI::Windows::ApplicationModel::Activation::IActivatedEventArgs* args, FlutterDesktopEngineRef engine) { std::unique_ptr window_wrapper = - std::make_unique(window); + std::make_unique(application_view); auto state = std::make_unique(); state->view = diff --git a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h index 76802ba567..14f8ea464a 100644 --- a/engine/src/flutter/shell/platform/windows/public/flutter_windows.h +++ b/engine/src/flutter/shell/platform/windows/public/flutter_windows.h @@ -77,11 +77,11 @@ typedef struct { // FlutterDesktopViewControllerDestroy. Returns a null pointer in the event of // an error. #ifdef WINUWP -// The CoreWindow implementation accepts a pointer to the host CoreWindow -// and view hookup is performed in the construction path. +// The CoreApplicationView implementation accepts a pointer to the host +// CoreApplicationView and view hookup is performed in the construction path. FLUTTER_EXPORT FlutterDesktopViewControllerRef -FlutterDesktopViewControllerCreateFromCoreWindow( - ABI::Windows::UI::Core::CoreWindow* window, +FlutterDesktopViewControllerCreateFromCoreApplicationView( + ABI::Windows::ApplicationModel::Core::CoreApplicationView* window, ABI::Windows::ApplicationModel::Activation::IActivatedEventArgs* args, FlutterDesktopEngineRef engine); #else //! WINUWP @@ -192,7 +192,12 @@ FlutterDesktopEngineGetTextureRegistrar( // ========== View ========== -#ifndef WINUWP +#ifdef WINUWP +// Return backing CoreApplicationView for manipulation of CoreWindow and +// CoreTitleBar in host application. +FLUTTER_EXPORT ABI::Windows::ApplicationModel::Core::CoreApplicationView* +FlutterDesktopViewGetCoreApplicationView(FlutterDesktopViewRef view); +#else // Return backing HWND for manipulation in host application. FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view); #endif diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h b/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h index bac12fe065..d3619df55c 100644 --- a/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h +++ b/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler.h @@ -25,6 +25,7 @@ class MockWindowBindingHandler : public WindowBindingHandler { MOCK_METHOD1(SetView, void(WindowBindingHandlerDelegate* view)); MOCK_METHOD0(GetRenderTarget, WindowsRenderTarget()); + MOCK_METHOD0(GetPlatformWindow, PlatformWindow()); MOCK_METHOD0(GetDpiScale, float()); MOCK_METHOD0(OnWindowResized, void()); MOCK_METHOD0(GetPhysicalWindowBounds, PhysicalWindowBounds()); diff --git a/engine/src/flutter/shell/platform/windows/window_binding_handler.h b/engine/src/flutter/shell/platform/windows/window_binding_handler.h index 674d6990b5..f699bda24f 100644 --- a/engine/src/flutter/shell/platform/windows/window_binding_handler.h +++ b/engine/src/flutter/shell/platform/windows/window_binding_handler.h @@ -15,6 +15,7 @@ #include "flutter/shell/platform/windows/window_binding_handler_delegate.h" #ifdef WINUWP +#include #include #endif @@ -28,6 +29,16 @@ struct PhysicalWindowBounds { size_t height; }; +// Type representing an underlying platform window. +#ifdef WINUWP +using PlatformWindow = + winrt::Windows::ApplicationModel::Core::CoreApplicationView; +#else +using PlatformWindow = HWND; +#endif + +// Type representing a platform object that can be accepted by the Angle +// rendering layer to bind to and render pixels into. #ifdef WINUWP using WindowsRenderTarget = std::variant