From ceb25d38d0653b8f98712295e00f2ab13cfa47c4 Mon Sep 17 00:00:00 2001 From: yaakovschectman <109111084+yaakovschectman@users.noreply.github.com> Date: Thu, 25 Aug 2022 16:24:21 -0400 Subject: [PATCH] Detect high contrast mode on Windows (flutter/engine#35682) * Recognize high contrast theme switch * Trigger high contrast mode based on OS * Confirm updated status and flag via unit test * Remove null check, format * Formatting * More formatting * Import order * Refactor for PR * Formatting * Rename Update to Send * Remove queued flags * Add OnThemeChange unit test * Format for linux_unopt * Remove unneeded expect * Test SendInitialAccessibilityFeatures * Keep null check to embedder --- .../shell/platform/windows/flutter_window.cc | 14 +++++++ .../shell/platform/windows/flutter_window.h | 6 +++ .../windows/flutter_window_unittests.cc | 23 ++++++++++++ .../shell/platform/windows/flutter_windows.cc | 1 + .../windows/flutter_windows_engine.cc | 29 +++++++++++++++ .../platform/windows/flutter_windows_engine.h | 14 +++++++ .../flutter_windows_engine_unittests.cc | 37 +++++++++++++++++++ .../platform/windows/flutter_windows_view.cc | 8 ++++ .../platform/windows/flutter_windows_view.h | 6 +++ .../platform/windows/testing/mock_window.h | 2 + .../testing/mock_window_binding_handler.h | 1 + .../mock_window_binding_handler_delegate.h | 1 + .../platform/windows/testing/test_keyboard.cc | 6 +++ .../flutter/shell/platform/windows/window.cc | 16 ++++++++ .../flutter/shell/platform/windows/window.h | 6 +++ .../platform/windows/window_binding_handler.h | 3 ++ .../windows/window_binding_handler_delegate.h | 3 ++ 17 files changed, 176 insertions(+) diff --git a/engine/src/flutter/shell/platform/windows/flutter_window.cc b/engine/src/flutter/shell/platform/windows/flutter_window.cc index 67926e0a8e..968744209f 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window.cc @@ -4,11 +4,16 @@ #include "flutter/shell/platform/windows/flutter_window.h" +#include #include + #include #include #include "flutter/fml/logging.h" +#include "flutter/shell/platform/embedder/embedder.h" +#include "flutter/shell/platform/windows/flutter_windows_engine.h" +#include "flutter/shell/platform/windows/flutter_windows_view.h" namespace flutter { @@ -277,4 +282,13 @@ PointerLocation FlutterWindow::GetPrimaryPointerLocation() { return {(size_t)point.x, (size_t)point.y}; } +void FlutterWindow::OnThemeChange() { + binding_handler_delegate_->UpdateHighContrastEnabled( + GetHighContrastEnabled()); +} + +void FlutterWindow::SendInitialAccessibilityFeatures() { + OnThemeChange(); +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_window.h b/engine/src/flutter/shell/platform/windows/flutter_window.h index c9137fb198..29c9c69554 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window.h +++ b/engine/src/flutter/shell/platform/windows/flutter_window.h @@ -142,6 +142,12 @@ class FlutterWindow : public Window, public WindowBindingHandler { // |FlutterWindowBindingHandler| PointerLocation GetPrimaryPointerLocation() override; + // |Window| + void OnThemeChange() override; + + // |WindowBindingHandler| + void SendInitialAccessibilityFeatures() override; + private: // A pointer to a FlutterWindowsView that can be used to update engine // windowing and input state. diff --git a/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc index 5f136d6236..9044c601c3 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_window_unittests.cc @@ -128,6 +128,7 @@ class MockFlutterWindow : public FlutterWindow { void(double, double, FlutterPointerDeviceKind, int32_t)); MOCK_METHOD0(OnSetCursor, void()); MOCK_METHOD0(GetScrollOffsetMultiplier, float()); + MOCK_METHOD0(GetHighContrastEnabled, bool()); MOCK_METHOD0(GetDpiScale, float()); MOCK_METHOD0(IsVisible, bool()); MOCK_METHOD1(UpdateCursorRect, void(const Rect&)); @@ -377,5 +378,27 @@ TEST(FlutterWindowTest, OnWindowRepaint) { win32window.InjectWindowMessage(WM_PAINT, 0, 0); } +TEST(FlutterWindowTest, OnThemeChange) { + MockFlutterWindow win32window; + MockWindowBindingHandlerDelegate delegate; + win32window.SetView(&delegate); + + ON_CALL(win32window, GetHighContrastEnabled()).WillByDefault(Return(true)); + EXPECT_CALL(delegate, UpdateHighContrastEnabled(true)).Times(1); + + win32window.InjectWindowMessage(WM_THEMECHANGED, 0, 0); +} + +TEST(FlutterWindowTest, InitialAccessibilityFeatures) { + MockFlutterWindow win32window; + MockWindowBindingHandlerDelegate delegate; + win32window.SetView(&delegate); + + ON_CALL(win32window, GetHighContrastEnabled()).WillByDefault(Return(true)); + EXPECT_CALL(delegate, UpdateHighContrastEnabled(true)).Times(1); + + win32window.SendInitialAccessibilityFeatures(); +} + } // namespace testing } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows.cc b/engine/src/flutter/shell/platform/windows/flutter_windows.cc index c26157e7c7..54a71ba3f6 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows.cc @@ -85,6 +85,7 @@ FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate( // Must happen after engine is running. state->view->SendInitialBounds(); + state->view->SendInitialAccessibilityFeatures(); return state.release(); } diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc index 9396a9e2c1..8eabd6d1f9 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc @@ -607,4 +607,33 @@ std::string FlutterWindowsEngine::GetExecutableName() const { return "Flutter"; } +void FlutterWindowsEngine::UpdateAccessibilityFeatures( + FlutterAccessibilityFeature flags) { + embedder_api_.UpdateAccessibilityFeatures(engine_, flags); +} + +void FlutterWindowsEngine::UpdateHighContrastEnabled(bool enabled) { + high_contrast_enabled_ = enabled; + int flags = EnabledAccessibilityFeatures(); + if (enabled) { + flags |= + FlutterAccessibilityFeature::kFlutterAccessibilityFeatureHighContrast; + } else { + flags &= + ~FlutterAccessibilityFeature::kFlutterAccessibilityFeatureHighContrast; + } + UpdateAccessibilityFeatures(static_cast(flags)); +} + +int FlutterWindowsEngine::EnabledAccessibilityFeatures() const { + int flags = 0; + if (high_contrast_enabled()) { + flags |= + FlutterAccessibilityFeature::kFlutterAccessibilityFeatureHighContrast; + } + // As more accessibility features are enabled for Windows, + // the corresponding checks and flags should be added here. + return flags; +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h index 700f7889a2..2e261917ce 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine.h @@ -204,6 +204,15 @@ class FlutterWindowsEngine { // Returns true if the semantics tree is enabled. bool semantics_enabled() const { return semantics_enabled_; } + // Update the high contrast feature state. + void UpdateHighContrastEnabled(bool enabled); + + // Returns the flags for all currently enabled accessibility features + int EnabledAccessibilityFeatures() const; + + // Returns true if the high contrast feature is enabled. + bool high_contrast_enabled() const { return high_contrast_enabled_; } + // Returns the native accessibility node with the given id. gfx::NativeViewAccessible GetNativeAccessibleFromId(AccessibilityNodeId id); @@ -223,6 +232,9 @@ class FlutterWindowsEngine { // Returns the executable name for this process or "Flutter" if unknown. std::string GetExecutableName() const; + // Updates accessibility, e.g. switch to high contrast mode + void UpdateAccessibilityFeatures(FlutterAccessibilityFeature flags); + private: // Allows swapping out embedder_api_ calls in tests. friend class EngineModifier; @@ -293,6 +305,8 @@ class FlutterWindowsEngine { bool semantics_enabled_ = false; + bool high_contrast_enabled_ = false; + std::shared_ptr accessibility_bridge_; // The manager for WindowProc delegate registration and callbacks. diff --git a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc index 1417fe48a3..87482e1988 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_engine_unittests.cc @@ -76,6 +76,11 @@ TEST(FlutterWindowsEngine, RunDoesExpectedInitialization) { THREAD_PRIORITY_ABOVE_NORMAL); return kSuccess; })); + // Accessibility updates must do nothing when the embedder engine is mocked + modifier.embedder_api().UpdateAccessibilityFeatures = MOCK_ENGINE_PROC( + UpdateAccessibilityFeatures, + [](FLUTTER_API_SYMBOL(FlutterEngine) engine, + FlutterAccessibilityFeature flags) { return kSuccess; }); // It should send locale info. bool update_locales_called = false; @@ -193,6 +198,11 @@ TEST(FlutterWindowsEngine, RunWithoutANGLEUsesSoftware) { EXPECT_EQ(config->type, kSoftware); return kSuccess; })); + // Accessibility updates must do nothing when the embedder engine is mocked + modifier.embedder_api().UpdateAccessibilityFeatures = MOCK_ENGINE_PROC( + UpdateAccessibilityFeatures, + [](FLUTTER_API_SYMBOL(FlutterEngine) engine, + FlutterAccessibilityFeature flags) { return kSuccess; }); // Stub out UpdateLocales and SendPlatformMessage as we don't have a fully // initialized engine instance. @@ -409,5 +419,32 @@ TEST(FlutterWindowsEngine, GetExecutableName) { EXPECT_EQ(engine->GetExecutableName(), "flutter_windows_unittests.exe"); } +// Ensure that after setting or resetting the high contrast feature, +// the corresponding status flag can be retrieved from the engine. +TEST(FlutterWindowsEngine, UpdateHighContrastFeature) { + std::unique_ptr engine = GetTestEngine(); + EngineModifier modifier(engine.get()); + + bool called = false; + modifier.embedder_api().UpdateAccessibilityFeatures = MOCK_ENGINE_PROC( + UpdateAccessibilityFeatures, ([&called](auto engine, auto flags) { + called = true; + return kSuccess; + })); + + engine->UpdateHighContrastEnabled(true); + EXPECT_TRUE( + engine->EnabledAccessibilityFeatures() & + FlutterAccessibilityFeature::kFlutterAccessibilityFeatureHighContrast); + EXPECT_TRUE(engine->high_contrast_enabled()); + EXPECT_TRUE(called); + + engine->UpdateHighContrastEnabled(false); + EXPECT_FALSE( + engine->EnabledAccessibilityFeatures() & + FlutterAccessibilityFeature::kFlutterAccessibilityFeatureHighContrast); + EXPECT_FALSE(engine->high_contrast_enabled()); +} + } // namespace testing } // namespace flutter 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 d9f901219b..aefff5d522 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.cc @@ -616,6 +616,14 @@ void FlutterWindowsView::DestroyRenderSurface() { } } +void FlutterWindowsView::SendInitialAccessibilityFeatures() { + binding_handler_->SendInitialAccessibilityFeatures(); +} + +void FlutterWindowsView::UpdateHighContrastEnabled(bool enabled) { + engine_->UpdateHighContrastEnabled(enabled); +} + WindowsRenderTarget* FlutterWindowsView::GetRenderTarget() const { return render_target_.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 1fed9fbdf7..45dc6fdb6a 100644 --- a/engine/src/flutter/shell/platform/windows/flutter_windows_view.h +++ b/engine/src/flutter/shell/platform/windows/flutter_windows_view.h @@ -87,6 +87,12 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate, // Send initial bounds to embedder. Must occur after engine has initialized. void SendInitialBounds(); + // Send the initial accessibility features to the window + void SendInitialAccessibilityFeatures(); + + // |WindowBindingHandlerDelegate| + void UpdateHighContrastEnabled(bool enabled) override; + // Returns the frame buffer id for the engine to render to. uint32_t GetFrameBufferId(size_t width, size_t height); diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_window.h b/engine/src/flutter/shell/platform/windows/testing/mock_window.h index 8885096fa1..fc50235745 100644 --- a/engine/src/flutter/shell/platform/windows/testing/mock_window.h +++ b/engine/src/flutter/shell/platform/windows/testing/mock_window.h @@ -65,6 +65,8 @@ class MockWindow : public Window { MOCK_METHOD2(OnComposeChange, void(const std::u16string&, int)); MOCK_METHOD3(OnImeComposition, void(UINT const, WPARAM const, LPARAM const)); + MOCK_METHOD0(OnThemeChange, void()); + void CallOnImeComposition(UINT const message, WPARAM const wparam, LPARAM const lparam); 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 2489595d09..dc8ea7433f 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 @@ -36,6 +36,7 @@ class MockWindowBindingHandler : public WindowBindingHandler { MOCK_METHOD3(OnBitmapSurfaceUpdated, bool(const void* allocation, size_t row_bytes, size_t height)); MOCK_METHOD0(GetPrimaryPointerLocation, PointerLocation()); + MOCK_METHOD0(SendInitialAccessibilityFeatures, void()); }; } // namespace testing diff --git a/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler_delegate.h b/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler_delegate.h index 30d7137685..a7ad72cbe2 100644 --- a/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler_delegate.h +++ b/engine/src/flutter/shell/platform/windows/testing/mock_window_binding_handler_delegate.h @@ -61,6 +61,7 @@ class MockWindowBindingHandlerDelegate : public WindowBindingHandlerDelegate { FlutterPointerDeviceKind, int32_t)); MOCK_METHOD0(OnPlatformBrightnessChanged, void()); + MOCK_METHOD1(UpdateHighContrastEnabled, void(bool enabled)); }; } // namespace testing diff --git a/engine/src/flutter/shell/platform/windows/testing/test_keyboard.cc b/engine/src/flutter/shell/platform/windows/testing/test_keyboard.cc index 03a452fac2..06a5b86c38 100644 --- a/engine/src/flutter/shell/platform/windows/testing/test_keyboard.cc +++ b/engine/src/flutter/shell/platform/windows/testing/test_keyboard.cc @@ -180,6 +180,12 @@ void MockEmbedderApiForKeyboard( return kSuccess; }; + // Any time the associated EmbedderEngine will be mocked, such as here, + // the Update accessibility features must not attempt to actually push the + // update + modifier.embedder_api().UpdateAccessibilityFeatures = + [](FLUTTER_API_SYMBOL(FlutterEngine) engine, + FlutterAccessibilityFeature flags) { return kSuccess; }; modifier.embedder_api().UpdateLocales = [](auto engine, const FlutterLocale** locales, size_t locales_count) { return kSuccess; diff --git a/engine/src/flutter/shell/platform/windows/window.cc b/engine/src/flutter/shell/platform/windows/window.cc index 08f5368a14..913a3f85fe 100644 --- a/engine/src/flutter/shell/platform/windows/window.cc +++ b/engine/src/flutter/shell/platform/windows/window.cc @@ -537,6 +537,9 @@ Window::HandleMessage(UINT const message, // DefWindowProc will send WM_CHAR for this WM_UNICHAR. break; } + case WM_THEMECHANGED: + OnThemeChange(); + break; case WM_DEADCHAR: case WM_SYSDEADCHAR: case WM_CHAR: @@ -632,4 +635,17 @@ UINT Window::Win32DispatchMessage(UINT Msg, WPARAM wParam, LPARAM lParam) { return ::SendMessage(window_handle_, Msg, wParam, lParam); } +bool Window::GetHighContrastEnabled() { + HIGHCONTRAST high_contrast = {.cbSize = sizeof(HIGHCONTRAST)}; + // API call is only supported on Windows 8+ + if (SystemParametersInfoW(SPI_GETHIGHCONTRAST, sizeof(HIGHCONTRAST), + &high_contrast, 0)) { + return high_contrast.dwFlags & HCF_HIGHCONTRASTON; + } else { + FML_LOG(INFO) << "Failed to get status of high contrast feature," + << "support only for Windows 8 + "; + return false; + } +} + } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/window.h b/engine/src/flutter/shell/platform/windows/window.h index 7782803b10..e31be30f16 100644 --- a/engine/src/flutter/shell/platform/windows/window.h +++ b/engine/src/flutter/shell/platform/windows/window.h @@ -207,6 +207,9 @@ class Window : public KeyboardManager::WindowDelegate { // Returns the current pixel per scroll tick value. virtual float GetScrollOffsetMultiplier(); + // Check if the high contrast feature is enabled on the OS + virtual bool GetHighContrastEnabled(); + protected: // Win32's DefWindowProc. // @@ -224,6 +227,9 @@ class Window : public KeyboardManager::WindowDelegate { // gestures. std::unique_ptr direct_manipulation_owner_; + // Called when a theme change message is issued + virtual void OnThemeChange() = 0; + private: // Release OS resources associated with window. void Destroy(); 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 594ef355a9..abde74e598 100644 --- a/engine/src/flutter/shell/platform/windows/window_binding_handler.h +++ b/engine/src/flutter/shell/platform/windows/window_binding_handler.h @@ -89,6 +89,9 @@ class WindowBindingHandler { // Returns the last known position of the primary pointer in window // coordinates. virtual PointerLocation GetPrimaryPointerLocation() = 0; + + // Called to set the initial state of accessibility features + virtual void SendInitialAccessibilityFeatures() = 0; }; } // namespace flutter diff --git a/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h b/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h index 2d73e9cfb5..38569fda29 100644 --- a/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h +++ b/engine/src/flutter/shell/platform/windows/window_binding_handler_delegate.h @@ -128,6 +128,9 @@ class WindowBindingHandlerDelegate { // Returns the root view accessibility node, or nullptr if none. virtual gfx::NativeViewAccessible GetNativeViewAccessible() = 0; + + // Update the status of the high contrast feature + virtual void UpdateHighContrastEnabled(bool enabled) = 0; }; } // namespace flutter