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
This commit is contained in:
yaakovschectman
2022-08-25 16:24:21 -04:00
committed by GitHub
parent 6633f66050
commit ceb25d38d0
17 changed files with 176 additions and 0 deletions

View File

@@ -4,11 +4,16 @@
#include "flutter/shell/platform/windows/flutter_window.h"
#include <WinUser.h>
#include <dwmapi.h>
#include <chrono>
#include <map>
#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

View File

@@ -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.

View File

@@ -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

View File

@@ -85,6 +85,7 @@ FlutterDesktopViewControllerRef FlutterDesktopViewControllerCreate(
// Must happen after engine is running.
state->view->SendInitialBounds();
state->view->SendInitialAccessibilityFeatures();
return state.release();
}

View File

@@ -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<FlutterAccessibilityFeature>(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

View File

@@ -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<AccessibilityBridge> accessibility_bridge_;
// The manager for WindowProc delegate registration and callbacks.

View File

@@ -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<FlutterWindowsEngine> 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

View File

@@ -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();
}

View File

@@ -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);

View File

@@ -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);

View File

@@ -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

View File

@@ -61,6 +61,7 @@ class MockWindowBindingHandlerDelegate : public WindowBindingHandlerDelegate {
FlutterPointerDeviceKind,
int32_t));
MOCK_METHOD0(OnPlatformBrightnessChanged, void());
MOCK_METHOD1(UpdateHighContrastEnabled, void(bool enabled));
};
} // namespace testing

View File

@@ -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;

View File

@@ -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

View File

@@ -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<DirectManipulationOwner> direct_manipulation_owner_;
// Called when a theme change message is issued
virtual void OnThemeChange() = 0;
private:
// Release OS resources associated with window.
void Destroy();

View File

@@ -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

View File

@@ -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