mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Add an explicit API for font change notification (#21164)
Originally font change notification was handled by forwarding WM_FONTCHANGE to the Flutter HWND, to avoid adding new API surface, but that's not a good solution in a multi-window scenario, and it would require a completely different solution for UWP. It also requires non-obvious plumbing in the runner. This replaces that with an explicit API, so that there's a clean and obvious way for the runner to trigger this event.
This commit is contained in:
parent
2226c2a1ec
commit
efe7683311
@ -64,6 +64,10 @@ std::chrono::nanoseconds FlutterEngine::ProcessMessages() {
|
||||
return std::chrono::nanoseconds(FlutterDesktopEngineProcessMessages(engine_));
|
||||
}
|
||||
|
||||
void FlutterEngine::ReloadSystemFonts() {
|
||||
FlutterDesktopEngineReloadSystemFonts(engine_);
|
||||
}
|
||||
|
||||
FlutterDesktopPluginRegistrarRef FlutterEngine::GetRegistrarForPlugin(
|
||||
const std::string& plugin_name) {
|
||||
if (!engine_) {
|
||||
|
||||
@ -38,16 +38,22 @@ class TestFlutterWindowsApi : public testing::StubFlutterWindowsApi {
|
||||
// |flutter::testing::StubFlutterWindowsApi|
|
||||
uint64_t EngineProcessMessages() override { return 99; }
|
||||
|
||||
// |flutter::testing::StubFlutterWindowsApi|
|
||||
void EngineReloadSystemFonts() override { reload_fonts_called_ = true; }
|
||||
|
||||
bool create_called() { return create_called_; }
|
||||
|
||||
bool run_called() { return run_called_; }
|
||||
|
||||
bool destroy_called() { return destroy_called_; }
|
||||
|
||||
bool reload_fonts_called() { return reload_fonts_called_; }
|
||||
|
||||
private:
|
||||
bool create_called_ = false;
|
||||
bool run_called_ = false;
|
||||
bool destroy_called_ = false;
|
||||
bool reload_fonts_called_ = false;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
@ -93,6 +99,18 @@ TEST(FlutterEngineTest, ProcessMessages) {
|
||||
EXPECT_EQ(next_event_time.count(), 99);
|
||||
}
|
||||
|
||||
TEST(FlutterEngineTest, ReloadFonts) {
|
||||
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
|
||||
std::make_unique<TestFlutterWindowsApi>());
|
||||
auto test_api = static_cast<TestFlutterWindowsApi*>(scoped_api_stub.stub());
|
||||
|
||||
FlutterEngine engine(DartProject(L"fake/project/path"));
|
||||
engine.Run();
|
||||
|
||||
engine.ReloadSystemFonts();
|
||||
EXPECT_TRUE(test_api->reload_fonts_called());
|
||||
}
|
||||
|
||||
TEST(FlutterEngineTest, GetMessenger) {
|
||||
DartProject project(L"data");
|
||||
testing::ScopedStubFlutterWindowsApi scoped_api_stub(
|
||||
|
||||
@ -53,6 +53,11 @@ class FlutterEngine : public PluginRegistry {
|
||||
// last return value from this function.
|
||||
std::chrono::nanoseconds ProcessMessages();
|
||||
|
||||
// Tells the engine that the system font list has changed. Should be called
|
||||
// by clients when OS-level font changes happen (e.g., WM_FONTCHANGE in a
|
||||
// Win32 application).
|
||||
void ReloadSystemFonts();
|
||||
|
||||
// flutter::PluginRegistry:
|
||||
FlutterDesktopPluginRegistrarRef GetRegistrarForPlugin(
|
||||
const std::string& plugin_name) override;
|
||||
|
||||
@ -108,6 +108,12 @@ uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine) {
|
||||
if (s_stub_implementation) {
|
||||
s_stub_implementation->EngineReloadSystemFonts();
|
||||
}
|
||||
}
|
||||
|
||||
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(
|
||||
FlutterDesktopEngineRef engine,
|
||||
const char* plugin_name) {
|
||||
|
||||
@ -62,6 +62,9 @@ class StubFlutterWindowsApi {
|
||||
// Called for FlutterDesktopEngineProcessMessages.
|
||||
virtual uint64_t EngineProcessMessages() { return 0; }
|
||||
|
||||
// Called for FlutterDesktopEngineReloadSystemFonts.
|
||||
virtual void EngineReloadSystemFonts() {}
|
||||
|
||||
// Called for FlutterDesktopViewGetHWND.
|
||||
virtual HWND ViewGetHWND() { return reinterpret_cast<HWND>(1); }
|
||||
|
||||
|
||||
@ -136,6 +136,10 @@ uint64_t FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine) {
|
||||
return EngineFromHandle(engine)->task_runner()->ProcessTasks().count();
|
||||
}
|
||||
|
||||
void FlutterDesktopEngineReloadSystemFonts(FlutterDesktopEngineRef engine) {
|
||||
FlutterEngineReloadSystemFonts(EngineFromHandle(engine)->engine());
|
||||
}
|
||||
|
||||
FlutterDesktopPluginRegistrarRef FlutterDesktopEngineGetPluginRegistrar(
|
||||
FlutterDesktopEngineRef engine,
|
||||
const char* plugin_name) {
|
||||
|
||||
@ -105,13 +105,6 @@ void FlutterWindowsView::OnScroll(double x,
|
||||
SendScroll(x, y, delta_x, delta_y, scroll_offset_multiplier);
|
||||
}
|
||||
|
||||
void FlutterWindowsView::OnFontChange() {
|
||||
if (engine_->engine() == nullptr) {
|
||||
return;
|
||||
}
|
||||
FlutterEngineReloadSystemFonts(engine_->engine());
|
||||
}
|
||||
|
||||
// Sends new size information to FlutterEngine.
|
||||
void FlutterWindowsView::SendWindowMetrics(size_t width,
|
||||
size_t height,
|
||||
|
||||
@ -98,9 +98,6 @@ class FlutterWindowsView : public WindowBindingHandlerDelegate {
|
||||
double delta_y,
|
||||
int scroll_offset_multiplier) override;
|
||||
|
||||
// |WindowBindingHandlerDelegate|
|
||||
void OnFontChange() override;
|
||||
|
||||
private:
|
||||
// Struct holding the mouse state. The engine doesn't keep track of which
|
||||
// mouse buttons have been pressed, so it's the embedding's responsibility.
|
||||
|
||||
@ -143,6 +143,9 @@ FLUTTER_EXPORT bool FlutterDesktopEngineRun(FlutterDesktopEngineRef engine,
|
||||
FLUTTER_EXPORT uint64_t
|
||||
FlutterDesktopEngineProcessMessages(FlutterDesktopEngineRef engine);
|
||||
|
||||
FLUTTER_EXPORT void FlutterDesktopEngineReloadSystemFonts(
|
||||
FlutterDesktopEngineRef engine);
|
||||
|
||||
// Returns the plugin registrar handle for the plugin with the given name.
|
||||
//
|
||||
// The name must be unique across the application.
|
||||
|
||||
@ -38,7 +38,6 @@ class MockWin32Window : public Win32Window {
|
||||
MOCK_METHOD1(OnText, void(const std::u16string&));
|
||||
MOCK_METHOD4(OnKey, void(int, int, int, char32_t));
|
||||
MOCK_METHOD2(OnScroll, void(double, double));
|
||||
MOCK_METHOD0(OnFontChange, void());
|
||||
};
|
||||
|
||||
} // namespace testing
|
||||
|
||||
@ -12,12 +12,5 @@ Win32FlutterWindowTest::Win32FlutterWindowTest(int width, int height)
|
||||
|
||||
Win32FlutterWindowTest::~Win32FlutterWindowTest() = default;
|
||||
|
||||
void Win32FlutterWindowTest::OnFontChange() {
|
||||
on_font_change_called_ = true;
|
||||
}
|
||||
|
||||
bool Win32FlutterWindowTest::OnFontChangeWasCalled() {
|
||||
return on_font_change_called_;
|
||||
}
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
@ -19,11 +19,6 @@ class Win32FlutterWindowTest : public Win32FlutterWindow {
|
||||
Win32FlutterWindowTest(Win32FlutterWindowTest const&) = delete;
|
||||
Win32FlutterWindowTest& operator=(Win32FlutterWindowTest const&) = delete;
|
||||
|
||||
// |Win32Window|
|
||||
void OnFontChange() override;
|
||||
|
||||
bool OnFontChangeWasCalled();
|
||||
|
||||
private:
|
||||
bool on_font_change_called_ = false;
|
||||
};
|
||||
|
||||
@ -171,8 +171,4 @@ void Win32FlutterWindow::OnScroll(double delta_x, double delta_y) {
|
||||
kScrollOffsetMultiplier);
|
||||
}
|
||||
|
||||
void Win32FlutterWindow::OnFontChange() {
|
||||
binding_handler_delegate_->OnFontChange();
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
@ -59,9 +59,6 @@ class Win32FlutterWindow : public Win32Window, public WindowBindingHandler {
|
||||
// |Win32Window|
|
||||
void OnScroll(double delta_x, double delta_y) override;
|
||||
|
||||
// |Win32Window|
|
||||
void OnFontChange() override;
|
||||
|
||||
// |FlutterWindowBindingHandler|
|
||||
void SetView(WindowBindingHandlerDelegate* view) override;
|
||||
|
||||
|
||||
@ -13,13 +13,5 @@ TEST(Win32FlutterWindowTest, CreateDestroy) {
|
||||
ASSERT_TRUE(TRUE);
|
||||
}
|
||||
|
||||
TEST(Win32FlutterWindowTest, CanFontChange) {
|
||||
Win32FlutterWindowTest window(800, 600);
|
||||
HWND hwnd = window.GetWindowHandle();
|
||||
LRESULT result = SendMessage(hwnd, WM_FONTCHANGE, NULL, NULL);
|
||||
ASSERT_EQ(result, 0);
|
||||
ASSERT_TRUE(window.OnFontChangeWasCalled());
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
@ -130,9 +130,6 @@ Win32Window::HandleMessage(UINT const message,
|
||||
current_height_ = height;
|
||||
HandleResize(width, height);
|
||||
break;
|
||||
case WM_FONTCHANGE:
|
||||
OnFontChange();
|
||||
break;
|
||||
case WM_MOUSEMOVE:
|
||||
TrackMouseLeaveEvent(window_handle_);
|
||||
|
||||
|
||||
@ -97,9 +97,6 @@ class Win32Window {
|
||||
// Called when mouse scrollwheel input occurs.
|
||||
virtual void OnScroll(double delta_x, double delta_y) = 0;
|
||||
|
||||
// Called when the system font change.
|
||||
virtual void OnFontChange() = 0;
|
||||
|
||||
UINT GetCurrentDPI();
|
||||
|
||||
UINT GetCurrentWidth();
|
||||
|
||||
@ -50,10 +50,6 @@ class WindowBindingHandlerDelegate {
|
||||
double delta_x,
|
||||
double delta_y,
|
||||
int scroll_offset_multiplier) = 0;
|
||||
|
||||
// Notifies delegate that backing window size has had system font change.
|
||||
// Typically called by currently configured WindowBindingHandler
|
||||
virtual void OnFontChange() = 0;
|
||||
};
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user